Skip to content
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

Allow update of AWS roleArn associated with catalog when the AWS account IDs are same #587

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -918,14 +918,14 @@ public void testCreateListUpdateAndDeleteCatalog() {
}

// Reject update of fields that can't be currently updated
StorageConfigInfo modifiedStorageConfig =
StorageConfigInfo invalidModifiedStorageConfig =
new AwsStorageConfigInfo(
"arn:aws:iam::123456789011:role/newrole", StorageConfigInfo.StorageTypeEnum.S3);
"arn:aws:iam::123456789012:role/newrole", StorageConfigInfo.StorageTypeEnum.S3);
UpdateCatalogRequest badUpdateRequest =
new UpdateCatalogRequest(
fetchedCatalog.getEntityVersion(),
Map.of("default-base-location", "s3://newbucket/"),
modifiedStorageConfig);
invalidModifiedStorageConfig);
try (Response response =
newRequest("http://localhost:%d/api/management/v1/catalogs/mycatalog")
.put(Entity.json(badUpdateRequest))) {
Expand All @@ -939,11 +939,17 @@ public void testCreateListUpdateAndDeleteCatalog() {
.startsWith("Cannot modify");
}

// Allow update of fields that are supported (e.g. new default-base-location and role ARN when
// AWS
// account IDs are same)
StorageConfigInfo validModifiedStorageConfig =
new AwsStorageConfigInfo(
"arn:aws:iam::123456789011:role/newrole", StorageConfigInfo.StorageTypeEnum.S3);
UpdateCatalogRequest updateRequest =
new UpdateCatalogRequest(
fetchedCatalog.getEntityVersion(),
Map.of("default-base-location", "s3://newbucket/"),
storageConfig);
validModifiedStorageConfig);

// 200 successful update
try (Response response =
Expand All @@ -954,6 +960,9 @@ public void testCreateListUpdateAndDeleteCatalog() {

assertThat(fetchedCatalog.getProperties().toMap())
.isEqualTo(Map.of("default-base-location", "s3://newbucket/"));
assertThat(fetchedCatalog.getStorageConfigInfo())
.isInstanceOf(AwsStorageConfigInfo.class)
.hasFieldOrPropertyWithValue("roleArn", "arn:aws:iam::123456789011:role/newrole");
}

// 200 GET after update should show new properties
Expand Down
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 @@ -81,7 +82,7 @@ public String getFileIoImplClassName() {
return "org.apache.iceberg.aws.s3.S3FileIO";
}

public void validateArn(String arn) {
public static void validateArn(String arn) {
if (arn == null || arn.isEmpty()) {
throw new IllegalArgumentException("ARN cannot be null or empty");
}
Expand Down Expand Up @@ -122,6 +123,22 @@ public void setRegion(@Nullable String region) {
this.region = region;
}

@JsonIgnore
public String getAwsAccountId() {
MonkeyCanCode marked this conversation as resolved.
Show resolved Hide resolved
return parseAwsAccountId(roleARN);
}

private static 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
Loading