Skip to content

Commit

Permalink
SNOW-1689931 Adding flag to skip token file permission verification
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pmotacki committed Nov 25, 2024
1 parent 47e67b7 commit a3834f3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static ConnectionParameters buildConnectionParameters() throws SnowflakeS
if (!token.isEmpty()) {
putPropertyIfNotNull(connectionProperties, "token", token.trim());
} else {
throw new SnowflakeSQLException("Token must be set when the authenticator type is OAUTH");
throw new SnowflakeSQLException("Non-empty token must be set when the authenticator type is OAUTH");
}
} catch (Exception ex) {
throw new SnowflakeSQLException(ex, "There is a problem during reading token from file");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public void setUp() throws IOException {
tempPath = Files.createTempDirectory(".snowflake");
ENV_VARIABLES_KEYS
.stream()
.forEach(key -> envVariables.put(key, SnowflakeUtil.systemGetEnv(key)));
.forEach(key -> {
if (SnowflakeUtil.systemGetEnv(key) != null) {
envVariables.put(key, SnowflakeUtil.systemGetEnv(key));
}
});
}

@After
Expand All @@ -58,9 +62,8 @@ public void close() throws IOException {
SnowflakeUtil.systemUnsetEnv(SKIP_TOKEN_FILE_PERMISSIONS_VERIFICATION);
Files.walk(tempPath).map(Path::toFile).forEach(File::delete);
Files.delete(tempPath);
ENV_VARIABLES_KEYS
.stream()
.forEach(key -> SnowflakeUtil.systemSetEnv(key, envVariables.get(key)));
envVariables
.forEach((key, value) -> SnowflakeUtil.systemSetEnv(key, value));
}

@Test
Expand Down Expand Up @@ -162,6 +165,19 @@ public void shouldThrowExceptionIfNoneOfHostAndAccountIsSet() throws IOException
Assert.assertThrows(
SnowflakeSQLException.class, () -> SFConnectionConfigParser.buildConnectionParameters());
}
@Test
public void shouldThrowExceptionIfTokenIsNotSetForOauth() throws IOException {
SnowflakeUtil.systemSetEnv(SNOWFLAKE_HOME_KEY, tempPath.toString());
SnowflakeUtil.systemSetEnv(SNOWFLAKE_DEFAULT_CONNECTION_NAME_KEY, "default");
SnowflakeUtil.systemSetEnv(SKIP_TOKEN_FILE_PERMISSIONS_VERIFICATION, "true");
File tokenFile = new File(Paths.get(tempPath.toString(), "token").toUri());
// File emptyTokenFile = new File(Paths.get(tempPath.toString(), "emptyToken").toUri());
prepareConnectionConfigurationTomlFile(
Collections.singletonMap("token_file_path", tokenFile.toString()), true, false, "");

Assert.assertThrows(
SnowflakeSQLException.class, () -> SFConnectionConfigParser.buildConnectionParameters());
}

private void prepareConnectionConfigurationTomlFile() throws IOException {
prepareConnectionConfigurationTomlFile(null, true, true);
Expand All @@ -172,7 +188,13 @@ private void prepareConnectionConfigurationTomlFile(Map moreParameters) throws I
}

private void prepareConnectionConfigurationTomlFile(
Map moreParameters, boolean onlyUserPermissionConnection, boolean onlyUserPermissionToken)
Map moreParameters, boolean onlyUserPermissionConnection, boolean onlyUserPermissionToken)
throws IOException {
prepareConnectionConfigurationTomlFile(moreParameters, onlyUserPermissionConnection, onlyUserPermissionToken, "token_from_file");
}

private void prepareConnectionConfigurationTomlFile(
Map moreParameters, boolean onlyUserPermissionConnection, boolean onlyUserPermissionToken, String token)
throws IOException {
Path path = Paths.get(tempPath.toString(), "connections.toml");
Path filePath = createFilePathWithPermission(path, onlyUserPermissionConnection);
Expand All @@ -196,7 +218,12 @@ private void prepareConnectionConfigurationTomlFile(
createFilePathWithPermission(
Paths.get(configurationParams.get("token_file_path").toString()),
onlyUserPermissionToken);
Files.write(tokenFilePath, "token_from_file".getBytes());
Files.write(tokenFilePath, token.getBytes());
Path emptyTokenFilePath =
createFilePathWithPermission(
Paths.get(configurationParams.get("token_file_path").toString().replaceAll("token", "emptytoken")),
onlyUserPermissionToken);
Files.write(emptyTokenFilePath, "".getBytes());
}
}

Expand Down

0 comments on commit a3834f3

Please sign in to comment.