Skip to content

Commit

Permalink
WIP: add deanonymize endpoint to api
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaykarle committed Oct 9, 2024
1 parent 39ef094 commit 8b3747a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ def anonymize() -> Response:
)
return jsonify(error=e.args[0]), 500

@self.app.route("/deanonymize", methods=["POST"])
def deanonymize() -> Response:
"""Execute the deanonymizer function."""
try:
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400

filepath = f'{UPLOAD_DIR}/{uuid.uuid4()}'
file.save(filepath)
self.logger.info(f"Successfully saved file: {filepath}")

anonymizer_results = json.loads(request.form['anonymizer_results'])
return jsonify(anonymizer_results)
except Exception as e:
self.logger.error(
f"A fatal error occurred during execution of "
f"deanonymize. {e}"
)
return jsonify(error=e.args[0]), 500


if __name__ == "__main__":
port = int(os.environ.get("PORT", DEFAULT_PORT))
Expand Down
18 changes: 18 additions & 0 deletions tests/app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,21 @@ def test_vault_anonymize_csv_pii(client):
"./tests/sample_data/vault_encrypted.csv", "r"
).read()
assert anonymizer_data.replace("\r", "") == expected_anonymized_data

def test_vault_deanonymize_csv(client):
with mock.patch.object(hvac, "Client"):
expected_plain_text = "text"
fake_client = mock.MagicMock()
fake_client.secrets.transit.decrypt_data.return_value = {"data": {"plaintext": expected_plain_text}}
hvac.Client.return_value = fake_client

deanonymizer_response = client.post("/deanonymize", data={
"file": open('./tests/sample_data/vault_encrypted.csv', 'rb'),
"vault_config": '{"url": "http://127.0.0.1:8200", "key": "foobar"}',
"anonymizer_results": '{}'
})

assert deanonymizer_response.status_code == 200
deanonymizer_data = deanonymizer_response.get_data(as_text=True)
expected_deanonymized_data = open('./tests/sample_data/sample_data.csv', 'r').read()
assert deanonymizer_data.replace("\r", "") == expected_deanonymized_data

0 comments on commit 8b3747a

Please sign in to comment.