Skip to content

Commit 4a8a17e

Browse files
Baarsgaardweisdd
andauthored
fix(Grafana): .spec.config.security.admin_* as fallback for external instances (#2092)
* fix(Grafana): Authenticate with .spec.config.security.admin_* as fallback * test: Retrieving Grafana credentials from CR Spec * chore: remove print statement --------- Co-authored-by: Igor Beliakov <demtis.register@gmail.com>
1 parent 61fe99e commit 4a8a17e

File tree

2 files changed

+332
-16
lines changed

2 files changed

+332
-16
lines changed

controllers/client/grafana_client.go

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,42 @@ import (
1717
)
1818

1919
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+
}
2356
}
2457

2558
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.
3871
return credentials, nil
3972
}
4073

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)
4377
if err != nil {
4478
return nil, err
4579
}
4680

47-
password, err := GetValueFromSecretKey(ctx, grafana.Spec.External.AdminPassword, c, grafana.Namespace)
81+
credentials.adminPassword, err = getExternalAdminPassword(ctx, c, grafana)
4882
if err != nil {
4983
return nil, err
5084
}
5185

52-
credentials.username = string(username)
53-
credentials.password = string(password)
54-
5586
return credentials, nil
5687
}
5788

@@ -70,7 +101,7 @@ func getAdminCredentials(ctx context.Context, c client.Client, grafana *v1beta1.
70101
for _, env := range container.Env {
71102
if env.Name == config.GrafanaAdminUserEnvVar {
72103
if env.Value != "" {
73-
credentials.username = env.Value
104+
credentials.adminUser = env.Value
74105
continue
75106
}
76107

@@ -81,14 +112,14 @@ func getAdminCredentials(ctx context.Context, c client.Client, grafana *v1beta1.
81112
return nil, err
82113
}
83114

84-
credentials.username = string(usernameFromSecret)
115+
credentials.adminUser = string(usernameFromSecret)
85116
}
86117
}
87118
}
88119

89120
if env.Name == config.GrafanaAdminPasswordEnvVar {
90121
if env.Value != "" {
91-
credentials.password = env.Value
122+
credentials.adminPassword = env.Value
92123
continue
93124
}
94125

@@ -99,7 +130,7 @@ func getAdminCredentials(ctx context.Context, c client.Client, grafana *v1beta1.
99130
return nil, err
100131
}
101132

102-
credentials.password = string(passwordFromSecret)
133+
credentials.adminPassword = string(passwordFromSecret)
103134
}
104135
}
105136
}
@@ -118,7 +149,7 @@ func InjectAuthHeaders(ctx context.Context, c client.Client, grafana *v1beta1.Gr
118149
if creds.apikey != "" {
119150
req.Header.Add("Authorization", "Bearer "+creds.apikey)
120151
} else {
121-
req.SetBasicAuth(creds.username, creds.password)
152+
req.SetBasicAuth(creds.adminUser, creds.adminPassword)
122153
}
123154

124155
return nil
@@ -185,8 +216,8 @@ func NewGeneratedGrafanaClient(ctx context.Context, c client.Client, grafana *v1
185216
Timeout: timeout * time.Second,
186217
},
187218
}
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)
190221
}
191222

192223
cl := genapi.NewHTTPClientWithConfig(nil, cfg)

0 commit comments

Comments
 (0)