|
| 1 | +/* |
| 2 | +Copyright 2023 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 tlsutils |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/tls" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | +) |
| 29 | + |
| 30 | +var rsaCertPEM = `-----BEGIN CERTIFICATE----- |
| 31 | +MIIB0zCCAX2gAwIBAgIJAI/M7BYjwB+uMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV |
| 32 | +BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX |
| 33 | +aWRnaXRzIFB0eSBMdGQwHhcNMTIwOTEyMjE1MjAyWhcNMTUwOTEyMjE1MjAyWjBF |
| 34 | +MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50 |
| 35 | +ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANLJ |
| 36 | +hPHhITqQbPklG3ibCVxwGMRfp/v4XqhfdQHdcVfHap6NQ5Wok/4xIA+ui35/MmNa |
| 37 | +rtNuC+BdZ1tMuVCPFZcCAwEAAaNQME4wHQYDVR0OBBYEFJvKs8RfJaXTH08W+SGv |
| 38 | +zQyKn0H8MB8GA1UdIwQYMBaAFJvKs8RfJaXTH08W+SGvzQyKn0H8MAwGA1UdEwQF |
| 39 | +MAMBAf8wDQYJKoZIhvcNAQEFBQADQQBJlffJHybjDGxRMqaRmDhX0+6v02TUKZsW |
| 40 | +r5QuVbpQhH6u+0UgcW0jp9QwpxoPTLTWGXEWBBBurxFwiCBhkQ+V |
| 41 | +-----END CERTIFICATE----- |
| 42 | +` |
| 43 | + |
| 44 | +var rsaKeyPEM = testingKey(`-----BEGIN RSA TESTING KEY----- |
| 45 | +MIIBOwIBAAJBANLJhPHhITqQbPklG3ibCVxwGMRfp/v4XqhfdQHdcVfHap6NQ5Wo |
| 46 | +k/4xIA+ui35/MmNartNuC+BdZ1tMuVCPFZcCAwEAAQJAEJ2N+zsR0Xn8/Q6twa4G |
| 47 | +6OB1M1WO+k+ztnX/1SvNeWu8D6GImtupLTYgjZcHufykj09jiHmjHx8u8ZZB/o1N |
| 48 | +MQIhAPW+eyZo7ay3lMz1V01WVjNKK9QSn1MJlb06h/LuYv9FAiEA25WPedKgVyCW |
| 49 | +SmUwbPw8fnTcpqDWE3yTO3vKcebqMSsCIBF3UmVue8YU3jybC3NxuXq3wNm34R8T |
| 50 | +xVLHwDXh/6NJAiEAl2oHGGLz64BuAfjKrqwz7qMYr9HCLIe/YsoWq/olzScCIQDi |
| 51 | +D2lWusoe2/nEqfDVVWGWlyJ7yOmqaVm/iNUN9B2N2g== |
| 52 | +-----END RSA TESTING KEY----- |
| 53 | +`) |
| 54 | + |
| 55 | +func testingKey(s string) string { return strings.ReplaceAll(s, "TESTING KEY", "PRIVATE KEY") } |
| 56 | + |
| 57 | +func TestCreateTLSConfig(t *testing.T) { |
| 58 | + |
| 59 | + tests := []struct { |
| 60 | + title string |
| 61 | + prefix string |
| 62 | + caFile string |
| 63 | + certFile string |
| 64 | + keyFile string |
| 65 | + isInsecureStr string |
| 66 | + serverName string |
| 67 | + assertions func(actual *tls.Config, err error) |
| 68 | + }{ |
| 69 | + { |
| 70 | + "Provide only CA returns error", |
| 71 | + "prefix", |
| 72 | + "", |
| 73 | + rsaCertPEM, |
| 74 | + "", |
| 75 | + "", |
| 76 | + "", |
| 77 | + func(actual *tls.Config, err error) { |
| 78 | + assert.Contains(t, err.Error(), "either both cert and key or none must be provided") |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + "Invalid cert and key returns error", |
| 83 | + "prefix", |
| 84 | + "", |
| 85 | + "invalid-cert", |
| 86 | + "invalid-key", |
| 87 | + "", |
| 88 | + "", |
| 89 | + func(actual *tls.Config, err error) { |
| 90 | + assert.Contains(t, err.Error(), "could not load TLS cert") |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + "Valid cert and key return a valid tls.Config with a certificate", |
| 95 | + "prefix", |
| 96 | + "", |
| 97 | + rsaCertPEM, |
| 98 | + rsaKeyPEM, |
| 99 | + "", |
| 100 | + "server-name", |
| 101 | + func(actual *tls.Config, err error) { |
| 102 | + require.NoError(t, err) |
| 103 | + assert.Equal(t, "server-name", actual.ServerName) |
| 104 | + assert.NotNil(t, actual.Certificates[0]) |
| 105 | + assert.False(t, actual.InsecureSkipVerify) |
| 106 | + assert.Equal(t, actual.MinVersion, uint16(defaultMinVersion)) |
| 107 | + }, |
| 108 | + }, |
| 109 | + { |
| 110 | + "Invalid CA file returns error", |
| 111 | + "prefix", |
| 112 | + "invalid-ca-content", |
| 113 | + "", |
| 114 | + "", |
| 115 | + "", |
| 116 | + "", |
| 117 | + func(actual *tls.Config, err error) { |
| 118 | + assert.Error(t, err) |
| 119 | + assert.Contains(t, err.Error(), "could not read root certs") |
| 120 | + }, |
| 121 | + }, |
| 122 | + { |
| 123 | + "Invalid CA file path returns error", |
| 124 | + "prefix", |
| 125 | + "ca-path-does-not-exist", |
| 126 | + "", |
| 127 | + "", |
| 128 | + "", |
| 129 | + "server-name", |
| 130 | + func(actual *tls.Config, err error) { |
| 131 | + assert.Error(t, err) |
| 132 | + assert.Contains(t, err.Error(), "error reading /path/does/not/exist") |
| 133 | + }, |
| 134 | + }, |
| 135 | + { |
| 136 | + "Complete config with CA, cert, and key returns valid tls.Config", |
| 137 | + "prefix", |
| 138 | + rsaCertPEM, |
| 139 | + rsaCertPEM, |
| 140 | + rsaKeyPEM, |
| 141 | + "", |
| 142 | + "server-name", |
| 143 | + func(actual *tls.Config, err error) { |
| 144 | + require.NoError(t, err) |
| 145 | + assert.Equal(t, "server-name", actual.ServerName) |
| 146 | + assert.NotNil(t, actual.Certificates[0]) |
| 147 | + assert.NotNil(t, actual.RootCAs) |
| 148 | + assert.False(t, actual.InsecureSkipVerify) |
| 149 | + }, |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + for _, tc := range tests { |
| 154 | + t.Run(tc.title, func(t *testing.T) { |
| 155 | + // setup |
| 156 | + dir := t.TempDir() |
| 157 | + |
| 158 | + if tc.caFile != "" { |
| 159 | + path := fmt.Sprintf("%s/caFile", dir) |
| 160 | + writeToFile(path, tc.caFile) |
| 161 | + t.Setenv(fmt.Sprintf("%s_CA_FILE", tc.prefix), path) |
| 162 | + } |
| 163 | + |
| 164 | + if tc.caFile == "ca-path-does-not-exist" { |
| 165 | + t.Setenv(fmt.Sprintf("%s_CA_FILE", tc.prefix), "/path/does/not/exist") |
| 166 | + } |
| 167 | + |
| 168 | + if tc.certFile != "" { |
| 169 | + path := fmt.Sprintf("%s/certFile", dir) |
| 170 | + writeToFile(path, tc.certFile) |
| 171 | + t.Setenv(fmt.Sprintf("%s_CERT_FILE", tc.prefix), path) |
| 172 | + } |
| 173 | + |
| 174 | + if tc.keyFile != "" { |
| 175 | + path := fmt.Sprintf("%s/keyFile", dir) |
| 176 | + writeToFile(path, tc.keyFile) |
| 177 | + t.Setenv(fmt.Sprintf("%s_KEY_FILE", tc.prefix), path) |
| 178 | + } |
| 179 | + |
| 180 | + if tc.serverName != "" { |
| 181 | + t.Setenv(fmt.Sprintf("%s_TLS_SERVER_NAME", tc.prefix), tc.serverName) |
| 182 | + } |
| 183 | + |
| 184 | + if tc.isInsecureStr != "" { |
| 185 | + t.Setenv(fmt.Sprintf("%s_INSECURE", tc.prefix), tc.isInsecureStr) |
| 186 | + } |
| 187 | + |
| 188 | + // test |
| 189 | + actual, err := CreateTLSConfig(tc.prefix) |
| 190 | + tc.assertions(actual, err) |
| 191 | + }) |
| 192 | + } |
| 193 | + |
| 194 | +} |
| 195 | + |
| 196 | +func writeToFile(filename string, content string) error { |
| 197 | + file, fileErr := os.Create(filename) |
| 198 | + if fileErr != nil { |
| 199 | + _ = fmt.Errorf("failed to create file: %w", fileErr) |
| 200 | + } |
| 201 | + defer file.Close() |
| 202 | + _, writeErr := file.WriteString(content) |
| 203 | + if writeErr != nil { |
| 204 | + _ = fmt.Errorf("failed to write to file: %s", filename) |
| 205 | + } |
| 206 | + return nil |
| 207 | +} |
0 commit comments