Skip to content

Commit

Permalink
Fix 582
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkeyCanCode committed Dec 21, 2024
1 parent 9089989 commit 5c3eeea
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.polaris.core.storage.PolarisStorageConfigurationInfo;

Expand All @@ -35,8 +36,8 @@ public class AwsStorageConfigurationInfo extends PolarisStorageConfigurationInfo
// for allowed read and write locations for subscoping creds.
@JsonIgnore private static final int MAX_ALLOWED_LOCATIONS = 5;

// Technically, it should be ^arn:(aws|aws-cn|aws-us-gov):iam::\d{12}:role/.+$,
@JsonIgnore public static final String ROLE_ARN_PATTERN = "^arn:aws:iam::\\d{12}:role/.+$";
// Technically, it should be ^arn:(aws|aws-cn|aws-us-gov):iam::(\d{12}):role/.+$,
@JsonIgnore public static final String ROLE_ARN_PATTERN = "^arn:aws:iam::(\\d{12}):role/.+$";

// AWS role to be assumed
private final @Nonnull String roleARN;
Expand Down Expand Up @@ -122,6 +123,21 @@ public void setRegion(@Nullable String region) {
this.region = region;
}

public String getAwsAccountId() {
return parseAwsAccountId(roleARN);
}

private String parseAwsAccountId(String arn) {
validateArn(arn);
Pattern pattern = Pattern.compile(ROLE_ARN_PATTERN);
Matcher matcher = pattern.matcher(arn);
if (matcher.matches()) {
return matcher.group(1);
} else {
throw new IllegalArgumentException("ARN does not match the expected role ARN pattern");
}
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ public void testValidateAccessToLocations() {
new PolarisStorageIntegration.ValidationResult(false, "")));
}

@Test
public void testAwsAccountIdParsing() {
AwsStorageConfigurationInfo awsConfig =
new AwsStorageConfigurationInfo(
PolarisStorageConfigurationInfo.StorageType.S3,
List.of("s3://bucket/path/to/warehouse"),
"arn:aws:iam::012345678901:role/jdoe",
"us-east-2");

String expectedAccountId = "012345678901";
String actualAccountId = awsConfig.getAwsAccountId();

Assertions.assertThat(actualAccountId).isEqualTo(expectedAccountId);
}

@Test
public void testValidateAccessToLocationsWithWildcard() {
MockInMemoryStorageIntegration storage = new MockInMemoryStorageIntegration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,7 @@ private void validateUpdateCatalogDiffOrThrow(
if (currentStorageConfig instanceof AwsStorageConfigurationInfo currentAwsConfig
&& newStorageConfig instanceof AwsStorageConfigurationInfo newAwsConfig) {

if (!currentAwsConfig.getRoleARN().equals(newAwsConfig.getRoleARN())
|| !newAwsConfig.getRoleARN().equals(currentAwsConfig.getRoleARN())) {
if (!currentAwsConfig.getAwsAccountId().equals(newAwsConfig.getAwsAccountId())) {
throw new BadRequestException(
"Cannot modify Role ARN in storage config from %s to %s",
currentStorageConfig, newStorageConfig);
Expand Down

0 comments on commit 5c3eeea

Please sign in to comment.