|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +"""bless_client |
| 4 | +A sample client to invoke the BLESS Host SSH Cert Lambda function and save the signed SSH Certificate. |
| 5 | +
|
| 6 | +Usage: |
| 7 | + bless_client_host.py region lambda_function_name hostnames <id_rsa.pub to sign> <output id_rsa-cert.pub> |
| 8 | +
|
| 9 | + region: AWS region where your lambda is deployed. |
| 10 | +
|
| 11 | + lambda_function_name: The AWS Lambda function's alias or ARN to invoke. |
| 12 | +
|
| 13 | + hostnames: Comma-separated list of hostname(s) to include in this host certificate. |
| 14 | +
|
| 15 | + id_rsa.pub to sign: The id_rsa.pub that will be used in the SSH request. This is |
| 16 | + enforced in the issued certificate. |
| 17 | +
|
| 18 | + output id_rsa-cert.pub: The file where the certificate should be saved. Per man SSH(1): |
| 19 | + "ssh will also try to load certificate information from the filename |
| 20 | + obtained by appending -cert.pub to identity filenames" e.g. the <id_rsa.pub to sign>. |
| 21 | +""" |
| 22 | +import json |
| 23 | +import os |
| 24 | +import stat |
| 25 | +import sys |
| 26 | + |
| 27 | +import boto3 |
| 28 | + |
| 29 | + |
| 30 | +def main(argv): |
| 31 | + if len(argv) != 5: |
| 32 | + print( |
| 33 | + 'Usage: bless_client_host.py region lambda_function_name hostnames <id_rsa.pub to sign> ' |
| 34 | + '<output id_rsa-cert.pub>') |
| 35 | + print(len(argv)) |
| 36 | + return -1 |
| 37 | + |
| 38 | + region, lambda_function_name, hostnames, public_key_filename, certificate_filename = argv |
| 39 | + |
| 40 | + with open(public_key_filename, 'r') as f: |
| 41 | + public_key = f.read().strip() |
| 42 | + |
| 43 | + payload = {'hostnames': hostnames, 'public_key_to_sign': public_key} |
| 44 | + |
| 45 | + payload_json = json.dumps(payload) |
| 46 | + |
| 47 | + print('Executing:') |
| 48 | + print('payload_json is: \'{}\''.format(payload_json)) |
| 49 | + lambda_client = boto3.client('lambda', region_name=region) |
| 50 | + response = lambda_client.invoke(FunctionName=lambda_function_name, |
| 51 | + InvocationType='RequestResponse', LogType='None', |
| 52 | + Payload=payload_json) |
| 53 | + print('{}\n'.format(response['ResponseMetadata'])) |
| 54 | + |
| 55 | + if response['StatusCode'] != 200: |
| 56 | + print('Error creating cert.') |
| 57 | + return -1 |
| 58 | + |
| 59 | + payload = json.loads(response['Payload'].read()) |
| 60 | + |
| 61 | + if 'certificate' not in payload: |
| 62 | + print(payload) |
| 63 | + return -1 |
| 64 | + |
| 65 | + cert = payload['certificate'] |
| 66 | + |
| 67 | + with os.fdopen(os.open(certificate_filename, os.O_WRONLY | os.O_CREAT, 0o600), |
| 68 | + 'w') as cert_file: |
| 69 | + cert_file.write(cert) |
| 70 | + |
| 71 | + # If cert_file already existed with the incorrect permissions, fix them. |
| 72 | + file_status = os.stat(certificate_filename) |
| 73 | + if 0o600 != (file_status.st_mode & 0o777): |
| 74 | + os.chmod(certificate_filename, stat.S_IRUSR | stat.S_IWUSR) |
| 75 | + |
| 76 | + print('Wrote Certificate to: ' + certificate_filename) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + main(sys.argv[1:]) |
0 commit comments