Skip to content

Commit

Permalink
Merge remote-tracking branch 'avoidik/feature-compressed-key'
Browse files Browse the repository at this point in the history
Also cleaned up and added bz2 support to Netflix#67 .
  • Loading branch information
russell-lewis committed Jul 26, 2018
2 parents a9ad291 + 3f37e17 commit b685728
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 12 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ def lambda_handler(event, context):
- Provide your desired ./lambda_configs/ca_key_name.pem prior to Publishing a new Lambda .zip
- Set the permissions of ./lambda_configs/ca_key_name.pem to 444.

You can now provide your private key and/or encrypted private key password via the lambda environment or config file.
In the `[Bless CA]` section, you can set `ca_private_key` instead of the `ca_private_key_file` with a base64 encoded
version of your .pem (e.g. `cat key.pem | base64` ).

Because every config file option is supported in the environment, you can also just set `bless_ca_default_password`
and/or `bless_ca_ca_private_key`. Due to limits on AWS Lambda environment variables, you'll need to compress RSA 4096
private keys, which you can now do by setting `bless_ca_ca_private_key_compression`. For example, set
`bless_ca_ca_private_key_compression = bz2` and `bless_ca_ca_private_key` to the output of
`cat ca-key.pem | bzip2 | base64`.

### BLESS Config File
- Refer to the the [Example BLESS Config File](bless/config/bless_deploy_example.cfg) and its
included documentation.
Expand Down
42 changes: 38 additions & 4 deletions bless/config/bless_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import base64
import os
import re
import zlib
import bz2

BLESS_OPTIONS_SECTION = 'Bless Options'
CERTIFICATE_VALIDITY_BEFORE_SEC_OPTION = 'certificate_validity_before_seconds'
Expand Down Expand Up @@ -36,6 +38,8 @@
BLESS_CA_SECTION = 'Bless CA'
CA_PRIVATE_KEY_FILE_OPTION = 'ca_private_key_file'
CA_PRIVATE_KEY_OPTION = 'ca_private_key'
CA_PRIVATE_KEY_COMPRESSION_OPTION = 'ca_private_key_compression'
CA_PRIVATE_KEY_COMPRESSION_OPTION_DEFAULT = None

REGION_PASSWORD_OPTION_SUFFIX = '_password'

Expand Down Expand Up @@ -96,11 +100,15 @@ def __init__(self, aws_region, config_file):
REMOTE_USERNAMES_VALIDATION_OPTION: REMOTE_USERNAMES_VALIDATION_DEFAULT,
VALIDATE_REMOTE_USERNAMES_AGAINST_IAM_GROUPS_OPTION: VALIDATE_REMOTE_USERNAMES_AGAINST_IAM_GROUPS_DEFAULT,
IAM_GROUP_NAME_VALIDATION_FORMAT_OPTION: IAM_GROUP_NAME_VALIDATION_FORMAT_DEFAULT,
REMOTE_USERNAMES_BLACKLIST_OPTION: REMOTE_USERNAMES_BLACKLIST_DEFAULT
REMOTE_USERNAMES_BLACKLIST_OPTION: REMOTE_USERNAMES_BLACKLIST_DEFAULT,
CA_PRIVATE_KEY_COMPRESSION_OPTION: CA_PRIVATE_KEY_COMPRESSION_OPTION_DEFAULT
}
configparser.RawConfigParser.__init__(self, defaults=defaults)
self.read(config_file)

if not self.has_section(BLESS_CA_SECTION):
self.add_section(BLESS_CA_SECTION)

if not self.has_section(BLESS_OPTIONS_SECTION):
self.add_section(BLESS_OPTIONS_SECTION)

Expand Down Expand Up @@ -129,14 +137,21 @@ def getkmsauthkeyids(self):
return list(map(str.strip, self.get(KMSAUTH_SECTION, KMSAUTH_KEY_ID_OPTION).split(',')))

def getprivatekey(self):
"""
Get a private key from either a file specified in the config file, or from an environment variable. Env
Vars in Lambda can't contain a 4096 RSA key uncompressed, so compressed keys are also supported.
:return: byte string that contains the private key in PEM format (ascii).
"""
compression = self.get(BLESS_CA_SECTION, CA_PRIVATE_KEY_COMPRESSION_OPTION)

if self.has_option(BLESS_CA_SECTION, CA_PRIVATE_KEY_OPTION):
return base64.b64decode(self.get(BLESS_CA_SECTION, CA_PRIVATE_KEY_OPTION))
return self._decompress(base64.b64decode(self.get(BLESS_CA_SECTION, CA_PRIVATE_KEY_OPTION)), compression)

ca_private_key_file = self.get(BLESS_CA_SECTION, CA_PRIVATE_KEY_FILE_OPTION)

# read the private key .pem
with open(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, ca_private_key_file), 'r') as f:
return f.read().encode('ascii')
with open(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, ca_private_key_file), 'rb') as f:
return self._decompress(f.read(), compression)

def has_option(self, section, option):
"""
Expand Down Expand Up @@ -171,3 +186,22 @@ def get(self, section, option, **kwargs):
@staticmethod
def _environment_key(section, option):
return (re.sub('\W+', '_', section) + '_' + re.sub('\W+', '_', option)).lower()

@staticmethod
def _decompress(data, algorithm):
"""
Decompress a byte string based of the provided algorithm.
:param data: byte string
:param algorithm: string with the name of the compression algorithm used
:return: decompressed byte string.
"""
if algorithm is None or algorithm == 'none':
result = data
elif algorithm == 'zlib':
result = zlib.decompress(data)
elif algorithm == 'bz2':
result = bz2.decompress(data)
else:
raise ValueError("Compression {} is not supported.".format(algorithm))

return result
Binary file added tests/aws_lambda/only-use-for-unit-tests.pem.bz2
Binary file not shown.
Binary file added tests/aws_lambda/only-use-for-unit-tests.zlib
Binary file not shown.
133 changes: 133 additions & 0 deletions tests/aws_lambda/test_bless_lambda.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import zlib

import pytest

Expand Down Expand Up @@ -230,6 +231,138 @@ def test_basic_local_username_validation_email_remote_usernames_useradd(monkeypa
assert output['certificate'].startswith('ssh-rsa-cert-v01@openssh.com ')


def test_basic_ca_private_key_file_bz2(monkeypatch):
extra_environment_variables = {
'bless_ca_default_password': '<INSERT_DEFAULT_KMS_ENCRYPTED_BASE64_ENCODED_PEM_PASSWORD_HERE>',
'bless_ca_ca_private_key_file': 'tests/aws_lambda/only-use-for-unit-tests.pem.bz2',
'bless_ca_ca_private_key_compression': 'bz2',
'bless_options_username_validation': 'email',
'bless_options_remote_usernames_validation': 'useradd',
}

for k, v in extra_environment_variables.items():
monkeypatch.setenv(k, v)

output = lambda_handler(VALID_TEST_REQUEST_USERNAME_VALIDATION_EMAIL_REMOTE_USERNAMES_USERADD, context=Context,
ca_private_key_password=RSA_CA_PRIVATE_KEY_PASSWORD,
entropy_check=False,
config_file=os.path.join(os.path.dirname(__file__), ''))
assert output['certificate'].startswith('ssh-rsa-cert-v01@openssh.com ')


def test_basic_ca_private_key_env_bz2(monkeypatch):
extra_environment_variables = {
'bless_ca_default_password': '<INSERT_DEFAULT_KMS_ENCRYPTED_BASE64_ENCODED_PEM_PASSWORD_HERE>',
'bless_ca_ca_private_key': 'QlpoOTFBWSZTWadq1y0AAD9fgCAQQA7/8D////A////wYAhvr3b709499zXnfbb5333dbobvZ9vvvve9e+d3e9ZiqntTamQwTTaCGp6ZNNGmCnqeA0aCEGVT9mhMmU9GBNTaaaYnoT0MgBMaQqYOninkZMCY1PUMptpkyTU/VPInppgEyjQZVP8TATE0aejI0yEaNMgegmCZIapjKn6ank0fqNGQE0MieCZME0ZGGlP1KPUxVU/yMRqPQmmU8ApsE01T2BMmp4SbUmm0EACITlGJPkAA72rrnlOel4E7KfRSXbkjUxZ3d06nQ7lyxcbem0o5sL6PykCQKgNYeUMx+oIVrb8kV2vUU7sXpuM5c2PP3iELdRPcwYdeQvgJu8VYAfSIO4ISJN+dP31H1z/o6w+oBe4/dvmHwhM5ixIfNLkGxwBWz5Rm/kam1XX4Lpfr4zZh39Nw69G6GPq6POEIO02v34m3J0Zm1F8mn5sc4X28E1v7lfSop4VgCPltGwK10SPaAbxtBnHtmzDH/MHUHqUtGiZnSLmrP296mbIbVqKit1J89MFlKxOrENO6Im+dS9NweVV3UqamYPacc9iDyTnKfBsUiryWSKFZdHGhQW+Z0xbLLo0XjD0U0b47zRj0/JZZaIAtoB+9XLunM4q3kMGNp+eOVheJ8rc7Znh+JkHVIjg7CPNNLeTUZBdH0MwoHp1oIoPZ6Y+egjfRge69B7UVwC7FutqAElbq+sCU6anf0gnV3e4j1gosbU3bZvoPl4PSNhmCQY7+0KCWSTAHZ/HWZ4HQsaVC0r3w9Y/I7h3gEhgJRxwd0qDTjts6aHSoy77NmNi6JdDg78aC5S1XQRcnufbmbcptG/YnCD9ZxU8Fz4K/uk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K5cJo8+5KT8WFyQhNm7mMlfeai6IypNi/8cff92PZpapqZSdKkoT0kMT+3ETf5CWzIaMWB2xFY0gaQt51+bdqKbl0olo8qUY5rpGoVUlU7xWAMKLDovD7qadMJ4boR3+WEekP4XOKvw4iHrOoEx1bgCuDEkRSCFx4fc9x1uORdUVUYi3Xg+cOC17TR/adYaskkfdOidCnpde9OULUzpjXfwisVvD4FdfK6Pqwo4V4NF0NYPFrJg+iIHPLvG8WU4yOCXhKLJSxfHjwk4688t2Ymj8E2nwHbsQuagzTCnVnEheSqWCaVahd4uIVRm2i+CeneJc4/VD7HEDj0sdPbXOg+jy8qkUboO60ZiTMk3J2ywaVyVr5TMPQggw21zFpybPNL5x8a41ECJZDM90JQ8EjAWOO9xfnOIcxruEQLa7A4NphTjTcQ4MXg1jfr52OvnK0EYkwmYDTlarVBvOI5bGK7W+8q1ZRyThbDMxNuQZd3/IM8RKFSt9Y7KUYPVSinSpAaegEObwnNpRU+gk5WvA5f4XckU4UJCnatctA==',
'bless_ca_ca_private_key_compression': 'bz2',
'bless_options_username_validation': 'email',
'bless_options_remote_usernames_validation': 'useradd',
}

for k, v in extra_environment_variables.items():
monkeypatch.setenv(k, v)

output = lambda_handler(VALID_TEST_REQUEST_USERNAME_VALIDATION_EMAIL_REMOTE_USERNAMES_USERADD, context=Context,
ca_private_key_password=RSA_CA_PRIVATE_KEY_PASSWORD,
entropy_check=False,
config_file=os.path.join(os.path.dirname(__file__), ''))
assert output['certificate'].startswith('ssh-rsa-cert-v01@openssh.com ')


def test_basic_ca_private_key_file_zlib(monkeypatch):
extra_environment_variables = {
'bless_ca_default_password': '<INSERT_DEFAULT_KMS_ENCRYPTED_BASE64_ENCODED_PEM_PASSWORD_HERE>',
'bless_ca_ca_private_key_file': 'tests/aws_lambda/only-use-for-unit-tests.zlib',
'bless_ca_ca_private_key_compression': 'zlib',
'bless_options_username_validation': 'email',
'bless_options_remote_usernames_validation': 'useradd',
}

for k, v in extra_environment_variables.items():
monkeypatch.setenv(k, v)

output = lambda_handler(VALID_TEST_REQUEST_USERNAME_VALIDATION_EMAIL_REMOTE_USERNAMES_USERADD, context=Context,
ca_private_key_password=RSA_CA_PRIVATE_KEY_PASSWORD,
entropy_check=False,
config_file=os.path.join(os.path.dirname(__file__), ''))
assert output['certificate'].startswith('ssh-rsa-cert-v01@openssh.com ')

def test_basic_ca_private_key_env_zlib(monkeypatch):
extra_environment_variables = {
'bless_ca_default_password': '<INSERT_DEFAULT_KMS_ENCRYPTED_BASE64_ENCODED_PEM_PASSWORD_HERE>',
'bless_ca_ca_private_key': 'eJxtl7XOxYqOhfs8xelzj8J0u3Cywwxddphhh59+/pl63FqyLOtby/a///4FJ8qq+Y/rsf/YrhqyvviPJib/m/gXsLc5/9d/lvK//+D/EU3eTWxfFABB1P5Vp2r+7z+s6P2LoPS/PMf/hycwHGEIikYlVBAonCNRhJYkCRcwiSIoEgB+w3h+RpVdTXl6Rhw5/VxZi8XKgvTzZUqocTd5QJq1dtz6rHKOyVGCuEhfo+4ocCYPwDOSZG3thKq8U6HaSLjU5NVSauoBcmQ3jTpC/8FO2RmKAeEch7WsJlei9GEjtZlbENCan0aJTWLo+aTubI5EkvU6DxNoThKlIWteqS74Q52Z6Jw0SO44el1pgcyNl5htZQfMsVRK9Rmm5cnn6vrjfrZ/i89HKVTeO54Sd09Umz1Fu/zuXUtCEkZYF5t9IUU3LfEZ8MZ2qPpRxb7Xt5nSRctxuBfkX+V/rStY2WipNZWUJ1/WaFk3kUA57o+/qdPupJAvfgGGuXDNrj/CStoJ+fREroqsBoaRAR3Gb4TiXMAxNNljqNK6SbK+wwc2YNa9eaixC7YG7FdSX0jc6s8MBp1mxiF5yaQAo1j0ewmxRUs6TjXcOX+cmVz2qa22gBP3x4J6LBafGUg7Lb8/HkkyZODpu2126ZU1zgFeRBiY807sfQKlp7mY/vW1X527ofDYCd/jen1Q3hIYxQkT5iPWnJeC0DbGfz2Yt7aXTtP8jduPOZhBVFnQp0FZf6kNFsxiRzL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X/mWM1KUetir1HiCb2XXBKhMwgzZ9WGs6FCNwlxIGvMLf2bN7r0uCHS9TXQo9Q6zngNB4fUx40t2xjIVjbi9rfbtTu0T8iBmkrlPz5kLABJZXOUbR+wk3n9NuKEtf3ZBP5rXlAvTJvgY5u9yf+DT4tNSwPxw11wBoDbMfIXlPLBNiO5Hi7GX+jW2prRTid45m8vI3fi/jz7gNdg+jsB9sYP+fEovyeDWEsvs+8wPdLCCfghri6Mw9jKbeXu+j3T+3DIBBC5d5ncGXJb+K5lqm3kYiHpbfocDSX2rzt5v07aW3yC8BJT1B/LOPGTh4NyzPRuuQ7bDP8Wh8+FK09drVfH4Nb9FSEZqwHPccnuZSXdtjlQAGWQ8Ukl8y26ufjhs44mik8QvtyuW6qqC5ngvgN6f3PLESFsTE+pHAeLyS/TZuG/kIPAKcd1FpxwmOKFFmEHVr7OYr++x2wckg3Jim+fdIcyTKKuwuNxnxEiXD4Mtu7LsLGEPB/X4xoCv//d/M/z4BBFQ==',
'bless_ca_ca_private_key_compression': 'zlib',
'bless_options_username_validation': 'email',
'bless_options_remote_usernames_validation': 'useradd',
}

for k, v in extra_environment_variables.items():
monkeypatch.setenv(k, v)

output = lambda_handler(VALID_TEST_REQUEST_USERNAME_VALIDATION_EMAIL_REMOTE_USERNAMES_USERADD, context=Context,
ca_private_key_password=RSA_CA_PRIVATE_KEY_PASSWORD,
entropy_check=False,
config_file=os.path.join(os.path.dirname(__file__), ''))
assert output['certificate'].startswith('ssh-rsa-cert-v01@openssh.com ')


def test_basic_ca_private_key_file_none_compression(monkeypatch):
extra_environment_variables = {
'bless_ca_default_password': '<INSERT_DEFAULT_KMS_ENCRYPTED_BASE64_ENCODED_PEM_PASSWORD_HERE>',
'bless_ca_ca_private_key_file': 'tests/aws_lambda/only-use-for-unit-tests.pem',
'bless_ca_ca_private_key_compression': 'none',
'bless_options_username_validation': 'email',
'bless_options_remote_usernames_validation': 'useradd',
}

for k, v in extra_environment_variables.items():
monkeypatch.setenv(k, v)

output = lambda_handler(VALID_TEST_REQUEST_USERNAME_VALIDATION_EMAIL_REMOTE_USERNAMES_USERADD, context=Context,
ca_private_key_password=RSA_CA_PRIVATE_KEY_PASSWORD,
entropy_check=False,
config_file=os.path.join(os.path.dirname(__file__), ''))
assert output['certificate'].startswith('ssh-rsa-cert-v01@openssh.com ')


def test_invalid_uncompressed_with_zlib(monkeypatch):
extra_environment_variables = {
'bless_ca_default_password': '<INSERT_DEFAULT_KMS_ENCRYPTED_BASE64_ENCODED_PEM_PASSWORD_HERE>',
'bless_ca_ca_private_key_file': 'tests/aws_lambda/only-use-for-unit-tests.pem',
'bless_ca_ca_private_key_compression': 'zlib',
'bless_options_username_validation': 'email',
'bless_options_remote_usernames_validation': 'useradd',
}

for k, v in extra_environment_variables.items():
monkeypatch.setenv(k, v)

with pytest.raises(zlib.error):
lambda_handler(VALID_TEST_REQUEST_USERNAME_VALIDATION_EMAIL_REMOTE_USERNAMES_USERADD, context=Context,
ca_private_key_password=RSA_CA_PRIVATE_KEY_PASSWORD,
entropy_check=False,
config_file=os.path.join(os.path.dirname(__file__), ''))


def test_invalid_uncompressed_with_bz2(monkeypatch):
extra_environment_variables = {
'bless_ca_default_password': '<INSERT_DEFAULT_KMS_ENCRYPTED_BASE64_ENCODED_PEM_PASSWORD_HERE>',
'bless_ca_ca_private_key_file': 'tests/aws_lambda/only-use-for-unit-tests.pem',
'bless_ca_ca_private_key_compression': 'bz2',
'bless_options_username_validation': 'email',
'bless_options_remote_usernames_validation': 'useradd',
}

for k, v in extra_environment_variables.items():
monkeypatch.setenv(k, v)

with pytest.raises(OSError):
lambda_handler(VALID_TEST_REQUEST_USERNAME_VALIDATION_EMAIL_REMOTE_USERNAMES_USERADD, context=Context,
ca_private_key_password=RSA_CA_PRIVATE_KEY_PASSWORD,
entropy_check=False,
config_file=os.path.join(os.path.dirname(__file__), ''))


def test_invalid_username_request():
output = lambda_handler(INVALID_TEST_REQUEST_USERNAME_INVALID, context=Context,
ca_private_key_password=RSA_CA_PRIVATE_KEY_PASSWORD,
Expand Down
14 changes: 14 additions & 0 deletions tests/config/full-zlib.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Bless Options]
# The default values are sane, these are not.
certificate_validity_after_seconds = 1
certificate_validity_before_seconds = 1
entropy_minimum_bits = 2
random_seed_bytes = 3
logging_level = DEBUG
username_validation = debian

[Bless CA]
us-east-1_password = <INSERT_US-EAST-1_KMS_ENCRYPTED_BASE64_ENCODED_PEM_PASSWORD_HERE>
us-west-2_password = <INSERT_US-WEST-2_KMS_ENCRYPTED_BASE64_ENCODED_PEM_PASSWORD_HERE>
ca_private_key = <INSERT_YOUR_ENCRYPTED_PEM_FILE_NAME>
ca_private_key_compression = zlib
1 change: 1 addition & 0 deletions tests/config/full.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ username_validation = debian
us-east-1_password = <INSERT_US-EAST-1_KMS_ENCRYPTED_BASE64_ENCODED_PEM_PASSWORD_HERE>
us-west-2_password = <INSERT_US-WEST-2_KMS_ENCRYPTED_BASE64_ENCODED_PEM_PASSWORD_HERE>
ca_private_key_file = <INSERT_YOUR_ENCRYPTED_PEM_FILE_NAME>
ca_private_key_compression = zlib
Loading

0 comments on commit b685728

Please sign in to comment.