This is the function that will be called when the lambda function starts. :param event: Dictionary of the json request. :param context: AWS LambdaContext Object http://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html :param ca_private_key_password: For local testi
(
event, context=None, ca_private_key_password=None,
entropy_check=True,
config_file=None)
| 17 | |
| 18 | |
| 19 | def lambda_handler_host( |
| 20 | event, context=None, ca_private_key_password=None, |
| 21 | entropy_check=True, |
| 22 | config_file=None): |
| 23 | """ |
| 24 | This is the function that will be called when the lambda function starts. |
| 25 | :param event: Dictionary of the json request. |
| 26 | :param context: AWS LambdaContext Object |
| 27 | http://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html |
| 28 | :param ca_private_key_password: For local testing, if the password is provided, skip the KMS |
| 29 | decrypt. |
| 30 | :param entropy_check: For local testing, if set to false, it will skip checking entropy and |
| 31 | won't try to fetch additional random from KMS. |
| 32 | :param config_file: The config file to load the SSH CA private key from, and additional settings. |
| 33 | :return: the SSH Certificate that can be written to id_rsa-cert.pub or similar file. |
| 34 | """ |
| 35 | bless_cache = setup_lambda_cache(ca_private_key_password, config_file) |
| 36 | |
| 37 | # Load the deployment config values |
| 38 | config = bless_cache.config |
| 39 | |
| 40 | logger = set_logger(config) |
| 41 | |
| 42 | certificate_validity_before_seconds = config.getint(BLESS_OPTIONS_SECTION, |
| 43 | SERVER_CERTIFICATE_VALIDITY_BEFORE_SEC_OPTION) |
| 44 | certificate_validity_after_seconds = config.getint(BLESS_OPTIONS_SECTION, |
| 45 | SERVER_CERTIFICATE_VALIDITY_AFTER_SEC_OPTION) |
| 46 | |
| 47 | ca_private_key = config.getprivatekey() |
| 48 | |
| 49 | # Process cert request |
| 50 | schema = BlessHostSchema(strict=True) |
| 51 | schema.context[HOSTNAME_VALIDATION_OPTION] = config.get(BLESS_OPTIONS_SECTION, HOSTNAME_VALIDATION_OPTION) |
| 52 | |
| 53 | try: |
| 54 | request = schema.load(event).data |
| 55 | except ValidationError as e: |
| 56 | return error_response('InputValidationError', str(e)) |
| 57 | |
| 58 | # todo: You'll want to bring your own hostnames validation. |
| 59 | logger.info('Bless lambda invoked by [public_key: {}] for hostnames[{}]'.format(request.public_key_to_sign, |
| 60 | request.hostnames)) |
| 61 | |
| 62 | # Make sure we have the ca private key password |
| 63 | if bless_cache.ca_private_key_password is None: |
| 64 | return error_response('ClientError', bless_cache.ca_private_key_password_error) |
| 65 | else: |
| 66 | ca_private_key_password = bless_cache.ca_private_key_password |
| 67 | |
| 68 | # if running as a Lambda, we can check the entropy pool and seed it with KMS if desired |
| 69 | if entropy_check: |
| 70 | check_entropy(config, logger) |
| 71 | |
| 72 | # cert values determined only by lambda and its configs |
| 73 | current_time = int(time.time()) |
| 74 | valid_before = current_time + certificate_validity_after_seconds |
| 75 | valid_after = current_time - certificate_validity_before_seconds |
| 76 |