|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package provisioning |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestParseProvisioningSource(t *testing.T) { |
| 16 | + defer leaktest.AfterTest(t)() |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + sourceStr string |
| 20 | + wantErr bool |
| 21 | + expectedMethod string |
| 22 | + expectedIDP string |
| 23 | + expectedErrMsg string |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "valid ldap source", |
| 27 | + sourceStr: "ldap:ldap.bar.com", |
| 28 | + wantErr: false, |
| 29 | + expectedMethod: "ldap", |
| 30 | + expectedIDP: "ldap.bar.com", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "valid ldap source with example.com", |
| 34 | + sourceStr: "ldap:ldap.example.com", |
| 35 | + wantErr: false, |
| 36 | + expectedMethod: "ldap", |
| 37 | + expectedIDP: "ldap.example.com", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "valid ldap source with simple hostname", |
| 41 | + sourceStr: "ldap:foo.bar", |
| 42 | + wantErr: false, |
| 43 | + expectedMethod: "ldap", |
| 44 | + expectedIDP: "foo.bar", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "missing auth method prefix", |
| 48 | + sourceStr: "ldap.example.com", |
| 49 | + wantErr: true, |
| 50 | + expectedErrMsg: `PROVISIONSRC "ldap.example.com" was not prefixed with any valid auth methods ["ldap"]`, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "invalid characters in IDP", |
| 54 | + sourceStr: "ldap:[]!@#%#^$&*", |
| 55 | + wantErr: true, |
| 56 | + expectedErrMsg: `provided IDP "[]!@#%#^$&*" in PROVISIONSRC is non parseable`, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "empty string", |
| 60 | + sourceStr: "", |
| 61 | + wantErr: true, |
| 62 | + expectedErrMsg: `PROVISIONSRC "" was not prefixed with any valid auth methods ["ldap"]`, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "invalid auth method", |
| 66 | + sourceStr: "oauth:example.com", |
| 67 | + wantErr: true, |
| 68 | + expectedErrMsg: `PROVISIONSRC "oauth:example.com" was not prefixed with any valid auth methods ["ldap"]`, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "only auth method without IDP", |
| 72 | + sourceStr: "ldap:", |
| 73 | + wantErr: true, |
| 74 | + expectedErrMsg: `PROVISIONSRC IDP cannot be empty`, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "IDP url with port", |
| 78 | + sourceStr: "ldap:example.com:389", |
| 79 | + wantErr: true, |
| 80 | + expectedErrMsg: "unknown PROVISIONSRC IDP url format in \"example.com:389\"", |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "IDP url starts with double slash", |
| 84 | + sourceStr: "ldap://ldap.example.com", |
| 85 | + wantErr: false, |
| 86 | + expectedMethod: "ldap", |
| 87 | + expectedIDP: "//ldap.example.com", |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "space in IDP url", |
| 91 | + sourceStr: "ldap:ldap1 ldap2", |
| 92 | + wantErr: false, |
| 93 | + expectedMethod: "ldap", |
| 94 | + expectedIDP: "ldap1%20ldap2", |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + for _, tt := range tests { |
| 99 | + t.Run(tt.name, func(t *testing.T) { |
| 100 | + source, err := ParseProvisioningSource(tt.sourceStr) |
| 101 | + if tt.wantErr { |
| 102 | + require.Error(t, err) |
| 103 | + require.Nil(t, source) |
| 104 | + require.Contains(t, err.Error(), tt.expectedErrMsg) |
| 105 | + } else { |
| 106 | + require.NoError(t, err) |
| 107 | + require.NotNil(t, source) |
| 108 | + require.Equal(t, tt.expectedMethod, source.authMethod) |
| 109 | + require.Equal(t, tt.expectedIDP, source.idp.String()) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestValidateSource(t *testing.T) { |
| 116 | + defer leaktest.AfterTest(t)() |
| 117 | + tests := []struct { |
| 118 | + name string |
| 119 | + sourceStr string |
| 120 | + wantErr bool |
| 121 | + expectedErrMsg string |
| 122 | + }{ |
| 123 | + { |
| 124 | + name: "valid ldap source", |
| 125 | + sourceStr: "ldap:ldap.bar.com", |
| 126 | + wantErr: false, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "valid ldap source with example.com", |
| 130 | + sourceStr: "ldap:ldap.example.com", |
| 131 | + wantErr: false, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "valid ldap source with simple hostname", |
| 135 | + sourceStr: "ldap:foo.bar", |
| 136 | + wantErr: false, |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "missing auth method prefix", |
| 140 | + sourceStr: "ldap.example.com", |
| 141 | + wantErr: true, |
| 142 | + expectedErrMsg: `PROVISIONSRC "ldap.example.com" was not prefixed with any valid auth methods ["ldap"]`, |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "invalid characters in IDP", |
| 146 | + sourceStr: "ldap:[]!@#%#^$&*", |
| 147 | + wantErr: true, |
| 148 | + expectedErrMsg: `provided IDP "[]!@#%#^$&*" in PROVISIONSRC is non parseable`, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "empty string", |
| 152 | + sourceStr: "", |
| 153 | + wantErr: true, |
| 154 | + expectedErrMsg: `PROVISIONSRC "" was not prefixed with any valid auth methods ["ldap"]`, |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "invalid auth method", |
| 158 | + sourceStr: "oauth:example.com", |
| 159 | + wantErr: true, |
| 160 | + expectedErrMsg: `PROVISIONSRC "oauth:example.com" was not prefixed with any valid auth methods ["ldap"]`, |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "only auth method without IDP", |
| 164 | + sourceStr: "ldap:", |
| 165 | + wantErr: true, |
| 166 | + expectedErrMsg: `PROVISIONSRC IDP cannot be empty`, |
| 167 | + }, |
| 168 | + } |
| 169 | + |
| 170 | + for _, tt := range tests { |
| 171 | + t.Run(tt.name, func(t *testing.T) { |
| 172 | + err := ValidateSource(tt.sourceStr) |
| 173 | + if tt.wantErr { |
| 174 | + require.Error(t, err) |
| 175 | + require.Contains(t, err.Error(), tt.expectedErrMsg) |
| 176 | + } else { |
| 177 | + require.NoError(t, err) |
| 178 | + } |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
0 commit comments