Debian Codegraph @

pkg/server/dynamiccertificates/union_content.go

raw
1 /*
2 Copyright 2019 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package dynamiccertificates
18
19 import (
20 "bytes"
21 "context"
22 "crypto/x509"
23 "strings"
24
25 utilerrors "k8s.io/apimachinery/pkg/util/errors"
26 )
27
28 type unionCAContent []CAContentProvider
29
30 var _ CAContentProvider = &unionCAContent{}
31 var _ ControllerRunner = &unionCAContent{}
32
33 // NewUnionCAContentProvider returns a CAContentProvider that is a union of other CAContentProviders
34 func NewUnionCAContentProvider(caContentProviders ...CAContentProvider) CAContentProvider {
35 return unionCAContent(caContentProviders)
36 }
37
38 // Name is just an identifier
39 func (c unionCAContent) Name() string {
40 names := []string{}
41 for _, curr := range c {
42 names = append(names, curr.Name())
43 }
44 return strings.Join(names, ",")
45 }
46
47 // CurrentCABundleContent provides ca bundle byte content
48 func (c unionCAContent) CurrentCABundleContent() []byte {
49 caBundles := [][]byte{}
50 for _, curr := range c {
51 if currCABytes := curr.CurrentCABundleContent(); len(currCABytes) > 0 {
52 caBundles = append(caBundles, []byte(strings.TrimSpace(string(currCABytes))))
53 }
54 }
55
56 return bytes.Join(caBundles, []byte("\n"))
57 }
58
59 // CurrentCABundleContent provides ca bundle byte content
60 func (c unionCAContent) VerifyOptions() (x509.VerifyOptions, bool) {
61 currCABundle := c.CurrentCABundleContent()
62 if len(currCABundle) == 0 {
63 return x509.VerifyOptions{}, false
64 }
65
66 // TODO make more efficient. This isn't actually used in any of our mainline paths. It's called to build the TLSConfig
67 // TODO on file changes, but the actual authentication runs against the individual items, not the union.
68 ret, err := newCABundleAndVerifier(c.Name(), c.CurrentCABundleContent())
69 if err != nil {
70 // because we're made up of already vetted values, this indicates some kind of coding error
71 panic(err)
72 }
73
74 return ret.verifyOptions, true
75 }
76
77 // AddListener adds a listener to be notified when the CA content changes.
78 func (c unionCAContent) AddListener(listener Listener) {
79 for _, curr := range c {
80 curr.AddListener(listener)
81 }
82 }
83
84 // AddListener adds a listener to be notified when the CA content changes.
85 func (c unionCAContent) RunOnce(ctx context.Context) error {
86 errors := []error{}
87 for _, curr := range c {
88 if controller, ok := curr.(ControllerRunner); ok {
89 if err := controller.RunOnce(ctx); err != nil {
90 errors = append(errors, err)
91 }
92 }
93 }
94
95 return utilerrors.NewAggregate(errors)
96 }
97
98 // Run runs the controller
99 func (c unionCAContent) Run(ctx context.Context, workers int) {
100 for _, curr := range c {
101 if controller, ok := curr.(ControllerRunner); ok {
102 go controller.Run(ctx, workers)
103 }
104 }
105 }