-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start working on a simple vault integration
- Loading branch information
1 parent
1cbcfe2
commit 2d4da8a
Showing
6 changed files
with
103 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from typing import Optional | ||
from presidio_analyzer.analyzer_engine import AnalyzerEngine | ||
from presidio_anonymizer.anonymizer_engine import AnonymizerEngine | ||
from config.nlp_engine_config import FlairNLPEngine | ||
|
||
NLP_ENGINE = "flair/ner-english-large" | ||
|
||
def text_analyzer(text, language): | ||
nlp_engine = FlairNLPEngine(NLP_ENGINE) | ||
nlp_engine, registry = nlp_engine.create_nlp_engine() | ||
engine = AnalyzerEngine(registry=registry, nlp_engine=nlp_engine) | ||
|
||
return engine.analyze( | ||
text=text, | ||
language=language | ||
) | ||
|
||
|
||
def text_anonymizer(text: str, analyzer_results, operators: Optional[dict] = None): | ||
anonymizer = AnonymizerEngine() | ||
return anonymizer.anonymize(text, analyzer_results, operators) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from text.text import text_analyzer, text_anonymizer | ||
from presidio_anonymizer.entities import OperatorConfig | ||
import base64 | ||
import hvac | ||
import sys | ||
|
||
|
||
def base64ify(bytes_or_str): | ||
"""Helper method to perform base64 encoding across Python 2.7 and Python 3.X""" | ||
if isinstance(bytes_or_str, str): | ||
input_bytes = bytes_or_str.encode('utf8') | ||
else: | ||
input_bytes = bytes_or_str | ||
|
||
output_bytes = base64.urlsafe_b64encode(input_bytes) | ||
return output_bytes.decode('ascii') | ||
|
||
VAULT_URL = "https://127.0.0.1:8200" | ||
|
||
def vault_encrypt(text): | ||
print(f'plaintext is: {text} and {text.__class__}') | ||
print(f'b64 plaintext is: {base64.b64encode(text.encode())}') | ||
client = hvac.Client(url=VAULT_URL) | ||
|
||
encrypt_data_response = client.secrets.transit.encrypt_data( | ||
name='orders', | ||
plaintext=base64ify(text.encode()), | ||
) | ||
print(f"Response: {encrypt_data_response}") | ||
|
||
ciphertext = encrypt_data_response['data']['ciphertext'] | ||
print(f'Encrypted plaintext ciphertext is: {ciphertext}') | ||
return ciphertext | ||
|
||
|
||
vault_encrypt("PII") | ||
|
||
# operators = {"DEFAULT": OperatorConfig("custom", {"lambda": vault_encrypt})} | ||
# t = "Hi my name is Qwerty and I live in London. My number is 07440 123456." | ||
# res = text_analyzer(t, "en") | ||
# anon_res = text_anonymizer(t, res, operators) | ||
|
||
# print(anon_res) |