/
openidmetadata.go
222 lines (191 loc) · 6.34 KB
/
openidmetadata.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
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package openidmetadata tests the OIDC discovery endpoints which are part of
// the ServiceAccountIssuerDiscovery feature.
package openidmetadata
import (
"context"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
"runtime"
"time"
"github.com/coreos/go-oidc"
"github.com/spf13/cobra"
"golang.org/x/oauth2"
"gopkg.in/square/go-jose.v2/jwt"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/rest"
)
// CmdTestServiceAccountIssuerDiscovery is used by agnhost Cobra.
var CmdTestServiceAccountIssuerDiscovery = &cobra.Command{
Use: "test-service-account-issuer-discovery",
Short: "Tests the ServiceAccountIssuerDiscovery feature",
Long: "Reads in a mounted token and attempts to verify it against the API server's " +
"OIDC endpoints, using a third-party OIDC implementation.",
Args: cobra.MaximumNArgs(0),
Run: main,
}
var (
tokenPath string
audience string
)
func init() {
fs := CmdTestServiceAccountIssuerDiscovery.Flags()
fs.StringVar(&tokenPath, "token-path", "", "Path to read service account token from.")
fs.StringVar(&audience, "audience", "", "Audience to check on received token.")
}
func main(cmd *cobra.Command, args []string) {
raw, err := gettoken()
if err != nil {
log.Fatal(err)
}
log.Print("OK: Got token")
/*
To support both in-cluster discovery and external (non kube-apiserver)
discovery:
1. Attempt with in-cluster discovery. Only trust Cluster CA.
If pass, exit early, successfully. This attempt includes the bearer
token, so we only trust the Cluster CA to avoid sending tokens to
some external endpoint by accident.
2. If in-cluster discovery doesn't pass, then try again assuming both
discovery doc and JWKS endpoints are external rather than being
served from kube-apiserver. This attempt does not pass the bearer
token at all.
*/
log.Print("validating with in-cluster discovery")
inClusterCtx, err := withInClusterOauth2Client(context.Background())
if err != nil {
log.Fatal(err)
}
if err := validate(inClusterCtx, raw); err == nil {
os.Exit(0)
} else {
log.Print("failed to validate with in-cluster discovery: ", err)
}
log.Print("falling back to validating with external discovery")
externalCtx, err := withExternalOAuth2Client(context.Background())
if err != nil {
log.Fatal(err)
}
if err := validate(externalCtx, raw); err != nil {
log.Fatal(err)
}
}
func validate(ctx context.Context, raw string) error {
tok, err := jwt.ParseSigned(raw)
if err != nil {
log.Fatal(err)
}
var unsafeClaims claims
if err := tok.UnsafeClaimsWithoutVerification(&unsafeClaims); err != nil {
log.Fatal(err)
}
log.Printf("OK: got issuer %s", unsafeClaims.Issuer)
log.Printf("Full, not-validated claims: \n%#v", unsafeClaims)
if runtime.GOOS == "windows" {
if err := ensureWindowsDNSAvailability(unsafeClaims.Issuer); err != nil {
log.Fatal(err)
}
}
iss, err := oidc.NewProvider(ctx, unsafeClaims.Issuer)
if err != nil {
return err
}
log.Printf("OK: Constructed OIDC provider for issuer %v", unsafeClaims.Issuer)
validTok, err := iss.Verifier(&oidc.Config{
ClientID: audience,
SupportedSigningAlgs: []string{oidc.RS256, oidc.ES256},
}).Verify(ctx, raw)
if err != nil {
return err
}
log.Print("OK: Validated signature on JWT")
var safeClaims claims
if err := validTok.Claims(&safeClaims); err != nil {
return err
}
log.Print("OK: Got valid claims from token!")
log.Printf("Full, validated claims: \n%#v", &safeClaims)
return nil
}
type kubeName struct {
Name string `json:"name"`
UID string `json:"uid"`
}
type kubeClaims struct {
Namespace string `json:"namespace"`
ServiceAccount kubeName `json:"serviceaccount"`
}
type claims struct {
jwt.Claims
Kubernetes kubeClaims `json:"kubernetes.io"`
}
func (k *claims) String() string {
return fmt.Sprintf("%s/%s for %s", k.Kubernetes.Namespace, k.Kubernetes.ServiceAccount.Name, k.Audience)
}
func gettoken() (string, error) {
b, err := os.ReadFile(tokenPath)
return string(b), err
}
func withExternalOAuth2Client(ctx context.Context) (context.Context, error) {
// Use the default http transport with the system root bundle,
// since it's validating against the external internet.
return context.WithValue(ctx,
// The `oidc` library respects the oauth2.HTTPClient context key; if it is set,
// the library will use the provided http.Client rather than the default HTTP client.
oauth2.HTTPClient, &http.Client{
Transport: http.DefaultTransport,
}), nil
}
func withInClusterOauth2Client(ctx context.Context) (context.Context, error) {
// Use the in-cluster config so we can trust and authenticate with kube-apiserver
cfg, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
rt, err := rest.TransportFor(cfg)
if err != nil {
return nil, fmt.Errorf("could not get roundtripper: %v", err)
}
return context.WithValue(ctx,
// The `oidc` library respects the oauth2.HTTPClient context key; if it is set,
// the library will use the provided http.Client rather than the default HTTP client.
oauth2.HTTPClient, &http.Client{
Transport: rt,
}), nil
}
// DNS can be available sometime after the container starts due to the way
// networking is set up for Windows nodes with dockershim as the container runtime.
// In this case, we should make sure we are able to resolve the issuer before
// invoking oidc.NewProvider.
// See https://github.com/kubernetes/kubernetes/issues/99470 for more details.
func ensureWindowsDNSAvailability(issuer string) error {
log.Println("Ensuring Windows DNS availability")
u, err := url.Parse(issuer)
if err != nil {
return err
}
return wait.PollImmediate(5*time.Second, 20*time.Second, func() (bool, error) {
ips, err := net.LookupHost(u.Host)
if err != nil {
log.Println(err)
return false, nil
}
log.Printf("OK: Resolved host %s: %v", u.Host, ips)
return true, nil
})
}