-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SNOW-1825471: PAT authentication support (#1995)
- Loading branch information
1 parent
e8dc943
commit a45009a
Showing
17 changed files
with
261 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/test/java/net/snowflake/client/core/ProgrammaticAccessTokenAuthFlowLatestIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2024 Snowflake Computing Inc. All rights reserved. | ||
*/ | ||
|
||
package net.snowflake.client.core; | ||
|
||
import java.util.HashMap; | ||
import net.snowflake.client.category.TestTags; | ||
import net.snowflake.client.core.auth.AuthenticatorType; | ||
import net.snowflake.client.jdbc.BaseWiremockTest; | ||
import net.snowflake.client.jdbc.SnowflakeSQLException; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@Tag(TestTags.CORE) | ||
public class ProgrammaticAccessTokenAuthFlowLatestIT extends BaseWiremockTest { | ||
|
||
private static final String SCENARIOS_BASE_DIR = MAPPINGS_BASE_DIR + "/pat"; | ||
private static final String SUCCESSFUL_FLOW_SCENARIO_MAPPINGS = | ||
SCENARIOS_BASE_DIR + "/successful_scenario_mapping.json"; | ||
private static final String INVALID_TOKEN_SCENARIO_MAPPINGS = | ||
SCENARIOS_BASE_DIR + "/invalid_token_scenario_mapping.json"; | ||
|
||
@Test | ||
public void successfulFlowScenarioPatAsToken() throws SFException, SnowflakeSQLException { | ||
importMappingFromResources(SUCCESSFUL_FLOW_SCENARIO_MAPPINGS); | ||
SFLoginInput loginInputWithPatAsToken = createLoginInputStub("MOCK_TOKEN", null); | ||
SFLoginOutput loginOutput = | ||
SessionUtil.newSession(loginInputWithPatAsToken, new HashMap<>(), "INFO"); | ||
assertSuccessfulLoginOutput(loginOutput); | ||
} | ||
|
||
@Test | ||
public void successfulFlowScenarioPatAsPassword() throws SFException, SnowflakeSQLException { | ||
importMappingFromResources(SUCCESSFUL_FLOW_SCENARIO_MAPPINGS); | ||
SFLoginInput loginInputWithPatAsPassword = createLoginInputStub(null, "MOCK_TOKEN"); | ||
SFLoginOutput loginOutput = | ||
SessionUtil.newSession(loginInputWithPatAsPassword, new HashMap<>(), "INFO"); | ||
assertSuccessfulLoginOutput(loginOutput); | ||
} | ||
|
||
@Test | ||
public void invalidTokenScenario() { | ||
importMappingFromResources(INVALID_TOKEN_SCENARIO_MAPPINGS); | ||
SnowflakeSQLException e = | ||
Assertions.assertThrows( | ||
SnowflakeSQLException.class, | ||
() -> | ||
SessionUtil.newSession( | ||
createLoginInputStub("MOCK_TOKEN", null), new HashMap<>(), "INFO")); | ||
Assertions.assertEquals("Programmatic access token is invalid.", e.getMessage()); | ||
} | ||
|
||
private void assertSuccessfulLoginOutput(SFLoginOutput loginOutput) { | ||
Assertions.assertNotNull(loginOutput); | ||
Assertions.assertEquals("session token", loginOutput.getSessionToken()); | ||
Assertions.assertEquals("master token", loginOutput.getMasterToken()); | ||
Assertions.assertEquals(14400, loginOutput.getMasterTokenValidityInSeconds()); | ||
Assertions.assertEquals("8.48.0", loginOutput.getDatabaseVersion()); | ||
Assertions.assertEquals("TEST_DHEYMAN", loginOutput.getSessionDatabase()); | ||
Assertions.assertEquals("TEST_JDBC", loginOutput.getSessionSchema()); | ||
Assertions.assertEquals("ANALYST", loginOutput.getSessionRole()); | ||
Assertions.assertEquals("TEST_XSMALL", loginOutput.getSessionWarehouse()); | ||
Assertions.assertEquals("1172562260498", loginOutput.getSessionId()); | ||
Assertions.assertEquals(1, loginOutput.getCommonParams().size()); | ||
Assertions.assertEquals(4, loginOutput.getCommonParams().get("CLIENT_PREFETCH_THREADS")); | ||
} | ||
|
||
private SFLoginInput createLoginInputStub(String token, String password) { | ||
SFLoginInput input = new SFLoginInput(); | ||
input.setAuthenticator(AuthenticatorType.PROGRAMMATIC_ACCESS_TOKEN.name()); | ||
input.setServerUrl(String.format("http://%s:%d/", WIREMOCK_HOST, wiremockHttpPort)); | ||
input.setUserName("MOCK_USERNAME"); | ||
input.setAccountName("MOCK_ACCOUNT_NAME"); | ||
input.setAppId("MOCK_APP_ID"); | ||
input.setAppVersion("MOCK_APP_VERSION"); | ||
input.setToken(token); | ||
input.setPassword(password); | ||
input.setOCSPMode(OCSPMode.FAIL_OPEN); | ||
input.setHttpClientSettingsKey(new HttpClientSettingsKey(OCSPMode.FAIL_OPEN)); | ||
input.setLoginTimeout(1000); | ||
input.setSessionParameters(new HashMap<>()); | ||
return input; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
60 changes: 60 additions & 0 deletions
60
src/test/resources/wiremock/mappings/pat/invalid_token_scenario_mapping.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ | ||
"mappings": [ | ||
{ | ||
"scenarioName": "Successful PAT authentication flow", | ||
"requiredScenarioState": "Started", | ||
"newScenarioState": "Authenticated", | ||
"request": { | ||
"urlPathPattern": "/session/v1/login-request.*", | ||
"method": "POST", | ||
"headers": { | ||
"CLIENT_APP_ID": { | ||
"equalTo": "MOCK_APP_ID" | ||
}, | ||
"CLIENT_APP_VERSION": { | ||
"equalTo": "MOCK_APP_VERSION" | ||
}, | ||
"Authorization": { | ||
"equalTo": "Basic" | ||
}, | ||
"accept": { | ||
"equalTo": "application/json" | ||
} | ||
}, | ||
"bodyPatterns": [ | ||
{ | ||
"equalToJson" : { | ||
"data": { | ||
"ACCOUNT_NAME": "MOCK_ACCOUNT_NAME", | ||
"CLIENT_APP_ID": "MOCK_APP_ID", | ||
"CLIENT_ENVIRONMENT": { | ||
"tracing": "INFO", | ||
"OCSP_MODE": "FAIL_OPEN" | ||
}, | ||
"CLIENT_APP_VERSION": "MOCK_APP_VERSION", | ||
"TOKEN": "MOCK_TOKEN", | ||
"LOGIN_NAME": "MOCK_USERNAME", | ||
"AUTHENTICATOR": "PROGRAMMATIC_ACCESS_TOKEN" | ||
} | ||
}, | ||
"ignoreExtraElements" : true | ||
} | ||
] | ||
}, | ||
"response": { | ||
"status": 200, | ||
"jsonBody": { | ||
"data": { | ||
"nextAction": "RETRY_LOGIN", | ||
"authnMethod": "PAT", | ||
"signInOptions": {} | ||
}, | ||
"code": "394400", | ||
"message": "Programmatic access token is invalid.", | ||
"success": false, | ||
"headers": null | ||
} | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.