github.com/Venafi/vcert/v5@v5.10.2/pkg/playbook/app/domain/connection_test.go (about) 1 /* 2 * Copyright 2023 Venafi, Inc. 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 domain 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/Venafi/vcert/v5/pkg/venafi" 24 "github.com/stretchr/testify/suite" 25 26 "github.com/Venafi/vcert/v5/pkg/endpoint" 27 ) 28 29 type ConnectionSuite struct { 30 suite.Suite 31 testCases []struct { 32 name string 33 c Connection 34 expectedCType endpoint.ConnectorType 35 expectedValid bool 36 expectedErr error 37 } 38 } 39 40 func (s *ConnectionSuite) SetupTest() { 41 s.testCases = []struct { 42 name string 43 c Connection 44 expectedCType endpoint.ConnectorType 45 expectedValid bool 46 expectedErr error 47 }{ 48 // FIREFLY USE CASES 49 { 50 name: "Firefly_valid_secret", 51 c: Connection{ 52 Platform: venafi.Firefly, 53 Credentials: Authentication{ 54 Authentication: endpoint.Authentication{ 55 ClientSecret: "mySecret", 56 ClientId: "myClientID", 57 IdentityProvider: &endpoint.OAuthProvider{ 58 TokenURL: "https://my.okta.instance.com/token", 59 }, 60 }, 61 }, 62 URL: "https://my.firefly.instance.com", 63 }, 64 expectedCType: endpoint.ConnectorTypeFirefly, 65 expectedValid: true, 66 }, 67 { 68 name: "Firefly_valid_password", 69 c: Connection{ 70 Platform: venafi.Firefly, 71 Credentials: Authentication{ 72 Authentication: endpoint.Authentication{ 73 User: "myUser", 74 Password: "myPassword", 75 ClientId: "myClientID", 76 IdentityProvider: &endpoint.OAuthProvider{ 77 TokenURL: "https://my.okta.instance.com/token", 78 }, 79 }, 80 }, 81 URL: "https://my.firefly.instance.com", 82 }, 83 expectedCType: endpoint.ConnectorTypeFirefly, 84 expectedValid: true, 85 }, 86 { 87 name: "Firefly_valid_token", 88 c: Connection{ 89 Platform: venafi.Firefly, 90 Credentials: Authentication{ 91 Authentication: endpoint.Authentication{ 92 AccessToken: "foo123Token", 93 }, 94 }, 95 URL: "https://my.firefly.instance.com", 96 }, 97 expectedCType: endpoint.ConnectorTypeFirefly, 98 expectedValid: true, 99 }, 100 { 101 name: "Firefly_invalid_no_url", 102 c: Connection{ 103 Platform: venafi.Firefly, 104 Credentials: Authentication{}, 105 }, 106 expectedCType: endpoint.ConnectorTypeFirefly, 107 expectedValid: false, 108 expectedErr: ErrNoFireflyURL, 109 }, 110 { 111 name: "Firefly_invalid_empty_credentials", 112 c: Connection{ 113 Platform: venafi.Firefly, 114 Credentials: Authentication{}, 115 URL: "https://my.firefly.instance.com", 116 }, 117 expectedCType: endpoint.ConnectorTypeFirefly, 118 expectedValid: false, 119 expectedErr: ErrNoCredentials, 120 }, 121 { 122 name: "Firefly_invalid_no_clientID", 123 c: Connection{ 124 Platform: venafi.Firefly, 125 Credentials: Authentication{ 126 Authentication: endpoint.Authentication{ 127 ClientSecret: "mySecret", 128 }, 129 }, 130 URL: "https://my.firefly.instance.com", 131 }, 132 expectedCType: endpoint.ConnectorTypeFirefly, 133 expectedValid: false, 134 expectedErr: ErrNoClientId, 135 }, 136 { 137 name: "Firefly_invalid_no_IdP", 138 c: Connection{ 139 Platform: venafi.Firefly, 140 Credentials: Authentication{ 141 Authentication: endpoint.Authentication{ 142 ClientSecret: "mySecret", 143 ClientId: "myClientID", 144 }, 145 }, 146 URL: "https://my.firefly.instance.com", 147 }, 148 expectedCType: endpoint.ConnectorTypeFirefly, 149 expectedValid: false, 150 expectedErr: ErrNoIdentityProviderURL, 151 }, 152 // TPP USE CASES 153 { 154 name: "TPP_valid", 155 c: Connection{ 156 Platform: venafi.TPP, 157 Credentials: Authentication{ 158 Authentication: endpoint.Authentication{ 159 AccessToken: "123abc###", 160 }, 161 }, 162 URL: "https://my.tpp.instance.com", 163 TrustBundlePath: "", 164 Insecure: false, 165 }, 166 expectedCType: endpoint.ConnectorTypeTPP, 167 expectedValid: true, 168 }, 169 { 170 name: "TPP_invalid_empty_credentials", 171 c: Connection{ 172 Platform: venafi.TPP, 173 Credentials: Authentication{}, 174 URL: "https://my.tpp.instance.com", 175 }, 176 expectedCType: endpoint.ConnectorTypeTPP, 177 expectedValid: false, 178 expectedErr: ErrNoCredentials, 179 }, 180 { 181 name: "TPP_invalid_no_url", 182 c: Connection{ 183 Platform: venafi.TPP, 184 Credentials: Authentication{ 185 Authentication: endpoint.Authentication{ 186 AccessToken: "123abc###", 187 }, 188 }, 189 }, 190 expectedCType: endpoint.ConnectorTypeTPP, 191 expectedValid: false, 192 expectedErr: ErrNoTPPURL, 193 }, 194 { 195 name: "TPP_invalid_trustbundle_not_exist", 196 c: Connection{ 197 Platform: venafi.TPP, 198 Credentials: Authentication{ 199 Authentication: endpoint.Authentication{ 200 AccessToken: "123abc###", 201 }, 202 }, 203 URL: "https://my.tpp.instance.com", 204 TrustBundlePath: "/foo/bar/trustbundle.pem", 205 }, 206 expectedCType: endpoint.ConnectorTypeTPP, 207 expectedValid: false, 208 expectedErr: ErrTrustBundleNotExist, 209 }, 210 // VAAS USE CASES 211 { 212 name: "VaaS_valid", 213 c: Connection{ 214 Platform: venafi.TLSPCloud, 215 Credentials: Authentication{ 216 Authentication: endpoint.Authentication{ 217 APIKey: "xxx-XXX-xxx", 218 }, 219 }, 220 }, 221 expectedCType: endpoint.ConnectorTypeCloud, 222 expectedValid: true, 223 }, 224 { 225 name: "VaaS_invalid_empty_credentials", 226 c: Connection{ 227 Platform: venafi.TLSPCloud, 228 Credentials: Authentication{}, 229 }, 230 expectedCType: endpoint.ConnectorTypeCloud, 231 expectedValid: false, 232 expectedErr: ErrNoCredentials, 233 }, 234 // UNKNOWN USE CASES 235 { 236 name: "Unknown_invalid", 237 c: Connection{ 238 Platform: venafi.Undefined, 239 }, 240 expectedCType: endpoint.ConnectorTypeFake, 241 expectedValid: false, 242 expectedErr: fmt.Errorf("invalid connection type %v", venafi.Undefined), 243 }, 244 } 245 } 246 247 func TestConnection(t *testing.T) { 248 suite.Run(t, new(ConnectionSuite)) 249 } 250 251 func (s *ConnectionSuite) TestConnection_GetConnectorType() { 252 for _, tc := range s.testCases { 253 s.Run(tc.name, func() { 254 currentType := tc.c.GetConnectorType() 255 s.Equal(tc.expectedCType, currentType) 256 }) 257 } 258 } 259 260 func (s *ConnectionSuite) TestConnection_IsValid() { 261 for _, tc := range s.testCases { 262 s.Run(tc.name, func() { 263 result, err := tc.c.IsValid() 264 s.Equal(tc.expectedValid, result) 265 266 if tc.expectedValid { 267 s.Nil(err) 268 } else { 269 s.NotNil(err) 270 s.Error(err) 271 s.Contains(err.Error(), tc.expectedErr.Error()) 272 } 273 }) 274 } 275 }