Skip to content

Commit 7624e62

Browse files
authored
Merge pull request brancz#127 from s-urbaniak/reload-client-ca
pkg/authn/delegating: dynamically reload client CA
2 parents 9aae0e5 + b9a29b8 commit 7624e62

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,18 @@ func main() {
171171
if err != nil {
172172
klog.Fatalf("Failed to instantiate OIDC authenticator: %v", err)
173173
}
174-
175174
} else {
176175
//Use Delegating authenticator
177176
klog.Infof("Valid token audiences: %s", strings.Join(cfg.auth.Authentication.Token.Audiences, ", "))
178177

179178
tokenClient := kubeClient.AuthenticationV1().TokenReviews()
180-
authenticator, err = authn.NewDelegatingAuthenticator(tokenClient, cfg.auth.Authentication)
179+
delegatingAuthenticator, err := authn.NewDelegatingAuthenticator(tokenClient, cfg.auth.Authentication)
181180
if err != nil {
182181
klog.Fatalf("Failed to instantiate delegating authenticator: %v", err)
183182
}
184183

184+
go delegatingAuthenticator.Run(1, context.Background().Done())
185+
authenticator = delegatingAuthenticator
185186
}
186187

187188
sarClient := kubeClient.AuthorizationV1().SubjectAccessReviews()

pkg/authn/delegating.go

+31-15
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ package authn
1818

1919
import (
2020
"errors"
21-
"fmt"
22-
"io/ioutil"
21+
"net/http"
2322
"time"
2423

2524
"k8s.io/apiserver/pkg/authentication/authenticator"
@@ -28,27 +27,23 @@ import (
2827
authenticationclient "k8s.io/client-go/kubernetes/typed/authentication/v1"
2928
)
3029

30+
type DelegatingAuthenticator struct {
31+
dynamicClientCA *dynamiccertificates.DynamicFileCAContent
32+
requestAuthenticator authenticator.Request
33+
}
34+
3135
// NewDelegatingAuthenticator creates an authenticator compatible with the kubelet's needs
32-
func NewDelegatingAuthenticator(client authenticationclient.TokenReviewInterface, authn *AuthnConfig) (authenticator.Request, error) {
36+
func NewDelegatingAuthenticator(client authenticationclient.TokenReviewInterface, authn *AuthnConfig) (*DelegatingAuthenticator, error) {
3337
if client == nil {
3438
return nil, errors.New("tokenAccessReview client not provided, cannot use webhook authentication")
3539
}
3640

3741
var (
38-
p authenticatorfactory.CAContentProvider
42+
p *dynamiccertificates.DynamicFileCAContent
3943
err error
4044
)
4145
if len(authn.X509.ClientCAFile) > 0 {
42-
if len(authn.X509.ClientCAFile) == 0 {
43-
return nil, fmt.Errorf("missing filename for ca bundle")
44-
}
45-
46-
caBundle, err := ioutil.ReadFile(authn.X509.ClientCAFile)
47-
if err != nil {
48-
return nil, err
49-
}
50-
51-
p, err = dynamiccertificates.NewStaticCAContent(authn.X509.ClientCAFile, caBundle)
46+
p, err = dynamiccertificates.NewDynamicCAContentFromFile("client-ca", authn.X509.ClientCAFile)
5247
if err != nil {
5348
return nil, err
5449
}
@@ -63,5 +58,26 @@ func NewDelegatingAuthenticator(client authenticationclient.TokenReviewInterface
6358
}
6459

6560
authenticator, _, err := authenticatorConfig.New()
66-
return authenticator, err
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
return &DelegatingAuthenticator{requestAuthenticator: authenticator, dynamicClientCA: p}, nil
66+
}
67+
68+
func (a *DelegatingAuthenticator) AuthenticateRequest(req *http.Request) (*authenticator.Response, bool, error) {
69+
return a.requestAuthenticator.AuthenticateRequest(req)
70+
}
71+
72+
func (a *DelegatingAuthenticator) RunOnce() error {
73+
if a.dynamicClientCA != nil {
74+
return a.dynamicClientCA.RunOnce()
75+
}
76+
return nil
77+
}
78+
79+
func (a *DelegatingAuthenticator) Run(workers int, stopCh <-chan struct{}) {
80+
if a.dynamicClientCA != nil {
81+
a.dynamicClientCA.Run(workers, stopCh)
82+
}
6783
}

0 commit comments

Comments
 (0)