Skip to content

Commit a9a342f

Browse files
Merge pull request #582 from boozallen/577-policy-location-undefined
#577 the policies location property can be undefined for encryption policies
2 parents d6d093c + 7964767 commit a9a342f

File tree

8 files changed

+141
-22
lines changed

8 files changed

+141
-22
lines changed

foundation/aissemble-foundation-core-python/src/policy_manager/configuration/policy_configuration.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ def policiesLocation(self) -> str:
2525
Configures the location and file name of the file that contains the
2626
policies.
2727
"""
28-
return self.properties["policies-location"]
28+
try:
29+
return self.properties["policies-location"]
30+
except TypeError:
31+
return None

foundation/foundation-mda/src/main/resources/templates/data-delivery-pyspark/encryption.py.vm

+21-20
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,27 @@
2020

2121
directory = PolicyConfiguration().policiesLocation()
2222

23-
policy_manager = DataEncryptionPolicyManager.getInstance()
24-
retrieved_policies = policy_manager.policies
25-
26-
#if (${step.hasInboundNativeCollectionTypeAndRelations()})
27-
if(len(retrieved_policies.items()) > 0):
28-
raise NotImplementedError("Encryption of records that contain relations is not yet supported.")
29-
#else
30-
for key, encrypt_policy in retrieved_policies.items():
31-
# Encryption policies have a property called encryptPhase.
32-
# If that property is missing then we should ignore the policy.
33-
if encrypt_policy.encryptPhase:
34-
if self.step_phase.lower() == encrypt_policy.encryptPhase.lower():
35-
encrypt_fields = encrypt_policy.encryptFields
36-
input_fields = self.get_fields_list(inbound)
37-
field_intersection = list(set(encrypt_fields) & set(input_fields))
38-
39-
return_payload = self.apply_encryption_to_dataset(inbound, field_intersection, encrypt_policy.encryptAlgorithm)
40-
else:
41-
${step.capitalizedName}Base.logger.info('Encryption policy does not apply to this phase: ' + self.step_phase)
42-
#end
23+
if directory is not None and os.path.isdir(directory):
24+
policy_manager = DataEncryptionPolicyManager.getInstance()
25+
retrieved_policies = policy_manager.policies
26+
27+
#if (${step.hasInboundNativeCollectionTypeAndRelations()})
28+
if(len(retrieved_policies.items()) > 0):
29+
raise NotImplementedError("Encryption of records that contain relations is not yet supported.")
30+
#else
31+
for key, encrypt_policy in retrieved_policies.items():
32+
# Encryption policies have a property called encryptPhase.
33+
# If that property is missing then we should ignore the policy.
34+
if encrypt_policy.encryptPhase:
35+
if self.step_phase.lower() == encrypt_policy.encryptPhase.lower():
36+
encrypt_fields = encrypt_policy.encryptFields
37+
input_fields = self.get_fields_list(inbound)
38+
field_intersection = list(set(encrypt_fields) & set(input_fields))
39+
40+
return_payload = self.apply_encryption_to_dataset(inbound, field_intersection, encrypt_policy.encryptAlgorithm)
41+
else:
42+
${step.capitalizedName}Base.logger.info('Encryption policy does not apply to this phase: ' + self.step_phase)
43+
#end
4344

4445
return return_payload
4546
#end

foundation/foundation-mda/src/main/resources/templates/data-delivery-spark/encryption.java.vm

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ${step.encryptionSignature} {
2020
EncryptionPolicyManager encryptionPolicyManager = EncryptionPolicyManager.getInstance();
2121
String filePath = encryptionPolicyManager.getPoliciesLocation();
2222

23-
if(Files.isDirectory(Paths.get(filePath))) {
23+
if(filePath != null && Files.isDirectory(Paths.get(filePath))) {
2424
Map<String, EncryptionPolicy> policies = encryptionPolicyManager.getEncryptPolicies();
2525

2626
if(!policies.isEmpty()) {

test/test-mda-models/aissemble-test-data-delivery-pyspark-model/tests/features/pyspark_data_encryption.feature

+6
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ Feature: Data encryption
2222
Given a pipeline with native inbound collection and inbound record type
2323
When AES encryption is requested
2424
Then the correct AES algorithm is applied to the data set
25+
26+
Scenario: The policies location property can be undefined for encryption policies
27+
Given a pipeline with an inbound data type
28+
And the policies location property is not defined
29+
When the check and apply encryption method is called
30+
Then the method completes without applying encryption

test/test-mda-models/aissemble-test-data-delivery-pyspark-model/tests/features/steps/pyspark_data_encryption_steps.py

+37
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from behave import given, when, then # pylint: disable=no-name-in-module
1919
import nose.tools as nt
20+
import os
2021
from aissemble_test_data_delivery_pyspark_model.step.native_inbound_with_custom_types import (
2122
NativeInboundWithCustomTypes,
2223
)
@@ -26,6 +27,9 @@
2627
from aissemble_test_data_delivery_pyspark_model.step.native_inbound_with_custom_collection_type import (
2728
NativeInboundWithCustomCollectionType,
2829
)
30+
from aissemble_test_data_delivery_pyspark_model.step.native_inbound_and_outbound import (
31+
NativeInboundAndOutbound,
32+
)
2933
from aissemble_test_data_delivery_pyspark_model.record.custom_data import CustomData
3034
from krausening.logging import LogManager
3135

@@ -83,6 +87,19 @@ def step_impl(context):
8387
)
8488

8589

90+
@given("a pipeline with an inbound data type")
91+
def step_impl(context):
92+
context.pipeline = NativeInboundAndOutbound()
93+
94+
95+
@given("the policies location property is not defined")
96+
def step_impl(context):
97+
# Get the current krausening base dir for restoring after the test
98+
context.default_krausening_base = os.environ.get("KRAUSENING_BASE")
99+
100+
os.environ["KRAUSENING_BASE"] = "invalid/path"
101+
102+
86103
@when("encryption is called on the inbound record")
87104
def step_impl(context):
88105
context.encrypted_dataset = context.pipeline.apply_encryption_to_dataset(
@@ -101,6 +118,15 @@ def step_impl(context):
101118
logger.info(context.encrypted_dataset)
102119

103120

121+
@when("the check and apply encryption method is called")
122+
def step_impl(context):
123+
context.exception = None
124+
try:
125+
context.pipeline.check_and_apply_encryption_policy(None)
126+
except Exception as e:
127+
context.exception = e
128+
129+
104130
@then("the correct fields are encrypted")
105131
def step_impl(context):
106132
for record in context.encrypted_dataset:
@@ -227,3 +253,14 @@ def step_impl(context):
227253
logger.info(
228254
"Set[(dataframe)] - The Vault encrypted value is: " + encrypted_field
229255
)
256+
257+
258+
@then("the method completes without applying encryption")
259+
def step_impl(context):
260+
nt.ok_(
261+
context.exception is None,
262+
"An exception was thrown",
263+
)
264+
265+
# Restore the krausening base dir to the value from before the test
266+
os.environ["KRAUSENING_BASE"] = context.default_krausening_base

test/test-mda-models/test-data-delivery-spark-model/pom.xml

+2
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@
3636
<exclude>test/java/com/boozallen/aiops/mda/pattern/RelationTest.java</exclude>
3737
<exclude>test/java/com/boozallen/aiops/mda/pattern/RelationValidationTest.java</exclude>
3838
<exclude>test/java/com/boozallen/aiops/mda/pattern/SparkSchemaTest.java</exclude>
39+
<exclude>test/java/com/boozallen/aiops/mda/pattern/EncryptionTest.java</exclude>
3940
<exclude>test/resources/config/</exclude>
4041
<exclude>test/resources/specifications/record.feature</exclude>
4142
<exclude>test/resources/specifications/lineage.feature</exclude>
4243
<exclude>test/resources/specifications/relation.feature</exclude>
4344
<exclude>test/resources/specifications/relation-validation.feature</exclude>
4445
<exclude>test/resources/specifications/sparkSchema.feature</exclude>
46+
<exclude>test/resources/specifications/encryption.feature</exclude>
4547
<exclude>test/resources/krausening/base/</exclude>
4648
<exclude>test/resources/apps</exclude>
4749
</excludes>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.boozallen.aiops.mda.pattern;
2+
3+
/*-
4+
* #%L
5+
* aiSSEMBLE::Test::MDA::Data Delivery Spark
6+
* %%
7+
* Copyright (C) 2021 Booz Allen
8+
* %%
9+
* This software package is licensed under the Booz Allen Public License. All Rights Reserved.
10+
* #L%
11+
*/
12+
13+
import io.cucumber.java.en.Given;
14+
import io.cucumber.java.en.Then;
15+
import io.cucumber.java.en.When;
16+
17+
import org.technologybrewery.krausening.Krausening;
18+
19+
import static org.junit.Assert.assertNull;
20+
21+
/**
22+
* Implementation steps for encryption.feature
23+
*/
24+
public class EncryptionTest {
25+
private String defaultKrauseningBaseDir;
26+
private NativeInboundAndOutbound pipeline;
27+
private Exception exception;
28+
private Krausening krausening = Krausening.getInstance();
29+
30+
@Given("a pipeline with an inbound data type")
31+
public void a_pipeline_with_an_inbound_data_type() {
32+
this.pipeline = new NativeInboundAndOutbound();
33+
}
34+
35+
@Given("the policies location property is not defined")
36+
public void the_policies_location_property_is_not_defined() {
37+
// Get the current krausening base dir for restoring after the test
38+
this.defaultKrauseningBaseDir = System.getProperty("KRAUSENING_BASE");
39+
40+
// Force krausening to reload with the new base dir
41+
System.setProperty("KRAUSENING_BASE", "invalid/path");
42+
this.krausening.loadProperties();
43+
}
44+
45+
@When("the check and apply encryption method is called")
46+
public void the_check_and_apply_encryption_method_is_called() {
47+
try {
48+
this.pipeline.checkAndApplyEncryptionPolicy(null);
49+
} catch (Exception e) {
50+
this.exception = e;
51+
}
52+
}
53+
54+
@Then("the method completes without applying encryption")
55+
public void the_method_completes_without_applying_encryption() {
56+
assertNull("An exception was thrown", this.exception);
57+
58+
// Restore the krausening base dir to the value from before the test
59+
System.setProperty("KRAUSENING_BASE", this.defaultKrauseningBaseDir);
60+
this.krausening.loadProperties();
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@encryption
2+
Feature: Step encryption are generated correctly and function as expected
3+
4+
Scenario: The policies location property can be undefined for encryption policies
5+
Given a pipeline with an inbound data type
6+
And the policies location property is not defined
7+
When the check and apply encryption method is called
8+
Then the method completes without applying encryption

0 commit comments

Comments
 (0)