From a313484a959bc4c76e99fc72f43de15ad11d0b66 Mon Sep 17 00:00:00 2001 From: Richard87 Date: Tue, 9 Apr 2024 10:49:35 +0200 Subject: [PATCH] Replace viper with go-envconfig for easier parsing, enable github guild cache --- .env | 5 ----- .github/workflows/build.yaml | 7 ++++++ go.mod | 25 +++++----------------- main.go | 41 +++++++++++++++++++----------------- 4 files changed, 34 insertions(+), 44 deletions(-) delete mode 100644 .env diff --git a/.env b/.env deleted file mode 100644 index b21908e..0000000 --- a/.env +++ /dev/null @@ -1,5 +0,0 @@ -ISSUER=https://northeurope.oic.prod-aks.azure.com/3aa4a235-b6e2-48d5-9195-7fcf05b459b0/a2d93ba1-cbde-4408-8979-c100cce7b448/ -AUDIENCE=extmonprom -SUBJECT_REGEX=system:serviceaccount:monitor:prometheus-operator-prometheus -LOG_PRETTY=true -LOG_LEVEL=debug diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 94b1f5c..983d09e 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -27,6 +27,7 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v4 + - name: Log in to the Container registry uses: docker/login-action@e92390c5fb421da1463c202d546fed0ec5c39f20 # v3.1.0 with: @@ -34,6 +35,9 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@d70bba72b1f3fd22344832f00baa16ece964efeb # v3.3.0 + - name: Extract metadata (tags, labels) for Docker id: meta uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.1.1 @@ -47,3 +51,6 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max + diff --git a/go.mod b/go.mod index 9d9ac4a..a0a33d6 100644 --- a/go.mod +++ b/go.mod @@ -5,41 +5,26 @@ go 1.22 require ( github.com/coreos/go-oidc/v3 v3.10.0 github.com/rs/zerolog v1.32.0 - github.com/spf13/viper v1.18.1 // 1.18.2 removes automatic bind env variables ) -require github.com/stretchr/testify v1.8.4 +require ( + github.com/sethvargo/go-envconfig v1.0.1 + github.com/stretchr/testify v1.8.4 +) require ( github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect - github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-jose/go-jose/v4 v4.0.1 // indirect github.com/golang/protobuf v1.5.3 // indirect - github.com/google/go-cmp v0.6.0 // indirect - github.com/hashicorp/hcl v1.0.0 // indirect - github.com/magiconair/properties v1.8.7 // indirect + github.com/kr/pretty v0.3.1 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect - github.com/mitchellh/mapstructure v1.5.0 // indirect - github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect - github.com/sagikazarmark/locafero v0.4.0 // indirect - github.com/sagikazarmark/slog-shim v0.1.0 // indirect - github.com/sourcegraph/conc v0.3.0 // indirect - github.com/spf13/afero v1.11.0 // indirect - github.com/spf13/cast v1.6.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect - github.com/subosito/gotenv v1.6.0 // indirect - go.uber.org/atomic v1.9.0 // indirect - go.uber.org/multierr v1.9.0 // indirect golang.org/x/crypto v0.19.0 // indirect - golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/oauth2 v0.15.0 // indirect golang.org/x/sys v0.17.0 // indirect - golang.org/x/text v0.14.0 // indirect google.golang.org/appengine v1.6.8 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect - gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/main.go b/main.go index a88521f..d2b007e 100644 --- a/main.go +++ b/main.go @@ -12,29 +12,37 @@ import ( "github.com/rs/zerolog" "github.com/rs/zerolog/log" - "github.com/spf13/viper" + "github.com/sethvargo/go-envconfig" ) type Options struct { - Issuer string `mapstructure:"issuer"` - Audience string `mapstructure:"audience"` - SubjectRegex string `mapstructure:"subject_regex"` - LogLevel string `mapstructure:"log_level"` - LogPretty bool `mapstructure:"log_pretty"` - Subjects []string `mapstructure:"subjects"` + Issuer string `env:"ISSUER, required"` + Audience string `env:"AUDIENCE, required"` + LogLevel string `env:"LOG_LEVEL, default=info"` + LogPretty bool `env:"LOG_PRETTY"` + Subjects []string `env:"SUBJECTS, required"` } func main() { + ctx := context.Background() var opts Options + err := envconfig.Process(ctx, &opts) + initLogger(opts) + + log.Info().Msg("Starting") + log.Info().Str("ISSUER", opts.Issuer).Send() + log.Info().Str("AUDIENCE", opts.Audience).Send() + log.Info().Str("LOG_LEVEL", opts.LogLevel).Send() + log.Info().Bool("LOG_PRETTY", opts.LogPretty).Send() + log.Info().Strs("SUBJECTS", opts.Subjects).Send() - viper.AutomaticEnv() - if err := viper.Unmarshal(&opts); err != nil { + // Print any failures from proccessing ENV here, + // se we can see available options + if err != nil { log.Fatal().Msg(err.Error()) } - initLogger(opts) - - Run(context.Background(), opts) + Run(ctx, opts) } func initLogger(opts Options) { @@ -60,7 +68,6 @@ func initLogger(opts Options) { } func Run(ctx context.Context, opts Options) { - log.Info().Interface("options", opts).Msg("Starting...") provider, err := oidc.NewProvider(ctx, opts.Issuer) if err != nil { @@ -73,13 +80,9 @@ func Run(ctx context.Context, opts Options) { verifier := provider.Verifier(oidcConfig) authHandler := AuthHandler(opts.Subjects, verifier) - http.Handle("POST /auth", authHandler) - http.Handle("GET /", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(404) - _, _ = w.Write([]byte("404 Not Found")) - })) + http.Handle("/auth", authHandler) - log.Info().Msg("Starting server on :8000...") + log.Info().Msg("Listening on http://localhost:8000...") err = http.ListenAndServe(":8000", nil) if err != nil && !errors.Is(err, http.ErrServerClosed) { log.Fatal().Err(err).Msgf("listen: %s", err)