Skip to content

Commit

Permalink
Fix permission and logging; sync index changes (#53)
Browse files Browse the repository at this point in the history
* Add sso permissions

* Cherry-pick Index changes

* Do not log requests
  • Loading branch information
KHC831 committed May 8, 2024
1 parent d972d4d commit c70bb90
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 15 deletions.
5 changes: 5 additions & 0 deletions aws-qbusiness-application/resource-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ Resources:
- "qbusiness:TagResource"
- "qbusiness:UntagResource"
- "qbusiness:UpdateApplication"
- "sso:CreateApplication"
- "sso:DeleteApplication"
- "sso:PutApplicationAccessScope"
- "sso:PutApplicationAuthenticationMethod"
- "sso:PutApplicationGrant"
Resource: "*"
Outputs:
ExecutionRoleArn:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ protected GetApplicationResponse getApplication(ResourceModel model, ProxyClient
}

protected ProgressEvent<ResourceModel, CallbackContext> handleError(
QBusinessRequest QBusinessRequest,
QBusinessRequest qBusinessRequest,
ResourceModel resourceModel,
Exception error,
CallbackContext context,
Logger logger,
String apiName
) {
logger.log("[ERROR] Failed Request: %s to API: %s. Error Message: %s".formatted(QBusinessRequest, apiName, error.getMessage()));
logger.log("[ERROR] Failed Request: %s. Error Message: %s".formatted(apiName, error.getMessage()));
BaseHandlerException cfnException;

var primaryIdentifier = Optional.ofNullable(resourceModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ protected GetDataSourceResponse callGetDataSource(GetDataSourceRequest request,
}

protected ProgressEvent<ResourceModel, CallbackContext> handleError(
QBusinessRequest QBusinessRequest,
QBusinessRequest qBusinessRequest,
ResourceModel resourceModel,
Exception error,
CallbackContext context,
Logger logger,
String apiName
) {
logger.log("[ERROR] Failed Request: %s to API: %s. Error Message: %s".formatted(QBusinessRequest, apiName, error.getMessage()));
logger.log("[ERROR] Failed Request: %s. Error Message: %s".formatted(apiName, error.getMessage()));
BaseHandlerException cfnException;

var primaryIdentifier = Optional.ofNullable(resourceModel)
Expand Down
3 changes: 2 additions & 1 deletion aws-qbusiness-index/aws-qbusiness-index.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@
"qbusiness:CreateIndex",
"qbusiness:GetIndex",
"qbusiness:ListTagsForResource",
"qbusiness:TagResource"
"qbusiness:TagResource",
"qbusiness:UpdateIndex"
]
},
"read": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ protected GetIndexResponse getIndex(ResourceModel model, ProxyClient<QBusinessCl
}

protected ProgressEvent<ResourceModel, CallbackContext> handleError(
final QBusinessRequest QBusinessRequest,
final QBusinessRequest qBusinessRequest,
final ResourceModel resourceModel,
final Exception error,
final CallbackContext context,
final Logger logger,
final String apiName) {

logger.log("[ERROR] Failed Request: %s to API: %s. Error Message: %s".formatted(QBusinessRequest, apiName, error.getMessage()));
logger.log("[ERROR] Failed Request: %s. Error Message: %s".formatted(apiName, error.getMessage()));
BaseHandlerException cfnException;

var primaryIdentifier = Optional.ofNullable(resourceModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import software.amazon.awssdk.services.qbusiness.model.CreateIndexResponse;
import software.amazon.awssdk.services.qbusiness.model.GetIndexResponse;
import software.amazon.awssdk.services.qbusiness.model.IndexStatus;
import software.amazon.awssdk.services.qbusiness.model.UpdateIndexRequest;
import software.amazon.awssdk.services.qbusiness.model.UpdateIndexResponse;
import software.amazon.awssdk.utils.StringUtils;
import software.amazon.cloudformation.exceptions.CfnNotStabilizedException;
import software.amazon.cloudformation.proxy.AmazonWebServicesClientProxy;
Expand All @@ -18,6 +20,7 @@
import java.util.Objects;

import static software.amazon.qbusiness.index.Constants.API_CREATE_INDEX;
import static software.amazon.qbusiness.index.Constants.API_UPDATE_INDEX;

public class CreateHandler extends BaseHandlerStd {

Expand Down Expand Up @@ -59,7 +62,28 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleRequest(
.handleError((createReq, error, client, model, context) ->
handleError(createReq, model, error, context, logger, API_CREATE_INDEX))
.progress()
).then(progress ->
)
.then(progress -> {
var documentAttributionConfig = request.getDesiredResourceState().getDocumentAttributeConfigurations();
if (documentAttributionConfig == null || documentAttributionConfig.isEmpty()) {
return progress;
}
logger.log(
"[INFO] Document Attribute configuration is present. Will call Update Index for %s for Account: %s, ApplicationId: %s, and index: %s"
.formatted(request.getStackId(), request.getAwsAccountId(), request.getDesiredResourceState().getApplicationId(),
progress.getResourceModel().getIndexId())
);

return proxy.initiate("AWS-QBusiness-Index::PostCreateUpdate", proxyClient, progress.getResourceModel(), progress.getCallbackContext())
.translateToServiceRequest(Translator::translateToPostCreateUpdateRequest)
.makeServiceCall(this::callUpdateIndex)
.stabilize((updateIndexRequest, updateIndexResponse, clientProxyClient, model, context) ->
isStabilized(clientProxyClient, model, logger))
.handleError((updateIndexRequest, error, client, model, context) ->
handleError(updateIndexRequest, model, error, context, logger, API_UPDATE_INDEX))
.progress();
})
.then(progress ->
new ReadHandler().handleRequest(proxy, request, callbackContext, proxyClient, logger)
);
}
Expand Down Expand Up @@ -95,10 +119,17 @@ private boolean isStabilized(
}

private CreateIndexResponse callCreateIndex(final CreateIndexRequest request,
final ProxyClient<QBusinessClient> proxyClient,
final ResourceModel model) {
final ProxyClient<QBusinessClient> proxyClient,
final ResourceModel model) {
CreateIndexResponse response = proxyClient.injectCredentialsAndInvokeV2(request, proxyClient.client()::createIndex);
model.setIndexId(response.indexId());
return response;
}

private UpdateIndexResponse callUpdateIndex(
final UpdateIndexRequest updateIndexRequest,
final ProxyClient<QBusinessClient> proxyClient
) {
return proxyClient.injectCredentialsAndInvokeV2(updateIndexRequest, proxyClient.client()::updateIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ static ResourceModel translateFromReadResponse(final GetIndexResponse awsRespons
.displayName(awsResponse.displayName())
.applicationId(awsResponse.applicationId())
.indexId(awsResponse.indexId())
.indexArn(awsResponse.indexArn())
.indexStatistics(fromServiceIndexStatistics(awsResponse.indexStatistics()))
.status(awsResponse.statusAsString())
.description(awsResponse.description())
Expand Down Expand Up @@ -146,6 +147,14 @@ static UpdateIndexRequest translateToUpdateRequest(final ResourceModel model) {
.build();
}

static UpdateIndexRequest translateToPostCreateUpdateRequest(final ResourceModel model) {
return UpdateIndexRequest.builder()
.applicationId(model.getApplicationId())
.indexId(model.getIndexId())
.documentAttributeConfigurations(toServiceDocumentAttributeConfigurations(model.getDocumentAttributeConfigurations()))
.build();
}


/**
* Request to update properties of a previously created resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ public void handleCreateRequestWithDocumentAttributeConfiguration() {
.indexId(INDEX_ID)
.build()
);
when(QBusinessClient.updateIndex(any(UpdateIndexRequest.class)))
.thenReturn(UpdateIndexResponse.builder().build());
when(QBusinessClient.listTagsForResource(any(ListTagsForResourceRequest.class))).thenReturn(ListTagsForResourceResponse.builder()
.tags(List.of())
.build());
Expand Down Expand Up @@ -218,8 +220,15 @@ proxy, testRequest, new CallbackContext(), proxyClient, logger
assertThat(resultProgress).isNotNull();
assertThat(resultProgress.isSuccess()).isTrue();
verify(QBusinessClient).createIndex(any(CreateIndexRequest.class));
verify(QBusinessClient, times(2)).getIndex(any(GetIndexRequest.class));
verify(QBusinessClient, times(4)).getIndex(any(GetIndexRequest.class));
verify(QBusinessClient).listTagsForResource(any(ListTagsForResourceRequest.class));
verify(QBusinessClient).updateIndex(
argThat(
(ArgumentMatcher<UpdateIndexRequest>) t -> t.applicationId().equals(APP_ID)
&& t.indexId().equals(INDEX_ID)
&& t.hasDocumentAttributeConfigurations()
)
);
}

@Test
Expand Down
3 changes: 2 additions & 1 deletion aws-qbusiness-plugin/aws-qbusiness-plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@
"qbusiness:CreatePlugin",
"qbusiness:GetPlugin",
"qbusiness:ListTagsForResource",
"qbusiness:TagResource"
"qbusiness:TagResource",
"qbusiness:UpdatePlugin"
]
},
"read": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleError(
Logger logger,
String apiName
) {
logger.log("[ERROR] Failed Request: %s to API: %s. Error Message: %s".formatted(qBusinessRequest, apiName, error.getMessage()));
logger.log("[ERROR] Failed Request: %s. Error Message: %s".formatted(apiName, error.getMessage()));
BaseHandlerException cfnException;

var primaryIdentifier = Optional.ofNullable(resourceModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleError(
Logger logger,
String apiName
) {
logger.log("[ERROR] Failed Request: %s to API: %s. Error Message: %s".formatted(qBusinessRequest, apiName, error.getMessage()));
logger.log("[ERROR] Failed Request: %s. Error Message: %s".formatted(apiName, error.getMessage()));
BaseHandlerException cfnException;

var primaryIdentifier = Optional.ofNullable(resourceModel)
Expand Down
2 changes: 2 additions & 0 deletions aws-qbusiness-webexperience/resource-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Resources:
- "qbusiness:TagResource"
- "qbusiness:UntagResource"
- "qbusiness:UpdateWebExperience"
- "sso:PutApplicationGrant"
- "sso:UpdateApplication"
Resource: "*"
Outputs:
ExecutionRoleArn:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleError(
final Logger logger,
final String apiName) {

logger.log("[ERROR] Failed Request: %s to API: %s. Error Message: %s".formatted(qBusinessRequest, apiName, error.getMessage()));
logger.log("[ERROR] Failed Request: %s. Error Message: %s".formatted(apiName, error.getMessage()));
BaseHandlerException cfnException;

var primaryIdentifier = Optional.ofNullable(resourceModel)
Expand Down

0 comments on commit c70bb90

Please sign in to comment.