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;
}
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();
}
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"));
}
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());
}
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"));
}
Aggregations