k8s.io/apiserver@v0.31.1/pkg/server/options/serving_with_loopback.go (about) 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 22 "github.com/google/uuid" 23 24 "k8s.io/apiserver/pkg/server" 25 "k8s.io/apiserver/pkg/server/dynamiccertificates" 26 "k8s.io/client-go/rest" 27 certutil "k8s.io/client-go/util/cert" 28 ) 29 30 type SecureServingOptionsWithLoopback struct { 31 *SecureServingOptions 32 } 33 34 func (o *SecureServingOptions) WithLoopback() *SecureServingOptionsWithLoopback { 35 return &SecureServingOptionsWithLoopback{o} 36 } 37 38 // ApplyTo fills up serving information in the server configuration. 39 func (s *SecureServingOptionsWithLoopback) ApplyTo(secureServingInfo **server.SecureServingInfo, loopbackClientConfig **rest.Config) error { 40 if s == nil || s.SecureServingOptions == nil || secureServingInfo == nil { 41 return nil 42 } 43 44 if err := s.SecureServingOptions.ApplyTo(secureServingInfo); err != nil { 45 return err 46 } 47 48 if *secureServingInfo == nil || loopbackClientConfig == nil { 49 return nil 50 } 51 52 // create self-signed cert+key with the fake server.LoopbackClientServerNameOverride and 53 // let the server return it when the loopback client connects. 54 certPem, keyPem, err := certutil.GenerateSelfSignedCertKey(server.LoopbackClientServerNameOverride, nil, nil) 55 if err != nil { 56 return fmt.Errorf("failed to generate self-signed certificate for loopback connection: %v", err) 57 } 58 certProvider, err := dynamiccertificates.NewStaticSNICertKeyContent("self-signed loopback", certPem, keyPem, server.LoopbackClientServerNameOverride) 59 if err != nil { 60 return fmt.Errorf("failed to generate self-signed certificate for loopback connection: %v", err) 61 } 62 63 // Write to the front of SNICerts so that this overrides any other certs with the same name 64 (*secureServingInfo).SNICerts = append([]dynamiccertificates.SNICertKeyContentProvider{certProvider}, (*secureServingInfo).SNICerts...) 65 66 secureLoopbackClientConfig, err := (*secureServingInfo).NewLoopbackClientConfig(uuid.New().String(), certPem) 67 switch { 68 // if we failed and there's no fallback loopback client config, we need to fail 69 case err != nil && *loopbackClientConfig == nil: 70 (*secureServingInfo).SNICerts = (*secureServingInfo).SNICerts[1:] 71 return err 72 73 // if we failed, but we already have a fallback loopback client config (usually insecure), allow it 74 case err != nil && *loopbackClientConfig != nil: 75 76 default: 77 *loopbackClientConfig = secureLoopbackClientConfig 78 } 79 80 return nil 81 }