Search in sources :

Example 61 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method getAmazonResourceNamesOfAPI.

// AWS Lambda: rest api operation to get ARNs
@Override
public Response getAmazonResourceNamesOfAPI(String apiId, MessageContext messageContext) {
    JSONObject arns = new JSONObject();
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        API api = apiProvider.getAPIbyUUID(apiId, organization);
        String endpointConfigString = api.getEndpointConfig();
        if (!StringUtils.isEmpty(endpointConfigString)) {
            JSONParser jsonParser = new JSONParser();
            JSONObject endpointConfig = (JSONObject) jsonParser.parse(endpointConfigString);
            if (endpointConfig != null) {
                if (endpointConfig.containsKey(APIConstants.AMZN_ACCESS_KEY) && endpointConfig.containsKey(APIConstants.AMZN_SECRET_KEY) && endpointConfig.containsKey(APIConstants.AMZN_REGION)) {
                    String accessKey = (String) endpointConfig.get(APIConstants.AMZN_ACCESS_KEY);
                    String secretKey = (String) endpointConfig.get(APIConstants.AMZN_SECRET_KEY);
                    String region = (String) endpointConfig.get(APIConstants.AMZN_REGION);
                    AWSCredentialsProvider credentialsProvider;
                    AWSLambda awsLambda;
                    if (StringUtils.isEmpty(accessKey) && StringUtils.isEmpty(secretKey) && StringUtils.isEmpty(region)) {
                        credentialsProvider = DefaultAWSCredentialsProviderChain.getInstance();
                        awsLambda = AWSLambdaClientBuilder.standard().withCredentials(credentialsProvider).build();
                    } else if (!StringUtils.isEmpty(accessKey) && !StringUtils.isEmpty(secretKey) && !StringUtils.isEmpty(region)) {
                        if (secretKey.length() == APIConstants.AWS_ENCRYPTED_SECRET_KEY_LENGTH) {
                            CryptoUtil cryptoUtil = CryptoUtil.getDefaultCryptoUtil();
                            secretKey = new String(cryptoUtil.base64DecodeAndDecrypt(secretKey), APIConstants.DigestAuthConstants.CHARSET);
                        }
                        BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
                        credentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
                        awsLambda = AWSLambdaClientBuilder.standard().withCredentials(credentialsProvider).withRegion(region).build();
                    } else {
                        log.error("Missing AWS Credentials");
                        return null;
                    }
                    ListFunctionsResult listFunctionsResult = awsLambda.listFunctions();
                    List<FunctionConfiguration> functionConfigurations = listFunctionsResult.getFunctions();
                    arns.put("count", functionConfigurations.size());
                    JSONArray list = new JSONArray();
                    for (FunctionConfiguration functionConfiguration : functionConfigurations) {
                        list.put(functionConfiguration.getFunctionArn());
                    }
                    arns.put("list", list);
                    return Response.ok().entity(arns.toString()).build();
                }
            }
        }
    } catch (SdkClientException e) {
        if (e.getCause() instanceof UnknownHostException) {
            arns.put("error", "No internet connection to connect the given access method.");
            log.error("No internet connection to connect the given access method of API : " + apiId, e);
            return Response.serverError().entity(arns.toString()).build();
        } else {
            arns.put("error", "Unable to access Lambda functions under the given access method.");
            log.error("Unable to access Lambda functions under the given access method of API : " + apiId, e);
            return Response.serverError().entity(arns.toString()).build();
        }
    } catch (ParseException e) {
        String errorMessage = "Error while parsing endpoint config of the API: " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (CryptoException | UnsupportedEncodingException e) {
        String errorMessage = "Error while decrypting the secret key of the API: " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving the API: " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : AWSLambda(com.amazonaws.services.lambda.AWSLambda) ListFunctionsResult(com.amazonaws.services.lambda.model.ListFunctionsResult) UnknownHostException(java.net.UnknownHostException) FunctionConfiguration(com.amazonaws.services.lambda.model.FunctionConfiguration) JSONArray(org.json.JSONArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) CryptoUtil(org.wso2.carbon.core.util.CryptoUtil) JSONObject(org.json.simple.JSONObject) SdkClientException(com.amazonaws.SdkClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) CryptoException(org.wso2.carbon.core.util.CryptoException) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider)

Example 62 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project iaf by ibissource.

the class AmazonS3FileSystemTestHelper method open.

private void open() {
    BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey);
    AmazonS3ClientBuilder s3ClientBuilder = AmazonS3ClientBuilder.standard().withChunkedEncodingDisabled(chunkedEncodingDisabled).withAccelerateModeEnabled(accelerateModeEnabled).withForceGlobalBucketAccessEnabled(forceGlobalBucketAccessEnabled).withRegion(clientRegion.getName()).withCredentials(new AWSStaticCredentialsProvider(awsCreds)).withClientConfiguration(this.getProxyConfig());
    s3Client = s3ClientBuilder.build();
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) AmazonS3ClientBuilder(com.amazonaws.services.s3.AmazonS3ClientBuilder) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Example 63 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project aws-xray-sdk-java by aws.

the class TracingHandlerTest method testSNSPublish.

@Test
void testSNSPublish() {
    // Setup test
    // reference : https://docs.aws.amazon.com/sns/latest/api/API_Publish.html
    final String publishResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<PublishResponse xmlns=\"http://sns.amazonaws.com/doc/2010-03-31/\">" + "<PublishResult><MessageId>94f20ce6-13c5-43a0-9a9e-ca52d816e90b</MessageId>" + "</PublishResult>" + "</PublishResponse>";
    final String topicArn = "testTopicArn";
    AmazonSNS sns = AmazonSNSClientBuilder.standard().withRequestHandlers(new TracingHandler()).withRegion(Regions.US_EAST_1).withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("fake", "fake"))).build();
    mockHttpClient(sns, publishResponse);
    // Test logic
    Segment segment = AWSXRay.beginSegment("test");
    sns.publish(new PublishRequest(topicArn, "testMessage"));
    Assertions.assertEquals(1, segment.getSubsegments().size());
    Assertions.assertEquals("Publish", segment.getSubsegments().get(0).getAws().get("operation"));
    Assertions.assertEquals(topicArn, segment.getSubsegments().get(0).getAws().get("topic_arn"));
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) PublishRequest(com.amazonaws.services.sns.model.PublishRequest) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Segment(com.amazonaws.xray.entities.Segment) AmazonSNS(com.amazonaws.services.sns.AmazonSNS) Test(org.junit.jupiter.api.Test)

Example 64 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project aws-xray-sdk-java by aws.

the class TracingHandlerTest method testShouldNotTraceXRaySamplingOperations.

@Test
void testShouldNotTraceXRaySamplingOperations() {
    com.amazonaws.services.xray.AWSXRay xray = AWSXRayClientBuilder.standard().withRequestHandlers(new TracingHandler()).withRegion(Regions.US_EAST_1).withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("fake", "fake"))).build();
    mockHttpClient(xray, null);
    Segment segment = AWSXRay.beginSegment("test");
    xray.getSamplingRules(new GetSamplingRulesRequest());
    Assertions.assertEquals(0, segment.getSubsegments().size());
    xray.getSamplingTargets(new GetSamplingTargetsRequest());
    Assertions.assertEquals(0, segment.getSubsegments().size());
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) GetSamplingTargetsRequest(com.amazonaws.services.xray.model.GetSamplingTargetsRequest) GetSamplingRulesRequest(com.amazonaws.services.xray.model.GetSamplingRulesRequest) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.jupiter.api.Test)

Example 65 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project aws-xray-sdk-java by aws.

the class TracingHandlerTest method testLambdaInvokeSubsegmentContainsFunctionName.

@Test
void testLambdaInvokeSubsegmentContainsFunctionName() {
    // Setup test
    AWSLambda lambda = AWSLambdaClientBuilder.standard().withRequestHandlers(new TracingHandler()).withRegion(Regions.US_EAST_1).withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("fake", "fake"))).build();
    // Lambda returns "null" on successful fn. with no return value
    mockHttpClient(lambda, "null");
    // Test logic
    Segment segment = AWSXRay.beginSegment("test");
    InvokeRequest request = new InvokeRequest();
    request.setFunctionName("testFunctionName");
    lambda.invoke(request);
    Assertions.assertEquals(1, segment.getSubsegments().size());
    Assertions.assertEquals("Invoke", segment.getSubsegments().get(0).getAws().get("operation"));
    Assertions.assertEquals("testFunctionName", segment.getSubsegments().get(0).getAws().get("function_name"));
}
Also used : AWSLambda(com.amazonaws.services.lambda.AWSLambda) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) InvokeRequest(com.amazonaws.services.lambda.model.InvokeRequest) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.jupiter.api.Test)

Aggregations

AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)79 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)57 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)18 AWSCredentials (com.amazonaws.auth.AWSCredentials)17 Test (org.junit.Test)14 ClientConfiguration (com.amazonaws.ClientConfiguration)13 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)11 AmazonS3 (com.amazonaws.services.s3.AmazonS3)11 AmazonS3ClientBuilder (com.amazonaws.services.s3.AmazonS3ClientBuilder)11 SdkClientException (com.amazonaws.SdkClientException)10 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)9 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)9 AmazonClientException (com.amazonaws.AmazonClientException)8 Regions (com.amazonaws.regions.Regions)8 Test (org.junit.jupiter.api.Test)7 EndpointConfiguration (com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration)6 File (java.io.File)6 AmazonServiceException (com.amazonaws.AmazonServiceException)5 AWSLambda (com.amazonaws.services.lambda.AWSLambda)4 Segment (com.amazonaws.xray.entities.Segment)4