-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
290 lines (260 loc) · 8.3 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Command auth-webhook provides a reference implementation for the K8s Athenz web hook
// for authentication and authorization. This is an opinionated implementation that performs
// user and resource mapping from k8s to Athenz in a specific way. The mapping code is
// extensively documented for rationale. You can use the program directly if it meets your
// needs. If not, you can still use the underlying API and supply custom mapping code
// and the main entry point.
//
// It exposes three endpoints, `/authn` for the authentication handler, `/authz` for the
// authorization handler, and `/healthz` that always returns a 200 OK response.
//
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
api "github.com/yahoo/k8s-athenz-webhook"
"github.com/yahoo/athenz/libs/go/zmssvctoken"
)
const (
defaultPort = 443
defaultNtokenPath = `/tokens/ntoken`
defaultZMSURL = `https://localhost/zms/v1`
defaultZTSURL = `https://localhost/zts/v1`
)
var (
defaultDenyUsers = []string{"system:anonymous"}
defaultDenyResources = []string{"podsecuritypolicies"}
defaultAdminResources = []string{"resourcequotas", "limitranges", "namespaces"}
)
// Version gets set by the build script via LDFLAGS
var Version string
func getVersion() string {
if Version == "" {
return "development version"
}
return Version
}
type params struct {
addr string
tls bool
certFile string
keyFile string
authn api.AuthenticationConfig
authz api.AuthorizationConfig
closers []io.Closer
shutdown time.Duration
}
func (p *params) Close() error {
for _, c := range p.closers {
c.Close()
}
return nil
}
func splitNames(s string) []string {
if strings.Trim(s, " \t") == "" {
return nil
}
list := strings.Split(s, ",")
ret := make([]string, 0, len(list))
for _, e := range list {
ret = append(ret, strings.Trim(e, " \t"))
}
return ret
}
var errEarlyExit = errors.New("early exit")
func parseFlags(program string, args []string) (*params, error) {
f := flag.NewFlagSet(program, flag.ContinueOnError)
join := func(list []string) string { return strings.Join(list, ",") }
var p params
var c api.Config
var ntokenPath, logFile, adminDomain, k8sGroups, traceEvents, help string
var validateToken, showVersion bool
var tokenInterval, timeout, shutdownGrace string
var policyDenyUsers, policyDenyResources, policyAdminResources string
f.StringVar(&p.addr, "listen", fmt.Sprintf("127.0.0.1:%d", defaultPort), "<ip>:<port> to listen on")
f.BoolVar(&p.tls, "tls", true, "TLS enable/disable")
f.StringVar(&p.certFile, "cert", "", "Path to TLS cert")
f.StringVar(&p.keyFile, "key", "", "Path to TLS key file")
f.StringVar(&logFile, "logfile", "", "File to write logs to. Defaults to stderr")
f.StringVar(&ntokenPath, "ntoken-path", defaultNtokenPath, "Path to ntoken")
f.StringVar(&c.ZMSEndpoint, "zms-url", defaultZMSURL, "URL to the ZMS endpoint")
f.StringVar(&c.ZTSEndpoint, "zts-url", defaultZTSURL, "URL to the ZTS endpoint")
f.StringVar(&c.AuthHeader, "auth-header", "Athenz-Principal-Auth", "Athenz auth header name")
f.BoolVar(&validateToken, "validate-token", true, "Validate the identity ntoken on load")
f.StringVar(&k8sGroups, "groups", "", "comma-separated list of k8s groups to add to user info")
f.BoolVar(&showVersion, "version", false, "Show version information")
f.StringVar(&traceEvents, "trace-events", "", "comma-separated events to trace. Values recognized are server|athenz|mapping")
f.StringVar(&tokenInterval, "token-refresh-interval", "1m", "interval to refresh ntoken from file")
f.StringVar(&timeout, "athenz-timeout", "10s", "timeout for Athenz requests")
f.StringVar(&help, "help-message", "", "help message to be returned on authorization errors")
f.StringVar(&shutdownGrace, "shutdown-grace", "10s", "grace period for connections to drain at shutdown")
f.StringVar(&adminDomain, "cluster-domain", "kubernetes", "Athenz domain for administration of this cluster")
f.StringVar(&policyDenyUsers, "policy-deny-users", join(defaultDenyUsers), "comma-separated users to deny")
f.StringVar(&policyDenyResources, "policy-deny-resources", join(defaultDenyResources), "comma-separated resources to deny")
f.StringVar(&policyAdminResources, "policy-admin-resources", join(defaultAdminResources), "comma-separated resources to treat as admin")
err := f.Parse(args)
if err != nil {
return nil, err
}
if showVersion {
fmt.Println(getVersion())
return nil, errEarlyExit
}
// init logging
traces := splitNames(traceEvents)
for _, t := range traces {
switch strings.ToLower(t) {
case "server":
c.LogFlags |= api.LogTraceServer
case "athenz":
c.LogFlags |= api.LogTraceAthenz
case "mapping":
c.LogFlags |= api.LogVerboseMapping
default:
fmt.Fprintln(os.Stderr, "unsupported trace event", t, ", ignored")
}
}
w := os.Stderr
if logFile != "" {
w, err = os.OpenFile(logFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return nil, err
}
p.closers = append(p.closers, w)
}
c.LogProvider = NewLogProvider(w)
c.Timeout, err = time.ParseDuration(timeout)
if err != nil {
return nil, fmt.Errorf("invalid athenz client timeout %q, %v", timeout, err)
}
// init token provider
fti, err := time.ParseDuration(tokenInterval)
if err != nil {
return nil, fmt.Errorf("invalid token refresh interval %q, %v", tokenInterval, err)
}
ft, err := NewFileToken(ntokenPath, validateToken, fti)
if err != nil {
return nil, err
}
p.closers = append(p.closers, ft)
p.shutdown, err = time.ParseDuration(shutdownGrace)
if err != nil {
return nil, fmt.Errorf("invalid shutdown grace period %q, %v", shutdownGrace, err)
}
if p.tls {
if p.certFile == "" || p.keyFile == "" {
return nil, errors.New("must pass both key and cert files when TLS enabled")
}
}
// init mappers and configs
groups := splitNames(k8sGroups)
p.authn = api.AuthenticationConfig{
Config: c,
Mapper: &UserMapper{
Groups: groups,
},
}
p.authn.Config.Validator = zmssvctoken.NewTokenValidator(zmssvctoken.ValidationConfig{
ZTSBaseUrl: c.ZTSEndpoint,
PublicKeyFetchTimeout: 30 * time.Second,
CacheTTL: 2 * time.Hour,
})
p.authz = api.AuthorizationConfig{
Config: c,
Token: ft.TokenValue,
Mapper: &ResourceMapper{
AdminDomain: adminDomain,
DenyUsers: splitNames(policyDenyUsers),
DenyResources: splitNames(policyDenyResources),
AdminResources: splitNames(policyAdminResources),
},
HelpMessage: help,
}
return &p, nil
}
func run(program string, args []string, stopChan <-chan struct{}) error {
p, err := parseFlags(program, args)
if err != nil {
return err
}
defer p.Close()
postOnly := func(delegate http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, "only POST is supported for this endpoint")
return
}
delegate.ServeHTTP(w, r)
})
}
authn := api.NewAuthenticator(p.authn)
authz := api.NewAuthorizer(p.authz)
healthz := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "ok\n")
})
mux := http.NewServeMux()
mux.Handle("/authn", postOnly(authn))
mux.Handle("/authz", postOnly(authz))
mux.Handle("/healthz", healthz)
s := &http.Server{
Addr: p.addr,
Handler: mux,
}
done := make(chan error, 1)
go func() {
var err error
defer func() {
done <- err
}()
log.Printf("%s (%s)\n", program, getVersion())
if p.tls {
log.Printf("start TLS server on %s\n", p.addr)
err = s.ListenAndServeTLS(p.certFile, p.keyFile)
} else {
log.Printf("start server on %s\n", p.addr)
err = s.ListenAndServe()
}
}()
stopped := false
for {
select {
case err := <-done:
if stopped {
return nil
}
return err
case <-stopChan:
stopChan = nil // prevent additional channel firing
stopped = true
ctx, fn := context.WithTimeout(context.Background(), p.shutdown)
defer fn()
s.Shutdown(ctx)
}
}
}
func main() {
stopChan := make(chan struct{})
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, os.Interrupt)
go func() {
<-ch
log.Println("shutting down...")
close(stopChan)
}()
err := run(filepath.Base(os.Args[0]), os.Args[1:], stopChan)
if err != nil && err != errEarlyExit {
log.Fatalln("[FATAL]", err)
}
}