Search in sources :

Example 1 with RetryableDeploymentDocumentDownloadException

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;
}
Also used : RetryableDeploymentDocumentDownloadException(com.aws.greengrass.deployment.exceptions.RetryableDeploymentDocumentDownloadException) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) GetDeploymentConfigurationRequest(software.amazon.awssdk.services.greengrassv2data.model.GetDeploymentConfigurationRequest) GetDeploymentConfigurationResponse(software.amazon.awssdk.services.greengrassv2data.model.GetDeploymentConfigurationResponse) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException)

Example 2 with RetryableDeploymentDocumentDownloadException

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);
        }
    }
}
Also used : HttpExecuteRequest(software.amazon.awssdk.http.HttpExecuteRequest) RetryableDeploymentDocumentDownloadException(com.aws.greengrass.deployment.exceptions.RetryableDeploymentDocumentDownloadException) HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) InputStream(java.io.InputStream) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) IOException(java.io.IOException)

Example 3 with RetryableDeploymentDocumentDownloadException

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"));
}
Also used : RetryableDeploymentDocumentDownloadException(com.aws.greengrass.deployment.exceptions.RetryableDeploymentDocumentDownloadException) GetDeploymentConfigurationRequest(software.amazon.awssdk.services.greengrassv2data.model.GetDeploymentConfigurationRequest) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Test(org.junit.jupiter.api.Test)

Example 4 with RetryableDeploymentDocumentDownloadException

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."));
}
Also used : RetryableDeploymentDocumentDownloadException(com.aws.greengrass.deployment.exceptions.RetryableDeploymentDocumentDownloadException) GetDeploymentConfigurationRequest(software.amazon.awssdk.services.greengrassv2data.model.GetDeploymentConfigurationRequest) Test(org.junit.jupiter.api.Test)

Example 5 with RetryableDeploymentDocumentDownloadException

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"));
}
Also used : RetryableDeploymentDocumentDownloadException(com.aws.greengrass.deployment.exceptions.RetryableDeploymentDocumentDownloadException) GetDeploymentConfigurationRequest(software.amazon.awssdk.services.greengrassv2data.model.GetDeploymentConfigurationRequest) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Test(org.junit.jupiter.api.Test)

Aggregations

RetryableDeploymentDocumentDownloadException (com.aws.greengrass.deployment.exceptions.RetryableDeploymentDocumentDownloadException)9 GetDeploymentConfigurationRequest (software.amazon.awssdk.services.greengrassv2data.model.GetDeploymentConfigurationRequest)8 Test (org.junit.jupiter.api.Test)7 StringContains.containsString (org.hamcrest.core.StringContains.containsString)5 IOException (java.io.IOException)2 InputStream (java.io.InputStream)1 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)1 AwsServiceException (software.amazon.awssdk.awscore.exception.AwsServiceException)1 SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)1 AbortableInputStream (software.amazon.awssdk.http.AbortableInputStream)1 HttpExecuteRequest (software.amazon.awssdk.http.HttpExecuteRequest)1 HttpExecuteResponse (software.amazon.awssdk.http.HttpExecuteResponse)1 SdkHttpClient (software.amazon.awssdk.http.SdkHttpClient)1 GetDeploymentConfigurationResponse (software.amazon.awssdk.services.greengrassv2data.model.GetDeploymentConfigurationResponse)1