alopresto commented on a change in pull request #3542: NIFI-6363 Integrates AWS KMS SPP. Refactors SSPP. URL: https://github.com/apache/nifi/pull/3542#discussion_r296360246
########## File path: nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/test/groovy/org/apache/nifi/properties/sensitive/aws/kms/AWSKMSSensitivePropertyProviderIT.groovy ########## @@ -0,0 +1,253 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.properties.sensitive.aws.kms + +import com.amazonaws.auth.PropertiesCredentials +import com.amazonaws.services.kms.AWSKMSClient +import com.amazonaws.services.kms.AWSKMSClientBuilder +import com.amazonaws.services.kms.model.CreateAliasRequest +import com.amazonaws.services.kms.model.CreateKeyRequest +import com.amazonaws.services.kms.model.CreateKeyResult +import com.amazonaws.services.kms.model.DescribeKeyRequest +import com.amazonaws.services.kms.model.DescribeKeyResult +import com.amazonaws.services.kms.model.GenerateDataKeyRequest +import com.amazonaws.services.kms.model.GenerateDataKeyResult +import com.amazonaws.services.kms.model.ScheduleKeyDeletionRequest +import org.apache.nifi.properties.StandardNiFiProperties +import org.apache.nifi.properties.sensitive.ProtectedNiFiProperties +import org.apache.nifi.properties.sensitive.SensitivePropertyProtectionException +import org.apache.nifi.properties.sensitive.SensitivePropertyProvider +import org.apache.nifi.util.NiFiProperties +import org.junit.After +import org.junit.AfterClass +import org.junit.Before +import org.junit.BeforeClass +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.slf4j.Logger +import org.slf4j.LoggerFactory + +import java.security.SecureRandom + + +@RunWith(JUnit4.class) +class AWSKMSSensitivePropertyProviderIT extends GroovyTestCase { + private static final Logger logger = LoggerFactory.getLogger(AWSKMSSensitivePropertyProviderIT.class) + protected final static String CREDENTIALS_FILE = System.getProperty("user.home") + "/aws-credentials.properties"; + private static String[] knownGoodKeys = [] + private static AWSKMSClient client + + /** + * This method creates a CMK, DEK, and an alias to that DEK for exercising the AWS KMS calls. + * + * @throws Exception + */ + @BeforeClass + static void setUpOnce() throws Exception { + final FileInputStream fis + try { + fis = new FileInputStream(CREDENTIALS_FILE) + } catch (FileNotFoundException e1) { + fail("Could not open credentials file " + CREDENTIALS_FILE + ": " + e1.getLocalizedMessage()); + return + } + final PropertiesCredentials credentials = new PropertiesCredentials(fis) + + // We're copying the properties directly so the standard builder works. + System.setProperty("aws.accessKeyId", credentials.AWSAccessKeyId) + System.setProperty("aws.secretKey", credentials.AWSSecretKey) + System.setProperty("aws.region", "us-east-2") + + client = AWSKMSClientBuilder.standard().build() as AWSKMSClient + + // generate a cmk + CreateKeyRequest cmkRequest = new CreateKeyRequest().withDescription("CMK for unit tests") + CreateKeyResult cmkResult = client.createKey(cmkRequest) + + // from the cmk, generate a dek + GenerateDataKeyRequest dekRequest = new GenerateDataKeyRequest().withKeyId(cmkResult.keyMetadata.getKeyId()).withKeySpec("AES_128") + GenerateDataKeyResult dekResult = client.generateDataKey(dekRequest) + + // add an alias to the dek + final String aliasName = "alias/aws-kms-spp-integration-test-" + UUID.randomUUID().toString() + CreateAliasRequest aliasReq = new CreateAliasRequest().withAliasName(aliasName).withTargetKeyId(dekResult.getKeyId()) + client.createAlias(aliasReq) + + // re-read the dek so we have the arn + DescribeKeyRequest descRequest = new DescribeKeyRequest().withKeyId(dekResult.getKeyId()) + DescribeKeyResult descResult = client.describeKey(descRequest) + + knownGoodKeys = [ + dekResult.getKeyId(), + descResult.keyMetadata.getArn(), + aliasName + ] + } + + @Before + void setUp() throws Exception { + } + + @After + void tearDown() throws Exception { + } + + /** + * This method schedules the deletion of the CMK created during setup. The delete will cascade to the DEK and DEK alias. + */ + @AfterClass + static void tearDownOnce() { + if (knownGoodKeys.size() > 0) { + ScheduleKeyDeletionRequest req = new ScheduleKeyDeletionRequest().withKeyId(knownGoodKeys[0]).withPendingWindowInDays(7) + client.scheduleKeyDeletion(req) + } + } Review comment: Would be good to delete the system properties containing the AWS credentials here, with a flag that indicated if they were overwritten during `#setUpOnce()`. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services