-
Notifications
You must be signed in to change notification settings - Fork 93
/
auth.go
419 lines (364 loc) · 12.5 KB
/
auth.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package main
import (
"crypto/ed25519"
cryptorand "crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"flag"
"fmt"
"math/big"
"math/rand"
"net"
"net/http"
"os"
"sort"
"strings"
"time"
"github.com/google/go-cmp/cmp"
)
const (
defaultRSABits = 4096
keyAlgorithmRSA = "rsa"
keyAlgorithmEd25519 = "ed25519"
)
// isFlagSet returns true if the flag was explicitly set in the command line by the user.
func isFlagSet(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
type tlsConfig struct {
// Provide a custom certificate.
certPath string
keyPath string
// Create a new self-signed certificate.
createSelfSigned bool
keyAlgorithm string
serverIP string
serverName string
// General mTLS flags.
insecureSkipVerify bool
minVersion uint
maxVersion uint
}
func newTLSConfigFromFlags() *tlsConfig {
c := &tlsConfig{}
flag.StringVar(&c.certPath, "tls-cert", "", "Path to the server TLS certificate")
flag.StringVar(&c.keyPath, "tls-key", "", "Path to the server TLS key")
flag.BoolVar(&c.createSelfSigned, "tls-create-self-signed", false, "If true, a self-signed certificate will be created and used as the TLS server certificate.")
flag.StringVar(&c.keyAlgorithm, "tls-key-algorithm", keyAlgorithmRSA, fmt.Sprintf("Which algorithm to use when creating a self-signed certificate. Supports %q or %q", keyAlgorithmRSA, keyAlgorithmEd25519))
flag.StringVar(&c.serverName, "tls-server-name", "Example", "Name of the server, used to verify the TLS certificate")
flag.StringVar(&c.serverIP, "tls-server-ip", "", "IP of the server. If unset, this will look for the POD_IP environment variable")
flag.BoolVar(&c.insecureSkipVerify, "tls-insecure-skip-verify", false, "Whether to skip verifying the certificate")
flag.UintVar(&c.minVersion, "tls-min-version", tls.VersionTLS12, "Minimum TLS version")
flag.UintVar(&c.maxVersion, "tls-max-version", tls.VersionTLS13, "Maximum TLS version")
return c
}
func (c *tlsConfig) isUserProvidedCertificate() bool {
return c.certPath != "" || c.keyPath != ""
}
func (c *tlsConfig) isSelfSignedCertificate() bool {
return c.createSelfSigned || isFlagSet("tls-key-algorithm") || isFlagSet("tls-server-name") || isFlagSet("tls-server-ip")
}
func (c *tlsConfig) hasCertificate() bool {
return c.isUserProvidedCertificate() || c.isSelfSignedCertificate()
}
func (c *tlsConfig) isEnabled() bool {
return c.hasCertificate() || isFlagSet("tls-insecure-skip-verify") || isFlagSet("tls-min-version") || isFlagSet("tls-max-version")
}
func (c *tlsConfig) validate() error {
errs := []error{}
if c.createSelfSigned && c.isUserProvidedCertificate() {
errs = append(errs, errors.New("--tls-create-self-signed and cannot be used together with use-provided certificate flags --tls-cert or --tls-key"))
}
if !c.createSelfSigned {
for _, flagName := range []string{"tls-key-algorithm", "tls-server-name", "tls-server-ip"} {
if isFlagSet(flagName) {
errs = append(errs, fmt.Errorf("--%s can only be specified with --tls-create-self-signed", flagName))
}
}
}
if c.isUserProvidedCertificate() && (c.certPath == "" || c.keyPath == "") {
errs = append(errs, errors.New("--tls-cert and --tls-key must both be set"))
}
if c.isEnabled() && !c.hasCertificate() {
for _, flagName := range []string{"tls-insecure-skip-verify", "tls-min-version", "tls-max-version"} {
if isFlagSet(flagName) {
errs = append(errs, fmt.Errorf("--%s can only be specified with --tls-cert or --tls-create-self-signed", flagName))
}
}
}
if c.keyAlgorithm != keyAlgorithmRSA && c.keyAlgorithm != keyAlgorithmEd25519 {
errs = append(errs, fmt.Errorf("key algorithm %q is invalid", c.keyAlgorithm))
}
if c.serverIP == "" {
c.serverIP = os.Getenv("POD_IP")
}
return errors.Join(errs...)
}
func (c *tlsConfig) getTLSConfig() (*tls.Config, error) {
if !c.isEnabled() {
return nil, nil
}
config := &tls.Config{
ServerName: c.serverName,
InsecureSkipVerify: c.insecureSkipVerify,
MinVersion: uint16(c.minVersion),
MaxVersion: uint16(c.maxVersion),
}
if c.createSelfSigned {
var privateKey, publicKey any
if c.keyAlgorithm == keyAlgorithmRSA {
rsaPrivateKey, err := rsa.GenerateKey(cryptorand.Reader, defaultRSABits)
if err != nil {
return nil, fmt.Errorf("unable to generate RSA key: %w", err)
}
privateKey = rsaPrivateKey
publicKey = &rsaPrivateKey.PublicKey
} else {
var err error
publicKey, privateKey, err = ed25519.GenerateKey(cryptorand.Reader)
if err != nil {
return nil, fmt.Errorf("unable to generate ed25519 key: %w", err)
}
}
template := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
Organization: []string{c.serverName},
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour * 24 * 30),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
if c.serverIP != "" {
template.IPAddresses = append(template.IPAddresses, net.ParseIP(c.serverIP))
}
certBytes, err := x509.CreateCertificate(cryptorand.Reader, &template, &template, publicKey, privateKey)
if err != nil {
return nil, fmt.Errorf("unable to create self-signed certificate: %w", err)
}
certPem := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: certBytes,
})
privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return nil, fmt.Errorf("unable to marshal private key: %w", err)
}
privateKeyPem := pem.EncodeToMemory(&pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKeyBytes,
})
cert, err := tls.X509KeyPair(certPem, privateKeyPem)
if err != nil {
return nil, fmt.Errorf("unable to encode self-signed certificate: %w", err)
}
config.Certificates = []tls.Certificate{cert}
} else if c.certPath != "" && c.keyPath != "" {
cert, err := tls.LoadX509KeyPair(c.certPath, c.keyPath)
if err != nil {
return nil, fmt.Errorf("unable to load server cert and key: %w", err)
}
config.Certificates = []tls.Certificate{cert}
}
return config, nil
}
type basicAuthConfig struct {
username string
password string
}
func newBasicAuthConfigFromFlags() *basicAuthConfig {
c := &basicAuthConfig{}
flag.StringVar(&c.username, "basic-auth-username", "", "BasicAuth username")
flag.StringVar(&c.password, "basic-auth-password", "", "BasicAuth password")
return c
}
func (c *basicAuthConfig) isEnabled() bool {
return c.username != "" || c.password != ""
}
func (c *basicAuthConfig) handle(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if ok && username == c.username && password == c.password {
handler.ServeHTTP(w, r)
return
}
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
})
}
func authorizationHandler(handler http.Handler, scheme, parameters string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
expected := scheme + " " + parameters
if strings.TrimSpace(auth) == strings.TrimSpace(expected) {
handler.ServeHTTP(w, r)
return
}
w.Header().Set("WWW-Authenticate", scheme+` realm="restricted", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
})
}
type authorizationConfig struct {
scheme string
parameters string
}
func newAuthorizationConfigFromFlags() *authorizationConfig {
c := &authorizationConfig{}
flag.StringVar(&c.scheme, "auth-scheme", "", "Authorization header scheme")
flag.StringVar(&c.parameters, "auth-parameters", "", "Data to require in the Authorization header")
return c
}
func (c *authorizationConfig) isEnabled() bool {
return c.scheme != "" || c.parameters != ""
}
func (c *authorizationConfig) validate() error {
var errs []error
if c.scheme == "" && c.parameters != "" {
errs = append(errs, errors.New("must specify --auth-scheme when using --auth-parameters"))
}
if c.scheme == "Basic" {
errs = append(errs, errors.New("use --basic-auth flags to specify BasicAuth"))
}
return errors.Join(errs...)
}
func (c *authorizationConfig) handle(handler http.Handler) http.Handler {
return authorizationHandler(handler, c.scheme, c.parameters)
}
type oauth2Config struct {
clientID string
clientSecret string
scopes string
accessToken string
}
func newOAuth2ConfigFromFlags() *oauth2Config {
c := &oauth2Config{}
flag.StringVar(&c.clientID, "oauth2-client-id", "", "OAuth2 client ID")
flag.StringVar(&c.clientSecret, "oauth2-client-secret", "", "OAuth2 client secret")
flag.StringVar(&c.scopes, "oauth2-scopes", "", "Required OAuth2 comma-separated scopes")
flag.StringVar(&c.accessToken, "oauth2-access-token", "", "OAuth2 access token or empty to generate one. /token will provision this token")
return c
}
func (c *oauth2Config) isEnabled() bool {
return c.clientID != "" || c.clientSecret != "" || c.scopes != "" || isFlagSet("oauth2-access-token")
}
const oauth2TokenCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~+/"
const defaultOAuth2TokenLength = 8
func (c *oauth2Config) validate() error {
if c.accessToken == "" {
builder := strings.Builder{}
builder.Grow(defaultOAuth2TokenLength)
for i := 0; i < builder.Cap(); i++ {
builder.WriteByte(oauth2TokenCharset[rand.Intn(len(oauth2TokenCharset))])
}
c.accessToken = builder.String()
}
return nil
}
func oauthTokenErrorResponse(errorCode string) []byte {
return []byte(fmt.Sprintf("{\n\terror: %q,\n}\n", errorCode))
}
func (c *oauth2Config) tokenHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
grantType := r.URL.Query().Get("grant_type")
clientID := r.URL.Query().Get("client_id")
clientSecret := r.URL.Query().Get("client_secret")
scopes := r.URL.Query().Get("scope")
if grantType != "client_credentials" {
w.WriteHeader(http.StatusBadRequest)
w.Write(oauthTokenErrorResponse("unsupported_grant_type"))
return
}
if clientID != c.clientID || clientSecret != c.clientSecret {
w.WriteHeader(http.StatusUnauthorized)
w.Write(oauthTokenErrorResponse("invalid_client"))
return
}
if len(c.scopes) > 0 {
requiredScopes := strings.Split(c.scopes, ",")
sort.Strings(requiredScopes)
requestedScopes := strings.Split(scopes, " ")
sort.Strings(requestedScopes)
if !cmp.Equal(requestedScopes, requiredScopes) {
w.WriteHeader(http.StatusUnauthorized)
w.Write(oauthTokenErrorResponse("invalid_scope"))
return
}
}
response := fmt.Sprintf("{\n\taccess_token: %q,\n\ttoken_type: %q\n}\n", c.accessToken, "bearer")
w.Write([]byte(response))
})
}
func (c *oauth2Config) handle(handler http.Handler) http.Handler {
return authorizationHandler(handler, "Bearer", c.accessToken)
}
type httpClientConfig struct {
tls *tlsConfig
basicAuth *basicAuthConfig
auth *authorizationConfig
oauth2 *oauth2Config
}
func newHttpClientConfigFromFlags() *httpClientConfig {
return &httpClientConfig{
tls: newTLSConfigFromFlags(),
basicAuth: newBasicAuthConfigFromFlags(),
auth: newAuthorizationConfigFromFlags(),
oauth2: newOAuth2ConfigFromFlags(),
}
}
func (c *httpClientConfig) validate() error {
var errs []error
if err := c.tls.validate(); err != nil {
errs = append(errs, err)
}
if c.basicAuth.isEnabled() {
if c.auth.isEnabled() {
errs = append(errs, errors.New("cannot specify both --basic-auth and --auth flags"))
}
if c.oauth2.isEnabled() {
errs = append(errs, errors.New("cannot specify both --basic-auth and --oauth2 flags"))
}
}
if c.auth.isEnabled() && c.oauth2.isEnabled() {
errs = append(errs, errors.New("cannot specify both --auth and --oa2uth flags"))
}
if err := c.auth.validate(); err != nil {
errs = append(errs, err)
}
if err := c.oauth2.validate(); err != nil {
errs = append(errs, err)
}
return errors.Join(errs...)
}
func (c *httpClientConfig) register(mux *http.ServeMux) {
if c.oauth2.isEnabled() {
mux.Handle("/token", c.oauth2.tokenHandler())
}
}
func (c *httpClientConfig) handle(handler http.Handler) http.Handler {
if c.oauth2.isEnabled() {
return c.oauth2.handle(handler)
}
if c.auth.isEnabled() {
return c.auth.handle(handler)
}
if c.basicAuth.isEnabled() {
return c.basicAuth.handle(handler)
}
return handler
}
func (c *httpClientConfig) getTLSConfig() (*tls.Config, error) {
return c.tls.getTLSConfig()
}