|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import traceback |
| 4 | + |
| 5 | +from ssh_audit import exitcodes |
| 6 | +from ssh_audit.auditconf import AuditConf |
| 7 | +from ssh_audit.outputbuffer import OutputBuffer |
| 8 | +from ssh_audit.ssh_audit import audit |
| 9 | + |
| 10 | + |
| 11 | +def lambda_handler(event, _context): |
| 12 | + out = OutputBuffer() |
| 13 | + aconf = AuditConf() |
| 14 | + |
| 15 | + aconf.batch = event["audit_conf"].get("batch", aconf.batch) |
| 16 | + aconf.client_audit = event["audit_conf"].get("client_audit", aconf.client_audit) |
| 17 | + aconf.colors = event["audit_conf"].get("colors", aconf.colors) |
| 18 | + aconf.conn_rate_test_enabled = event["audit_conf"].get( |
| 19 | + "conn_rate_test_enabled", aconf.conn_rate_test_enabled |
| 20 | + ) |
| 21 | + aconf.conn_rate_test_target_rate = event["audit_conf"].get( |
| 22 | + "conn_rate_test_target_rate", aconf.conn_rate_test_target_rate |
| 23 | + ) |
| 24 | + aconf.conn_rate_test_threads = event["audit_conf"].get( |
| 25 | + "conn_rate_test_threads", aconf.conn_rate_test_threads |
| 26 | + ) |
| 27 | + aconf.debug = event["audit_conf"].get("debug", aconf.debug) |
| 28 | + aconf.dheat = event["audit_conf"].get("dheat", aconf.dheat) |
| 29 | + aconf.dheat_concurrent_connections = event["audit_conf"].get( |
| 30 | + "dheat_concurrent_connections", aconf.dheat_concurrent_connections |
| 31 | + ) |
| 32 | + aconf.dheat_e_length = event["audit_conf"].get( |
| 33 | + "dheat_e_length", aconf.dheat_e_length |
| 34 | + ) |
| 35 | + aconf.dheat_target_alg = event["audit_conf"].get( |
| 36 | + "dheat_target_alg", aconf.dheat_target_alg |
| 37 | + ) |
| 38 | + aconf.gex_test = event["audit_conf"].get("gex_test", aconf.gex_test) |
| 39 | + aconf.host = event["audit_conf"].get("host", aconf.host) |
| 40 | + aconf.ip_version_preference = event["audit_conf"].get( |
| 41 | + "ip_version_preference", aconf.ip_version_preference |
| 42 | + ) |
| 43 | + aconf.ipv4 = event["audit_conf"].get("ipv4", aconf.ipv4) |
| 44 | + aconf.ipv6 = event["audit_conf"].get("ipv6", aconf.ipv6) |
| 45 | + aconf.json = event["audit_conf"].get("json", aconf.json) |
| 46 | + aconf.json_print_indent = event["audit_conf"].get( |
| 47 | + "json_print_indent", aconf.json_print_indent |
| 48 | + ) |
| 49 | + aconf.level = event["audit_conf"].get("level", aconf.level) |
| 50 | + aconf.list_policies = event["audit_conf"].get("list_policies", aconf.list_policies) |
| 51 | + aconf.lookup = event["audit_conf"].get("lookup", aconf.lookup) |
| 52 | + aconf.make_policy = event["audit_conf"].get("make_policy", aconf.make_policy) |
| 53 | + aconf.manual = event["audit_conf"].get("manual", aconf.manual) |
| 54 | + aconf.policy = event["audit_conf"].get("policy", aconf.policy) |
| 55 | + aconf.policy_file = event["audit_conf"].get("policy_file", aconf.policy_file) |
| 56 | + aconf.port = event["audit_conf"].get("port", aconf.port) |
| 57 | + aconf.skip_rate_test = event["audit_conf"].get( |
| 58 | + "skip_rate_test", aconf.skip_rate_test |
| 59 | + ) |
| 60 | + aconf.ssh1 = event["audit_conf"].get("ssh1", aconf.ssh1) |
| 61 | + aconf.ssh2 = event["audit_conf"].get("ssh2", aconf.ssh2) |
| 62 | + aconf.target_file = event["audit_conf"].get("target_file", aconf.target_file) |
| 63 | + aconf.target_list = event["audit_conf"].get("target_list", aconf.target_list) |
| 64 | + aconf.threads = event["audit_conf"].get("threads", aconf.threads) |
| 65 | + aconf.timeout = event["audit_conf"].get("timeout", aconf.timeout) |
| 66 | + aconf.timeout_set = event["audit_conf"].get("timeout_set", aconf.timeout_set) |
| 67 | + aconf.verbose = event["audit_conf"].get("verbose", aconf.verbose) |
| 68 | + |
| 69 | + try: |
| 70 | + exit_code = audit(out, aconf) |
| 71 | + report = out.get_buffer() |
| 72 | + except Exception: |
| 73 | + exit_code = exitcodes.UNKNOWN_ERROR |
| 74 | + report = traceback.format_exc() |
| 75 | + |
| 76 | + match exit_code: |
| 77 | + case exitcodes.UNKNOWN_ERROR: |
| 78 | + http_code = 500 |
| 79 | + case exitcodes.CONNECTION_ERROR: |
| 80 | + http_code = 400 |
| 81 | + case _: |
| 82 | + http_code = 200 |
| 83 | + |
| 84 | + return { |
| 85 | + "statusCode": http_code, |
| 86 | + "report": report, |
| 87 | + } |
0 commit comments