|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +from os import walk |
| 5 | + |
| 6 | +# If modifying the template below, |
| 7 | +# make sure to double escape newlines you want in the C-string (ie: '\\n') |
| 8 | +# and to double-up brackets ('{' and '}' become '{{' and '}}') or else |
| 9 | +# formatting of the template string will fail |
| 10 | +header_template = ( |
| 11 | + """ |
| 12 | +// AWS Certificates |
| 13 | +
|
| 14 | +#ifndef AWS_CREDENTIALS_H |
| 15 | +#define AWS_CREDENTIALS_H |
| 16 | +
|
| 17 | +namespace aws {{ |
| 18 | +namespace credentials {{ |
| 19 | +/* |
| 20 | + * PEM-encoded root CA certificate |
| 21 | + */ |
| 22 | +const char rootCA[] = |
| 23 | +{0}; |
| 24 | +
|
| 25 | +/* |
| 26 | + * PEM-encoded client certificate |
| 27 | + */ |
| 28 | +const char clientCrt[] = |
| 29 | +{1}; |
| 30 | +
|
| 31 | +/* |
| 32 | + * PEM-encoded client private key. |
| 33 | + */ |
| 34 | +const char clientKey[] = |
| 35 | +{2}; |
| 36 | +
|
| 37 | +}} |
| 38 | +}} |
| 39 | +
|
| 40 | +#endif |
| 41 | + """ |
| 42 | +) |
| 43 | + |
| 44 | +default_out_file_name = "aws_credentials.h" |
| 45 | + |
| 46 | +# Formats the given file into a C-string with newlines |
| 47 | +def format_file(file_path): |
| 48 | + out = '' |
| 49 | + with open(file_path, 'r') as f: |
| 50 | + lines = f.readlines() |
| 51 | + for l in lines: |
| 52 | + # Do not add a newline to the last line |
| 53 | + line_format = '"{}\\n"\n' |
| 54 | + if l is lines[-1]: |
| 55 | + line_format = '"{}"\n' |
| 56 | + out = out + line_format.format(l.strip()) |
| 57 | + return out |
| 58 | + |
| 59 | +def main(): |
| 60 | + parser = argparse.ArgumentParser(description='Convert AWS credential files to a compatible C-style header format') |
| 61 | + parser.add_argument('certs_directory', metavar='d', help='Path to directory containing credential files downloaded from AWS IoT Console') |
| 62 | + global default_out_file_name |
| 63 | + parser.add_argument('--output-file', help='Output file name (optional, defaults to: {}'.format(default_out_file_name)) |
| 64 | + args = parser.parse_args() |
| 65 | + |
| 66 | + # Walk the given directory to find the cert files we need |
| 67 | + f = [] |
| 68 | + for (dirpath, dirnames, filenames) in walk(args.certs_directory): |
| 69 | + f.extend(filenames) |
| 70 | + break |
| 71 | + |
| 72 | + # These are the files we need, make sure they exist |
| 73 | + root_ca_file = None |
| 74 | + client_crt_file = None |
| 75 | + client_key_file = None |
| 76 | + for cert_file in f: |
| 77 | + if "root" in cert_file.lower(): |
| 78 | + root_ca_file = args.certs_directory + "/" + cert_file |
| 79 | + elif ".pem.crt" in cert_file: |
| 80 | + client_crt_file = args.certs_directory + "/" + cert_file |
| 81 | + elif "private.pem.key" in cert_file: |
| 82 | + client_key_file = args.certs_directory + "/" + cert_file |
| 83 | + |
| 84 | + if root_ca_file: |
| 85 | + print("Root CA file found: {}".format(root_ca_file)) |
| 86 | + else: |
| 87 | + print("Could not find Root CA file (eg: AmazonRootCA1.pem)!") |
| 88 | + exit() |
| 89 | + |
| 90 | + if client_crt_file: |
| 91 | + print("Client certificate file found: {}".format(client_crt_file)) |
| 92 | + else: |
| 93 | + print("Could not find client certificate file (eg: xxxxxxxxxx-certificate.pem.crt)!") |
| 94 | + exit() |
| 95 | + |
| 96 | + if client_key_file: |
| 97 | + print("Client key file found: {}".format(client_key_file)) |
| 98 | + else: |
| 99 | + print("Could not find client private key file (eg: xxxxxxxxxx-private.pem.key)!") |
| 100 | + exit() |
| 101 | + |
| 102 | + print("Generating C-style header from given credentials...") |
| 103 | + |
| 104 | + global header_template |
| 105 | + out_file_name = default_out_file_name |
| 106 | + if args.output_file: |
| 107 | + out_file_name = args.output_file |
| 108 | + |
| 109 | + with open(out_file_name, 'w+') as out: |
| 110 | + out.write(header_template.format(format_file(root_ca_file),\ |
| 111 | + format_file(client_crt_file),\ |
| 112 | + format_file(client_key_file))) |
| 113 | + |
| 114 | + print("Saved generated credentials header to {}!".format(out_file_name)) |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments