|
| 1 | +package oauth |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 8 | + |
| 9 | + configv1 "github.com/openshift/api/config/v1" |
| 10 | +) |
| 11 | + |
| 12 | +func TestValidateGitHubIdentityProvider(t *testing.T) { |
| 13 | + type args struct { |
| 14 | + provider *configv1.GitHubIdentityProvider |
| 15 | + mappingMethod configv1.MappingMethodType |
| 16 | + fieldPath *field.Path |
| 17 | + } |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + args args |
| 21 | + errors field.ErrorList |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "cannot use GH as hostname", |
| 25 | + args: args{ |
| 26 | + provider: &configv1.GitHubIdentityProvider{ |
| 27 | + ClientID: "client", |
| 28 | + ClientSecret: configv1.SecretNameReference{Name: "secret"}, |
| 29 | + Organizations: []string{"org1"}, |
| 30 | + Teams: nil, |
| 31 | + Hostname: "github.com", |
| 32 | + CA: configv1.ConfigMapNameReference{Name: "caconfigmap"}, |
| 33 | + }, |
| 34 | + mappingMethod: "", |
| 35 | + }, |
| 36 | + errors: field.ErrorList{ |
| 37 | + {Type: field.ErrorTypeInvalid, Field: "hostname", BadValue: "github.com", Detail: "cannot equal [*.]github.com"}, |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "cannot use GH subdomain as hostname", |
| 42 | + args: args{ |
| 43 | + provider: &configv1.GitHubIdentityProvider{ |
| 44 | + ClientID: "client", |
| 45 | + ClientSecret: configv1.SecretNameReference{Name: "secret"}, |
| 46 | + Organizations: []string{"org1"}, |
| 47 | + Teams: nil, |
| 48 | + Hostname: "foo.github.com", |
| 49 | + CA: configv1.ConfigMapNameReference{Name: "caconfigmap"}, |
| 50 | + }, |
| 51 | + mappingMethod: "", |
| 52 | + }, |
| 53 | + errors: field.ErrorList{ |
| 54 | + {Type: field.ErrorTypeInvalid, Field: "hostname", BadValue: "foo.github.com", Detail: "cannot equal [*.]github.com"}, |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "valid domain hostname", |
| 59 | + args: args{ |
| 60 | + provider: &configv1.GitHubIdentityProvider{ |
| 61 | + ClientID: "client", |
| 62 | + ClientSecret: configv1.SecretNameReference{Name: "secret"}, |
| 63 | + Organizations: []string{"org1"}, |
| 64 | + Teams: nil, |
| 65 | + Hostname: "company.com", |
| 66 | + CA: configv1.ConfigMapNameReference{Name: "caconfigmap"}, |
| 67 | + }, |
| 68 | + mappingMethod: "", |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "valid ip hostname", |
| 73 | + args: args{ |
| 74 | + provider: &configv1.GitHubIdentityProvider{ |
| 75 | + ClientID: "client", |
| 76 | + ClientSecret: configv1.SecretNameReference{Name: "secret"}, |
| 77 | + Organizations: []string{"org1"}, |
| 78 | + Teams: nil, |
| 79 | + Hostname: "192.168.8.1", |
| 80 | + CA: configv1.ConfigMapNameReference{Name: "caconfigmap"}, |
| 81 | + }, |
| 82 | + mappingMethod: "", |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "invalid ip hostname with port", |
| 87 | + args: args{ |
| 88 | + provider: &configv1.GitHubIdentityProvider{ |
| 89 | + ClientID: "client", |
| 90 | + ClientSecret: configv1.SecretNameReference{Name: "secret"}, |
| 91 | + Organizations: []string{"org1"}, |
| 92 | + Teams: nil, |
| 93 | + Hostname: "192.168.8.1:8080", |
| 94 | + CA: configv1.ConfigMapNameReference{Name: "caconfigmap"}, |
| 95 | + }, |
| 96 | + mappingMethod: "", |
| 97 | + }, |
| 98 | + errors: field.ErrorList{ |
| 99 | + {Type: field.ErrorTypeInvalid, Field: "hostname", BadValue: "192.168.8.1:8080", Detail: "must be a valid DNS subdomain or IP address"}, |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "invalid domain hostname", |
| 104 | + args: args{ |
| 105 | + provider: &configv1.GitHubIdentityProvider{ |
| 106 | + ClientID: "client", |
| 107 | + ClientSecret: configv1.SecretNameReference{Name: "secret"}, |
| 108 | + Organizations: []string{"org1"}, |
| 109 | + Teams: nil, |
| 110 | + Hostname: "google-.com", |
| 111 | + CA: configv1.ConfigMapNameReference{Name: "caconfigmap"}, |
| 112 | + }, |
| 113 | + mappingMethod: "", |
| 114 | + }, |
| 115 | + errors: field.ErrorList{ |
| 116 | + {Type: field.ErrorTypeInvalid, Field: "hostname", BadValue: "google-.com", Detail: "must be a valid DNS subdomain or IP address"}, |
| 117 | + }, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "invalid name in ca ref and no hostname", |
| 121 | + args: args{ |
| 122 | + provider: &configv1.GitHubIdentityProvider{ |
| 123 | + ClientID: "client", |
| 124 | + ClientSecret: configv1.SecretNameReference{Name: "secret"}, |
| 125 | + Organizations: []string{"org1"}, |
| 126 | + Teams: nil, |
| 127 | + Hostname: "", |
| 128 | + CA: configv1.ConfigMapNameReference{Name: "ca&config-map"}, |
| 129 | + }, |
| 130 | + mappingMethod: "", |
| 131 | + }, |
| 132 | + errors: field.ErrorList{ |
| 133 | + {Type: field.ErrorTypeInvalid, Field: "ca.name", BadValue: "ca&config-map", Detail: "a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')"}, |
| 134 | + {Type: field.ErrorTypeInvalid, Field: "ca", BadValue: configv1.ConfigMapNameReference{Name: "ca&config-map"}, Detail: "cannot be specified when hostname is empty"}, |
| 135 | + }, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "valid ca and hostname", |
| 139 | + args: args{ |
| 140 | + provider: &configv1.GitHubIdentityProvider{ |
| 141 | + ClientID: "client", |
| 142 | + ClientSecret: configv1.SecretNameReference{Name: "secret"}, |
| 143 | + Organizations: []string{"org1"}, |
| 144 | + Teams: nil, |
| 145 | + Hostname: "mo.co", |
| 146 | + CA: configv1.ConfigMapNameReference{Name: "ca-config-map"}, |
| 147 | + }, |
| 148 | + mappingMethod: "", |
| 149 | + }, |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "GitHub requires client ID and secret", |
| 153 | + args: args{ |
| 154 | + provider: &configv1.GitHubIdentityProvider{ |
| 155 | + ClientID: "", |
| 156 | + ClientSecret: configv1.SecretNameReference{}, |
| 157 | + Organizations: []string{"org1"}, |
| 158 | + Teams: nil, |
| 159 | + Hostname: "", |
| 160 | + CA: configv1.ConfigMapNameReference{}, |
| 161 | + }, |
| 162 | + mappingMethod: "", |
| 163 | + }, |
| 164 | + errors: field.ErrorList{ |
| 165 | + {Type: field.ErrorTypeRequired, Field: "provider.clientID", BadValue: "", Detail: ""}, |
| 166 | + {Type: field.ErrorTypeRequired, Field: "provider.clientSecret.name", BadValue: "", Detail: ""}, |
| 167 | + }, |
| 168 | + }, |
| 169 | + { |
| 170 | + name: "GitHub warns when not constrained to organizations or teams without lookup", |
| 171 | + args: args{ |
| 172 | + provider: &configv1.GitHubIdentityProvider{ |
| 173 | + ClientID: "client", |
| 174 | + ClientSecret: configv1.SecretNameReference{Name: "secret"}, |
| 175 | + Organizations: nil, |
| 176 | + Teams: nil, |
| 177 | + Hostname: "", |
| 178 | + CA: configv1.ConfigMapNameReference{}, |
| 179 | + }, |
| 180 | + mappingMethod: "", |
| 181 | + }, |
| 182 | + errors: field.ErrorList{ |
| 183 | + {Type: field.ErrorTypeInvalid, Field: "", BadValue: nil, Detail: "one of organizations or teams must be specified unless hostname is set or lookup is used"}, |
| 184 | + }, |
| 185 | + }, |
| 186 | + { |
| 187 | + name: "GitHub does not warn when not constrained to organizations or teams with lookup", |
| 188 | + args: args{ |
| 189 | + provider: &configv1.GitHubIdentityProvider{ |
| 190 | + ClientID: "client", |
| 191 | + ClientSecret: configv1.SecretNameReference{Name: "secret"}, |
| 192 | + Organizations: nil, |
| 193 | + Teams: nil, |
| 194 | + Hostname: "", |
| 195 | + CA: configv1.ConfigMapNameReference{}, |
| 196 | + }, |
| 197 | + mappingMethod: "lookup", |
| 198 | + }, |
| 199 | + }, |
| 200 | + { |
| 201 | + name: "invalid cannot specific both organizations and teams", |
| 202 | + args: args{ |
| 203 | + provider: &configv1.GitHubIdentityProvider{ |
| 204 | + ClientID: "client", |
| 205 | + ClientSecret: configv1.SecretNameReference{Name: "secret"}, |
| 206 | + Organizations: []string{"org1"}, |
| 207 | + Teams: []string{"org1/team1"}, |
| 208 | + Hostname: "", |
| 209 | + CA: configv1.ConfigMapNameReference{}, |
| 210 | + }, |
| 211 | + mappingMethod: "", |
| 212 | + }, |
| 213 | + errors: field.ErrorList{ |
| 214 | + {Type: field.ErrorTypeInvalid, Field: "organizations", BadValue: []string{"org1"}, Detail: "specify organizations or teams, not both"}, |
| 215 | + {Type: field.ErrorTypeInvalid, Field: "teams", BadValue: []string{"org1/team1"}, Detail: "specify organizations or teams, not both"}, |
| 216 | + }, |
| 217 | + }, |
| 218 | + { |
| 219 | + name: "invalid team format", |
| 220 | + args: args{ |
| 221 | + provider: &configv1.GitHubIdentityProvider{ |
| 222 | + ClientID: "client", |
| 223 | + ClientSecret: configv1.SecretNameReference{Name: "secret"}, |
| 224 | + Organizations: nil, |
| 225 | + Teams: []string{"org1/team1", "org2/not/team2", "org3//team3", "", "org4/team4"}, |
| 226 | + Hostname: "", |
| 227 | + CA: configv1.ConfigMapNameReference{}, |
| 228 | + }, |
| 229 | + mappingMethod: "", |
| 230 | + }, |
| 231 | + errors: field.ErrorList{ |
| 232 | + {Type: field.ErrorTypeInvalid, Field: "teams[1]", BadValue: "org2/not/team2", Detail: "must be in the format <org>/<team>"}, |
| 233 | + {Type: field.ErrorTypeInvalid, Field: "teams[2]", BadValue: "org3//team3", Detail: "must be in the format <org>/<team>"}, |
| 234 | + {Type: field.ErrorTypeInvalid, Field: "teams[3]", BadValue: "", Detail: "must be in the format <org>/<team>"}, |
| 235 | + }, |
| 236 | + }, |
| 237 | + } |
| 238 | + for _, tt := range tests { |
| 239 | + t.Run(tt.name, func(t *testing.T) { |
| 240 | + got := ValidateGitHubIdentityProvider(tt.args.provider, tt.args.mappingMethod, tt.args.fieldPath) |
| 241 | + if tt.errors == nil && len(got) == 0 { |
| 242 | + return |
| 243 | + } |
| 244 | + if !reflect.DeepEqual(got, tt.errors) { |
| 245 | + t.Errorf("ValidateGitHubIdentityProvider() = %v, want %v", got, tt.errors) |
| 246 | + } |
| 247 | + }) |
| 248 | + } |
| 249 | +} |
0 commit comments