Skip to content

Commit

Permalink
Replace viper with go-envconfig for easier parsing, enable github gui…
Browse files Browse the repository at this point in the history
…ld cache
  • Loading branch information
Richard87 committed Apr 9, 2024
1 parent 5233b6c commit a313484
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 44 deletions.
5 changes: 0 additions & 5 deletions .env

This file was deleted.

7 changes: 7 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ 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:
registry: ${{ env.REGISTRY }}
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
Expand All @@ -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

25 changes: 5 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
41 changes: 22 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit a313484

Please sign in to comment.