Skip to content

Commit 52db096

Browse files
committed
Update grant method in Dynamo
1 parent f49de8f commit 52db096

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

core/src/integration-test/java/com/scalar/db/storage/cassandra/CassandraEnv.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static Properties getProperties(String testName) {
3939

4040
return properties;
4141
}
42-
42+
4343
public static Properties getPropertiesForNormalUser(String testName) {
4444
String contactPoints =
4545
System.getProperty(PROP_CASSANDRA_CONTACT_POINTS, DEFAULT_CASSANDRA_CONTACT_POINTS);

core/src/integration-test/java/com/scalar/db/storage/dynamo/DynamoPermissionTestUtils.java

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.scalar.db.config.DatabaseConfig;
44
import com.scalar.db.util.PermissionTestUtils;
5+
import java.util.Optional;
56
import java.util.Properties;
67
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
78
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
@@ -11,7 +12,12 @@
1112
import software.amazon.awssdk.regions.Region;
1213
import software.amazon.awssdk.services.iam.IamClient;
1314
import software.amazon.awssdk.services.iam.model.AttachUserPolicyRequest;
15+
import software.amazon.awssdk.services.iam.model.AttachedPolicy;
16+
import software.amazon.awssdk.services.iam.model.CreatePolicyRequest;
1417
import software.amazon.awssdk.services.iam.model.CreatePolicyVersionRequest;
18+
import software.amazon.awssdk.services.iam.model.DeletePolicyVersionRequest;
19+
import software.amazon.awssdk.services.iam.model.ListAttachedUserPoliciesRequest;
20+
import software.amazon.awssdk.services.iam.model.ListPolicyVersionsRequest;
1521
import software.amazon.awssdk.services.iam.model.User;
1622

1723
public class DynamoPermissionTestUtils implements PermissionTestUtils {
@@ -69,22 +75,19 @@ public void dropNormalUser(String userName) {
6975
@Override
7076
public void grantRequiredPermission(String userName) {
7177
try {
72-
// Get the account ID to construct the ARN\
7378
User user = client.getUser().user();
74-
String accountId = user.arn().split(":")[4];
75-
String policyArn = String.format("arn:aws:iam::%s:policy/%s", accountId, IAM_POLICY_NAME);
76-
77-
// Create a new version of the existing policy
78-
client.createPolicyVersion(
79-
CreatePolicyVersionRequest.builder()
80-
.policyArn(policyArn)
81-
.policyDocument(POLICY.toJson())
82-
.setAsDefault(true)
83-
.build());
84-
85-
// Attach the policy to the user
86-
client.attachUserPolicy(
87-
AttachUserPolicyRequest.builder().userName(user.userName()).policyArn(policyArn).build());
79+
Optional<String> attachedPolicyArn = getAttachedPolicyArn(user.userName(), IAM_POLICY_NAME);
80+
if (attachedPolicyArn.isPresent()) {
81+
deleteStalePolicyVersions(attachedPolicyArn.get());
82+
createNewPolicyVersion(attachedPolicyArn.get());
83+
} else {
84+
String policyArn = createNewPolicy();
85+
client.attachUserPolicy(
86+
AttachUserPolicyRequest.builder()
87+
.userName(user.userName())
88+
.policyArn(policyArn)
89+
.build());
90+
}
8891
} catch (Exception e) {
8992
throw new RuntimeException("Failed to grant required permissions", e);
9093
}
@@ -94,4 +97,52 @@ public void grantRequiredPermission(String userName) {
9497
public void close() {
9598
client.close();
9699
}
100+
101+
private Optional<String> getAttachedPolicyArn(String userName, String policyName) {
102+
AttachedPolicy attachedPolicy =
103+
client
104+
.listAttachedUserPolicies(
105+
ListAttachedUserPoliciesRequest.builder().userName(userName).build())
106+
.attachedPolicies()
107+
.stream()
108+
.filter(policy -> policy.policyName().equals(policyName))
109+
.findFirst()
110+
.orElse(null);
111+
return Optional.ofNullable(attachedPolicy).map(AttachedPolicy::policyArn);
112+
}
113+
114+
private String createNewPolicy() {
115+
return client
116+
.createPolicy(
117+
CreatePolicyRequest.builder()
118+
.policyName(IAM_POLICY_NAME)
119+
.policyDocument(POLICY.toJson())
120+
.build())
121+
.policy()
122+
.arn();
123+
}
124+
125+
private void deleteStalePolicyVersions(String policyArn) {
126+
client
127+
.listPolicyVersions(ListPolicyVersionsRequest.builder().policyArn(policyArn).build())
128+
.versions()
129+
.stream()
130+
.filter(version -> !version.isDefaultVersion())
131+
.forEach(
132+
version ->
133+
client.deletePolicyVersion(
134+
DeletePolicyVersionRequest.builder()
135+
.policyArn(policyArn)
136+
.versionId(version.versionId())
137+
.build()));
138+
}
139+
140+
private void createNewPolicyVersion(String policyArn) {
141+
client.createPolicyVersion(
142+
CreatePolicyVersionRequest.builder()
143+
.policyArn(policyArn)
144+
.policyDocument(POLICY.toJson())
145+
.setAsDefault(true)
146+
.build());
147+
}
97148
}

0 commit comments

Comments
 (0)