Example usage for com.amazonaws.services.s3 AmazonS3EncryptionClient AmazonS3EncryptionClient

List of usage examples for com.amazonaws.services.s3 AmazonS3EncryptionClient AmazonS3EncryptionClient

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3EncryptionClient AmazonS3EncryptionClient.

Prototype

@Deprecated
public AmazonS3EncryptionClient(AWSCredentialsProvider credentialsProvider,
        EncryptionMaterialsProvider encryptionMaterialsProvider) 

Source Link

Document

Constructs a new Amazon S3 Encryption client using the specified AWS credentials to access Amazon S3.

Usage

From source file:S3ClientSideEncryptionWithSymmetricMasterKey.java

License:Apache License

public static void main(String[] args) throws Exception {
    SecretKey mySymmetricKey = loadSymmetricAESKey(masterKeyDir, "AES");

    EncryptionMaterials encryptionMaterials = new EncryptionMaterials(mySymmetricKey);

    AWSCredentials credentials = new BasicAWSCredentials("Q3AM3UQ867SPQQA43P2F",
            "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
    AmazonS3EncryptionClient encryptionClient = new AmazonS3EncryptionClient(credentials,
            new StaticEncryptionMaterialsProvider(encryptionMaterials));
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    encryptionClient.setRegion(usEast1);
    encryptionClient.setEndpoint("https://play.minio.io:9000");

    final S3ClientOptions clientOptions = S3ClientOptions.builder().setPathStyleAccess(true).build();
    encryptionClient.setS3ClientOptions(clientOptions);

    // Create the bucket
    encryptionClient.createBucket(bucketName);

    // Upload object using the encryption client.
    byte[] plaintext = "Hello World, S3 Client-side Encryption Using Asymmetric Master Key!".getBytes();
    System.out.println("plaintext's length: " + plaintext.length);
    encryptionClient.putObject(new PutObjectRequest(bucketName, objectKey, new ByteArrayInputStream(plaintext),
            new ObjectMetadata()));

    // Download the object.
    S3Object downloadedObject = encryptionClient.getObject(bucketName, objectKey);
    byte[] decrypted = IOUtils.toByteArray(downloadedObject.getObjectContent());

    // Verify same data.
    Assert.assertTrue(Arrays.equals(plaintext, decrypted));
    //deleteBucketAndAllContents(encryptionClient);
}

From source file:S3ClientSideEncryptionAsymmetricMasterKey.java

License:Apache License

public static void main(String[] args) throws Exception {

    // 1. Load keys from files
    byte[] bytes = FileUtils.readFileToByteArray(new File(keyDir + "/private.key"));
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(bytes);
    PrivateKey pk = kf.generatePrivate(ks);

    bytes = FileUtils.readFileToByteArray(new File(keyDir + "/public.key"));
    PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes));

    KeyPair loadedKeyPair = new KeyPair(publicKey, pk);

    // 2. Construct an instance of AmazonS3EncryptionClient.
    EncryptionMaterials encryptionMaterials = new EncryptionMaterials(loadedKeyPair);
    AWSCredentials credentials = new BasicAWSCredentials("Q3AM3UQ867SPQQA43P2F",
            "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
    AmazonS3EncryptionClient encryptionClient = new AmazonS3EncryptionClient(credentials,
            new StaticEncryptionMaterialsProvider(encryptionMaterials));
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    encryptionClient.setRegion(usEast1);
    encryptionClient.setEndpoint("https://play.minio.io:9000");

    final S3ClientOptions clientOptions = S3ClientOptions.builder().setPathStyleAccess(true).build();
    encryptionClient.setS3ClientOptions(clientOptions);

    // Create the bucket
    encryptionClient.createBucket(bucketName);
    // 3. Upload the object.
    byte[] plaintext = "Hello World, S3 Client-side Encryption Using Asymmetric Master Key!".getBytes();
    System.out.println("plaintext's length: " + plaintext.length);
    encryptionClient.putObject(new PutObjectRequest(bucketName, objectKey, new ByteArrayInputStream(plaintext),
            new ObjectMetadata()));

    // 4. Download the object.
    S3Object downloadedObject = encryptionClient.getObject(bucketName, objectKey);
    byte[] decrypted = IOUtils.toByteArray(downloadedObject.getObjectContent());
    Assert.assertTrue(Arrays.equals(plaintext, decrypted));
    System.out.println("decrypted length: " + decrypted.length);
    //deleteBucketAndAllContents(encryptionClient);
}

From source file:com.emc.vipr.services.s3.S3ClientFactory.java

License:Open Source License

/**
 * Creates an EncryptionClient for testing.  Loads the public and private keys from
 * the properties file (not suitable for production).
 *
 * @return//from w  ww  .  java2  s .com
 * @throws IOException
 */
public static AmazonS3EncryptionClient getEncryptionClient() throws IOException {
    try {
        Properties props = ViprConfig.getProperties();

        String accessKey = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_S3_ACCESS_KEY_ID);
        String secretKey = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_S3_SECRET_KEY);
        String endpoint = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_S3_ENDPOINT);
        String publicKey = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_PUBLIC_KEY);
        String privateKey = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_PRIVATE_KEY);

        byte[] pubKeyBytes = Base64.decodeBase64(publicKey.getBytes("US-ASCII"));
        byte[] privKeyBytes = Base64.decodeBase64(privateKey.getBytes("US-ASCII"));

        X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyBytes);
        PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privKeyBytes);

        PublicKey pubKey;
        PrivateKey privKey;
        try {
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            pubKey = keyFactory.generatePublic(pubKeySpec);
            privKey = keyFactory.generatePrivate(privKeySpec);
        } catch (GeneralSecurityException e) {
            throw new RuntimeException("Could not load key pair: " + e, e);
        }

        EncryptionMaterials keys = new EncryptionMaterials(new KeyPair(pubKey, privKey));

        BasicAWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey);
        AmazonS3EncryptionClient client = new AmazonS3EncryptionClient(creds, keys);
        client.setEndpoint(endpoint);

        checkProxyConfig(client, props);

        return client;
    } catch (Exception e) {
        log.info("Could not load configuration: " + e);
        return null;
    }
}

From source file:com.intuit.s3encrypt.S3Encrypt.java

License:Open Source License

public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {

    // create Options object
    Options options = new Options();
    options.addOption(create_bucket);/*from   ww w  .j  a v  a2  s.c  o  m*/
    options.addOption(create_key);
    options.addOption(delete_bucket);
    options.addOption(get);
    options.addOption(help);
    options.addOption(inspect);
    options.addOption(keyfile);
    options.addOption(list_buckets);
    options.addOption(list_objects);
    options.addOption(put);
    options.addOption(remove);
    options.addOption(rotate);
    options.addOption(rotateall);
    options.addOption(rotateKey);

    //      CommandLineParser parser = new GnuParser();
    //       Changed from above GnuParser to below PosixParser because I found code which allows for multiple arguments 
    PosixParser parser = new PosixParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(options, args);
        Logger.getRootLogger().setLevel(Level.OFF);

        if (cmd.hasOption("help")) {
            HelpFormatter help = new HelpFormatter();
            System.out.println();
            help.printHelp("S3Encrypt", options);
            System.out.println();
            System.exit(1);
        } else if (cmd.hasOption("create_key")) {
            keyname = cmd.getOptionValue("keyfile");
            createKeyFile(keyname);
            key = new File(keyname);
        } else {
            if (cmd.hasOption("keyfile")) {
                keyname = cmd.getOptionValue("keyfile");
            }
            key = new File(keyname);
        }

        if (!(key.exists())) {
            System.out.println("Key does not exist or not provided");
            System.exit(1);
        }

        //         AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
        ClasspathPropertiesFileCredentialsProvider credentials = new ClasspathPropertiesFileCredentialsProvider(
                ".s3encrypt");
        EncryptionMaterials encryptionMaterials = new EncryptionMaterials(getKeyFile(keyname));
        AmazonS3EncryptionClient s3 = new AmazonS3EncryptionClient(credentials.getCredentials(),
                encryptionMaterials);
        //          Region usWest2 = Region.getRegion(Regions.US_WEST_2);
        //          s3.setRegion(usWest2);

        if (cmd.hasOption("create_bucket")) {
            String bucket = cmd.getOptionValue("create_bucket");
            System.out.println("Creating bucket " + bucket + "\n");
            s3.createBucket(bucket);
        } else if (cmd.hasOption("delete_bucket")) {
            String bucket = cmd.getOptionValue("delete_bucket");
            System.out.println("Deleting bucket " + bucket + "\n");
            s3.deleteBucket(bucket);
        } else if (cmd.hasOption("get")) {
            String[] searchArgs = cmd.getOptionValues("get");
            String bucket = searchArgs[0];
            String filename = searchArgs[1];
            getS3Object(cmd, s3, bucket, filename);
        } else if (cmd.hasOption("inspect")) {
            String[] searchArgs = cmd.getOptionValues("inspect");
            String bucket = searchArgs[0];
            String filename = searchArgs[1];
            String keyname = "encryption_key";
            String metadata = inspectS3Object(cmd, s3, bucket, filename, keyname);
            System.out.println(metadata);
        } else if (cmd.hasOption("list_buckets")) {
            System.out.println("Listing buckets");
            for (Bucket bucket : s3.listBuckets()) {
                System.out.println(bucket.getName());
            }
            System.out.println();
        } else if (cmd.hasOption("list_objects")) {
            String bucket = cmd.getOptionValue("list_objects");
            System.out.println("Listing objects");
            ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName(bucket));
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                System.out.println(objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
            }
            System.out.println();
        } else if (cmd.hasOption("put")) {
            String[] searchArgs = cmd.getOptionValues("put");
            String bucket = searchArgs[0];
            String filename = searchArgs[1];
            String metadataKeyname = "encryption_key";
            String key = keyname;
            putS3Object(cmd, s3, bucket, filename, metadataKeyname, key);
        } else if (cmd.hasOption("remove")) {
            String[] searchArgs = cmd.getOptionValues("remove");
            String bucket = searchArgs[0];
            String filename = searchArgs[1];
            System.out.println("Removing object in S3 from BUCKET = " + bucket + " FILENAME = " + filename);
            s3.deleteObject(new DeleteObjectRequest(bucket, filename));
            System.out.println();
        } else if (cmd.hasOption("rotate")) {
            String[] searchArgs = cmd.getOptionValues("rotate");
            String bucket = searchArgs[0];
            String filename = searchArgs[1];
            String key1 = cmd.getOptionValue("keyfile");
            String key2 = cmd.getOptionValue("rotateKey");
            String metadataKeyname = "encryption_key";
            System.out.println("Supposed to get object from here OPTION VALUE = " + bucket + " FILENAME = "
                    + filename + " KEY1 = " + key1 + " KEY2 = " + key2);

            EncryptionMaterials rotateEncryptionMaterials = new EncryptionMaterials(getKeyFile(key2));
            AmazonS3EncryptionClient rotateS3 = new AmazonS3EncryptionClient(credentials.getCredentials(),
                    rotateEncryptionMaterials);

            getS3Object(cmd, s3, bucket, filename);
            putS3Object(cmd, rotateS3, bucket, filename, metadataKeyname, key2);
        } else if (cmd.hasOption("rotateall")) {
            String[] searchArgs = cmd.getOptionValues("rotateall");
            String bucket = searchArgs[0];
            String key1 = searchArgs[1];
            String key2 = searchArgs[2];
            System.out.println("Supposed to rotateall here for BUCKET NAME = " + bucket + " KEY1 = " + key1
                    + " KEY2 = " + key2);
        } else {
            System.out.println("Something went wrong... ");
            System.exit(1);
        }

    } catch (ParseException e) {
        e.printStackTrace();
    } 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.zotoh.cloudapi.aws.AWSCloud.java

License:Open Source License

private void createAWSClients(Properties ps) {
    AWSCredentials cc = new BasicAWSCredentials(ps.getProperty(P_ID), ps.getProperty(P_PWD));
    AmazonWebServiceClient c;//from  w w  w  .j a  va  2 s . c  om

    _WEB.put("ec2", new AmazonEC2Client(cc));

    _WEB.put("s3", new AmazonS3Client(cc));

    // SIMPLE-DB
    c = new AmazonSimpleDBClient(cc);
    _WEB.put("sdb", c);
    c.setEndpoint("sdb.amazonaws.com");

    // LOAD BALANCER
    c = new AmazonElasticLoadBalancingClient(cc);
    _WEB.put("elb", c);
    c.setEndpoint("elasticloadbalancing.amazonaws.com");

    _WEB.put("cloudwatch", new AmazonCloudWatchClient(cc));
    _WEB.put("autoscale", new AmazonAutoScalingClient(cc));

    // NOTIFICATION
    c = new AmazonSNSClient(cc);
    _WEB.put("sns", c);
    c.setEndpoint("sns.us-east-1.amazonaws.com");

    _WEB.put("sqs", new AmazonSQSClient(cc));
    _WEB.put("rds", new AmazonRDSClient(cc));
    _WEB.put("s3s", new AmazonS3EncryptionClient(cc, new EncryptionMaterials((KeyPair) null)));

}