List of usage examples for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials
public BasicAWSCredentials(String accessKey, String secretKey)
From source file:com.cloud.utils.S3Utils.java
License:Apache License
private static AmazonS3 acquireClient(final ClientOptions clientOptions) { final AWSCredentials credentials = new BasicAWSCredentials(clientOptions.getAccessKey(), clientOptions.getSecretKey()); final ClientConfiguration configuration = new ClientConfiguration(); if (clientOptions.isHttps() != null) { configuration.setProtocol(clientOptions.isHttps() == true ? HTTPS : HTTP); }/*from w w w . j a va 2s .c o m*/ if (clientOptions.getConnectionTimeout() != null) { configuration.setConnectionTimeout(clientOptions.getConnectionTimeout()); } if (clientOptions.getMaxErrorRetry() != null) { configuration.setMaxErrorRetry(clientOptions.getMaxErrorRetry()); } if (clientOptions.getSocketTimeout() != null) { configuration.setSocketTimeout(clientOptions.getSocketTimeout()); } if (LOGGER.isDebugEnabled()) { LOGGER.debug(format( "Creating S3 client with configuration: [protocol: %1$s, connectionTimeOut: " + "%2$s, maxErrorRetry: %3$s, socketTimeout: %4$s]", configuration.getProtocol(), configuration.getConnectionTimeout(), configuration.getMaxErrorRetry(), configuration.getSocketTimeout())); } final AmazonS3Client client = new AmazonS3Client(credentials, configuration); if (isNotBlank(clientOptions.getEndPoint())) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(format("Setting the end point for S3 client %1$s to %2$s.", client, clientOptions.getEndPoint())); } client.setEndpoint(clientOptions.getEndPoint()); } return client; }
From source file:com.cloudbees.demo.beesshop.service.AmazonS3FileStorageService.java
License:Apache License
public void setAmazonCredentials(String awsAccessKey, String awsSecretKey) { this.awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey); this.amazonS3 = new AmazonS3Client(awsCredentials); }
From source file:com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl.java
License:Open Source License
public AWSCredentials getCredentials() { AWSCredentials initialCredentials = new BasicAWSCredentials(accessKey, secretKey.getPlainText()); if (StringUtils.isBlank(iamRoleArn)) { return initialCredentials; } else {//from w ww . j av a 2s . co m // Check for available region from the SDK, otherwise specify default String clientRegion = null; DefaultAwsRegionProviderChain sdkRegionLookup = new DefaultAwsRegionProviderChain(); try { clientRegion = sdkRegionLookup.getRegion(); } catch (com.amazonaws.SdkClientException e) { LOGGER.log(Level.WARNING, "Could not find default region using SDK lookup.", e); } if (clientRegion == null) { clientRegion = Regions.DEFAULT_REGION.getName(); } AWSSecurityTokenService client; // Handle the case of delegation to instance profile if (StringUtils.isBlank(accessKey) && StringUtils.isBlank(secretKey.getPlainText())) { client = AWSSecurityTokenServiceClientBuilder.standard().withRegion(clientRegion).build(); } else { client = AWSSecurityTokenServiceClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(initialCredentials)) .withRegion(clientRegion).build(); } AssumeRoleRequest assumeRequest = createAssumeRoleRequest(iamRoleArn) .withDurationSeconds(this.getStsTokenDuration()); AssumeRoleResult assumeResult = client.assumeRole(assumeRequest); return new BasicSessionCredentials(assumeResult.getCredentials().getAccessKeyId(), assumeResult.getCredentials().getSecretAccessKey(), assumeResult.getCredentials().getSessionToken()); } }
From source file:com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl.java
License:Open Source License
public AWSCredentials getCredentials(String mfaToken) { AWSCredentials initialCredentials = new BasicAWSCredentials(accessKey, secretKey.getPlainText()); AssumeRoleRequest assumeRequest = createAssumeRoleRequest(iamRoleArn).withSerialNumber(iamMfaSerialNumber) .withTokenCode(mfaToken).withDurationSeconds(this.getStsTokenDuration()); AssumeRoleResult assumeResult = new AWSSecurityTokenServiceClient(initialCredentials) .assumeRole(assumeRequest);//from w ww. j ava 2 s . co m return new BasicSessionCredentials(assumeResult.getCredentials().getAccessKeyId(), assumeResult.getCredentials().getSecretAccessKey(), assumeResult.getCredentials().getSessionToken()); }
From source file:com.clouddrive.parth.AmazonOperations.java
public AmazonOperations() { String secretKey = "kxDFnyETb02UrLr4YT3bRjiET+/FNGUMrE3DrU4j"; String accessKey = "AKIAII3DXT3OYD5UV4WQ"; BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey); s3 = new AmazonS3Client(awsCreds); Region usWest2 = Region.getRegion(Regions.US_WEST_2); ((AmazonWebServiceClient) s3).setRegion(usWest2); }
From source file:com.clouddrive.parth.NewClass.java
public static void init(String instanceType, String noOfinstances) throws Exception { INSTANCE_COUNT = Integer.parseInt(noOfinstances); switch (instanceType) { case "C1Medium": INSTANCE_TYPE = InstanceType.C1Medium.toString(); break;//from w w w . j a v a 2s . c o m case "C1Xlarge": INSTANCE_TYPE = InstanceType.C1Xlarge.toString(); break; case "C32xlarge": INSTANCE_TYPE = InstanceType.C32xlarge.toString(); break; case "C34xlarge": INSTANCE_TYPE = InstanceType.C34xlarge.toString(); break; case "C38xlarge": INSTANCE_TYPE = InstanceType.C38xlarge.toString(); break; case "C3Large": INSTANCE_TYPE = InstanceType.C3Large.toString(); break; case "C3Xlarge": INSTANCE_TYPE = InstanceType.C3Xlarge.toString(); break; case "Cc14xlarge": INSTANCE_TYPE = InstanceType.Cc14xlarge.toString(); break; case "Cc28xlarge": INSTANCE_TYPE = InstanceType.Cc28xlarge.toString(); break; case "Cg14xlarge": INSTANCE_TYPE = InstanceType.Cg14xlarge.toString(); break; case "Cr18xlarge": INSTANCE_TYPE = InstanceType.Cr18xlarge.toString(); break; case "G22xlarge": INSTANCE_TYPE = InstanceType.G22xlarge.toString(); break; case "T1Micro": INSTANCE_TYPE = InstanceType.T1Micro.toString(); break; } //AWSCredentials credentials = new PropertiesCredentials(NewClass.class.getClassLoader().getResourceAsStream( // "AwsCredentials.properties")); // s3 = new AmazonS3Client(credentials); String secretKey = "kxDFnyETb02UrLr4YT3bRjiET+/FNGUMrE3DrU4j"; String accessKey = "AKIAII3DXT3OYD5UV4WQ"; BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey); s3 = new AmazonS3Client(awsCreds); Region usWest2 = Region.getRegion(Regions.US_WEST_2); ((AmazonWebServiceClient) s3).setRegion(usWest2); emr = new AmazonElasticMapReduceClient(awsCreds); emr.setRegion(Region.getRegion(Regions.EU_WEST_1)); }
From source file:com.comcast.cmb.common.controller.AdminServletBase.java
License:Apache License
/** * Method to set the aws credentials for sqs and sns handlers * @param userId/*from w w w. j a v a2s . co m*/ * @throws ServletException */ protected void connect(HttpServletRequest request) throws ServletException { String userId = request.getParameter("userId"); IUserPersistence userHandler = PersistenceFactory.getUserPersistence(); try { user = userHandler.getUserById(userId); } catch (PersistenceException ex) { throw new ServletException(ex); } if (user == null) { throw new ServletException("User " + userId + " does not exist"); } if (!user.getUserName().equals(getAuthenticatedUser(request).getUserName()) && !getAuthenticatedUser(request).getIsAdmin()) { throw new ServletException("Only admin may impersonate other users"); } awsCredentials = new BasicAWSCredentials(user.getAccessKey(), user.getAccessSecret()); sqs = new AmazonSQSClient(awsCredentials); sqs.setEndpoint(CMBProperties.getInstance().getCQSServiceUrl()); sns = new AmazonSNSClient(awsCredentials); sns.setEndpoint(CMBProperties.getInstance().getCNSServiceUrl()); }
From source file:com.comcast.cmb.test.tools.CMBTutorial.java
License:Apache License
public static void main(String[] args) { try {//from ww w . j a v a 2 s . co m Util.initLog4jTest(); //TODO: set user id and credentials for two distinct users // user "cqs_test_1" //BasicAWSCredentials user1Credentials = new BasicAWSCredentials("<access_key>", "<secret_key>"); BasicAWSCredentials user1Credentials = new BasicAWSCredentials("Z2DVBFRNZ2C2SSXDWS5F", "bH2UQiJkpctBaE3eaDob19fj5J9Q1FVafrZantBp"); // user "cqs_test_2" //String user2Id = "<user_id>"; String user2Id = "389653920093"; //BasicAWSCredentials user2Credentials = new BasicAWSCredentials("<access_key>", "<secret_key>"); BasicAWSCredentials user2Credentials = new BasicAWSCredentials("QL8Q1VOBCSJC5FZ2DMIU", "n6a82gyJZ04Z+Xqp7OgfqPtbbKqVc3UbuOTNrF+7"); // service urls //TODO: add service URLs //String cqsServerUrl = "http://<host>:<port>"; //String cnsServerUrl = "http://<host>:<port>"; String cqsServerUrl = "http://localhost:6059"; String cnsServerUrl = "http://localhost:6061"; // initialize service AmazonSQSClient sqs = new AmazonSQSClient(user1Credentials); sqs.setEndpoint(cqsServerUrl); AmazonSNSClient sns = new AmazonSNSClient(user2Credentials); sns.setEndpoint(cnsServerUrl); // create queue Random randomGenerator = new Random(); String queueName = QUEUE_PREFIX + randomGenerator.nextLong(); HashMap<String, String> attributeParams = new HashMap<String, String>(); CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName); createQueueRequest.setAttributes(attributeParams); String queueUrl = sqs.createQueue(createQueueRequest).getQueueUrl(); AddPermissionRequest addPermissionRequest = new AddPermissionRequest(); addPermissionRequest.setQueueUrl(queueUrl); addPermissionRequest.setActions(Arrays.asList("SendMessage")); addPermissionRequest.setLabel(UUID.randomUUID().toString()); addPermissionRequest.setAWSAccountIds(Arrays.asList(user2Id)); sqs.addPermission(addPermissionRequest); // create topic String topicName = "TSTT" + randomGenerator.nextLong(); CreateTopicRequest createTopicRequest = new CreateTopicRequest(topicName); CreateTopicResult createTopicResult = sns.createTopic(createTopicRequest); String topicArn = createTopicResult.getTopicArn(); // subscribe and confirm cqs endpoint SubscribeRequest subscribeRequest = new SubscribeRequest(); String queueArn = getArnForQueueUrl(queueUrl); subscribeRequest.setEndpoint(queueArn); subscribeRequest.setProtocol("cqs"); subscribeRequest.setTopicArn(topicArn); SubscribeResult subscribeResult = sns.subscribe(subscribeRequest); String subscriptionArn = subscribeResult.getSubscriptionArn(); if (subscriptionArn.equals("pending confirmation")) { Thread.sleep(500); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(); receiveMessageRequest.setQueueUrl(queueUrl); receiveMessageRequest.setMaxNumberOfMessages(1); ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest); List<Message> messages = receiveMessageResult.getMessages(); if (messages != null && messages.size() == 1) { JSONObject o = new JSONObject(messages.get(0).getBody()); if (!o.has("SubscribeURL")) { throw new Exception("message is not a confirmation messsage"); } String subscriptionUrl = o.getString("SubscribeURL"); httpGet(subscriptionUrl); DeleteMessageRequest deleteMessageRequest = new DeleteMessageRequest(); deleteMessageRequest.setReceiptHandle(messages.get(0).getReceiptHandle()); deleteMessageRequest.setQueueUrl(queueUrl); sqs.deleteMessage(deleteMessageRequest); } else { throw new Exception("no confirmation message found"); } } // publish and receive message PublishRequest publishRequest = new PublishRequest(); String messageText = "quamvis sint sub aqua, sub aqua maledicere temptant"; publishRequest.setMessage(messageText); publishRequest.setSubject("unit test message"); publishRequest.setTopicArn(topicArn); sns.publish(publishRequest); Thread.sleep(500); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(); receiveMessageRequest.setQueueUrl(queueUrl); receiveMessageRequest.setMaxNumberOfMessages(1); ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest); List<Message> messages = receiveMessageResult.getMessages(); if (messages != null && messages.size() == 1) { String messageBody = messages.get(0).getBody(); if (!messageBody.contains(messageText)) { throw new Exception("message text not found"); } DeleteMessageRequest deleteMessageRequest = new DeleteMessageRequest(); deleteMessageRequest.setReceiptHandle(messages.get(0).getReceiptHandle()); deleteMessageRequest.setQueueUrl(queueUrl); sqs.deleteMessage(deleteMessageRequest); } else { throw new Exception("no messages found"); } // subscribe and confirm http endpoint String id = randomGenerator.nextLong() + ""; String endPointUrl = CMBTestingConstants.HTTP_ENDPOINT_BASE_URL + "recv/" + id; String lastMessageUrl = CMBTestingConstants.HTTP_ENDPOINT_BASE_URL + "info/" + id + "?showLast=true"; subscribeRequest = new SubscribeRequest(); subscribeRequest.setEndpoint(endPointUrl); subscribeRequest.setProtocol("http"); subscribeRequest.setTopicArn(topicArn); subscribeResult = sns.subscribe(subscribeRequest); subscriptionArn = subscribeResult.getSubscriptionArn(); if (subscriptionArn.equals("pending confirmation")) { Thread.sleep(500); String response = httpGet(lastMessageUrl); JSONObject o = new JSONObject(response); if (!o.has("SubscribeURL")) { throw new Exception("message is not a confirmation messsage"); } String subscriptionUrl = o.getString("SubscribeURL"); response = httpGet(subscriptionUrl); } // publish and receive message publishRequest = new PublishRequest(); publishRequest.setMessage(messageText); publishRequest.setSubject("unit test message"); publishRequest.setTopicArn(topicArn); sns.publish(publishRequest); Thread.sleep(500); String response = httpGet(lastMessageUrl); if (response != null && response.length() > 0) { if (!response.contains(messageText)) { throw new Exception("message text not found"); } } else { throw new Exception("no messages found"); } // delete queue and topic DeleteTopicRequest deleteTopicRequest = new DeleteTopicRequest(topicArn); sns.deleteTopic(deleteTopicRequest); sqs.deleteQueue(new DeleteQueueRequest(queueUrl)); System.out.println("OK"); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.comcast.cns.controller.CNSPublishAction.java
License:Apache License
/** * The method simply gets the information from the user and request to call publish the message passed in * the "Message" field in the request parameters, and publish them to all endpoints subscribed to the topic * designated by the "TopicArn" field in the request, then take the response and generate an XML response * and put it in the parameter response/* www . j av a 2 s. co m*/ * @param user the user for whom we are listing the subscription * @param asyncContext */ @Override public boolean doAction(User user, AsyncContext asyncContext) throws Exception { CQSHttpServletRequest request = (CQSHttpServletRequest) asyncContext.getRequest(); HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse(); if (cnsInternalUser == null) { IUserPersistence userHandler = PersistenceFactory.getUserPersistence(); cnsInternalUser = userHandler.getUserByName(CMBProperties.getInstance().getCNSUserName()); if (cnsInternalUser == null) { cnsInternalUser = userHandler.createDefaultUser(); } } if (awsCredentials == null) { awsCredentials = new BasicAWSCredentials(cnsInternalUser.getAccessKey(), cnsInternalUser.getAccessSecret()); } if (sqs == null) { sqs = new AmazonSQSClient(awsCredentials); sqs.setEndpoint(CMBProperties.getInstance().getCQSServiceUrl()); } String userId = user.getUserId(); String message = request.getParameter("Message"); String topicArn = request.getParameter("TopicArn"); String messageStructure = null; String subject = null; CNSMessage cnsMessage = new CNSMessage(); cnsMessage.generateMessageId(); cnsMessage.setTimestamp(new Date()); cnsMessage.setMessage(message); //TODO: optional shortcut if (request.getParameter("MessageStructure") != null) { messageStructure = request.getParameter("MessageStructure"); if (!messageStructure.equals("json")) { logger.error( "event=cns_publish error_code=InvalidParameters message=" + message + " message_structure=" + messageStructure + " topic_arn=" + topicArn + " user_id=" + userId); throw new CMBException(CNSErrorCodes.CNS_InvalidParameter, "Invalid parameter: Invalid Message Structure parameter: " + messageStructure); } cnsMessage.setMessageStructure(CNSMessage.CNSMessageStructure.valueOf(messageStructure)); } if (request.getParameter("Subject") != null) { subject = request.getParameter("Subject"); cnsMessage.setSubject(subject); } if ((userId == null) || (message == null)) { logger.error("event=cns_publish error_code=InvalidParameters message=" + message + " topic_arn=" + topicArn + " user_id=" + userId); throw new CMBException(CNSErrorCodes.CNS_ValidationError, "1 validation error detected: Value null at 'message' failed to satisfy constraint: Member must not be null"); } if ((topicArn == null) || !Util.isValidTopicArn(topicArn)) { logger.error("event=cns_publish error_code=InvalidParameters message=" + message + " topic_arn=" + topicArn + " user_id=" + userId); throw new CMBException(CNSErrorCodes.CNS_InvalidParameter, "TopicArn"); } CNSTopic topic = CNSCache.getTopic(topicArn); if (topic == null) { logger.error("event=cns_publish error_code=NotFound message=" + message + " topic_arn=" + topicArn + " user_id=" + userId); throw new CMBException(CNSErrorCodes.CNS_NotFound, "Resource not found."); } cnsMessage.setUserId(topic.getUserId()); cnsMessage.setTopicArn(topicArn); cnsMessage.setMessageType(CNSMessageType.Notification); cnsMessage.checkIsValid(); CNSTopicAttributes topicAttributes = CNSCache.getTopicAttributes(topicArn); List<String> receiptHandles = new ArrayList<String>(); boolean success = true; if (topicAttributes != null && topicAttributes.getSubscriptionsConfirmed() == 0) { // optimization: don't do anything if there are no confirmed subscribers logger.warn("event=no_confirmed_subscribers action=publish topic_arn=" + topicArn); } else if (CMBProperties.getInstance().isCNSBypassPublishJobQueueForSmallTopics() && topicAttributes != null && topicAttributes.getSubscriptionsConfirmed() <= CMBProperties.getInstance() .getCNSMaxSubscriptionsPerEndpointPublishJob()) { // optimization: if there's only one chunk due to few subscribers, write directly into endpoint publish queue bypassing the publish job queue logger.debug("event=using_job_queue_overpass"); List<CNSEndpointPublishJob.CNSEndpointSubscriptionInfo> subscriptions = CNSEndpointPublisherJobProducer .getSubscriptionsForTopic(topicArn); if (subscriptions != null && subscriptions.size() > 0) { List<CNSEndpointPublishJob> epPublishJobs = CNSEndpointPublisherJobProducer .createEndpointPublishJobs(cnsMessage, subscriptions); if (epPublishJobs.size() != 1) { logger.warn("event=unexpected_number_of_endpoint_publish_jobs count=" + epPublishJobs.size()); } for (CNSEndpointPublishJob epPublishJob : epPublishJobs) { String handle = sendMessageOnRandomShardAndCreateQueueIfAbsent(CNS_ENDPOINT_QUEUE_NAME_PREFIX, CMBProperties.getInstance().getCNSNumEndpointPublishJobQueues(), epPublishJob.serialize(), cnsInternalUser.getUserId()); if (handle != null && !handle.equals("")) { receiptHandles.add(handle); } else { success = false; } } } } else { // otherwise pick publish job queue logger.debug("event=going_through_job_queue_town_center"); String handle = sendMessageOnRandomShardAndCreateQueueIfAbsent(CNS_PUBLISH_QUEUE_NAME_PREFIX, CMBProperties.getInstance().getCNSNumPublishJobQueues(), cnsMessage.serialize(), cnsInternalUser.getUserId()); if (handle != null && !handle.equals("")) { receiptHandles.add(handle); } else { success = false; } } if (!success) { throw new CMBException(CMBErrorCodes.InternalError, "Failed to place message on internal cns queue"); } request.setReceiptHandles(receiptHandles); String out = CNSSubscriptionPopulator.getPublishResponse(receiptHandles); writeResponse(out, response); return true; }
From source file:com.comcast.cns.io.CQSEndpointPublisher.java
License:Apache License
@Override public void send() throws Exception { if ((message == null) || (endpoint == null)) { throw new Exception("Message and Endpoint must both be set"); }//from www .j a va 2s . c o m String absoluteQueueUrl; if (com.comcast.cqs.util.Util.isValidQueueUrl(endpoint)) { absoluteQueueUrl = endpoint; } else { absoluteQueueUrl = com.comcast.cqs.util.Util.getAbsoluteQueueUrlForArn(endpoint); } try { String msg = null; if (message.getMessageStructure() == CNSMessageStructure.json) { msg = message.getProtocolSpecificMessage(CnsSubscriptionProtocol.cqs); } else { msg = message.getMessage(); } if (!rawMessageDelivery && message.getMessageType() == CNSMessageType.Notification) { msg = com.comcast.cns.util.Util.generateMessageJson(message, CnsSubscriptionProtocol.cqs); } if (msg == null) { logger.warn("event=message_is_null endpoint=" + endpoint); return; } if (CMBProperties.getInstance().useInlineApiCalls() && CMBProperties.getInstance().getCQSServiceEnabled()) { CQSAPI.sendMessage(user.getUserId(), Util.getRelativeForAbsoluteQueueUrl(absoluteQueueUrl), msg, null); } else { awsCredentials = new BasicAWSCredentials(user.getAccessKey(), user.getAccessSecret()); sqs = new AmazonSQSClient(awsCredentials); sqs.setEndpoint(CMBProperties.getInstance().getCQSServiceUrl()); sqs.sendMessage(new SendMessageRequest(absoluteQueueUrl, msg)); } if (CMBProperties.getInstance().getMaxMessagePayloadLogLength() > 0) { if (msg.length() > CMBProperties.getInstance().getMaxMessagePayloadLogLength()) { logger.debug("event=delivering_cqs_message endpoint=" + endpoint + "\" message=\"" + msg.substring(0, CMBProperties.getInstance().getMaxMessagePayloadLogLength() - 1)); } else { logger.debug("event=delivering_cqs_message endpoint=" + endpoint + "\" message=\"" + msg); } } else { logger.debug("event=delivering_cqs_message endpoint=" + endpoint); } } catch (Exception ex) { logger.warn("event=send_cqs_message endpoint=" + endpoint + "\" message=\"" + message, ex); throw new CMBException(CNSErrorCodes.InternalError, "internal service error"); } logger.debug("event=send_cqs_message endpoint=" + endpoint + " message=\"" + message + "\""); }