| 1 |
/* |
| 2 |
Copyright 2022 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 |
"os" |
| 22 |
"reflect" |
| 23 |
"testing" |
| 24 |
"time" |
| 25 |
|
| 26 |
"github.com/stretchr/testify/assert" |
| 27 |
) |
| 28 |
|
| 29 |
/* |
| 30 |
* TestDefaultServerOptions is intended to ensure we do not make a backward incompatible |
| 31 |
* change to the default flag values for the ANP server. |
| 32 |
*/ |
| 33 |
func TestDefaultServerOptions(t *testing.T) { |
| 34 |
defaultServerOptions := NewProxyRunOptions() |
| 35 |
assertDefaultValue(t, "ServerCert", defaultServerOptions.ServerCert, "") |
| 36 |
assertDefaultValue(t, "ServerKey", defaultServerOptions.ServerKey, "") |
| 37 |
assertDefaultValue(t, "ServerCaCert", defaultServerOptions.ServerCaCert, "") |
| 38 |
assertDefaultValue(t, "ClusterCert", defaultServerOptions.ClusterCert, "") |
| 39 |
assertDefaultValue(t, "ClusterKey", defaultServerOptions.ClusterKey, "") |
| 40 |
assertDefaultValue(t, "ClusterCaCert", defaultServerOptions.ClusterCaCert, "") |
| 41 |
assertDefaultValue(t, "Mode", defaultServerOptions.Mode, "grpc") |
| 42 |
assertDefaultValue(t, "UdsName", defaultServerOptions.UdsName, "") |
| 43 |
assertDefaultValue(t, "DeleteUDSFile", defaultServerOptions.DeleteUDSFile, true) |
| 44 |
assertDefaultValue(t, "ServerPort", defaultServerOptions.ServerPort, 8090) |
| 45 |
assertDefaultValue(t, "ServerBindAddress", defaultServerOptions.ServerBindAddress, "") |
| 46 |
assertDefaultValue(t, "AgentPort", defaultServerOptions.AgentPort, 8091) |
| 47 |
assertDefaultValue(t, "AgentBindAddress", defaultServerOptions.AgentBindAddress, "") |
| 48 |
assertDefaultValue(t, "HealthPort", defaultServerOptions.HealthPort, 8092) |
| 49 |
assertDefaultValue(t, "HealthBindAddress", defaultServerOptions.HealthBindAddress, "") |
| 50 |
assertDefaultValue(t, "AdminPort", defaultServerOptions.AdminPort, 8095) |
| 51 |
assertDefaultValue(t, "AdminBindAddress", defaultServerOptions.AdminBindAddress, "127.0.0.1") |
| 52 |
assertDefaultValue(t, "KeepaliveTime", defaultServerOptions.KeepaliveTime, 1*time.Hour) |
| 53 |
assertDefaultValue(t, "FrontendKeepaliveTime", defaultServerOptions.FrontendKeepaliveTime, 1*time.Hour) |
| 54 |
assertDefaultValue(t, "EnableProfiling", defaultServerOptions.EnableProfiling, false) |
| 55 |
assertDefaultValue(t, "EnableContentionProfiling", defaultServerOptions.EnableContentionProfiling, false) |
| 56 |
assertDefaultValue(t, "ServerCount", defaultServerOptions.ServerCount, 1) |
| 57 |
assertDefaultValue(t, "AgentNamespace", defaultServerOptions.AgentNamespace, "") |
| 58 |
assertDefaultValue(t, "AgentServiceAccount", defaultServerOptions.AgentServiceAccount, "") |
| 59 |
assertDefaultValue(t, "KubeconfigPath", defaultServerOptions.KubeconfigPath, "") |
| 60 |
assertDefaultValue(t, "KubeconfigQPS", defaultServerOptions.KubeconfigQPS, float32(0)) |
| 61 |
assertDefaultValue(t, "KubeconfigBurst", defaultServerOptions.KubeconfigBurst, 0) |
| 62 |
assertDefaultValue(t, "AuthenticationAudience", defaultServerOptions.AuthenticationAudience, "") |
| 63 |
assertDefaultValue(t, "ProxyStrategies", defaultServerOptions.ProxyStrategies, "default") |
| 64 |
assertDefaultValue(t, "CipherSuites", defaultServerOptions.CipherSuites, make([]string, 0)) |
| 65 |
assertDefaultValue(t, "TLSMinVersion", defaultServerOptions.TLSMinVersion, "") |
| 66 |
assertDefaultValue(t, "XfrChannelSize", defaultServerOptions.XfrChannelSize, 10) |
| 67 |
assertDefaultValue(t, "APIContentType", defaultServerOptions.APIContentType, "application/vnd.kubernetes.protobuf") |
| 68 |
assertDefaultValue(t, "GracefulShutdownTimeout", defaultServerOptions.GracefulShutdownTimeout, 0*time.Second) |
| 69 |
assertDefaultValue(t, "BackendDialTimeout", defaultServerOptions.BackendDialTimeout, 0*time.Second) |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
func assertDefaultValue(t *testing.T, fieldName string, actual, expected interface{}) { |
| 74 |
t.Helper() |
| 75 |
assert.IsType(t, expected, actual, "For field %s, got the wrong type.", fieldName) |
| 76 |
assert.Equal(t, expected, actual, "For field %s, got the wrong value.", fieldName) |
| 77 |
} |
| 78 |
|
| 79 |
func TestValidate(t *testing.T) { |
| 80 |
tempFile, err := os.CreateTemp("", "server-ca-cert") |
| 81 |
if err != nil { |
| 82 |
t.Fatalf("failed to create temp file: %v", err) |
| 83 |
} |
| 84 |
defer os.Remove(tempFile.Name()) |
| 85 |
tempFile.Close() |
| 86 |
|
| 87 |
_, nonExistentErr := os.Stat("non-existent-file.crt") |
| 88 |
|
| 89 |
for desc, tc := range map[string]struct { |
| 90 |
field string |
| 91 |
value interface{} |
| 92 |
expected error |
| 93 |
}{ |
| 94 |
"ServerCaCert empty in TCP mode": { |
| 95 |
field: "ServerCaCert", |
| 96 |
value: "", |
| 97 |
expected: fmt.Errorf("server ca cert must be set when in TCP mode (uds-name is empty)"), |
| 98 |
}, |
| 99 |
"ServerCaCert non-existent in TCP mode": { |
| 100 |
field: "ServerCaCert", |
| 101 |
value: "non-existent-file.crt", |
| 102 |
expected: fmt.Errorf("error checking server CA cert non-existent-file.crt, got %v", nonExistentErr), |
| 103 |
}, |
| 104 |
"default": { |
| 105 |
field: "", |
| 106 |
value: nil, |
| 107 |
expected: nil, |
| 108 |
}, |
| 109 |
"ReservedServerPort": { |
| 110 |
field: "ServerPort", |
| 111 |
value: 1023, |
| 112 |
expected: fmt.Errorf("please do not try to use reserved port 1023 for the server port"), |
| 113 |
}, |
| 114 |
"StartValidServerPort": { |
| 115 |
field: "ServerPort", |
| 116 |
value: 1024, |
| 117 |
expected: nil, |
| 118 |
}, |
| 119 |
"EndValidServerPort": { |
| 120 |
field: "ServerPort", |
| 121 |
value: 49151, |
| 122 |
expected: nil, |
| 123 |
}, |
| 124 |
"StartEphemeralServerPort": { |
| 125 |
field: "ServerPort", |
| 126 |
value: 49152, |
| 127 |
expected: fmt.Errorf("please do not try to use ephemeral port 49152 for the server port"), |
| 128 |
}, |
| 129 |
"ReservedAdminPort": { |
| 130 |
field: "AdminPort", |
| 131 |
value: 1023, |
| 132 |
expected: fmt.Errorf("please do not try to use reserved port 1023 for the admin port"), |
| 133 |
}, |
| 134 |
"StartValidAdminPort": { |
| 135 |
field: "AdminPort", |
| 136 |
value: 1024, |
| 137 |
expected: nil, |
| 138 |
}, |
| 139 |
"EndValidAdminPort": { |
| 140 |
field: "AdminPort", |
| 141 |
value: 49151, |
| 142 |
expected: nil, |
| 143 |
}, |
| 144 |
"StartEphemeralAdminPort": { |
| 145 |
field: "AdminPort", |
| 146 |
value: 49152, |
| 147 |
expected: fmt.Errorf("please do not try to use ephemeral port 49152 for the admin port"), |
| 148 |
}, |
| 149 |
"ReservedHealthPort": { |
| 150 |
field: "HealthPort", |
| 151 |
value: 1023, |
| 152 |
expected: fmt.Errorf("please do not try to use reserved port 1023 for the health port"), |
| 153 |
}, |
| 154 |
"StartValidHealthPort": { |
| 155 |
field: "HealthPort", |
| 156 |
value: 1024, |
| 157 |
expected: nil, |
| 158 |
}, |
| 159 |
"EndValidHealthPort": { |
| 160 |
field: "HealthPort", |
| 161 |
value: 49151, |
| 162 |
expected: nil, |
| 163 |
}, |
| 164 |
"StartEphemeralHealthPort": { |
| 165 |
field: "HealthPort", |
| 166 |
value: 49152, |
| 167 |
expected: fmt.Errorf("please do not try to use ephemeral port 49152 for the health port"), |
| 168 |
}, |
| 169 |
"CommaSparatedCipherSuites": { |
| 170 |
field: "CipherSuites", |
| 171 |
value: "TLS_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", |
| 172 |
expected: nil, |
| 173 |
}, |
| 174 |
"ValidTLSMinVersion12": { |
| 175 |
field: "TLSMinVersion", |
| 176 |
value: "VersionTLS12", |
| 177 |
expected: nil, |
| 178 |
}, |
| 179 |
"ValidTLSMinVersion13": { |
| 180 |
field: "TLSMinVersion", |
| 181 |
value: "VersionTLS13", |
| 182 |
expected: nil, |
| 183 |
}, |
| 184 |
"InvalidTLSMinVersion": { |
| 185 |
field: "TLSMinVersion", |
| 186 |
value: "InvalidVersion", |
| 187 |
expected: fmt.Errorf("unsupported TLS version \"InvalidVersion\", supported values are: VersionTLS10, VersionTLS11, VersionTLS12, VersionTLS13"), |
| 188 |
}, |
| 189 |
"Empty proxy strategies": { |
| 190 |
field: "ProxyStrategies", |
| 191 |
value: "", |
| 192 |
expected: fmt.Errorf("ProxyStrategies cannot be empty"), |
| 193 |
}, |
| 194 |
"Invalid proxy strategies": { |
| 195 |
field: "ProxyStrategies", |
| 196 |
value: "invalid", |
| 197 |
expected: fmt.Errorf("invalid proxy strategies: unknown proxy strategy: invalid"), |
| 198 |
}, |
| 199 |
"ZeroXfrChannelSize": { |
| 200 |
field: "XfrChannelSize", |
| 201 |
value: 0, |
| 202 |
expected: fmt.Errorf("channel size 0 must be greater than 0"), |
| 203 |
}, |
| 204 |
"NegativeXfrChannelSize": { |
| 205 |
field: "XfrChannelSize", |
| 206 |
value: -10, |
| 207 |
expected: fmt.Errorf("channel size -10 must be greater than 0"), |
| 208 |
}, |
| 209 |
"NegativeGracefulShutdownTimeout": { |
| 210 |
field: "GracefulShutdownTimeout", |
| 211 |
value: -1 * time.Second, |
| 212 |
expected: fmt.Errorf("graceful-shutdown-timeout must be >= 0, got -1s"), |
| 213 |
}, |
| 214 |
"ZeroGracefulShutdownTimeout": { |
| 215 |
field: "GracefulShutdownTimeout", |
| 216 |
value: 0 * time.Second, |
| 217 |
expected: nil, |
| 218 |
}, |
| 219 |
"PositiveGracefulShutdownTimeout": { |
| 220 |
field: "GracefulShutdownTimeout", |
| 221 |
value: 30 * time.Second, |
| 222 |
expected: nil, |
| 223 |
}, |
| 224 |
"NegativeBackendDialTimeout": { |
| 225 |
field: "BackendDialTimeout", |
| 226 |
value: -1 * time.Second, |
| 227 |
expected: fmt.Errorf("backend-dial-timeout must be >= 0, got -1s"), |
| 228 |
}, |
| 229 |
"ZeroBackendDialTimeout": { |
| 230 |
field: "BackendDialTimeout", |
| 231 |
value: 0 * time.Second, |
| 232 |
expected: nil, |
| 233 |
}, |
| 234 |
"PositiveBackendDialTimeout": { |
| 235 |
field: "BackendDialTimeout", |
| 236 |
value: 30 * time.Second, |
| 237 |
expected: nil, |
| 238 |
}, |
| 239 |
} { |
| 240 |
t.Run(desc, func(t *testing.T) { |
| 241 |
testServerOptions := NewProxyRunOptions() |
| 242 |
if tc.field != "ServerCaCert" { |
| 243 |
testServerOptions.ServerCaCert = tempFile.Name() |
| 244 |
} |
| 245 |
if tc.field == "CipherSuites" { |
| 246 |
testServerOptions.Flags().Set("cipher-suites", tc.value.(string)) |
| 247 |
} else if tc.field != "" { |
| 248 |
rv := reflect.ValueOf(testServerOptions) |
| 249 |
rv = rv.Elem() |
| 250 |
fv := rv.FieldByName(tc.field) |
| 251 |
switch reflect.TypeOf(tc.value).Kind() { |
| 252 |
case reflect.String: |
| 253 |
svalue := tc.value.(string) |
| 254 |
fv.SetString(svalue) |
| 255 |
case reflect.Int: |
| 256 |
ivalue := tc.value.(int) |
| 257 |
fv.SetInt(int64(ivalue)) |
| 258 |
case reflect.Int64: |
| 259 |
if duration, ok := tc.value.(time.Duration); ok { |
| 260 |
fv.SetInt(int64(duration)) |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
actual := testServerOptions.Validate() |
| 265 |
assert.IsType(t, tc.expected, actual, "Validation for field %s, got the wrong type.", tc.field) |
| 266 |
assert.Equal(t, tc.expected, actual, "Validation for field %s, got the wrong value.", tc.field) |
| 267 |
}) |
| 268 |
} |
| 269 |
} |