|
9 | 9 | "crypto/rsa" |
10 | 10 | "crypto/x509" |
11 | 11 | "encoding/pem" |
| 12 | + "github.com/snowflakedb/gosnowflake" |
12 | 13 | "testing" |
13 | 14 |
|
14 | 15 | "github.com/stretchr/testify/require" |
@@ -41,65 +42,86 @@ func TestOpenSnowflake(t *testing.T) { |
41 | 42 | require.NotNil(t, db.Stats()) |
42 | 43 | } |
43 | 44 |
|
44 | | -// TestParseSnowflakeFieldsFromURL validates that URL |
45 | | -// parsing for keypair authentication works as expected |
46 | | -func TestParseSnowflakeFieldsFromURL(t *testing.T) { |
47 | | - tests := map[string]struct { |
48 | | - connectionURL string |
49 | | - wantAccount string |
50 | | - wantDB string |
51 | | - wantErr error |
| 45 | +func TestGetSnowflakeConfig(t *testing.T) { |
| 46 | + tt := map[string]struct { |
| 47 | + providedPrivateKey string |
| 48 | + username string |
| 49 | + connectionURL string |
| 50 | + expectedConfig *gosnowflake.Config |
| 51 | + expectedError string |
52 | 52 | }{ |
53 | | - "valid URL": { |
54 | | - connectionURL: "account.snowflakecomputing.com/db", |
55 | | - wantAccount: "account", |
56 | | - wantDB: "db", |
57 | | - wantErr: nil, |
58 | | - }, |
59 | | - "complex URL": { |
60 | | - connectionURL: "dev.org_v2.1.5-us-eas2-1.snowflakecomputing.com/secret-db.name/withslash", |
61 | | - wantAccount: "dev.org_v2.1.5-us-eas2-1", |
62 | | - wantDB: "secret-db.name/withslash", |
63 | | - wantErr: nil, |
64 | | - }, |
65 | | - "invalid URL": { |
66 | | - connectionURL: "invalid-url", |
67 | | - wantAccount: "", |
68 | | - wantDB: "", |
69 | | - wantErr: ErrInvalidSnowflakeURL, |
70 | | - }, |
71 | | - "missing account name": { |
72 | | - connectionURL: ".snowflakecomputing.com/db", |
73 | | - wantAccount: "", |
74 | | - wantDB: "", |
75 | | - wantErr: ErrInvalidSnowflakeURL, |
76 | | - }, |
77 | | - "missing database name": { |
78 | | - connectionURL: "account.snowflakecomputing.com/", |
79 | | - wantAccount: "", |
80 | | - wantDB: "", |
81 | | - wantErr: ErrInvalidSnowflakeURL, |
| 53 | + // confirms that the connection URL format upon initial release is correctly parsed |
| 54 | + "key pair connection URL format without params": { |
| 55 | + providedPrivateKey: testPrivateKey, |
| 56 | + username: "testvaultuser", |
| 57 | + connectionURL: "testaccount.snowflakecomputing.com/testdb", |
| 58 | + expectedConfig: &gosnowflake.Config{ |
| 59 | + Account: "testaccount", |
| 60 | + User: "testvaultuser", |
| 61 | + Database: "testdb", |
| 62 | + PrivateKey: func() *rsa.PrivateKey { |
| 63 | + key, _ := getPrivateKey([]byte(testPrivateKey)) |
| 64 | + return key |
| 65 | + }(), |
| 66 | + Authenticator: gosnowflake.AuthTypeJwt, |
| 67 | + }, |
82 | 68 | }, |
83 | | - "missing domain": { |
84 | | - connectionURL: "account..com/db", |
85 | | - wantAccount: "", |
86 | | - wantDB: "", |
87 | | - wantErr: ErrInvalidSnowflakeURL, |
| 69 | + // confirms that query params in the connection URL are correctly parsed |
| 70 | + "key pair connection URL format with query params": { |
| 71 | + providedPrivateKey: testPrivateKey, |
| 72 | + username: "testvaultuser", |
| 73 | + connectionURL: "testaccount.snowflakecomputing.com/testdb?disableOCSPChecks=true&maxRetryCount=5", |
| 74 | + expectedConfig: &gosnowflake.Config{ |
| 75 | + Account: "testaccount", |
| 76 | + User: "testvaultuser", |
| 77 | + Database: "testdb", |
| 78 | + PrivateKey: func() *rsa.PrivateKey { |
| 79 | + key, _ := getPrivateKey([]byte(testPrivateKey)) |
| 80 | + return key |
| 81 | + }(), |
| 82 | + DisableOCSPChecks: true, |
| 83 | + MaxRetryCount: 5, |
| 84 | + Authenticator: gosnowflake.AuthTypeJwt, |
| 85 | + }, |
88 | 86 | }, |
89 | | - "escape dots": { |
90 | | - connectionURL: "account.snowflakecomputingXcom/db", |
91 | | - wantAccount: "", |
92 | | - wantDB: "", |
93 | | - wantErr: ErrInvalidSnowflakeURL, |
| 87 | + // confirms that DB is optional in the connection URL |
| 88 | + "key pair connection URL without DB": { |
| 89 | + providedPrivateKey: testPrivateKey, |
| 90 | + username: "testvaultuser", |
| 91 | + connectionURL: "testaccount.snowflakecomputing.com?disableOCSPChecks=true&maxRetryCount=5", |
| 92 | + expectedConfig: &gosnowflake.Config{ |
| 93 | + Account: "testaccount", |
| 94 | + User: "testvaultuser", |
| 95 | + PrivateKey: func() *rsa.PrivateKey { |
| 96 | + key, _ := getPrivateKey([]byte(testPrivateKey)) |
| 97 | + return key |
| 98 | + }(), |
| 99 | + DisableOCSPChecks: true, |
| 100 | + MaxRetryCount: 5, |
| 101 | + Authenticator: gosnowflake.AuthTypeJwt, |
| 102 | + }, |
94 | 103 | }, |
95 | 104 | } |
96 | | - for name, tt := range tests { |
97 | | - t.Run(name, func(t *testing.T) { |
98 | | - user, db, err := parseSnowflakeFieldsFromURL(tt.connectionURL) |
99 | 105 |
|
100 | | - require.Equal(t, tt.wantAccount, user) |
101 | | - require.Equal(t, tt.wantDB, db) |
102 | | - require.Equal(t, tt.wantErr, err) |
| 106 | + for name, tc := range tt { |
| 107 | + t.Run(name, func(t *testing.T) { |
| 108 | + cfg, err := getSnowflakeConfig(tc.connectionURL, tc.username, []byte(tc.providedPrivateKey)) |
| 109 | + if tc.expectedError != "" { |
| 110 | + require.Error(t, err) |
| 111 | + require.Contains(t, err.Error(), tc.expectedError) |
| 112 | + return |
| 113 | + } |
| 114 | + require.NoError(t, err) |
| 115 | + require.NotNil(t, cfg) |
| 116 | + // Compare all relevant fields for this test |
| 117 | + // this confirms that the config was correctly parsed from the provided inputs |
| 118 | + require.Equal(t, tc.expectedConfig.Account, cfg.Account) |
| 119 | + require.Equal(t, tc.expectedConfig.User, cfg.User) |
| 120 | + require.Equal(t, tc.expectedConfig.Database, cfg.Database) |
| 121 | + require.Equal(t, tc.expectedConfig.Authenticator, cfg.Authenticator) |
| 122 | + require.Equal(t, tc.expectedConfig.DisableOCSPChecks, cfg.DisableOCSPChecks) |
| 123 | + require.Equal(t, tc.expectedConfig.KeepSessionAlive, cfg.KeepSessionAlive) |
| 124 | + require.NotNil(t, cfg.PrivateKey) |
103 | 125 | }) |
104 | 126 | } |
105 | 127 | } |
|
0 commit comments