List of usage examples for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials
public BasicAWSCredentials(String accessKey, String secretKey)
From source file:com.neu.cloud.Controller.SecondUseCaseController.java
private void uploadInS3(String uploadFilePath, String uploadFileName, String dateForFolder) { String bucketName = "reports-sppard"; String keyName = "UseCase2-" + dateForFolder + "/" + uploadFileName; AmazonS3 s3client = new AmazonS3Client( new BasicAWSCredentials("AKIAJ2E67YVFQ5PZSWQA", "xiVuejpUofGonrsiy2owvu/wgeNKq5nYjxYVC0ma")); try {/*from www. jav a 2 s . c o m*/ System.out.println("Uploading a new object to S3 from a file\n"); File file = new File(uploadFilePath + uploadFileName); s3client.putObject(new PutObjectRequest(bucketName, keyName, file)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:com.neu.cloud.Controller.ThirdUseCaseController.java
private void uploadInS3(String uploadFilePath, String uploadFileName, String dateForFolder) { String bucketName = "reports-sppard"; String keyName = "UseCase3-" + dateForFolder + "/" + uploadFileName; AmazonS3 s3client = new AmazonS3Client( new BasicAWSCredentials("AKIAJ2E67YVFQ5PZSWQA", "xiVuejpUofGonrsiy2owvu/wgeNKq5nYjxYVC0ma")); try {//w w w .j a v a 2s. co m System.out.println("Uploading a new object to S3 from a file\n"); File file = new File(uploadFilePath + uploadFileName); s3client.putObject(new PutObjectRequest(bucketName, keyName, file)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:com.neu.Spark.MainFrame.java
/** * Creates new form MainFrame// ww w . j a v a 2 s .c o m */ public MainFrame() { initComponents(); AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setProtocol(Protocol.HTTP); conn = new AmazonS3Client(credentials); conn.setEndpoint("s3.amazonaws.com"); }
From source file:com.nextdoor.bender.aws.S3MockClientFactory.java
License:Apache License
public S3MockClientFactory(int port, String username, String pass) { try {/*from w w w . ja va 2 s . com*/ this.s3.start(port, username, pass); } catch (IOException | InterruptedException | InvalidExitValueException | TimeoutException e) { throw new RuntimeException(e); } BasicAWSCredentials awsCredentials = new BasicAWSCredentials(username, pass); this.client = new AmazonS3Client(awsCredentials, new ClientConfiguration()); this.client.setEndpoint("http://127.0.0.1:" + port); this.client.createBucket(S3_BUCKET); }
From source file:com.noctarius.hazelcast.aws.HazelcastAwsDiscoveryStrategy.java
License:Open Source License
private AWSCredentialsProvider buildCredentialsProvider() { String accessKey = getOrNull(AwsProperties.ACCESS_KEY); String secretKey = getOrNull(AwsProperties.SECRET_KEY); if (accessKey == null && secretKey == null) { return new AWSCredentialsProviderChain(new EnvironmentVariableCredentialsProvider(), new SystemPropertiesCredentialsProvider(), new InstanceProfileCredentialsProvider()); }/* www . ja va2 s . co m*/ return new AWSCredentialsProviderChain( new StaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey))); }
From source file:com.nokia.mole.DynamoDB.java
License:Open Source License
public DynamoDB(int cachePeriod) { log.info("Starting Dynamo DB"); GsonBuilder gBuilder = new GsonBuilder(); gBuilder.registerTypeAdapter(Mac.class, new Mac().new MacDeserializer()).create(); gson = gBuilder.create();// w ww. ja v a2 s .com String key = MoleWS.getProperty("moleWS.aws_key"); String secret = MoleWS.getProperty("moleWS.aws_secret"); if (key == null || secret == null) { log.fatal("aws_key or aws_secret not found"); } AWSCredentials credentials = new BasicAWSCredentials(key, secret); client = new AmazonDynamoDBClient(credentials); if (cachePeriod > 0) { useCache = true; loc2fpCache = new TransientMapCache<Location, Fingerprint>(cachePeriod); mac2locCache = new TransientMapCache<Mac, Set<Location>>(cachePeriod); } else { useCache = false; } }
From source file:com.norbl.cbp.ppe.ParamsEc2.java
License:Open Source License
public AWSCredentials buildAWSCredentials() throws MissingParamsException { return (new BasicAWSCredentials(awsAccessKey, awsSecretKey)); }
From source file:com.norconex.committer.cloudsearch.CloudSearchCommitter.java
License:Apache License
private synchronized void ensureAWSClient() { if (StringUtils.isBlank(getDocumentEndpoint())) { throw new CommitterException("Document endpoint is undefined."); }/* w w w. jav a2 s . com*/ if (!needNewAwsClient) { return; } if (StringUtils.isAnyBlank(accessKey, secretKey)) { awsClient = new AmazonCloudSearchDomainClient(new DefaultAWSCredentialsProviderChain()); } else { awsClient = new AmazonCloudSearchDomainClient(new BasicAWSCredentials(accessKey, secretKey)); } awsClient.setEndpoint(documentEndpoint); needNewAwsClient = false; }
From source file:com.okta.tools.awscli.java
License:Open Source License
private static AssumeRoleWithSAMLResult assumeAWSRole(String resultSAML) { // Decode SAML response resultSAML = resultSAML.replace("+", "+").replace("=", "="); String resultSAMLDecoded = new String(Base64.decodeBase64(resultSAML)); ArrayList<String> principalArns = new ArrayList<String>(); ArrayList<String> roleArns = new ArrayList<String>(); //When the app is not assigned to you no assertion is returned if (!resultSAMLDecoded.contains("arn:aws")) { logger.error("\nYou do not have access to AWS through Okta. \nPlease contact your administrator."); System.exit(0);//from ww w . j a va 2 s . c o m } System.out.println("\nPlease choose the role you would like to assume: "); //Gather list of applicable AWS roles int i = 0; while (resultSAMLDecoded.indexOf("arn:aws") != -1) { /*Trying to parse the value of the Role SAML Assertion that typically looks like this: <saml2:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string"> arn:aws:iam::[AWS-ACCOUNT-ID]:saml-provider/Okta,arn:aws:iam::[AWS-ACCOUNT-ID]:role/[ROLE_NAME] </saml2:AttributeValue> </saml2:Attribute> */ int start = resultSAMLDecoded.indexOf("arn:aws"); int end = resultSAMLDecoded.indexOf("</saml2:", start); String resultSAMLRole = resultSAMLDecoded.substring(start, end); String[] parts = resultSAMLRole.split(","); principalArns.add(parts[0]); roleArns.add(parts[1]); System.out.println("[ " + (i + 1) + " ]: " + roleArns.get(i)); resultSAMLDecoded = (resultSAMLDecoded .substring(resultSAMLDecoded.indexOf("</saml2:AttributeValue") + 1)); i++; } //Prompt user for role selection int selection = numSelection(roleArns.size()); String principalArn = principalArns.get(selection); String roleArn = roleArns.get(selection); crossAccountRoleName = roleArn.substring(roleArn.indexOf("/") + 1); //creates empty AWS credentials to prevent the AWSSecurityTokenServiceClient object from unintentionally loading the previous profile we just created BasicAWSCredentials awsCreds = new BasicAWSCredentials("", ""); //use user credentials to assume AWS role AWSSecurityTokenServiceClient stsClient = new AWSSecurityTokenServiceClient(awsCreds); AssumeRoleWithSAMLRequest assumeRequest = new AssumeRoleWithSAMLRequest().withPrincipalArn(principalArn) .withRoleArn(roleArn).withSAMLAssertion(resultSAML).withDurationSeconds(3600); //default token duration to 12 hours return stsClient.assumeRoleWithSAML(assumeRequest); }
From source file:com.okta.tools.awscli.java
License:Open Source License
private static void GetRoleToAssume(String roleName) { if (roleName != null && !roleName.equals("") && awsIamKey != null && awsIamSecret != null && !awsIamKey.equals("") && !awsIamSecret.equals("")) { logger.debug("Creating the AWS Identity Management client"); AmazonIdentityManagementClient identityManagementClient = new AmazonIdentityManagementClient( new BasicAWSCredentials(awsIamKey, awsIamSecret)); logger.debug("Getting role: " + roleName); GetRoleResult roleresult = identityManagementClient .getRole(new GetRoleRequest().withRoleName(roleName)); logger.debug("GetRoleResult: " + roleresult.toString()); Role role = roleresult.getRole(); logger.debug("getRole: " + role.toString()); ListAttachedRolePoliciesResult arpr = identityManagementClient .listAttachedRolePolicies(new ListAttachedRolePoliciesRequest().withRoleName(roleName)); logger.debug("ListAttachedRolePoliciesResult: " + arpr.toString()); ListRolePoliciesResult lrpr = identityManagementClient .listRolePolicies(new ListRolePoliciesRequest().withRoleName(roleName)); logger.debug("ListRolePoliciesResult: " + lrpr.toString()); List<String> inlinePolicies = lrpr.getPolicyNames(); if (inlinePolicies.size() == 0) { logger.debug("There are no inlines policies"); }/*from w ww . jav a2s . c o m*/ List<AttachedPolicy> managedPolicies = arpr.getAttachedPolicies(); if (managedPolicies.size() == 0) { logger.debug("There are no managed policies"); } if (managedPolicies.size() >= 1) //we prioritize managed policies over inline policies { List<String> lstManagedPolicies = new ArrayList<String>(); for (AttachedPolicy managedPolicy : managedPolicies) { lstManagedPolicies.add(managedPolicy.getPolicyName()); } logger.debug("Managed Policies: " + managedPolicies.toString()); //TODO: handle more than 1 policy (ask the user to choose it?) AttachedPolicy attachedPolicy = managedPolicies.get(0); logger.debug("First Attached Policy " + attachedPolicy.toString()); GetPolicyRequest gpr = new GetPolicyRequest().withPolicyArn(attachedPolicy.getPolicyArn()); GetPolicyResult rpr = identityManagementClient.getPolicy(gpr); logger.debug("GetPolicyResult: " + attachedPolicy.toString()); Policy policy = rpr.getPolicy(); GetPolicyVersionResult pvr = identityManagementClient.getPolicyVersion(new GetPolicyVersionRequest() .withPolicyArn(policy.getArn()).withVersionId(policy.getDefaultVersionId())); logger.debug("GetPolicyVersionResult: " + pvr.toString()); String policyDoc = pvr.getPolicyVersion().getDocument(); roleToAssume = ProcessPolicyDocument(policyDoc); } else if (inlinePolicies.size() >= 1) //if we only have one policy, then use it by default { logger.debug("Inline Policies " + inlinePolicies.toString()); if (inlinePolicies.size() > 1) { //there are more than one policy } //Have to set the role name and the policy name (both are mandatory fields //TODO: handle more than 1 policy (ask the user to choose it?) GetRolePolicyRequest grpr = new GetRolePolicyRequest().withRoleName(roleName) .withPolicyName(inlinePolicies.get(0)); GetRolePolicyResult rpr = identityManagementClient.getRolePolicy(grpr); String policyDoc = rpr.getPolicyDocument(); roleToAssume = ProcessPolicyDocument(policyDoc); } } }