Need help for Client side encryption to work on snowflake

Viewed 320

We are trying to perform aws client side encryption and use the base 64 encoded key to create the snowflake stage to decrypt the file and load into the table via copy into command, but we are not able to make it work. We see that the encrypted file reaches the AWS s3 , but when we use the copy command from snowflake stage , it is not able to decrypt it.

Following is the java code to encrypt the content and load.

public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
        // TODO Auto-generated method stub
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(256);
        
        SecretKey Key = keyGen.generateKey();  //MASTER-KEY



String encodedKey = Base64.getEncoder().encodeToString(key.getEncoded());

        System.out.println(encodedKey + " Encoded");

SvzvMWPJtNNKlVpA1n/p//rv3YJjfNCb5s+oknX5YQQ=   // I keep the the base 64 encoded key stored in snowflake stage as MASTER_KEY        
        
        String s3ObjectKey = "emp_enc.csv";
        String s3ObjectContent = "1,PPK,2020";
        AmazonS3EncryptionV2 s3EncryptionClientV2 = AmazonS3EncryptionClientV2Builder.standard()
                .withRegion("us-east-1")
                .withClientConfiguration(new ClientConfiguration())
                .withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode(CryptoMode.AuthenticatedEncryption))
                .withEncryptionMaterialsProvider(new StaticEncryptionMaterialsProvider(new EncryptionMaterials(Key)))
                .build();

        s3EncryptionClientV2.putObject("s3-bucket-name/testing", s3ObjectKey, s3ObjectContent);
        
        
        
        
    }

    Following is the snowflake code to create the external stage to decrypt. 
    create stage encrypted_customer_stage
url='s3-bucket-name/testing'
credentials=(aws _key_id='ABCDEFGH' aws_secret_key='12345678')
encryption=(master_key='SvzvMWPJtNNKlVpA1n/p//rv3YJjfNCb5s+oknX5YQQ=');

create table users (id bigint, name varchar(500), purchases int);
copy into users from @encrypted_customer_stage;
0 Answers
Related