Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read custom cloud list from json string #141

Merged
merged 6 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions azsettings/cloud_settings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package azsettings

import (
"encoding/json"
"fmt"
)

Expand All @@ -10,10 +11,10 @@ type AzureCloudInfo struct {
}

type AzureCloudSettings struct {
Name string
DisplayName string
AadAuthority string
Properties map[string]string
Name string `json:"name"`
DisplayName string `json:"displayName"`
AadAuthority string `json:"aadAuthority"`
Properties map[string]string `json:"properties"`
}

var predefinedClouds = []*AzureCloudSettings{
Expand Down Expand Up @@ -55,8 +56,8 @@ var predefinedClouds = []*AzureCloudSettings{
},
}

func (*AzureSettings) GetCloud(cloudName string) (*AzureCloudSettings, error) {
clouds := getClouds()
func (settings *AzureSettings) GetCloud(cloudName string) (*AzureCloudSettings, error) {
clouds := settings.getClouds()

for _, cloud := range clouds {
if cloud.Name == cloudName {
Expand All @@ -68,17 +69,28 @@ func (*AzureSettings) GetCloud(cloudName string) (*AzureCloudSettings, error) {
}

// Returns all clouds configured on the instance, including custom clouds if any
func (*AzureSettings) Clouds() []AzureCloudInfo {
clouds := getClouds()
func (settings *AzureSettings) Clouds() []AzureCloudInfo {
clouds := settings.getClouds()
return mapCloudInfo(clouds)
}

// Returns only the custom clouds configured on the instance
func (*AzureSettings) CustomClouds() []AzureCloudInfo {
clouds := getCustomClouds()
func (settings *AzureSettings) CustomClouds() []AzureCloudInfo {
clouds := settings.getCustomClouds()
return mapCloudInfo(clouds)
}

// Parses the JSON list of custom clouds passed in, then stores the list on the instance
func (settings *AzureSettings) SetCustomClouds(customCloudsJSON string) error {
var customClouds []*AzureCloudSettings
if err := json.Unmarshal([]byte(customCloudsJSON), &customClouds); err != nil {
return err
}

settings.customClouds = customClouds
return nil
}

func mapCloudInfo(clouds []*AzureCloudSettings) []AzureCloudInfo {
results := make([]AzureCloudInfo, 0, len(clouds))
for _, cloud := range clouds {
Expand All @@ -91,16 +103,15 @@ func mapCloudInfo(clouds []*AzureCloudSettings) []AzureCloudInfo {
return results
}

func getClouds() []*AzureCloudSettings {
if clouds := getCustomClouds(); len(clouds) > 0 {
allClouds := append(clouds, predefinedClouds...)
func (settings *AzureSettings) getClouds() []*AzureCloudSettings {
if clouds := settings.getCustomClouds(); len(clouds) > 0 {
allClouds := append(predefinedClouds, clouds...)
return allClouds
}

return predefinedClouds
}

func getCustomClouds() []*AzureCloudSettings {
// Configuration of Azure clouds not yet supported
return nil
func (settings *AzureSettings) getCustomClouds() []*AzureCloudSettings {
return settings.customClouds
}
84 changes: 81 additions & 3 deletions azsettings/cloud_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,34 @@ import (
"github.com/stretchr/testify/require"
)

func TestGetClouds(t *testing.T) {
var testCustomClouds = []*AzureCloudSettings{
{
Name: "CustomCloud1",
DisplayName: "Custom Cloud 1",
AadAuthority: "https://login.contoso.com/",
Properties: map[string]string{
"azureDataExplorerSuffix": ".kusto.cloud1.contoso.com",
"logAnalytics": "https://api.loganalytics.cloud1.contoso.com",
"portal": "https://portal.azure.cloud1.contoso.com",
"prometheusResourceId": "https://prometheus.monitor.azure.cloud1.contoso.com",
"resourceManager": "https://management.azure.cloud1.contoso.com",
},
},
{
Name: "CustomCloud2",
DisplayName: "Custom Cloud 2",
AadAuthority: "https://login.cloud2.contoso.com/",
Properties: map[string]string{
"azureDataExplorerSuffix": ".kusto.cloud2.contoso.com",
"logAnalytics": "https://api.loganalytics.cloud2.contoso.com",
"portal": "https://portal.azure.cloud2.contoso.com",
"prometheusResourceId": "https://prometheus.monitor.cloud2.azure.contoso.com",
"resourceManager": "https://management.azure.cloud2.contoso.com",
},
},
}

func TestGetCloudsNoCustomClouds(t *testing.T) {
settings := &AzureSettings{}

clouds := settings.Clouds()
Expand All @@ -18,13 +45,29 @@ func TestGetClouds(t *testing.T) {
assert.Equal(t, clouds[2].Name, "AzureUSGovernment")
}

func TestGetCloudsWithCustomClouds(t *testing.T) {
settings := &AzureSettings{}
settings.customClouds = testCustomClouds

// should merge predefined and custom clouds into one list
clouds := settings.Clouds()

assert.Len(t, clouds, 5)
assert.Equal(t, clouds[0].Name, "AzureCloud")
assert.Equal(t, clouds[1].Name, "AzureChinaCloud")
assert.Equal(t, clouds[2].Name, "AzureUSGovernment")
assert.Equal(t, clouds[3].Name, "CustomCloud1")
assert.Equal(t, clouds[4].Name, "CustomCloud2")
}

func TestGetCustomClouds(t *testing.T) {
settings := &AzureSettings{}
settings.customClouds = testCustomClouds

// should return ONLY the custom clouds
clouds := settings.CustomClouds()

// Configuration of Azure clouds not yet supported
assert.Len(t, clouds, 0)
assert.Len(t, clouds, len(testCustomClouds))
}

func TestGetCloud(t *testing.T) {
Expand All @@ -43,3 +86,38 @@ func TestGetCloud(t *testing.T) {
assert.Error(t, err)
})
}

func TestSetCustomClouds(t *testing.T) {
settings := &AzureSettings{}

json := `[
{
"name":"CustomCloud1",
"displayName":"Custom Cloud 1",
"aadAuthority":"https://login.contoso.com/",
"properties":{
"azureDataExplorerSuffix":".kusto.cloud1.contoso.com",
"logAnalytics":"https://api.loganalytics.cloud1.contoso.com",
"portal":"https://portal.azure.cloud1.contoso.com",
"prometheusResourceId":"https://prometheus.monitor.azure.cloud1.contoso.com",
"resourceManager":"https://management.azure.cloud1.contoso.com"
}
}
]`

err := settings.SetCustomClouds(json)
assert.Nil(t, err)

clouds := settings.getCustomClouds()

assert.Len(t, clouds, 1)
cloud := clouds[0]
assert.Equal(t, cloud.Name, "CustomCloud1")
assert.Equal(t, cloud.DisplayName, "Custom Cloud 1")
assert.Equal(t, cloud.AadAuthority, "https://login.contoso.com/")
assert.Len(t, cloud.Properties, 5)

cloud2, err := settings.GetCloud("CustomCloud1")
assert.Nil(t, err)
assert.Equal(t, cloud2.Name, "CustomCloud1")
}
10 changes: 9 additions & 1 deletion azsettings/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

const (
AzureCloud = "GFAZPL_AZURE_CLOUD"
AzureCloud = "GFAZPL_AZURE_CLOUD"
AzureCustomCloudsConfig = "GFAZPL_AZURE_CLOUDS_CONFIG"

AzureAuthEnabled = "GFAZPL_AZURE_AUTH_ENABLED"

Expand Down Expand Up @@ -45,6 +46,13 @@ func ReadFromEnv() (*AzureSettings, error) {
azureSettings.AzureAuthEnabled = true
}

if customCloudsJSON := envutil.GetOrDefault(AzureCustomCloudsConfig, ""); customCloudsJSON != "" {
// this method will parse the JSON and set the custom cloud list in one go
if err := azureSettings.SetCustomClouds(customCloudsJSON); err != nil {
return nil, err
}
}

// Managed Identity authentication
if msiEnabled, err := envutil.GetBoolOrFallback(ManagedIdentityEnabled, fallbackManagedIdentityEnabled, false); err != nil {
err = fmt.Errorf("invalid Azure configuration: %w", err)
Expand Down
7 changes: 7 additions & 0 deletions azsettings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type AzureSettings struct {

// This field determines which plugins will receive the settings via plugin context
ForwardSettingsPlugins []string

customClouds []*AzureCloudSettings
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized we may need to export this customClouds field after all - I was trying to keep it private to the package to push callers to use the functions, but inside grafana/grafana, we need to write this list back out as a json string when we push settings over to plugins via env variables. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be fine to remain private for now. In grafana/grafana we can still call the Clouds or CustomClouds functions to retrieve the values.

Copy link
Contributor Author

@JonCole JonCole May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clouds() and CustomClouds() return AzureCloudInfo struct, which does not have all the fields that are on AzureCloudSettings struct. grafana/grafana sometimes needs the extra info on AzureCloudSettings. I think we might eventually want to get rid of the AzureCloudInfo and just return the full set of fields in one struct. Callers can use the same set of methods for all use cases. If a caller doesn't need the additional fields, they can ingore them. Thoughts? For now, I was just leaning toward exporting customClouds. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with exporting the field for now, there's nothing that would be considered a secret in there afaict

}

type WorkloadIdentitySettings struct {
Expand Down Expand Up @@ -60,6 +62,11 @@ func ReadFromContext(ctx context.Context) (*AzureSettings, bool) {
settings.AzureAuthEnabled = true
}

if customCloudsJSON := cfg.Get(AzureCustomCloudsConfig); customCloudsJSON != "" {
// this method will parse the JSON and set the custom cloud list in one go
_ = settings.SetCustomClouds(customCloudsJSON)
}

hasSettings := false
if v := cfg.Get(AzureCloud); v != "" {
settings.Cloud = v
Expand Down
73 changes: 36 additions & 37 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.10.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/grafana/grafana-plugin-sdk-go v0.219.0
github.com/grafana/grafana-plugin-sdk-go v0.230.0
github.com/stretchr/testify v1.9.0
google.golang.org/grpc v1.62.1
google.golang.org/grpc v1.63.2
)

require (
Expand All @@ -19,30 +19,30 @@ require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/apache/arrow/go/v15 v15.0.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cheekybits/genny v1.0.0 // indirect
github.com/chromedp/cdproto v0.0.0-20220208224320-6efb837e6bc2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/elazarl/goproxy v0.0.0-20230731152917-f99041a5c027 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/getkin/kin-openapi v0.120.0 // indirect
github.com/getkin/kin-openapi v0.124.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/flatbuffers v23.5.26+incompatible // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 // indirect
github.com/hashicorp/go-hclog v1.6.2 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/hashicorp/go-hclog v1.6.3 // indirect
github.com/hashicorp/go-plugin v1.6.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/invopop/yaml v0.2.0 // indirect
Expand All @@ -67,38 +67,37 @@ require (
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.46.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.53.0 // indirect
github.com/prometheus/procfs v0.14.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/unknwon/bra v0.0.0-20200517080246-1e3013ecaff8 // indirect
github.com/unknwon/com v1.0.1 // indirect
github.com/unknwon/log v0.0.0-20150304194804-e617c87089d3 // indirect
github.com/urfave/cli v1.22.14 // indirect
github.com/urfave/cli v1.22.15 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.49.0 // indirect
go.opentelemetry.io/contrib/propagators/jaeger v1.22.0 // indirect
go.opentelemetry.io/contrib/samplers/jaegerremote v0.18.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.opentelemetry.io/proto/otlp v1.1.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.51.0 // indirect
go.opentelemetry.io/contrib/propagators/jaeger v1.26.0 // indirect
go.opentelemetry.io/contrib/samplers/jaegerremote v0.20.0 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/sdk v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
go.opentelemetry.io/proto/otlp v1.2.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.14.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading