-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
187 lines (158 loc) · 5.57 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"sync/atomic"
"syscall"
"time"
"github.com/krateoplatformops/authn/internal/env"
kubeconfig "github.com/krateoplatformops/authn/internal/helpers/kube/config"
"github.com/krateoplatformops/authn/internal/helpers/kube/util"
"github.com/krateoplatformops/authn/internal/middlewares/cors"
"github.com/krateoplatformops/authn/internal/routes"
"github.com/krateoplatformops/authn/internal/routes/auth/basic"
"github.com/krateoplatformops/authn/internal/routes/auth/github"
"github.com/krateoplatformops/authn/internal/routes/auth/info"
"github.com/krateoplatformops/authn/internal/routes/auth/ldap"
"github.com/krateoplatformops/authn/internal/routes/auth/oidc"
"github.com/krateoplatformops/authn/internal/routes/auth/strategies"
"github.com/krateoplatformops/authn/internal/routes/health"
"github.com/rs/zerolog"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
serviceName = "auth-service"
)
var (
Version string
Build string
)
func main() {
// Flags
kconfig := flag.String(clientcmd.RecommendedConfigPathFlag, "", "absolute path to the kubeconfig file")
debugOn := flag.Bool("debug", env.Bool("AUTHN_DEBUG", false), "dump verbose output")
dumpEnv := flag.Bool("dump-env", env.Bool("AUTHN_DUMP_ENV", false), "dump environment variables")
corsOn := flag.Bool("cors", env.Bool("AUTHN_CORS", true), "enable or disable CORS")
servicePort := flag.Int("port", env.Int("AUTHN_PORT", 8082), "port to listen on")
certExpiresIn := flag.Duration("cert-expires",
env.Duration("AUTHN_KUBECONFIG_CRT_EXPIRES_IN", time.Hour*24), "generated certificate duration (default: 24h)")
clusterName := flag.String("kubeconfig-cluster-name",
env.String("AUTHN_KUBECONFIG_CLUSTER_NAME", "krateo"), "cluster name for generated kubeconfig")
kubernetesURL := flag.String("kubeconfig-server-url",
env.String("AUTHN_KUBECONFIG_SERVER_URL", ""), "kubernetes api server url for generated kubeconfig")
storageNamespace := flag.String("namespace",
env.String("AUTHN_NAMESPACE", ""), "namespace where to store secrets with generated config")
flag.Usage = func() {
fmt.Fprintln(flag.CommandLine.Output(), "Flags:")
flag.PrintDefaults()
}
flag.Parse()
if len(*storageNamespace) > 0 {
os.Setenv(util.NamespaceEnvVar, *storageNamespace)
}
// Initialize the logger
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
// Default level for this log is info, unless debug flag is present
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *debugOn {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
log := zerolog.New(os.Stdout).With().
Str("service", serviceName).
Timestamp().
Logger()
if log.Debug().Enabled() {
evt := log.Debug().
Str("version", Version).
Str("build", Build).
Str("debug", fmt.Sprintf("%t", *debugOn)).
Str("cors", fmt.Sprintf("%t", *corsOn)).
Str("port", fmt.Sprintf("%d", *servicePort)).
Str("clusterName", *clusterName).
Str("kubernetesURL", *kubernetesURL).
Dur("certExpire", *certExpiresIn)
if *dumpEnv {
evt = evt.Strs("env-vars", os.Environ())
}
evt.Msg("configuration and env vars info")
}
// Kubernetes configuration
var cfg *rest.Config
var err error
if len(*kconfig) > 0 {
cfg, err = clientcmd.BuildConfigFromFlags("", *kconfig)
} else {
cfg, err = rest.InClusterConfig()
}
if err != nil {
log.Fatal().Err(err).Msg("resolving kubeconfig for rest client")
}
gen := kubeconfig.NewGenerator(cfg,
kubeconfig.KubernetesURL(*kubernetesURL),
kubeconfig.CertDuration(*certExpiresIn),
kubeconfig.ClusterName(*clusterName),
kubeconfig.Log(log),
)
healthy := int32(0)
all := []routes.Route{}
all = append(all, health.Check(&healthy, Version, serviceName))
all = append(all, basic.Login(cfg, gen))
all = append(all, github.Login(cfg, gen))
all = append(all, ldap.Login(cfg, gen))
all = append(all, oidc.Login(cfg, gen))
all = append(all, strategies.List(cfg))
all = append(all, info.Info(cfg))
handler := routes.Serve(all, log)
if *corsOn {
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-Auth-Code"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
})
handler = c.Handler(handler)
}
server := &http.Server{
Addr: fmt.Sprintf(":%d", *servicePort),
Handler: handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 50 * time.Second,
IdleTimeout: 30 * time.Second,
}
ctx, stop := signal.NotifyContext(context.Background(), []os.Signal{
os.Interrupt,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGKILL,
syscall.SIGHUP,
syscall.SIGQUIT,
}...)
defer stop()
go func() {
atomic.StoreInt32(&healthy, 1)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal().Err(err).Msgf("could not listen on %s", server.Addr)
}
}()
// Listen for the interrupt signal.
log.Info().Msgf("server is ready to handle requests at @ %s", server.Addr)
<-ctx.Done()
// Restore default behavior on the interrupt signal and notify user of shutdown.
stop()
log.Info().Msg("server is shutting down gracefully, press Ctrl+C again to force")
atomic.StoreInt32(&healthy, 0)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
server.SetKeepAlivesEnabled(false)
if err := server.Shutdown(ctx); err != nil {
log.Fatal().Err(err).Msg("server forced to shutdown")
}
log.Info().Msg("server gracefully stopped")
}