-
Notifications
You must be signed in to change notification settings - Fork 40
Add DynamoDB permission test #2847
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2c5e6e5
Add DynamoDB permission test
KodaiD ae6b8a3
Add pull_request trigger to test workflow
KodaiD 06cc84e
Apply suggestions
KodaiD 04e6dcf
Fix workflow to specify arguments
KodaiD 4a542bf
[skip ci] Fix workflow file
KodaiD c884e44
[skip ci] Update error log message
KodaiD 5cd1285
[skip ci] Fix error handling
KodaiD 28cc919
[skip ci] Fix workflow file
KodaiD 0f74072
Apply suggestions
KodaiD 1184f55
Merge branch 'master' into dynamodb-permission-list
KodaiD 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 hidden or 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 hidden or 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
62 changes: 62 additions & 0 deletions
62
...egration-test/java/com/scalar/db/storage/dynamo/DynamoAdminPermissionIntegrationTest.java
This file contains hidden or 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,62 @@ | ||
| package com.scalar.db.storage.dynamo; | ||
|
|
||
| import static com.scalar.db.storage.dynamo.DynamoPermissionTestUtils.SLEEP_BETWEEN_TESTS_SECONDS; | ||
|
|
||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.util.concurrent.Uninterruptibles; | ||
| import com.scalar.db.api.DistributedStorageAdminPermissionIntegrationTestBase; | ||
| import com.scalar.db.util.AdminTestUtils; | ||
| import com.scalar.db.util.PermissionTestUtils; | ||
| import java.util.Map; | ||
| import java.util.Properties; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.junit.jupiter.api.Disabled; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class DynamoAdminPermissionIntegrationTest | ||
| extends DistributedStorageAdminPermissionIntegrationTestBase { | ||
| @Override | ||
| protected Properties getProperties(String testName) { | ||
| return DynamoEnv.getProperties(testName); | ||
| } | ||
|
|
||
| @Override | ||
| protected Properties getPropertiesForNormalUser(String testName) { | ||
| return DynamoEnv.getProperties(testName); | ||
| } | ||
|
|
||
| @Override | ||
| protected Map<String, String> getCreationOptions() { | ||
| return ImmutableMap.of(DynamoAdmin.NO_SCALING, "false", DynamoAdmin.NO_BACKUP, "false"); | ||
| } | ||
|
|
||
| @Override | ||
| protected AdminTestUtils getAdminTestUtils(String testName) { | ||
| return new DynamoAdminTestUtils(getProperties(testName)); | ||
| } | ||
|
|
||
| @Override | ||
| protected PermissionTestUtils getPermissionTestUtils(String testName) { | ||
| return new DynamoPermissionTestUtils(getProperties(testName)); | ||
| } | ||
|
|
||
| @Override | ||
| protected void sleepBetweenTests() { | ||
| Uninterruptibles.sleepUninterruptibly(SLEEP_BETWEEN_TESTS_SECONDS, TimeUnit.SECONDS); | ||
| } | ||
KodaiD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Test | ||
| @Override | ||
| @Disabled("Import-related functionality is not supported in DynamoDB") | ||
| public void getImportTableMetadata_WithSufficientPermission_ShouldSucceed() {} | ||
|
|
||
| @Test | ||
| @Override | ||
| @Disabled("Import-related functionality is not supported in DynamoDB") | ||
| public void addRawColumnToTable_WithSufficientPermission_ShouldSucceed() {} | ||
|
|
||
| @Test | ||
| @Override | ||
| @Disabled("Import-related functionality is not supported in DynamoDB") | ||
| public void importTable_WithSufficientPermission_ShouldSucceed() {} | ||
| } | ||
This file contains hidden or 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
36 changes: 36 additions & 0 deletions
36
...c/integration-test/java/com/scalar/db/storage/dynamo/DynamoPermissionIntegrationTest.java
This file contains hidden or 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,36 @@ | ||
| package com.scalar.db.storage.dynamo; | ||
|
|
||
| import com.google.common.collect.ImmutableMap; | ||
| import com.scalar.db.api.DistributedStoragePermissionIntegrationTestBase; | ||
| import com.scalar.db.util.AdminTestUtils; | ||
| import com.scalar.db.util.PermissionTestUtils; | ||
| import java.util.Map; | ||
| import java.util.Properties; | ||
|
|
||
| public class DynamoPermissionIntegrationTest | ||
| extends DistributedStoragePermissionIntegrationTestBase { | ||
| @Override | ||
| protected Properties getProperties(String testName) { | ||
| return DynamoEnv.getProperties(testName); | ||
| } | ||
|
|
||
| @Override | ||
| protected Properties getPropertiesForNormalUser(String testName) { | ||
| return DynamoEnv.getProperties(testName); | ||
| } | ||
|
|
||
| @Override | ||
| protected Map<String, String> getCreationOptions() { | ||
| return ImmutableMap.of(DynamoAdmin.NO_SCALING, "false", DynamoAdmin.NO_BACKUP, "false"); | ||
KodaiD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| protected PermissionTestUtils getPermissionTestUtils(String testName) { | ||
| return new DynamoPermissionTestUtils(getProperties(testName)); | ||
| } | ||
|
|
||
| @Override | ||
| protected AdminTestUtils getAdminTestUtils(String testName) { | ||
| return new DynamoAdminTestUtils(getProperties(testName)); | ||
| } | ||
| } | ||
146 changes: 146 additions & 0 deletions
146
core/src/integration-test/java/com/scalar/db/storage/dynamo/DynamoPermissionTestUtils.java
This file contains hidden or 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,146 @@ | ||
| package com.scalar.db.storage.dynamo; | ||
|
|
||
| import com.scalar.db.config.DatabaseConfig; | ||
| import com.scalar.db.util.PermissionTestUtils; | ||
| import java.util.Optional; | ||
| import java.util.Properties; | ||
| import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
| import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
| import software.amazon.awssdk.policybuilder.iam.IamEffect; | ||
| import software.amazon.awssdk.policybuilder.iam.IamPolicy; | ||
| import software.amazon.awssdk.policybuilder.iam.IamResource; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.iam.IamClient; | ||
| import software.amazon.awssdk.services.iam.model.AttachUserPolicyRequest; | ||
| import software.amazon.awssdk.services.iam.model.AttachedPolicy; | ||
| import software.amazon.awssdk.services.iam.model.CreatePolicyRequest; | ||
| import software.amazon.awssdk.services.iam.model.CreatePolicyVersionRequest; | ||
| import software.amazon.awssdk.services.iam.model.DeletePolicyVersionRequest; | ||
| import software.amazon.awssdk.services.iam.model.ListAttachedUserPoliciesRequest; | ||
| import software.amazon.awssdk.services.iam.model.ListPolicyVersionsRequest; | ||
| import software.amazon.awssdk.services.iam.model.User; | ||
|
|
||
| public class DynamoPermissionTestUtils implements PermissionTestUtils { | ||
| public static final int SLEEP_BETWEEN_TESTS_SECONDS = 10; | ||
| private static final String IAM_POLICY_NAME = "test-dynamodb-permissions"; | ||
| private static final IamPolicy POLICY = | ||
| IamPolicy.builder() | ||
| .addStatement( | ||
| s -> | ||
| s.effect(IamEffect.ALLOW) | ||
| .addAction("dynamodb:ConditionCheckItem") | ||
| .addAction("dynamodb:PutItem") | ||
| .addAction("dynamodb:ListTables") | ||
| .addAction("dynamodb:DeleteItem") | ||
| .addAction("dynamodb:Scan") | ||
| .addAction("dynamodb:Query") | ||
| .addAction("dynamodb:UpdateItem") | ||
| .addAction("dynamodb:DeleteTable") | ||
| .addAction("dynamodb:UpdateContinuousBackups") | ||
| .addAction("dynamodb:CreateTable") | ||
| .addAction("dynamodb:DescribeTable") | ||
| .addAction("dynamodb:GetItem") | ||
| .addAction("dynamodb:DescribeContinuousBackups") | ||
| .addAction("dynamodb:UpdateTable") | ||
| .addAction("application-autoscaling:RegisterScalableTarget") | ||
| .addAction("application-autoscaling:DeleteScalingPolicy") | ||
| .addAction("application-autoscaling:PutScalingPolicy") | ||
| .addAction("application-autoscaling:DeregisterScalableTarget") | ||
| .addAction("application-autoscaling:TagResource") | ||
| .addResource(IamResource.ALL)) | ||
| .build(); | ||
| private final IamClient client; | ||
|
|
||
| public DynamoPermissionTestUtils(Properties properties) { | ||
| DynamoConfig config = new DynamoConfig(new DatabaseConfig(properties)); | ||
| this.client = | ||
| IamClient.builder() | ||
| .region(Region.of(config.getRegion())) | ||
| .credentialsProvider( | ||
| StaticCredentialsProvider.create( | ||
| AwsBasicCredentials.create( | ||
| config.getAccessKeyId(), config.getSecretAccessKey()))) | ||
| .build(); | ||
| } | ||
|
|
||
| @Override | ||
| public void createNormalUser(String userName, String password) { | ||
| // Do nothing for DynamoDB. | ||
| } | ||
|
|
||
| @Override | ||
| public void dropNormalUser(String userName) { | ||
| // Do nothing for DynamoDB. | ||
| } | ||
KodaiD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public void grantRequiredPermission(String userName) { | ||
| try { | ||
| User user = client.getUser().user(); | ||
| Optional<String> attachedPolicyArn = getAttachedPolicyArn(user.userName()); | ||
| if (attachedPolicyArn.isPresent()) { | ||
| deleteStalePolicyVersions(attachedPolicyArn.get()); | ||
KodaiD marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| createNewPolicyVersion(attachedPolicyArn.get()); | ||
| } else { | ||
| String policyArn = createNewPolicy(); | ||
| client.attachUserPolicy( | ||
| AttachUserPolicyRequest.builder() | ||
| .userName(user.userName()) | ||
| .policyArn(policyArn) | ||
| .build()); | ||
| } | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to grant required permissions", e); | ||
KodaiD marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
KodaiD marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| client.close(); | ||
| } | ||
|
|
||
| private Optional<String> getAttachedPolicyArn(String userName) { | ||
| AttachedPolicy attachedPolicy = | ||
| client | ||
| .listAttachedUserPolicies( | ||
| ListAttachedUserPoliciesRequest.builder().userName(userName).build()) | ||
| .attachedPolicies().stream() | ||
| .filter(policy -> policy.policyName().equals(DynamoPermissionTestUtils.IAM_POLICY_NAME)) | ||
| .findFirst() | ||
| .orElse(null); | ||
| return Optional.ofNullable(attachedPolicy).map(AttachedPolicy::policyArn); | ||
| } | ||
|
|
||
| private String createNewPolicy() { | ||
| return client | ||
| .createPolicy( | ||
| CreatePolicyRequest.builder() | ||
| .policyName(IAM_POLICY_NAME) | ||
| .policyDocument(POLICY.toJson()) | ||
| .build()) | ||
| .policy() | ||
| .arn(); | ||
| } | ||
|
|
||
| private void deleteStalePolicyVersions(String policyArn) { | ||
| client.listPolicyVersions(ListPolicyVersionsRequest.builder().policyArn(policyArn).build()) | ||
| .versions().stream() | ||
| .filter(version -> !version.isDefaultVersion()) | ||
| .forEach( | ||
| version -> | ||
| client.deletePolicyVersion( | ||
| DeletePolicyVersionRequest.builder() | ||
| .policyArn(policyArn) | ||
| .versionId(version.versionId()) | ||
| .build())); | ||
| } | ||
|
|
||
| private void createNewPolicyVersion(String policyArn) { | ||
| client.createPolicyVersion( | ||
| CreatePolicyVersionRequest.builder() | ||
| .policyArn(policyArn) | ||
| .policyDocument(POLICY.toJson()) | ||
| .setAsDefault(true) | ||
| .build()); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.