| 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 egressselector |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"os" |
| 22 |
"strings" |
| 23 |
|
| 24 |
"k8s.io/apimachinery/pkg/runtime" |
| 25 |
"k8s.io/apimachinery/pkg/runtime/serializer" |
| 26 |
"k8s.io/apimachinery/pkg/util/sets" |
| 27 |
"k8s.io/apimachinery/pkg/util/validation/field" |
| 28 |
"k8s.io/apiserver/pkg/apis/apiserver" |
| 29 |
"k8s.io/apiserver/pkg/apis/apiserver/install" |
| 30 |
"k8s.io/utils/path" |
| 31 |
) |
| 32 |
|
| 33 |
var cfgScheme = runtime.NewScheme() |
| 34 |
|
| 35 |
// validEgressSelectorNames contains the set of valid egress selctor names. |
| 36 |
var validEgressSelectorNames = sets.NewString("controlplane", "cluster", "etcd") |
| 37 |
|
| 38 |
func init() { |
| 39 |
install.Install(cfgScheme) |
| 40 |
} |
| 41 |
|
| 42 |
// ReadEgressSelectorConfiguration reads the egress selector configuration at the specified path. |
| 43 |
// It returns the loaded egress selector configuration if the input file aligns with the required syntax. |
| 44 |
// If it does not align with the provided syntax, it returns a default configuration which should function as a no-op. |
| 45 |
// It does this by returning a nil configuration, which preserves backward compatibility. |
| 46 |
// This works because prior to this there was no egress selector configuration. |
| 47 |
// It returns an error if the file did not exist. |
| 48 |
func ReadEgressSelectorConfiguration(configFilePath string) (*apiserver.EgressSelectorConfiguration, error) { |
| 49 |
if configFilePath == "" { |
| 50 |
return nil, nil |
| 51 |
} |
| 52 |
// a file was provided, so we just read it. |
| 53 |
data, err := os.ReadFile(configFilePath) |
| 54 |
if err != nil { |
| 55 |
return nil, fmt.Errorf("unable to read egress selector configuration from %q [%v]", configFilePath, err) |
| 56 |
} |
| 57 |
config, gvk, err := serializer.NewCodecFactory(cfgScheme, serializer.EnableStrict).UniversalDecoder().Decode(data, nil, nil) |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
internalConfig, ok := config.(*apiserver.EgressSelectorConfiguration) |
| 62 |
if !ok { |
| 63 |
return nil, fmt.Errorf("unexpected config type: %v", gvk) |
| 64 |
} |
| 65 |
return internalConfig, nil |
| 66 |
} |
| 67 |
|
| 68 |
// ValidateEgressSelectorConfiguration checks the apiserver.EgressSelectorConfiguration for |
| 69 |
// common configuration errors. It will return error for problems such as configuring mtls/cert |
| 70 |
// settings for protocol which do not support security. It will also try to catch errors such as |
| 71 |
// incorrect file paths. It will return nil if it does not find anything wrong. |
| 72 |
func ValidateEgressSelectorConfiguration(config *apiserver.EgressSelectorConfiguration) field.ErrorList { |
| 73 |
allErrs := field.ErrorList{} |
| 74 |
if config == nil { |
| 75 |
return allErrs // Treating a nil configuration as valid |
| 76 |
} |
| 77 |
for _, service := range config.EgressSelections { |
| 78 |
fldPath := field.NewPath("service", "connection") |
| 79 |
switch service.Connection.ProxyProtocol { |
| 80 |
case apiserver.ProtocolDirect: |
| 81 |
allErrs = append(allErrs, validateDirectConnection(service.Connection, fldPath)...) |
| 82 |
case apiserver.ProtocolHTTPConnect: |
| 83 |
allErrs = append(allErrs, validateHTTPConnectTransport(service.Connection.Transport, fldPath)...) |
| 84 |
case apiserver.ProtocolGRPC: |
| 85 |
allErrs = append(allErrs, validateGRPCTransport(service.Connection.Transport, fldPath)...) |
| 86 |
default: |
| 87 |
allErrs = append(allErrs, field.NotSupported( |
| 88 |
fldPath.Child("protocol"), |
| 89 |
service.Connection.ProxyProtocol, |
| 90 |
[]string{ |
| 91 |
string(apiserver.ProtocolDirect), |
| 92 |
string(apiserver.ProtocolHTTPConnect), |
| 93 |
string(apiserver.ProtocolGRPC), |
| 94 |
})) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
seen := sets.String{} |
| 99 |
for i, service := range config.EgressSelections { |
| 100 |
canonicalName := strings.ToLower(service.Name) |
| 101 |
fldPath := field.NewPath("service", "connection") |
| 102 |
// no duplicate check |
| 103 |
if seen.Has(canonicalName) { |
| 104 |
allErrs = append(allErrs, field.Duplicate(fldPath.Index(i), canonicalName)) |
| 105 |
continue |
| 106 |
} |
| 107 |
seen.Insert(canonicalName) |
| 108 |
|
| 109 |
if !validEgressSelectorNames.Has(canonicalName) { |
| 110 |
allErrs = append(allErrs, field.NotSupported(fldPath, canonicalName, validEgressSelectorNames.List())) |
| 111 |
continue |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return allErrs |
| 116 |
} |
| 117 |
|
| 118 |
func validateHTTPConnectTransport(transport *apiserver.Transport, fldPath *field.Path) field.ErrorList { |
| 119 |
allErrs := field.ErrorList{} |
| 120 |
if transport == nil { |
| 121 |
allErrs = append(allErrs, field.Required( |
| 122 |
fldPath.Child("transport"), |
| 123 |
"transport must be set for HTTPConnect")) |
| 124 |
return allErrs |
| 125 |
} |
| 126 |
|
| 127 |
if transport.TCP != nil && transport.UDS != nil { |
| 128 |
allErrs = append(allErrs, field.Invalid( |
| 129 |
fldPath.Child("tcp"), |
| 130 |
transport.TCP, |
| 131 |
"TCP and UDS cannot both be set")) |
| 132 |
} else if transport.TCP == nil && transport.UDS == nil { |
| 133 |
allErrs = append(allErrs, field.Required( |
| 134 |
fldPath.Child("tcp"), |
| 135 |
"One of TCP or UDS must be set")) |
| 136 |
} else if transport.TCP != nil { |
| 137 |
allErrs = append(allErrs, validateTCPConnection(transport.TCP, fldPath)...) |
| 138 |
} else if transport.UDS != nil { |
| 139 |
allErrs = append(allErrs, validateUDSConnection(transport.UDS, fldPath)...) |
| 140 |
} |
| 141 |
return allErrs |
| 142 |
} |
| 143 |
|
| 144 |
func validateGRPCTransport(transport *apiserver.Transport, fldPath *field.Path) field.ErrorList { |
| 145 |
allErrs := field.ErrorList{} |
| 146 |
if transport == nil { |
| 147 |
allErrs = append(allErrs, field.Required( |
| 148 |
fldPath.Child("transport"), |
| 149 |
"transport must be set for GRPC")) |
| 150 |
return allErrs |
| 151 |
} |
| 152 |
|
| 153 |
if transport.UDS != nil { |
| 154 |
allErrs = append(allErrs, validateUDSConnection(transport.UDS, fldPath)...) |
| 155 |
} else { |
| 156 |
allErrs = append(allErrs, field.Required( |
| 157 |
fldPath.Child("uds"), |
| 158 |
"UDS must be set with GRPC")) |
| 159 |
} |
| 160 |
return allErrs |
| 161 |
} |
| 162 |
|
| 163 |
func validateDirectConnection(connection apiserver.Connection, fldPath *field.Path) field.ErrorList { |
| 164 |
if connection.Transport != nil { |
| 165 |
return field.ErrorList{field.Invalid( |
| 166 |
fldPath.Child("transport"), |
| 167 |
"direct", |
| 168 |
"Transport config should be absent for direct connect"), |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return nil |
| 173 |
} |
| 174 |
|
| 175 |
func validateUDSConnection(udsConfig *apiserver.UDSTransport, fldPath *field.Path) field.ErrorList { |
| 176 |
allErrs := field.ErrorList{} |
| 177 |
if udsConfig.UDSName == "" { |
| 178 |
allErrs = append(allErrs, field.Invalid( |
| 179 |
fldPath.Child("udsName"), |
| 180 |
"nil", |
| 181 |
"UDSName should be present for UDS connections")) |
| 182 |
} |
| 183 |
return allErrs |
| 184 |
} |
| 185 |
|
| 186 |
func validateTCPConnection(tcpConfig *apiserver.TCPTransport, fldPath *field.Path) field.ErrorList { |
| 187 |
allErrs := field.ErrorList{} |
| 188 |
|
| 189 |
if strings.HasPrefix(tcpConfig.URL, "http://") { |
| 190 |
if tcpConfig.TLSConfig != nil { |
| 191 |
allErrs = append(allErrs, field.Invalid( |
| 192 |
fldPath.Child("tlsConfig"), |
| 193 |
"nil", |
| 194 |
"TLSConfig config should not be present when using HTTP")) |
| 195 |
} |
| 196 |
} else if strings.HasPrefix(tcpConfig.URL, "https://") { |
| 197 |
return validateTLSConfig(tcpConfig.TLSConfig, fldPath) |
| 198 |
} else { |
| 199 |
allErrs = append(allErrs, field.Invalid( |
| 200 |
fldPath.Child("url"), |
| 201 |
tcpConfig.URL, |
| 202 |
"supported connection protocols are http:// and https://")) |
| 203 |
} |
| 204 |
return allErrs |
| 205 |
} |
| 206 |
|
| 207 |
func validateTLSConfig(tlsConfig *apiserver.TLSConfig, fldPath *field.Path) field.ErrorList { |
| 208 |
allErrs := field.ErrorList{} |
| 209 |
|
| 210 |
if tlsConfig == nil { |
| 211 |
allErrs = append(allErrs, field.Required( |
| 212 |
fldPath.Child("tlsConfig"), |
| 213 |
"TLSConfig must be present when using HTTPS")) |
| 214 |
return allErrs |
| 215 |
} |
| 216 |
if tlsConfig.CABundle != "" { |
| 217 |
if exists, err := path.Exists(path.CheckFollowSymlink, tlsConfig.CABundle); !exists || err != nil { |
| 218 |
allErrs = append(allErrs, field.Invalid( |
| 219 |
fldPath.Child("tlsConfig", "caBundle"), |
| 220 |
tlsConfig.CABundle, |
| 221 |
"TLS config ca bundle does not exist")) |
| 222 |
} |
| 223 |
} |
| 224 |
if tlsConfig.ClientCert == "" { |
| 225 |
allErrs = append(allErrs, field.Invalid( |
| 226 |
fldPath.Child("tlsConfig", "clientCert"), |
| 227 |
"nil", |
| 228 |
"Using TLS requires clientCert")) |
| 229 |
} else if exists, err := path.Exists(path.CheckFollowSymlink, tlsConfig.ClientCert); !exists || err != nil { |
| 230 |
allErrs = append(allErrs, field.Invalid( |
| 231 |
fldPath.Child("tlsConfig", "clientCert"), |
| 232 |
tlsConfig.ClientCert, |
| 233 |
"TLS client cert does not exist")) |
| 234 |
} |
| 235 |
if tlsConfig.ClientKey == "" { |
| 236 |
allErrs = append(allErrs, field.Invalid( |
| 237 |
fldPath.Child("tlsConfig", "clientKey"), |
| 238 |
"nil", |
| 239 |
"Using TLS requires requires clientKey")) |
| 240 |
} else if exists, err := path.Exists(path.CheckFollowSymlink, tlsConfig.ClientKey); !exists || err != nil { |
| 241 |
allErrs = append(allErrs, field.Invalid( |
| 242 |
fldPath.Child("tlsConfig", "clientKey"), |
| 243 |
tlsConfig.ClientKey, |
| 244 |
"TLS client key does not exist")) |
| 245 |
} |
| 246 |
return allErrs |
| 247 |
} |