Debian Codegraph @

pkg/server/options/serving_with_loopback.go

raw
1 /*
2 Copyright 2018 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 options
18
19 import (
20 "fmt"
21 "time"
22
23 "github.com/google/uuid"
24
25 "k8s.io/apiserver/pkg/server"
26 "k8s.io/apiserver/pkg/server/dynamiccertificates"
27 "k8s.io/client-go/rest"
28 certutil "k8s.io/client-go/util/cert"
29 )
30
31 type SecureServingOptionsWithLoopback struct {
32 *SecureServingOptions
33 }
34
35 func (o *SecureServingOptions) WithLoopback() *SecureServingOptionsWithLoopback {
36 return &SecureServingOptionsWithLoopback{o}
37 }
38
39 // ApplyTo fills up serving information in the server configuration.
40 func (s *SecureServingOptionsWithLoopback) ApplyTo(secureServingInfo **server.SecureServingInfo, loopbackClientConfig **rest.Config) error {
41 if s == nil || s.SecureServingOptions == nil || secureServingInfo == nil {
42 return nil
43 }
44
45 if err := s.SecureServingOptions.ApplyTo(secureServingInfo); err != nil {
46 return err
47 }
48
49 if *secureServingInfo == nil || loopbackClientConfig == nil {
50 return nil
51 }
52
53 // Set a validity period of approximately 3 years for the loopback certificate
54 // to avoid kube-apiserver disruptions due to certificate expiration.
55 // When this certificate expires, restarting kube-apiserver will automatically
56 // regenerate a new certificate with fresh validity dates.
57 maxAge := (3*365 + 1) * 24 * time.Hour
58
59 // create self-signed cert+key with the fake server.LoopbackClientServerNameOverride and
60 // let the server return it when the loopback client connects.
61 certPem, keyPem, err := certutil.GenerateSelfSignedCertKeyWithOptions(certutil.SelfSignedCertKeyOptions{
62 Host: server.LoopbackClientServerNameOverride,
63 MaxAge: maxAge,
64 })
65 if err != nil {
66 return fmt.Errorf("failed to generate self-signed certificate for loopback connection: %v", err)
67 }
68 certProvider, err := dynamiccertificates.NewStaticSNICertKeyContent("self-signed loopback", certPem, keyPem, server.LoopbackClientServerNameOverride)
69 if err != nil {
70 return fmt.Errorf("failed to generate self-signed certificate for loopback connection: %v", err)
71 }
72
73 // Write to the front of SNICerts so that this overrides any other certs with the same name
74 (*secureServingInfo).SNICerts = append([]dynamiccertificates.SNICertKeyContentProvider{certProvider}, (*secureServingInfo).SNICerts...)
75
76 secureLoopbackClientConfig, err := (*secureServingInfo).NewLoopbackClientConfig(uuid.New().String(), certPem)
77 switch {
78 // if we failed and there's no fallback loopback client config, we need to fail
79 case err != nil && *loopbackClientConfig == nil:
80 (*secureServingInfo).SNICerts = (*secureServingInfo).SNICerts[1:]
81 return err
82
83 // if we failed, but we already have a fallback loopback client config (usually insecure), allow it
84 case err != nil && *loopbackClientConfig != nil:
85
86 default:
87 *loopbackClientConfig = secureLoopbackClientConfig
88 }
89
90 return nil
91 }