|
6 | 6 | import java.util.Properties; |
7 | 7 | import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
8 | 8 | import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 9 | +import software.amazon.awssdk.core.exception.SdkException; |
9 | 10 | import software.amazon.awssdk.policybuilder.iam.IamEffect; |
10 | 11 | import software.amazon.awssdk.policybuilder.iam.IamPolicy; |
11 | 12 | import software.amazon.awssdk.policybuilder.iam.IamResource; |
@@ -79,18 +80,34 @@ public void grantRequiredPermission(String userName) { |
79 | 80 | User user = client.getUser().user(); |
80 | 81 | Optional<String> attachedPolicyArn = getAttachedPolicyArn(user.userName()); |
81 | 82 | if (attachedPolicyArn.isPresent()) { |
82 | | - deleteStalePolicyVersions(attachedPolicyArn.get()); |
83 | | - createNewPolicyVersion(attachedPolicyArn.get()); |
| 83 | + String policyArn = attachedPolicyArn.get(); |
| 84 | + try { |
| 85 | + deleteStalePolicyVersions(policyArn); |
| 86 | + createNewPolicyVersion(policyArn); |
| 87 | + } catch (SdkException e) { |
| 88 | + throw new RuntimeException( |
| 89 | + String.format( |
| 90 | + "Failed to update policy for user: %s, policyArn: %s", userName, policyArn), |
| 91 | + e); |
| 92 | + } |
84 | 93 | } else { |
85 | 94 | String policyArn = createNewPolicy(); |
86 | | - client.attachUserPolicy( |
87 | | - AttachUserPolicyRequest.builder() |
88 | | - .userName(user.userName()) |
89 | | - .policyArn(policyArn) |
90 | | - .build()); |
| 95 | + try { |
| 96 | + client.attachUserPolicy( |
| 97 | + AttachUserPolicyRequest.builder() |
| 98 | + .userName(user.userName()) |
| 99 | + .policyArn(policyArn) |
| 100 | + .build()); |
| 101 | + } catch (SdkException e) { |
| 102 | + throw new RuntimeException( |
| 103 | + String.format( |
| 104 | + "Failed to attach new policy for user: %s, policyArn: %s", userName, policyArn), |
| 105 | + e); |
| 106 | + } |
91 | 107 | } |
92 | | - } catch (Exception e) { |
93 | | - throw new RuntimeException("Failed to grant required permissions", e); |
| 108 | + } catch (SdkException e) { |
| 109 | + throw new RuntimeException( |
| 110 | + String.format("Failed to grant required permissions for user: %s", userName), e); |
94 | 111 | } |
95 | 112 | } |
96 | 113 |
|
|
0 commit comments