| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package cloudauth |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | func TestAzureADAuthConfigValidate(t *testing.T) { |
| 13 | tests := map[string]struct { |
| 14 | cfg AzureADAuthConfig |
| 15 | wantErr bool |
| 16 | }{ |
| 17 | "default mode": { |
| 18 | cfg: AzureADAuthConfig{Mode: AzureADAuthModeDefault}, |
| 19 | }, |
| 20 | "empty mode": { |
| 21 | cfg: AzureADAuthConfig{}, |
| 22 | wantErr: true, |
| 23 | }, |
| 24 | "managed identity mode": { |
| 25 | cfg: AzureADAuthConfig{Mode: AzureADAuthModeManagedIdentity}, |
| 26 | }, |
| 27 | "service principal mode": { |
| 28 | cfg: AzureADAuthConfig{ |
| 29 | Mode: AzureADAuthModeServicePrincipal, |
| 30 | ModeServicePrincipal: &AzureADModeServicePrincipalConfig{ |
| 31 | TenantID: "tenant", |
| 32 | ClientID: "client", |
| 33 | ClientSecret: "secret", |
| 34 | }, |
| 35 | }, |
| 36 | }, |
| 37 | "service principal missing secret": { |
| 38 | cfg: AzureADAuthConfig{ |
| 39 | Mode: AzureADAuthModeServicePrincipal, |
| 40 | ModeServicePrincipal: &AzureADModeServicePrincipalConfig{ |
| 41 | TenantID: "tenant", |
| 42 | ClientID: "client", |
| 43 | }, |
| 44 | }, |
| 45 | wantErr: true, |
| 46 | }, |
| 47 | "invalid mode": { |
| 48 | cfg: AzureADAuthConfig{Mode: "invalid_mode"}, |
| 49 | wantErr: true, |
| 50 | }, |
| 51 | } |
| 52 | |
| 53 | for name, tc := range tests { |
| 54 | t.Run(name, func(t *testing.T) { |
| 55 | err := tc.cfg.Validate() |
| 56 | if tc.wantErr { |
| 57 | require.Error(t, err) |
| 58 | return |
| 59 | } |
| 60 | require.NoError(t, err) |
| 61 | }) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestAzureADAuthConfigValidateWithPath(t *testing.T) { |
| 66 | tests := map[string]struct { |
| 67 | cfg AzureADAuthConfig |
| 68 | validatePath string |
| 69 | wantErrString string |
| 70 | }{ |
| 71 | "cloud auth path": { |
| 72 | cfg: AzureADAuthConfig{ |
| 73 | Mode: AzureADAuthModeServicePrincipal, |
| 74 | ModeServicePrincipal: &AzureADModeServicePrincipalConfig{ |
| 75 | TenantID: "tenant", |
| 76 | ClientID: "client", |
| 77 | }, |
| 78 | }, |
| 79 | validatePath: azureADAuthConfigPath, |
| 80 | wantErrString: "cloud_auth.azure_ad.mode_service_principal.client_secret is required", |
| 81 | }, |
| 82 | "root path": { |
| 83 | cfg: AzureADAuthConfig{ |
| 84 | Mode: AzureADAuthModeServicePrincipal, |
| 85 | ModeServicePrincipal: &AzureADModeServicePrincipalConfig{ |
| 86 | TenantID: "tenant", |
| 87 | ClientID: "client", |
| 88 | }, |
| 89 | }, |
| 90 | validatePath: "", |
| 91 | wantErrString: "mode_service_principal.client_secret is required", |
| 92 | }, |
| 93 | "missing mode": { |
| 94 | cfg: AzureADAuthConfig{}, |
| 95 | validatePath: azureADAuthConfigPath, |
| 96 | wantErrString: "cloud_auth.azure_ad.mode is required", |
| 97 | }, |
| 98 | } |
| 99 | |
| 100 | for name, tc := range tests { |
| 101 | t.Run(name, func(t *testing.T) { |
| 102 | err := tc.cfg.ValidateWithPath(tc.validatePath) |
| 103 | require.Error(t, err) |
| 104 | assert.ErrorContains(t, err, tc.wantErrString) |
| 105 | }) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestAzureADAuthConfigNewCredentialWithOptions(t *testing.T) { |
| 110 | tests := map[string]struct { |
| 111 | cfg AzureADAuthConfig |
| 112 | wantErr bool |
| 113 | wantErrString string |
| 114 | }{ |
| 115 | "valid default": { |
| 116 | cfg: AzureADAuthConfig{ |
| 117 | Mode: AzureADAuthModeDefault, |
| 118 | }, |
| 119 | }, |
| 120 | "invalid service principal": { |
| 121 | cfg: AzureADAuthConfig{ |
| 122 | Mode: AzureADAuthModeServicePrincipal, |
| 123 | ModeServicePrincipal: &AzureADModeServicePrincipalConfig{ |
| 124 | TenantID: "tenant", |
| 125 | ClientID: "client", |
| 126 | }, |
| 127 | }, |
| 128 | wantErr: true, |
| 129 | wantErrString: "cloud_auth.azure_ad.mode_service_principal.client_secret is required", |
| 130 | }, |
| 131 | } |
| 132 | |
| 133 | for name, tc := range tests { |
| 134 | t.Run(name, func(t *testing.T) { |
| 135 | cred, err := tc.cfg.NewCredentialWithOptions(nil) |
| 136 | if tc.wantErr { |
| 137 | require.Error(t, err) |
| 138 | assert.Nil(t, cred) |
| 139 | assert.ErrorContains(t, err, tc.wantErrString) |
| 140 | return |
| 141 | } |
| 142 | |
| 143 | require.NoError(t, err) |
| 144 | require.NotNil(t, cred) |
| 145 | }) |
| 146 | } |
| 147 | } |