@@ -17,9 +17,42 @@ import (
17
17
)
18
18
19
19
type grafanaAdminCredentials struct {
20
- username string
21
- password string
22
- apikey string
20
+ adminUser string
21
+ adminPassword string
22
+ apikey string
23
+ }
24
+
25
+ func getExternalAdminUser (ctx context.Context , c client.Client , cr * v1beta1.Grafana ) (string , error ) {
26
+ switch {
27
+ case cr .Spec .External .AdminUser != nil :
28
+ adminUser , err := GetValueFromSecretKey (ctx , cr .Spec .External .AdminUser , c , cr .Namespace )
29
+ if err != nil {
30
+ return "" , err
31
+ }
32
+
33
+ return string (adminUser ), nil
34
+ case cr .Spec .Config ["security" ] != nil && cr .Spec .Config ["security" ]["admin_user" ] != "" :
35
+ return cr .Spec .Config ["security" ]["admin_user" ], nil
36
+ default :
37
+ return "" , fmt .Errorf ("authentication undefined, set apiKey or userName for external instance: %s/%s" , cr .Namespace , cr .Name )
38
+ }
39
+ }
40
+
41
+ func getExternalAdminPassword (ctx context.Context , c client.Client , cr * v1beta1.Grafana ) (string , error ) {
42
+ switch {
43
+ case cr .Spec .External .AdminPassword != nil :
44
+ adminPassword , err := GetValueFromSecretKey (ctx , cr .Spec .External .AdminPassword , c , cr .Namespace )
45
+ if err != nil {
46
+ return "" , err
47
+ }
48
+
49
+ return string (adminPassword ), nil
50
+ case cr .Spec .Config ["security" ] != nil && cr .Spec .Config ["security" ]["admin_password" ] != "" :
51
+ return cr .Spec .Config ["security" ]["admin_password" ], nil
52
+ default :
53
+ // If username is defined, we can assume apiKey will not be used
54
+ return "" , fmt .Errorf ("password not set for external instance: %s/%s" , cr .Namespace , cr .Name )
55
+ }
23
56
}
24
57
25
58
func getAdminCredentials (ctx context.Context , c client.Client , grafana * v1beta1.Grafana ) (* grafanaAdminCredentials , error ) {
@@ -38,20 +71,18 @@ func getAdminCredentials(ctx context.Context, c client.Client, grafana *v1beta1.
38
71
return credentials , nil
39
72
}
40
73
41
- // rely on username and password otherwise
42
- username , err := GetValueFromSecretKey (ctx , grafana .Spec .External .AdminUser , c , grafana .Namespace )
74
+ var err error
75
+
76
+ credentials .adminUser , err = getExternalAdminUser (ctx , c , grafana )
43
77
if err != nil {
44
78
return nil , err
45
79
}
46
80
47
- password , err := GetValueFromSecretKey (ctx , grafana . Spec . External . AdminPassword , c , grafana . Namespace )
81
+ credentials . adminPassword , err = getExternalAdminPassword (ctx , c , grafana )
48
82
if err != nil {
49
83
return nil , err
50
84
}
51
85
52
- credentials .username = string (username )
53
- credentials .password = string (password )
54
-
55
86
return credentials , nil
56
87
}
57
88
@@ -70,7 +101,7 @@ func getAdminCredentials(ctx context.Context, c client.Client, grafana *v1beta1.
70
101
for _ , env := range container .Env {
71
102
if env .Name == config .GrafanaAdminUserEnvVar {
72
103
if env .Value != "" {
73
- credentials .username = env .Value
104
+ credentials .adminUser = env .Value
74
105
continue
75
106
}
76
107
@@ -81,14 +112,14 @@ func getAdminCredentials(ctx context.Context, c client.Client, grafana *v1beta1.
81
112
return nil , err
82
113
}
83
114
84
- credentials .username = string (usernameFromSecret )
115
+ credentials .adminUser = string (usernameFromSecret )
85
116
}
86
117
}
87
118
}
88
119
89
120
if env .Name == config .GrafanaAdminPasswordEnvVar {
90
121
if env .Value != "" {
91
- credentials .password = env .Value
122
+ credentials .adminPassword = env .Value
92
123
continue
93
124
}
94
125
@@ -99,7 +130,7 @@ func getAdminCredentials(ctx context.Context, c client.Client, grafana *v1beta1.
99
130
return nil , err
100
131
}
101
132
102
- credentials .password = string (passwordFromSecret )
133
+ credentials .adminPassword = string (passwordFromSecret )
103
134
}
104
135
}
105
136
}
@@ -118,7 +149,7 @@ func InjectAuthHeaders(ctx context.Context, c client.Client, grafana *v1beta1.Gr
118
149
if creds .apikey != "" {
119
150
req .Header .Add ("Authorization" , "Bearer " + creds .apikey )
120
151
} else {
121
- req .SetBasicAuth (creds .username , creds .password )
152
+ req .SetBasicAuth (creds .adminUser , creds .adminPassword )
122
153
}
123
154
124
155
return nil
@@ -185,8 +216,8 @@ func NewGeneratedGrafanaClient(ctx context.Context, c client.Client, grafana *v1
185
216
Timeout : timeout * time .Second ,
186
217
},
187
218
}
188
- if credentials .username != "" {
189
- cfg .BasicAuth = url .UserPassword (credentials .username , credentials .password )
219
+ if credentials .adminUser != "" {
220
+ cfg .BasicAuth = url .UserPassword (credentials .adminUser , credentials .adminPassword )
190
221
}
191
222
192
223
cl := genapi .NewHTTPClientWithConfig (nil , cfg )
0 commit comments