|
| 1 | +package sslutils |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/solo-io/gloo/projects/gloo/pkg/api/v1/ssl" |
| 9 | + "github.com/solo-io/go-utils/contextutils" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "go.uber.org/zap" |
| 12 | + "go.uber.org/zap/zapcore" |
| 13 | + "go.uber.org/zap/zaptest" |
| 14 | + "google.golang.org/protobuf/testing/protocmp" |
| 15 | + "google.golang.org/protobuf/types/known/wrapperspb" |
| 16 | + gwv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 17 | +) |
| 18 | + |
| 19 | +func TestApplySslExtensionOptions(t *testing.T) { |
| 20 | + testCases := []struct { |
| 21 | + name string |
| 22 | + out *ssl.SslConfig |
| 23 | + in *gwv1.GatewayTLSConfig |
| 24 | + errors []string |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "one_way_tls_true", |
| 28 | + out: &ssl.SslConfig{ |
| 29 | + OneWayTls: wrapperspb.Bool(true), |
| 30 | + }, |
| 31 | + in: &gwv1.GatewayTLSConfig{ |
| 32 | + Options: map[gwv1.AnnotationKey]gwv1.AnnotationValue{ |
| 33 | + GatewaySslOneWayTls: "true", |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "one_way_tls_true_incorrect_casing", |
| 39 | + out: &ssl.SslConfig{ |
| 40 | + OneWayTls: wrapperspb.Bool(true), |
| 41 | + }, |
| 42 | + in: &gwv1.GatewayTLSConfig{ |
| 43 | + Options: map[gwv1.AnnotationKey]gwv1.AnnotationValue{ |
| 44 | + GatewaySslOneWayTls: "True", |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "one_way_tls_false", |
| 50 | + out: &ssl.SslConfig{ |
| 51 | + OneWayTls: wrapperspb.Bool(false), |
| 52 | + }, |
| 53 | + in: &gwv1.GatewayTLSConfig{ |
| 54 | + Options: map[gwv1.AnnotationKey]gwv1.AnnotationValue{ |
| 55 | + GatewaySslOneWayTls: "false", |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "one_way_tls_false_incorrect_casing", |
| 61 | + out: &ssl.SslConfig{ |
| 62 | + OneWayTls: wrapperspb.Bool(false), |
| 63 | + }, |
| 64 | + in: &gwv1.GatewayTLSConfig{ |
| 65 | + Options: map[gwv1.AnnotationKey]gwv1.AnnotationValue{ |
| 66 | + GatewaySslOneWayTls: "False", |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "invalid_one_way_tls", |
| 72 | + out: &ssl.SslConfig{}, |
| 73 | + in: &gwv1.GatewayTLSConfig{ |
| 74 | + Options: map[gwv1.AnnotationKey]gwv1.AnnotationValue{ |
| 75 | + GatewaySslOneWayTls: "Foo", |
| 76 | + }, |
| 77 | + }, |
| 78 | + errors: []string{"invalid value for one-way-tls: Foo"}, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "cipher_suites", |
| 82 | + out: &ssl.SslConfig{ |
| 83 | + Parameters: &ssl.SslParameters{ |
| 84 | + CipherSuites: []string{"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + in: &gwv1.GatewayTLSConfig{ |
| 88 | + Options: map[gwv1.AnnotationKey]gwv1.AnnotationValue{ |
| 89 | + GatewaySslCipherSuites: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "subject_alt_names", |
| 95 | + out: &ssl.SslConfig{ |
| 96 | + VerifySubjectAltName: []string{"foo", "bar"}, |
| 97 | + }, |
| 98 | + in: &gwv1.GatewayTLSConfig{ |
| 99 | + Options: map[gwv1.AnnotationKey]gwv1.AnnotationValue{ |
| 100 | + GatewaySslVerifySubjectAltName: "foo,bar", |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "tls_max_version", |
| 106 | + out: &ssl.SslConfig{ |
| 107 | + Parameters: &ssl.SslParameters{ |
| 108 | + MaximumProtocolVersion: ssl.SslParameters_TLSv1_2, |
| 109 | + }, |
| 110 | + }, |
| 111 | + in: &gwv1.GatewayTLSConfig{ |
| 112 | + Options: map[gwv1.AnnotationKey]gwv1.AnnotationValue{ |
| 113 | + GatewaySslMaximumTlsVersion: "TLSv1_2", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "tls_min_version", |
| 119 | + out: &ssl.SslConfig{ |
| 120 | + Parameters: &ssl.SslParameters{ |
| 121 | + MinimumProtocolVersion: ssl.SslParameters_TLSv1_3, |
| 122 | + }, |
| 123 | + }, |
| 124 | + in: &gwv1.GatewayTLSConfig{ |
| 125 | + Options: map[gwv1.AnnotationKey]gwv1.AnnotationValue{ |
| 126 | + GatewaySslMinimumTlsVersion: "TLSv1_3", |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "invalid_tls_versions", |
| 132 | + out: &ssl.SslConfig{ |
| 133 | + Parameters: &ssl.SslParameters{}, |
| 134 | + }, |
| 135 | + in: &gwv1.GatewayTLSConfig{ |
| 136 | + Options: map[gwv1.AnnotationKey]gwv1.AnnotationValue{ |
| 137 | + GatewaySslMinimumTlsVersion: "TLSv1.3", |
| 138 | + GatewaySslMaximumTlsVersion: "TLSv1.2", |
| 139 | + }, |
| 140 | + }, |
| 141 | + errors: []string{ |
| 142 | + "invalid maximum tls version: TLSv1.2", |
| 143 | + "invalid minimum tls version: TLSv1.3", |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "maximium_tls_version_less_than_minimum", |
| 148 | + out: &ssl.SslConfig{ |
| 149 | + VerifySubjectAltName: []string{"foo", "bar"}, |
| 150 | + Parameters: &ssl.SslParameters{}, |
| 151 | + }, |
| 152 | + in: &gwv1.GatewayTLSConfig{ |
| 153 | + Options: map[gwv1.AnnotationKey]gwv1.AnnotationValue{ |
| 154 | + GatewaySslMinimumTlsVersion: "TLSv1_3", |
| 155 | + GatewaySslMaximumTlsVersion: "TLSv1_2", |
| 156 | + GatewaySslVerifySubjectAltName: "foo,bar", |
| 157 | + }, |
| 158 | + }, |
| 159 | + errors: []string{ |
| 160 | + "maximum tls version TLSv1_2 is less than minimum tls version TLSv1_3", |
| 161 | + }, |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "multiple_options", |
| 165 | + out: &ssl.SslConfig{ |
| 166 | + VerifySubjectAltName: []string{"foo", "bar"}, |
| 167 | + OneWayTls: wrapperspb.Bool(true), |
| 168 | + Parameters: &ssl.SslParameters{ |
| 169 | + MaximumProtocolVersion: ssl.SslParameters_TLSv1_3, |
| 170 | + MinimumProtocolVersion: ssl.SslParameters_TLSv1_2, |
| 171 | + CipherSuites: []string{"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"}, |
| 172 | + }, |
| 173 | + }, |
| 174 | + in: &gwv1.GatewayTLSConfig{ |
| 175 | + Options: map[gwv1.AnnotationKey]gwv1.AnnotationValue{ |
| 176 | + GatewaySslMaximumTlsVersion: "TLSv1_3", |
| 177 | + GatewaySslMinimumTlsVersion: "TLSv1_2", |
| 178 | + GatewaySslVerifySubjectAltName: "foo,bar", |
| 179 | + GatewaySslOneWayTls: "true", |
| 180 | + GatewaySslCipherSuites: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", |
| 181 | + }, |
| 182 | + }, |
| 183 | + }, |
| 184 | + { |
| 185 | + name: "misspelled_option", |
| 186 | + out: &ssl.SslConfig{}, |
| 187 | + in: &gwv1.GatewayTLSConfig{ |
| 188 | + Options: map[gwv1.AnnotationKey]gwv1.AnnotationValue{ |
| 189 | + GatewaySslMinimumTlsVersion + "s": "TLSv1_3", |
| 190 | + }, |
| 191 | + }, |
| 192 | + errors: []string{ |
| 193 | + "unknown ssl option: gateway.gloo.solo.io/ssl/minimum-tls-versions", |
| 194 | + }, |
| 195 | + }, |
| 196 | + } |
| 197 | + |
| 198 | + for _, tc := range testCases { |
| 199 | + t.Run(tc.name, func(t *testing.T) { |
| 200 | + b := &zaptest.Buffer{} |
| 201 | + logger := zap.New(zapcore.NewCore( |
| 202 | + zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig()), |
| 203 | + b, |
| 204 | + zapcore.DebugLevel, |
| 205 | + )) |
| 206 | + ctx := contextutils.WithExistingLogger(context.Background(), logger.Sugar()) |
| 207 | + out := &ssl.SslConfig{} |
| 208 | + ApplySslExtensionOptions(ctx, tc.in, out) |
| 209 | + assert.Empty(t, cmp.Diff(tc.out, out, protocmp.Transform())) |
| 210 | + if len(tc.errors) > 0 { |
| 211 | + assert.Contains(t, b.String(), "error applying ssl extension options") |
| 212 | + for _, err := range tc.errors { |
| 213 | + assert.Contains(t, b.String(), err) |
| 214 | + } |
| 215 | + } else { |
| 216 | + assert.Empty(t, b.String()) |
| 217 | + } |
| 218 | + }) |
| 219 | + |
| 220 | + } |
| 221 | +} |
0 commit comments