From 188a072afe96b6dd538bb73544432d3490dc13db Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Wed, 1 May 2024 07:32:30 -0400 Subject: [PATCH 1/4] fix(breaking): sampling ratio yaml config (#3034) --- config/flipt.schema.cue | 6 +++--- config/flipt.schema.json | 2 +- internal/config/testdata/tracing/otlp.yml | 2 +- internal/config/testdata/tracing/wrong_sampling_ratio.yml | 2 +- internal/config/tracing.go | 8 ++++---- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/config/flipt.schema.cue b/config/flipt.schema.cue index 6ece3a1d55..6f9f456f70 100644 --- a/config/flipt.schema.cue +++ b/config/flipt.schema.cue @@ -282,9 +282,9 @@ import "strings" } #tracing: { - enabled?: bool | *false - exporter?: *"jaeger" | "zipkin" | "otlp" - samplingRatio?: float & >=0 & <=1 | *1 + enabled?: bool | *false + exporter?: *"jaeger" | "zipkin" | "otlp" + sampling_ratio?: float & >=0 & <=1 | *1 propagators?: [ ..."tracecontext" | "baggage" | "b3" | "b3multi" | "jaeger" | "xray" | "ottrace" | "none", ] | *["tracecontext", "baggage"] diff --git a/config/flipt.schema.json b/config/flipt.schema.json index 15e3a6bdad..b9e420f577 100644 --- a/config/flipt.schema.json +++ b/config/flipt.schema.json @@ -977,7 +977,7 @@ "enum": ["jaeger", "zipkin", "otlp"], "default": "jaeger" }, - "samplingRatio": { + "sampling_ratio": { "type": "number", "default": 1, "minimum": 0, diff --git a/internal/config/testdata/tracing/otlp.yml b/internal/config/testdata/tracing/otlp.yml index 15a1125ee7..942098392b 100644 --- a/internal/config/testdata/tracing/otlp.yml +++ b/internal/config/testdata/tracing/otlp.yml @@ -1,7 +1,7 @@ tracing: enabled: true exporter: otlp - samplingRatio: 0.5 + sampling_ratio: 0.5 otlp: endpoint: http://localhost:9999 headers: diff --git a/internal/config/testdata/tracing/wrong_sampling_ratio.yml b/internal/config/testdata/tracing/wrong_sampling_ratio.yml index 12d40e4a00..f52714ac7c 100644 --- a/internal/config/testdata/tracing/wrong_sampling_ratio.yml +++ b/internal/config/testdata/tracing/wrong_sampling_ratio.yml @@ -1,3 +1,3 @@ tracing: enabled: true - samplingRatio: 1.1 + sampling_ratio: 1.1 diff --git a/internal/config/tracing.go b/internal/config/tracing.go index f771ddde81..e613037b99 100644 --- a/internal/config/tracing.go +++ b/internal/config/tracing.go @@ -17,7 +17,7 @@ type TracingConfig struct { Enabled bool `json:"enabled" mapstructure:"enabled" yaml:"enabled"` Exporter TracingExporter `json:"exporter,omitempty" mapstructure:"exporter" yaml:"exporter,omitempty"` Propagators []TracingPropagator `json:"propagators,omitempty" mapstructure:"propagators" yaml:"propagators,omitempty"` - SamplingRatio float64 `json:"samplingRatio,omitempty" mapstructure:"samplingRatio" yaml:"samplingRatio,omitempty"` + SamplingRatio float64 `json:"samplingRatio,omitempty" mapstructure:"sampling_ratio" yaml:"sampling_ratio,omitempty"` Jaeger JaegerTracingConfig `json:"jaeger,omitempty" mapstructure:"jaeger" yaml:"jaeger,omitempty"` Zipkin ZipkinTracingConfig `json:"zipkin,omitempty" mapstructure:"zipkin" yaml:"zipkin,omitempty"` OTLP OTLPTracingConfig `json:"otlp,omitempty" mapstructure:"otlp" yaml:"otlp,omitempty"` @@ -25,9 +25,9 @@ type TracingConfig struct { func (c *TracingConfig) setDefaults(v *viper.Viper) error { v.SetDefault("tracing", map[string]any{ - "enabled": false, - "exporter": TracingJaeger, - "samplingRatio": 1, + "enabled": false, + "exporter": TracingJaeger, + "sampling_ratio": 1, "propagators": []TracingPropagator{ TracingPropagatorTraceContext, TracingPropagatorBaggage, From 076b7c155786b9007fbda25254599ad9c56350ca Mon Sep 17 00:00:00 2001 From: George MacRorie Date: Wed, 1 May 2024 12:33:20 +0100 Subject: [PATCH 2/4] feat(cmd/flipt): wire in flipt client listener --- cmd/flipt/main.go | 40 +++++++++++++++++++++++++--------------- go.mod | 1 + go.sum | 2 ++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/cmd/flipt/main.go b/cmd/flipt/main.go index 65af452e42..640981c64d 100644 --- a/cmd/flipt/main.go +++ b/cmd/flipt/main.go @@ -3,9 +3,11 @@ package main import ( "bytes" "context" + "crypto/tls" "errors" "fmt" "io/fs" + "net/http" "os" "os/signal" "path/filepath" @@ -22,6 +24,8 @@ import ( "go.flipt.io/flipt/internal/info" "go.flipt.io/flipt/internal/release" "go.flipt.io/flipt/internal/telemetry" + "go.flipt.io/reverst/client" + "go.flipt.io/reverst/pkg/protocol" "go.uber.org/zap" "go.uber.org/zap/zapcore" "golang.org/x/sync/errgroup" @@ -370,26 +374,32 @@ func run(ctx context.Context, logger *zap.Logger, cfg *config.Config) error { if cfg.Server.Cloud.Enabled { // starts QUIC tunnel server to connect to Cloud + var ( + orgHost = fmt.Sprintf("%s.%s", cfg.Server.Cloud.Organization, cfg.Server.Cloud.Address) + tunnel = fmt.Sprintf("%s-%s", cfg.Server.Cloud.Instance, orgHost) + ) - // TODO: get organization, instance, and authentication from config - - // g.Go(func() error { - // tunnelServer := &client.Server{ - // TunnelGroup: fmt.Sprintf("%s.%s", cfg.Server.Cloud.Organization, cfg.Server.Cloud.Address), - // Handler: httpServer.Handler, - // Authenticator: client.BearerAuthenticator(cfg.Server.Cloud.Authentication.ApiKey), - // } + g.Go(func() error { + tunnelServer := &client.Server{ + TunnelGroup: tunnel, + Handler: httpServer.Handler, + Authenticator: client.BearerAuthenticator(cfg.Server.Cloud.Authentication.ApiKey), + TLSConfig: &tls.Config{ + NextProtos: []string{protocol.Name}, + ServerName: orgHost, + }, + } - // tunnel := fmt.Sprintf("%s-%s.%s", cfg.Server.Cloud.Instance, cfg.Server.Cloud.Organization, cfg.Server.Cloud.Address) + addr := fmt.Sprintf("%s:%d", tunnel, cfg.Server.Cloud.Port) - // logger.Info("cloud tunnel available", zap.String("address", tunnel), zap.Int("port", cfg.Server.Cloud.Port)) + logger.Info("cloud tunnel established", zap.String("address", fmt.Sprintf("https://%s", tunnel))) - // if err := tunnelServer.DialAndServe(ctx, fmt.Sprintf("%s:%d", tunnel, cfg.Server.Cloud.Port)); !errors.Is(err, http.ErrServerClosed) { - // return fmt.Errorf("cloud tunnel server: %w", err) - // } + if err := tunnelServer.DialAndServe(ctx, addr); err != nil && !errors.Is(err, http.ErrServerClosed) { + return fmt.Errorf("cloud tunnel server: %w", err) + } - // return nil - // }) + return nil + }) } // block until root context is cancelled diff --git a/go.mod b/go.mod index 3015811f3c..062ed45360 100644 --- a/go.mod +++ b/go.mod @@ -245,6 +245,7 @@ require ( github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect github.com/yusufpapurcu/wmi v1.2.3 // indirect + go.flipt.io/reverst v0.1.2 // indirect go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect go.opentelemetry.io/contrib/propagators/aws v1.25.0 // indirect diff --git a/go.sum b/go.sum index 18d6e64774..67712b0506 100644 --- a/go.sum +++ b/go.sum @@ -728,6 +728,8 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw= github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= +go.flipt.io/reverst v0.1.2 h1:L43Jx5oWAQwOCK6J/bCN9fXgIIGymnJx1yyp2UCbb14= +go.flipt.io/reverst v0.1.2/go.mod h1:0kDf22udIDgZAruOu6/9/dZIHRpWJhDcZ3fqoP33jc0= go.mongodb.org/mongo-driver v1.11.4/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g= go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= From 2868dd38dec1bc47a4986130dfeef187eaace578 Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Wed, 1 May 2024 09:28:23 -0400 Subject: [PATCH 3/4] chore: add test to catch invalid struct tags (#3035) * chore: add test to catch invalid struct tags * chore: add typeName for better output * chore: add more context --- go.mod | 1 + go.sum | 2 + internal/config/config_test.go | 93 ++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) diff --git a/go.mod b/go.mod index 2e9babe96f..b35abd5d3f 100644 --- a/go.mod +++ b/go.mod @@ -42,6 +42,7 @@ require ( github.com/hashicorp/cap v0.6.0 github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/golang-lru/v2 v2.0.7 + github.com/iancoleman/strcase v0.3.0 github.com/jackc/pgx/v5 v5.5.5 github.com/libsql/libsql-client-go v0.0.0-20230917132930-48c310b27e7b github.com/magefile/mage v1.15.0 diff --git a/go.sum b/go.sum index 0cb67132b6..6d46072be4 100644 --- a/go.sum +++ b/go.sum @@ -384,6 +384,8 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= +github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= diff --git a/internal/config/config_test.go b/internal/config/config_test.go index f1e94ee3e7..902d2f18a3 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -15,6 +15,7 @@ import ( "testing" "time" + "github.com/iancoleman/strcase" "github.com/santhosh-tekuri/jsonschema/v5" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -1475,3 +1476,95 @@ func TestGetConfigFile(t *testing.T) { }) } } + +var ( + // add any struct tags to match their camelCase equivalents here. + camelCaseMatchers = map[string]string{ + "requireTLS": "requireTLS", + "discoveryURL": "discoveryURL", + } +) + +func TestStructTags(t *testing.T) { + configType := reflect.TypeOf(Config{}) + configTags := getStructTags(configType) + + for k, v := range camelCaseMatchers { + strcase.ConfigureAcronym(k, v) + } + + // Validate the struct tags for the Config struct. + // recursively validate the struct tags for all sub-structs. + validateStructTags(t, configTags, configType) +} + +func validateStructTags(t *testing.T, tags map[string]map[string]string, tType reflect.Type) { + tName := tType.Name() + for fieldName, fieldTags := range tags { + fieldType, ok := tType.FieldByName(fieldName) + require.True(t, ok, "field %s not found in type %s", fieldName, tName) + + // Validate the `json` struct tag. + jsonTag, ok := fieldTags["json"] + if ok { + require.True(t, isCamelCase(jsonTag), "json tag for field '%s.%s' should be camelCase but is '%s'", tName, fieldName, jsonTag) + } + + // Validate the `mapstructure` struct tag. + mapstructureTag, ok := fieldTags["mapstructure"] + if ok { + require.True(t, isSnakeCase(mapstructureTag), "mapstructure tag for field '%s.%s' should be snake_case but is '%s'", tName, fieldName, mapstructureTag) + } + + // Validate the `yaml` struct tag. + yamlTag, ok := fieldTags["yaml"] + if ok { + require.True(t, isSnakeCase(yamlTag), "yaml tag for field '%s.%s' should be snake_case but is '%s'", tName, fieldName, yamlTag) + } + + // recursively validate the struct tags for all sub-structs. + if fieldType.Type.Kind() == reflect.Struct { + validateStructTags(t, getStructTags(fieldType.Type), fieldType.Type) + } + } +} + +func isCamelCase(s string) bool { + return s == strcase.ToLowerCamel(s) +} + +func isSnakeCase(s string) bool { + return s == strcase.ToSnake(s) +} + +func getStructTags(t reflect.Type) map[string]map[string]string { + tags := make(map[string]map[string]string) + + for i := 0; i < t.NumField(); i++ { + field := t.Field(i) + + // Get the field name. + fieldName := field.Name + + // Get the field tags. + fieldTags := make(map[string]string) + for _, tag := range []string{"json", "mapstructure", "yaml"} { + tagValue := field.Tag.Get(tag) + if tagValue == "-" { + fieldTags[tag] = "skip" + continue + } + values := strings.Split(tagValue, ",") + if len(values) > 1 { + tagValue = values[0] + } + if tagValue != "" { + fieldTags[tag] = tagValue + } + } + + tags[fieldName] = fieldTags + } + + return tags +} From 7f59b05949b2e86570f0848c0a88ee29da559eb5 Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Wed, 1 May 2024 10:40:43 -0400 Subject: [PATCH 4/4] chore: revert rm config --- config/flipt.schema.cue | 5 --- config/flipt.schema.json | 18 ---------- go.mod | 10 ++++-- go.sum | 12 +++++++ go.work.sum | 5 ++- internal/config/config_test.go | 25 +++++++++++-- internal/config/server.go | 35 ++++++++++++++++--- .../testdata/server/cloud_missing_address.yml | 4 +++ .../server/cloud_missing_authentication.yml | 7 ++++ .../server/cloud_missing_instance.yml | 8 +++++ .../server/cloud_missing_organization.yml | 8 +++++ .../testdata/server/cloud_missing_port.yml | 6 +++- 12 files changed, 109 insertions(+), 34 deletions(-) create mode 100644 internal/config/testdata/server/cloud_missing_authentication.yml create mode 100644 internal/config/testdata/server/cloud_missing_instance.yml create mode 100644 internal/config/testdata/server/cloud_missing_organization.yml diff --git a/config/flipt.schema.cue b/config/flipt.schema.cue index c5879bf128..6f9f456f70 100644 --- a/config/flipt.schema.cue +++ b/config/flipt.schema.cue @@ -269,11 +269,6 @@ import "strings" grpc_conn_max_idle_time?: =~#duration grpc_conn_max_age?: =~#duration grpc_conn_max_age_grace?: =~#duration - cloud?: { - enabled?: bool | *false - address?: string | *"https://flipt.cloud" - port?: int | *8443 - } } #metrics: { diff --git a/config/flipt.schema.json b/config/flipt.schema.json index 02751b5170..b9e420f577 100644 --- a/config/flipt.schema.json +++ b/config/flipt.schema.json @@ -928,24 +928,6 @@ "grpc_conn_max_age_grace": { "type": "string", "pattern": "^([0-9]+(ns|us|µs|ms|s|m|h))+$" - }, - "cloud": { - "type": "object", - "additionalProperties": false, - "properties": { - "enabled": { - "type": "boolean", - "default": false - }, - "address": { - "type": "string", - "default": "https://flipt.cloud" - }, - "port": { - "type": "integer", - "default": 8443 - } - } } }, "required": [], diff --git a/go.mod b/go.mod index 104cef3d11..558a8d1aa2 100644 --- a/go.mod +++ b/go.mod @@ -66,6 +66,7 @@ require ( go.flipt.io/flipt/errors v1.19.3 go.flipt.io/flipt/rpc/flipt v1.38.0 go.flipt.io/flipt/sdk/go v0.11.0 + go.flipt.io/reverst v0.1.2 go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 go.opentelemetry.io/contrib/propagators/autoprop v0.50.0 go.opentelemetry.io/otel v1.26.0 @@ -161,12 +162,13 @@ require ( github.com/go-logr/logr v1.4.1 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.2.6 // indirect - github.com/gobwas/ws v1.2.1 // indirect + github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt/v5 v5.2.1 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/go-querystring v1.1.0 // indirect + github.com/google/pprof v0.0.0-20240416155748-26353dc0451f // indirect github.com/google/s2a-go v0.1.7 // indirect github.com/google/wire v0.6.0 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect @@ -206,7 +208,7 @@ require ( github.com/moby/sys/user v0.1.0 // indirect github.com/moby/term v0.5.0 // indirect github.com/morikuni/aec v1.0.0 // indirect - github.com/onsi/gomega v1.30.0 // indirect + github.com/onsi/ginkgo/v2 v2.17.1 // indirect github.com/openzipkin/zipkin-go v0.4.2 // indirect github.com/paulmach/orb v0.11.1 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect @@ -219,6 +221,8 @@ require ( github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.48.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect + github.com/quic-go/qpack v0.4.0 // indirect + github.com/quic-go/quic-go v0.42.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect @@ -246,7 +250,6 @@ require ( github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect github.com/yusufpapurcu/wmi v1.2.3 // indirect - go.flipt.io/reverst v0.1.2 // indirect go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect go.opentelemetry.io/contrib/propagators/aws v1.25.0 // indirect @@ -255,6 +258,7 @@ require ( go.opentelemetry.io/contrib/propagators/ot v1.25.0 // indirect go.opentelemetry.io/proto/otlp v1.2.0 // indirect go.uber.org/atomic v1.11.0 // indirect + go.uber.org/mock v0.4.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/mod v0.17.0 // indirect golang.org/x/sys v0.19.0 // indirect diff --git a/go.sum b/go.sum index 1639a377af..1cce865785 100644 --- a/go.sum +++ b/go.sum @@ -260,6 +260,8 @@ github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpv github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= +github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= +github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= @@ -330,6 +332,8 @@ github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/ github.com/google/martian/v3 v3.3.2 h1:IqNFLAmvJOgVlpdEBiQbDc2EwKW77amAycfTuWKdfvw= github.com/google/martian/v3 v3.3.2/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/pprof v0.0.0-20240416155748-26353dc0451f h1:WpZiq8iqvGjJ3m3wzAVKL6+0vz7VkE79iSy9GII00II= +github.com/google/pprof v0.0.0-20240416155748-26353dc0451f/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o= github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw= @@ -557,6 +561,8 @@ github.com/onsi/ginkgo/v2 v2.3.0/go.mod h1:Eew0uilEqZmIEZr8JrvYlvOM7Rr6xzTmMV8Ay github.com/onsi/ginkgo/v2 v2.4.0/go.mod h1:iHkDK1fKGcBoEHT5W7YBq4RFWaQulw+caOMkAt4OrFo= github.com/onsi/ginkgo/v2 v2.5.0/go.mod h1:Luc4sArBICYCS8THh8v3i3i5CuSZO+RaQRaJoeNwomw= github.com/onsi/ginkgo/v2 v2.7.0/go.mod h1:yjiuMwPokqY1XauOgju45q3sJt6VzQ/Fict1LFVcsAo= +github.com/onsi/ginkgo/v2 v2.17.1 h1:V++EzdbhI4ZV4ev0UTIj0PzhzOcReJFyJaLjtSF55M8= +github.com/onsi/ginkgo/v2 v2.17.1/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= @@ -608,6 +614,10 @@ github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/protocolbuffers/txtpbfmt v0.0.0-20230328191034-3462fbc510c0 h1:sadMIsgmHpEOGbUs6VtHBXRR1OHevnj7hLx9ZcdNGW4= github.com/protocolbuffers/txtpbfmt v0.0.0-20230328191034-3462fbc510c0/go.mod h1:jgxiZysxFPM+iWKwQwPR+y+Jvo54ARd4EisXxKYpB5c= +github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo= +github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A= +github.com/quic-go/quic-go v0.42.0 h1:uSfdap0eveIl8KXnipv9K7nlwZ5IqLlYOpJ58u5utpM= +github.com/quic-go/quic-go v0.42.0/go.mod h1:132kz4kL3F9vxhW3CtQJLDVwcFe5wdWeJXXijhsO57M= github.com/redis/go-redis/v9 v9.0.0-rc.4/go.mod h1:Vo3EsyWnicKnSKCA7HhgnvnyA74wOA69Cd2Meli5mmA= github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8= github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= @@ -786,6 +796,8 @@ go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0 go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU= +go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= diff --git a/go.work.sum b/go.work.sum index 8675dea4ae..ecd18b2f9e 100644 --- a/go.work.sum +++ b/go.work.sum @@ -648,7 +648,6 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p github.com/hashicorp/memberlist v0.5.0/go.mod h1:yvyXLpo0QaGE59Y7hDTsTzDD25JYBZ4mHgHUZ8lrOI0= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4= -github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw= github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= @@ -903,6 +902,10 @@ github.com/prometheus/prometheus v0.50.1/go.mod h1:FvE8dtQ1Ww63IlyKBn1V4s+zMwF9k github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/quasilyte/go-ruleguard/dsl v0.3.22/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU= github.com/quasilyte/go-ruleguard/rules v0.0.0-20211022131956-028d6511ab71/go.mod h1:4cgAphtvu7Ftv7vOT2ZOYhC6CvBxZixcasr8qIOTA50= +github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo= +github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A= +github.com/quic-go/quic-go v0.42.0 h1:uSfdap0eveIl8KXnipv9K7nlwZ5IqLlYOpJ58u5utpM= +github.com/quic-go/quic-go v0.42.0/go.mod h1:132kz4kL3F9vxhW3CtQJLDVwcFe5wdWeJXXijhsO57M= github.com/rabbitmq/amqp091-go v1.8.1/go.mod h1:+jPrT9iY2eLjRaMSRHUhc3z14E/l85kv/f+6luSD3pc= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 8f9bbeae41..81cd746e5a 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -568,19 +568,40 @@ func TestLoad(t *testing.T) { expected: func() *Config { cfg := Default() cfg.Server.Cloud.Enabled = true + cfg.Server.Cloud.Authentication.ApiKey = "foo" cfg.Server.Cloud.Port = 8443 - cfg.Server.Cloud.Address = "https://flipt.cloud" + cfg.Server.Cloud.Address = "flipt.cloud" + cfg.Server.Cloud.Organization = "foo" + cfg.Server.Cloud.Instance = "bar" return cfg }, }, + { + name: "cloud missing organization", + path: "./testdata/server/cloud_missing_organization.yml", + wantErr: errors.New("field \"server.cloud.organization\": non-empty value is required"), + }, + { + name: "cloud missing instance", + path: "./testdata/server/cloud_missing_instance.yml", + wantErr: errors.New("field \"server.cloud.instance\": non-empty value is required"), + }, + { + name: "cloud missing authentication", + path: "./testdata/server/cloud_missing_authentication.yml", + wantErr: errors.New("field \"server.cloud.authentication.api_key\": non-empty value is required"), + }, { name: "cloud missing port", path: "./testdata/server/cloud_missing_port.yml", expected: func() *Config { cfg := Default() cfg.Server.Cloud.Enabled = true + cfg.Server.Cloud.Authentication.ApiKey = "foo" cfg.Server.Cloud.Port = 8443 - cfg.Server.Cloud.Address = "https://flipt.cloud" + cfg.Server.Cloud.Address = "flipt.cloud" + cfg.Server.Cloud.Organization = "foo" + cfg.Server.Cloud.Instance = "bar" return cfg }, }, diff --git a/internal/config/server.go b/internal/config/server.go index f74ef72298..ef995d497f 100644 --- a/internal/config/server.go +++ b/internal/config/server.go @@ -96,15 +96,30 @@ var ( ) type CloudConfig struct { - Enabled bool `json:"enabled,omitempty" mapstructure:"enabled" yaml:"enabled"` - Address string `json:"address,omitempty" mapstructure:"address" yaml:"address,omitempty"` - Port int `json:"port,omitempty" mapstructure:"port" yaml:"port,omitempty"` + Enabled bool `json:"enabled,omitempty" mapstructure:"enabled" yaml:"enabled"` + Authentication CloudAuthenticationConfig `json:"authentication,omitempty" mapstructure:"authentication" yaml:"authentication,omitempty"` + Address string `json:"address,omitempty" mapstructure:"address" yaml:"address,omitempty"` + Port int `json:"port,omitempty" mapstructure:"port" yaml:"port,omitempty"` + Organization string `json:"organization,omitempty" mapstructure:"organization" yaml:"organization,omitempty"` + Instance string `json:"instance,omitempty" mapstructure:"instance" yaml:"instance,omitempty"` +} + +type CloudAuthenticationConfig struct { + ApiKey string `json:"-" mapstructure:"api_key" yaml:"api_key,omitempty"` +} + +func (c *CloudAuthenticationConfig) validate() error { + if c.ApiKey == "" { + return errFieldRequired("server.cloud.authentication.api_key") + } + + return nil } func (c *CloudConfig) setDefaults(v *viper.Viper) error { v.SetDefault("server.cloud", map[string]any{ "enabled": false, - "address": "https://flipt.cloud", + "address": "flipt.cloud", "port": 8443, }) @@ -121,6 +136,18 @@ func (c *CloudConfig) validate() error { if c.Port == 0 { return errFieldRequired("server.cloud.port") } + + if c.Organization == "" { + return errFieldRequired("server.cloud.organization") + } + + if c.Instance == "" { + return errFieldRequired("server.cloud.instance") + } + + if c.Authentication.ApiKey == "" { + return errFieldRequired("server.cloud.authentication.api_key") + } } return nil diff --git a/internal/config/testdata/server/cloud_missing_address.yml b/internal/config/testdata/server/cloud_missing_address.yml index 9e1acb7bd8..61b713b3be 100644 --- a/internal/config/testdata/server/cloud_missing_address.yml +++ b/internal/config/testdata/server/cloud_missing_address.yml @@ -1,4 +1,8 @@ server: cloud: + authentication: + api_key: foo enabled: true port: 8443 + organization: foo + instance: bar diff --git a/internal/config/testdata/server/cloud_missing_authentication.yml b/internal/config/testdata/server/cloud_missing_authentication.yml new file mode 100644 index 0000000000..557664fc83 --- /dev/null +++ b/internal/config/testdata/server/cloud_missing_authentication.yml @@ -0,0 +1,7 @@ +server: + cloud: + enabled: true + address: "https://flipt.cloud" + port: 8443 + organization: foo + instance: bar diff --git a/internal/config/testdata/server/cloud_missing_instance.yml b/internal/config/testdata/server/cloud_missing_instance.yml new file mode 100644 index 0000000000..37cefe41e0 --- /dev/null +++ b/internal/config/testdata/server/cloud_missing_instance.yml @@ -0,0 +1,8 @@ +server: + cloud: + enabled: true + authentication: + api_key: foo + address: "https://flipt.cloud" + port: 8443 + organization: foo diff --git a/internal/config/testdata/server/cloud_missing_organization.yml b/internal/config/testdata/server/cloud_missing_organization.yml new file mode 100644 index 0000000000..3adaaf7868 --- /dev/null +++ b/internal/config/testdata/server/cloud_missing_organization.yml @@ -0,0 +1,8 @@ +server: + cloud: + enabled: true + authentication: + api_key: foo + address: "https://flipt.cloud" + port: 8443 + instance: bar diff --git a/internal/config/testdata/server/cloud_missing_port.yml b/internal/config/testdata/server/cloud_missing_port.yml index b75a2721bd..c855f1eed2 100644 --- a/internal/config/testdata/server/cloud_missing_port.yml +++ b/internal/config/testdata/server/cloud_missing_port.yml @@ -1,4 +1,8 @@ server: cloud: + authentication: + api_key: foo enabled: true - address: "https://flipt.cloud" + address: "flipt.cloud" + organization: foo + instance: bar