Skip to content

Commit 07050b0

Browse files
committed
feat(Grafana): Fallback to authenticate with config.security.admin_*
1 parent 563443b commit 07050b0

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

controllers/client/grafana_client.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,33 @@ func getAdminCredentials(ctx context.Context, c client.Client, grafana *v1beta1.
3636
return credentials, nil
3737
}
3838

39-
// rely on username and password otherwise
40-
username, err := GetValueFromSecretKey(ctx, grafana.Spec.External.AdminUser, c, grafana.Namespace)
41-
if err != nil {
42-
return nil, err
39+
switch {
40+
case grafana.Spec.External.AdminUser != nil:
41+
username, err := GetValueFromSecretKey(ctx, grafana.Spec.External.AdminUser, c, grafana.Namespace)
42+
if err != nil {
43+
return nil, err
44+
}
45+
credentials.username = string(username)
46+
case grafana.Spec.Config["security"]["admin_user"] != "":
47+
credentials.username = grafana.Spec.Config["security"]["admin_user"]
48+
default:
49+
return nil, fmt.Errorf("authentication undefined, set apiKey or userName for external instance: %s/%s", grafana.Namespace, grafana.Name)
4350
}
4451

45-
password, err := GetValueFromSecretKey(ctx, grafana.Spec.External.AdminPassword, c, grafana.Namespace)
46-
if err != nil {
47-
return nil, err
52+
switch {
53+
case grafana.Spec.External.AdminPassword != nil:
54+
password, err := GetValueFromSecretKey(ctx, grafana.Spec.External.AdminPassword, c, grafana.Namespace)
55+
if err != nil {
56+
return nil, err
57+
}
58+
credentials.password = string(password)
59+
case grafana.Spec.Config["security"]["admin_password"] != "":
60+
credentials.password = grafana.Spec.Config["security"]["admin_password"]
61+
default:
62+
// If username is defined, we can assume apiKey will not be used
63+
return nil, fmt.Errorf("password not set for external instance: %s/%s", grafana.Namespace, grafana.Name)
4864
}
4965

50-
credentials.username = string(username)
51-
credentials.password = string(password)
5266
return credentials, nil
5367
}
5468

0 commit comments

Comments
 (0)