use of com.aws.greengrass.deployment.exceptions.RetryableDeploymentDocumentDownloadException in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentDocumentDownloader method getDeploymentConfiguration.
private GetDeploymentConfigurationResponse getDeploymentConfiguration(String deploymentId) throws RetryableDeploymentDocumentDownloadException {
String thingName = Coerce.toString(deviceConfiguration.getThingName());
GetDeploymentConfigurationRequest getDeploymentConfigurationRequest = GetDeploymentConfigurationRequest.builder().deploymentId(deploymentId).coreDeviceThingName(thingName).build();
GetDeploymentConfigurationResponse deploymentConfiguration;
try {
logger.atInfo().kv("DeploymentId", deploymentId).kv("ThingName", thingName).log("Calling Greengrass cloud to get full deployment configuration.");
deploymentConfiguration = greengrassServiceClientFactory.getGreengrassV2DataClient().getDeploymentConfiguration(getDeploymentConfigurationRequest);
} catch (AwsServiceException e) {
throw new RetryableDeploymentDocumentDownloadException("Greengrass Cloud Service returned an error when getting full deployment configuration.", e);
} catch (SdkClientException e) {
throw new RetryableDeploymentDocumentDownloadException("Failed to contact Greengrass cloud or unable to parse response.", e);
}
return deploymentConfiguration;
}
use of com.aws.greengrass.deployment.exceptions.RetryableDeploymentDocumentDownloadException in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentDocumentDownloader method downloadFromUrl.
private String downloadFromUrl(String deploymentId, String preSignedUrl) throws RetryableDeploymentDocumentDownloadException, DeploymentTaskFailureException {
HttpExecuteRequest executeRequest = HttpExecuteRequest.builder().request(SdkHttpFullRequest.builder().uri(URI.create(preSignedUrl)).method(SdkHttpMethod.GET).build()).build();
// url is not logged for security concerns
logger.atDebug().kv("DeploymentId", deploymentId).log("Making HTTP request to the presigned url");
try (SdkHttpClient client = httpClientProvider.getSdkHttpClient()) {
HttpExecuteResponse executeResponse;
try {
executeResponse = client.prepareRequest(executeRequest).call();
} catch (IOException e) {
throw new RetryableDeploymentDocumentDownloadException("I/O error when making HTTP request with presigned url.", e);
}
validateHttpExecuteResponse(executeResponse);
try (InputStream in = executeResponse.responseBody().get()) {
// load directly into memory because it will be used for resolving configuration
return IoUtils.toUtf8String(in);
} catch (IOException e) {
throw new RetryableDeploymentDocumentDownloadException("I/O error when reading from HTTP response payload stream.", e);
}
}
}
use of com.aws.greengrass.deployment.exceptions.RetryableDeploymentDocumentDownloadException in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentDocumentDownloaderTest method GIVEN_gg_cloud_returns_different_digest_then_downloaded_content_WHEN_download_THEN_throws_with_proper_message.
@Test
void GIVEN_gg_cloud_returns_different_digest_then_downloaded_content_WHEN_download_THEN_throws_with_proper_message() throws Exception {
String url = "https://www.presigned.com/a.json";
String expectedDigest = "digest";
// mock gg client
when(greengrassV2DataClient.getDeploymentConfiguration(Mockito.any(GetDeploymentConfigurationRequest.class))).thenReturn(GetDeploymentConfigurationResponse.builder().preSignedUrl(url).integrityCheck(IntegrityCheck.builder().algorithm("SHA-256").digest(expectedDigest).build()).build());
// mock http client to return some random content so that digest is different
when(httpClientProvider.getSdkHttpClient()).thenReturn(httpClient);
when(httpClient.prepareRequest(any())).thenReturn(request);
when(request.call()).thenReturn(HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(HTTP_OK).build()).responseBody(AbortableInputStream.create(IOUtils.toInputStream("random"))).build());
RetryableDeploymentDocumentDownloadException exception = assertThrows(RetryableDeploymentDocumentDownloadException.class, () -> downloader.downloadDeploymentDocument(DEPLOYMENT_ID));
assertThat(exception.getMessage(), containsString("Integrity check failed because the calculated digest is different from provided digest"));
}
use of com.aws.greengrass.deployment.exceptions.RetryableDeploymentDocumentDownloadException in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentDocumentDownloaderTest method GIVEN_gg_client_throws_SdkClientException_WHEN_download_THEN_throws_with_proper_message.
@Test
void GIVEN_gg_client_throws_SdkClientException_WHEN_download_THEN_throws_with_proper_message() {
// mock gg client to throw SdkClientException
when(greengrassV2DataClient.getDeploymentConfiguration(Mockito.any(GetDeploymentConfigurationRequest.class))).thenThrow(SdkClientException.builder().build());
verifyNoInteractions(httpClient);
RetryableDeploymentDocumentDownloadException exception = assertThrows(RetryableDeploymentDocumentDownloadException.class, () -> downloader.downloadDeploymentDocument(DEPLOYMENT_ID));
assertThat(exception.getMessage(), containsString("Failed to contact Greengrass cloud or unable to parse response."));
}
use of com.aws.greengrass.deployment.exceptions.RetryableDeploymentDocumentDownloadException in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentDocumentDownloaderTest method GIVEN_get_pre_signed_url_returns_empty_body_WHEN_download_THEN_throw_with_proper_message.
@Test
void GIVEN_get_pre_signed_url_returns_empty_body_WHEN_download_THEN_throw_with_proper_message() throws Exception {
// mock gg client
String url = "https://www.presigned.com/a.json";
// mock gg client
when(greengrassV2DataClient.getDeploymentConfiguration(Mockito.any(GetDeploymentConfigurationRequest.class))).thenReturn(GetDeploymentConfigurationResponse.builder().preSignedUrl(url).integrityCheck(IntegrityCheck.builder().algorithm("SHA-256").digest("digest").build()).build());
// mock http client to return the test file
when(httpClientProvider.getSdkHttpClient()).thenReturn(httpClient);
when(httpClient.prepareRequest(any())).thenReturn(request);
when(request.call()).thenReturn(HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(HTTP_OK).build()).build());
RetryableDeploymentDocumentDownloadException exception = assertThrows(RetryableDeploymentDocumentDownloadException.class, () -> downloader.downloadDeploymentDocument(DEPLOYMENT_ID));
assertThat(exception.getMessage(), containsString("Received empty response body"));
}
Aggregations