Odyssey
Functions | Variables
aws_get_certificate Namespace Reference

Functions

def __derive_key__ (password, salt=None)
 
def __decrypt_data__ (password, salt, data)
 
def main (certificate_path, secret_id, region=None)
 
def make_directory (directory_name, mode=0o700)
 
def get_password_from_sm (secret_id, region)
 

Variables

string SALT = 'LTzkm1w/p3ReDm9kmfmnwQ=='
 
string ENCODING = 'utf-8'
 
int AES_KEY_BYTES = 32
 
 AWS_REGION = os.environ.get('AWS_REGION', 'us-east-2')
 
 CERTIFICATE_OUTPUT_DIR
 

Detailed Description

Retrieve Certificates and Private Keys from AWS

Query Amazon Certificate Manager and Secrets Manager to query for both
the certificate, certificate chain, and the private key.  This results
in a reconstruction of a typical PEM file.

Function Documentation

◆ __decrypt_data__()

def aws_get_certificate.__decrypt_data__ (   password,
  salt,
  data 
)
Decrypt given data using password and PBKDF2 key stretching

Definition at line 37 of file aws_get_certificate.py.

37 def __decrypt_data__(password, salt, data):
38  '''Decrypt given data using password and PBKDF2 key stretching'''
39 
40  key = __derive_key__(password, salt)
41  aes_cipher = AES.new(key, AES.MODE_CTR, counter=Counter.new(128))
42  return aes_cipher.decrypt(data).decode(ENCODING)
43 
44 

◆ __derive_key__()

def aws_get_certificate.__derive_key__ (   password,
  salt = None 
)
Return key using PBKDF2

Definition at line 26 of file aws_get_certificate.py.

26 def __derive_key__(password, salt=None):
27  '''Return key using PBKDF2'''
28  if not salt:
29  salt = SALT
30  return PBKDF2(
31  password,
32  salt.encode(ENCODING),
33  AES_KEY_BYTES
34  )
35 
36 

◆ main()

def aws_get_certificate.main (   certificate_path,
  secret_id,
  region = None 
)
Download and decrypt certificate file from EFS

Use AWS Secrets Manager to download shared key for decryption.

Arguments:

- `certificate_path`: Absolute path to encrypted certificate (PEM)
  file.  The basename of this file is used for the output of the
  unencrypted certificate contents.

- `secret_id`: AWS Secrets Manager secret identifier.  This is
  often referred to as the "secrets path" in conversation.  E.g.,
  `test/certs/rdc/foobar.homecu.io.pem`.  The `secret_id` will
  also be used for the path and name of the unencrypted certificate file.

- `region`: AWS Region to download secrets from, defaults to
  environment variable `AWS_REGION`.

Definition at line 45 of file aws_get_certificate.py.

45 def main(certificate_path, secret_id, region=None):
46  '''Download and decrypt certificate file from EFS
47 
48  Use AWS Secrets Manager to download shared key for decryption.
49 
50  Arguments:
51 
52  - `certificate_path`: Absolute path to encrypted certificate (PEM)
53  file. The basename of this file is used for the output of the
54  unencrypted certificate contents.
55 
56  - `secret_id`: AWS Secrets Manager secret identifier. This is
57  often referred to as the "secrets path" in conversation. E.g.,
58  `test/certs/rdc/foobar.homecu.io.pem`. The `secret_id` will
59  also be used for the path and name of the unencrypted certificate file.
60 
61  - `region`: AWS Region to download secrets from, defaults to
62  environment variable `AWS_REGION`.
63 
64  '''
65 
66  assert os.path.exists(certificate_path)
67  if not region:
68  region = AWS_REGION
69  secrets = get_password_from_sm(secret_id, region)
70  salt = secrets['salt']
71  password = secrets['password']
72 
73  with open(certificate_path, 'rb') as fh:
74  encrypted_certificate = fh.read()
75 
76  certificate = __decrypt_data__(password, salt, encrypted_certificate)
77 
78  certificate_output_name = os.path.join(CERTIFICATE_OUTPUT_DIR,
79  secret_id)
80 
81  make_directory(os.path.dirname(certificate_output_name))
82 
83  with open(certificate_output_name, 'w') as fh:
84  fh.write(certificate)
85 
86 

Variable Documentation

◆ CERTIFICATE_OUTPUT_DIR

aws_get_certificate.CERTIFICATE_OUTPUT_DIR
Initial value:
1 = os.environ.get('CERTIFICATE_OUTPUT_DIR',
2  '/tmp/odyssey')

Definition at line 22 of file aws_get_certificate.py.