22
33import com .scalar .db .config .DatabaseConfig ;
44import com .scalar .db .util .PermissionTestUtils ;
5+ import java .util .Optional ;
56import java .util .Properties ;
67import software .amazon .awssdk .auth .credentials .AwsBasicCredentials ;
78import software .amazon .awssdk .auth .credentials .StaticCredentialsProvider ;
1112import software .amazon .awssdk .regions .Region ;
1213import software .amazon .awssdk .services .iam .IamClient ;
1314import 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 ;
1417import 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 ;
1521import software .amazon .awssdk .services .iam .model .User ;
1622
1723public 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