-
Notifications
You must be signed in to change notification settings - Fork 2
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
Fix tag handling to account for tagging permissions failures #29
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d9c80c4
Fixes for tests
7562149
Merge branch 'aws-cloudformation:main' into main
cwoolum 87489fa
fix: add specific failure for tag based access denial
awinberg-aws 15d7b95
Update to Java 17 for the build
b4c126a
Dedupe Maven configuration
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 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 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 |
---|---|---|
@@ -1 +1 @@ | ||
java 11 | ||
java 17 |
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"java.compile.nullAnalysis.mode": "disabled", | ||
"java.configuration.updateBuildConfiguration": "interactive" | ||
"java.configuration.updateBuildConfiguration": "automatic" | ||
} |
This file contains 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 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 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
86 changes: 86 additions & 0 deletions
86
...ibuilder-common/src/main/java/software/amazon/amplifyuibuilder/common/TaggingHelpers.java
This file contains 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,86 @@ | ||
package software.amazon.amplifyuibuilder.common; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import software.amazon.awssdk.awscore.exception.AwsServiceException; | ||
import software.amazon.awssdk.services.amplifyuibuilder.AmplifyUiBuilderClient; | ||
import software.amazon.awssdk.services.amplifyuibuilder.model.TagResourceRequest; | ||
import software.amazon.awssdk.services.amplifyuibuilder.model.UntagResourceRequest; | ||
import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy; | ||
import software.amazon.cloudformation.proxy.ProxyClient; | ||
import software.amazon.cloudformation.proxy.Logger; | ||
|
||
public class TaggingHelpers { | ||
protected static final String ACCESS_DENIED_ERROR_CODE = "AccessDeniedException"; | ||
protected static final String ACESSS_DENIED_MESSAGE_MATCHER = "is not authorized to perform"; | ||
protected static final String[] TAGGING_PERMISSIONS = { "amplifyuibuilder:TagResource", | ||
"amplifyuibuilder:UntagResource" }; | ||
|
||
public static final String SAMPLE_TAGGING_ACCESS_DENIED_MESSAGE = "An error occurred (AccessDeniedException) when calling the CreateCustomActionType operation: User: arn:aws:sts::0123456789:assumed-role/admin/someUser is not authorized to perform: amplifyuibuilder:TagResource on resource: arn:aws:amplifyuibuilder:us-west-2:0123456789:actiontype:Custom/Source/TestingCustomSource/2 with an explicit deny in an identity-based policy"; | ||
|
||
static String getErrorCode(Exception e) { | ||
if (e instanceof AwsServiceException && ((AwsServiceException) e).awsErrorDetails() != null) { | ||
return ((AwsServiceException) e).awsErrorDetails().errorCode(); | ||
} | ||
return e.getMessage(); | ||
} | ||
|
||
public static Boolean isTagBasedAccessDenied(final Exception e) { | ||
String exceptionMessage = e.getMessage(); | ||
boolean isAccessDeniedException = ACCESS_DENIED_ERROR_CODE.equals(getErrorCode(e)) | ||
&& exceptionMessage != null | ||
&& exceptionMessage.contains(ACESSS_DENIED_MESSAGE_MATCHER); | ||
|
||
if (isAccessDeniedException) { | ||
for (String permission : TAGGING_PERMISSIONS) { | ||
if (e.getMessage().contains(permission)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static String generateArn(final String region, final String accountId, final String appId, final String environmentName, final String resource, final String id) { | ||
return String.format("arn:aws:amplifyuibuilder:%s:%s:app/%s/environment/%s/%s/%s",region, accountId, appId, environmentName, resource, id); | ||
} | ||
|
||
public static void updateTags(final AmazonWebServicesClientProxy proxy, | ||
final ProxyClient<AmplifyUiBuilderClient> proxyClient, | ||
final String appId, | ||
final String resourceArn, | ||
final String typeName, | ||
final Map<String, String> existingTags, | ||
final Map<String, String> desiredTags, | ||
final Logger logger) { | ||
|
||
Map<String, String> safeExistingTags = existingTags == null ? Map.of() : existingTags; | ||
Map<String, String> safeDesiredTags = desiredTags == null ? Map.of() : desiredTags; | ||
|
||
Map<String, String> tagsToAdd = safeDesiredTags.entrySet().stream() | ||
.filter(entry -> !entry.getValue().equals(safeExistingTags.get(entry.getKey()))) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
|
||
Map<String, String> tagsToRemove = safeExistingTags.entrySet().stream() | ||
.filter(entry -> !safeDesiredTags.containsKey(entry.getKey())) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
|
||
if (tagsToRemove.size() > 0) { | ||
Collection<String> tagKeys = tagsToRemove.keySet(); | ||
final UntagResourceRequest untagResourceRequest = UntagResourceRequest.builder().resourceArn(resourceArn) | ||
.tagKeys(tagKeys).build(); | ||
ClientWrapper.execute(proxy, untagResourceRequest, proxyClient.client()::untagResource, typeName, | ||
appId, logger); | ||
} | ||
|
||
if (tagsToAdd.size() > 0) { | ||
final TagResourceRequest tagResourceRequest = TagResourceRequest.builder() | ||
.resourceArn(resourceArn).tags(tagsToAdd).build(); | ||
ClientWrapper.execute(proxy, tagResourceRequest, proxyClient.client()::tagResource, typeName, | ||
appId, logger); | ||
} | ||
logger.log("INFO: Successfully Updated Tags"); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see this as a security risk, but maybe not a useful inclusion to have in the readme