-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
176 lines (147 loc) · 4.02 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
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/coreos/go-oidc"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
err := newRootCmd().Execute()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func newRootCmd() *cobra.Command {
var (
ca string
)
cmd := &cobra.Command{
Use: "k8s-s2s-auth",
TraverseChildren: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if ca != "" {
rootCAs, err := x509.SystemCertPool()
if err != nil {
rootCAs = x509.NewCertPool()
}
certs, err := ioutil.ReadFile(ca)
if err != nil {
return fmt.Errorf("could not read ca '%s': %w", ca, err)
}
_ = rootCAs.AppendCertsFromPEM(certs)
config := &tls.Config{
RootCAs: rootCAs,
}
// usually we should avoid this mess with global stuff
http.DefaultTransport.(*http.Transport).TLSClientConfig = config
}
cmd.SilenceUsage = true
cmd.SilenceErrors = true
return nil
},
}
cmd.PersistentFlags().StringVar(&ca, "ca", ca, "Add CA to trusted certificates")
cmd.AddCommand(
newServerCmd(),
newClientCmd(),
newAppCmd(),
)
return cmd
}
func newClientCmd() *cobra.Command {
var (
tokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
)
cmd := &cobra.Command{
Use: "client <targetURL>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
targetURL := args[0]
client, err := newClient(targetURL, tokenFile)
if err != nil {
return err
}
return client.run()
},
}
cmd.Flags().StringVar(&tokenFile, "token-file", tokenFile, "Path to the token file")
return cmd
}
func newServerCmd() *cobra.Command {
var (
handler http.Handler
kubeConfigOverrides = &clientcmd.ConfigOverrides{}
mode = "tokenreview"
jwtPubKeyFile = "sa.pub"
oidcIssuerURL = "https://kubernetes.default.svc"
audience string
listenAddr = ":8080"
)
cmd := &cobra.Command{
Use: "server",
RunE: func(cmd *cobra.Command, args []string) error {
logger := log.New(os.Stderr, "", log.LstdFlags)
appHandler := greetHandler(logger)
switch mode {
case "tokenreview":
client, err := loadKubeClient(kubeConfigOverrides)
if err != nil {
return err
}
config := &TokenReviewConfig{
log: logger,
tokenReviewer: client.AuthenticationV1().TokenReviews(),
}
if audience != "" {
config.audiences = []string{audience}
}
handler = TokenReviewAuth(config, appHandler)
case "jwt-pubkey":
pubKey, err := loadPubKey(jwtPubKeyFile)
if err != nil {
return err
}
config := &JWTPubKeyConfig{
pubKey: pubKey,
log: logger,
audience: audience,
}
handler = JWTPubKeyAuth(config, appHandler)
case "oidc-discovery":
provider, err := oidc.NewProvider(context.Background(), oidcIssuerURL)
if err != nil {
return err
}
oidcConfig := &oidc.Config{
ClientID: audience,
}
if audience == "" {
oidcConfig.SkipClientIDCheck = true
}
config := &OIDCDiscoveryConfig{
log: logger,
verifier: provider.Verifier(oidcConfig),
}
handler = OIDCDiscoveryAuth(config, appHandler)
default:
return fmt.Errorf("unknown mode: %s", mode)
}
return http.ListenAndServe(listenAddr, handler)
},
}
clientcmd.BindOverrideFlags(kubeConfigOverrides, cmd.Flags(), clientcmd.RecommendedConfigOverrideFlags("kube-"))
cmd.Flags().StringVar(&mode, "mode", mode, "Authentication mode (tokenreview, jwt-pubkey, oidc-discovery")
cmd.Flags().StringVar(&oidcIssuerURL, "issuer-url", oidcIssuerURL, "Issuer Discovery URL for oidc-discovery mode")
cmd.Flags().StringVar(&audience, "audience", audience, "The audience to check")
cmd.Flags().StringVar(&listenAddr, "addr", listenAddr, "Listen address of the server")
cmd.Flags().StringVar(&jwtPubKeyFile, "pub-key", jwtPubKeyFile, "Public Key to verify JWT")
return cmd
}