-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SNOW-1825471: PAT authentication support #1995
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it fine to reduce the visibility here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can reduce on internal methods