Skip to content

Commit

Permalink
basic auth opt for Grafana (#12524)
Browse files Browse the repository at this point in the history
* basic auth opt for grafana

* decode base64 or read plain text

* remove quotes from auth data
  • Loading branch information
skudasov authored Mar 21, 2024
1 parent f7e3411 commit 1b55769
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
62 changes: 48 additions & 14 deletions dashboard-lib/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package dashboard_lib

import "os"
import (
"encoding/base64"
"github.com/pkg/errors"
"os"
"strings"
)

type EnvConfig struct {
Platform string
GrafanaURL string
GrafanaToken string
GrafanaFolder string
DataSources DataSources
Platform string
GrafanaURL string
GrafanaToken string
GrafanaBasicAuthUser string
GrafanaBasicAuthPassword string
GrafanaFolder string
DataSources DataSources
}

type DataSources struct {
Expand Down Expand Up @@ -37,10 +44,6 @@ func ReadEnvDeployOpts() EnvConfig {
if grafanaURL == "" {
L.Fatal().Msg("GRAFANA_URL must be provided")
}
grafanaToken := os.Getenv("GRAFANA_TOKEN")
if grafanaToken == "" {
L.Fatal().Msg("GRAFANA_TOKEN must be provided")
}
grafanaFolder := os.Getenv("GRAFANA_FOLDER")
if grafanaFolder == "" {
L.Fatal().Msg("GRAFANA_FOLDER must be provided")
Expand All @@ -57,14 +60,45 @@ func ReadEnvDeployOpts() EnvConfig {
if prometheusDataSourceName == "" {
L.Fatal().Msg("PROMETHEUS_DATA_SOURCE_NAME must be provided")
}
ba := os.Getenv("GRAFANA_BASIC_AUTH")
grafanaToken := os.Getenv("GRAFANA_TOKEN")
if grafanaToken == "" && ba == "" {
L.Fatal().Msg("GRAFANA_TOKEN or GRAFANA_BASIC_AUTH must be provided")
}
var user, password string
var err error
if ba != "" {
user, password, err = DecodeBasicAuth(ba)
if err != nil {
L.Fatal().Err(err).Msg("failed to decode basic auth")
}
}
return EnvConfig{
GrafanaURL: grafanaURL,
GrafanaToken: grafanaToken,
GrafanaFolder: grafanaFolder,
Platform: platform,
GrafanaURL: grafanaURL,
GrafanaToken: grafanaToken,
GrafanaBasicAuthUser: user,
GrafanaBasicAuthPassword: password,
GrafanaFolder: grafanaFolder,
Platform: platform,
DataSources: DataSources{
Loki: loki,
Prometheus: prom,
},
}
}

func DecodeBasicAuth(authString string) (string, string, error) {
var data string
decodedBytes, err := base64.StdEncoding.DecodeString(authString)
if err != nil {
L.Warn().Err(err).Msg("failed to decode basic auth, plain text? reading auth data")
data = authString
} else {
data = string(decodedBytes[1 : len(decodedBytes)-1])
}
parts := strings.Split(data, ":")
if len(parts) != 2 {
return "", "", errors.New("invalid basic authentication format")
}
return parts[0], parts[1], nil
}
17 changes: 16 additions & 1 deletion dashboard-lib/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,22 @@ func (m *Dashboard) Deploy() error {
if err != nil {
return err
}
client := grabana.NewClient(&http.Client{}, m.DeployOpts.GrafanaURL, grabana.WithAPIToken(m.DeployOpts.GrafanaToken))
var client *grabana.Client
if m.DeployOpts.GrafanaBasicAuthUser != "" && m.DeployOpts.GrafanaBasicAuthPassword != "" {
L.Info().Msg("Authorizing using BasicAuth")
client = grabana.NewClient(
&http.Client{},
m.DeployOpts.GrafanaURL,
grabana.WithBasicAuth(m.DeployOpts.GrafanaBasicAuthUser, m.DeployOpts.GrafanaBasicAuthPassword),
)
} else {
L.Info().Msg("Authorizing using Bearer token")
client = grabana.NewClient(
&http.Client{},
m.DeployOpts.GrafanaURL,
grabana.WithAPIToken(m.DeployOpts.GrafanaToken),
)
}
fo, folderErr := client.FindOrCreateFolder(ctx, m.DeployOpts.GrafanaFolder)
if folderErr != nil {
return errors.Wrap(err, "could not find or create Grafana folder")
Expand Down

0 comments on commit 1b55769

Please sign in to comment.