-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (75 loc) · 2.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"context"
"errors"
"io"
"net/http"
"os"
"time"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/sethvargo/go-envconfig"
)
type Options struct {
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()
// Print any failures from proccessing ENV here,
// se we can see available options
if err != nil {
log.Fatal().Msg(err.Error())
}
Run(ctx, opts)
}
func initLogger(opts *Options) {
logLevel, err := zerolog.ParseLevel(opts.LogLevel)
if err != nil {
logLevel = zerolog.InfoLevel
log.Warn().Msgf("Invalid log level '%s', fallback to '%s'", opts.LogLevel, logLevel.String())
}
if logLevel == zerolog.NoLevel {
logLevel = zerolog.InfoLevel
}
opts.LogLevel = logLevel.String()
var logWriter io.Writer = os.Stderr
if opts.LogPretty {
logWriter = &zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.TimeOnly}
}
zerolog.DurationFieldUnit = time.Millisecond
logger := zerolog.New(logWriter).Level(logLevel).With().Timestamp().Logger()
log.Logger = logger
zerolog.DefaultContextLogger = &logger
}
func Run(ctx context.Context, opts Options) {
provider, err := oidc.NewProvider(ctx, opts.Issuer)
if err != nil {
log.Fatal().Err(err).Str("issuer", opts.Issuer).Msg("Failed to create oidc provider")
}
oidcConfig := &oidc.Config{
ClientID: opts.Audience,
}
verifier := provider.Verifier(oidcConfig)
authHandler := AuthHandler(opts.Subjects, verifier)
http.Handle("/auth", authHandler)
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)
}
log.Info().Msg("Server exiting")
}