|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Validates that the supplied framework (identified by the directory passed in) will function, to the best |
| 4 | +# of our knowledge, in an airgapped cluster. |
| 5 | +# |
| 6 | +# The checks are: |
| 7 | +# - No external URIs defined in anything but resource.json |
| 8 | +# - No images defined in anything but resource.json (and templated into the svc.yml) |
| 9 | +# |
| 10 | + |
| 11 | +import re |
| 12 | +import sys |
| 13 | +import os |
| 14 | + |
| 15 | + |
| 16 | +def extract_uris(file_name): |
| 17 | + with open(file_name, "r") as file: |
| 18 | + lines = file.readlines() |
| 19 | + |
| 20 | + matcher = re.compile(".*https?:\/\/([^\/\?]*)", re.IGNORECASE) |
| 21 | + matches = [] |
| 22 | + for line in lines: |
| 23 | + # Do not grab comments |
| 24 | + if line.startswith("*") or line.startswith("#") or line.startswith("//"): |
| 25 | + continue |
| 26 | + # Do not grab "id" lines |
| 27 | + if "\"id\":" in line: |
| 28 | + continue |
| 29 | + |
| 30 | + match = matcher.match(line) |
| 31 | + if match: |
| 32 | + matches.append(match.group(1)) |
| 33 | + |
| 34 | + return matches |
| 35 | + |
| 36 | + |
| 37 | +def validate_uris_in(file_name): |
| 38 | + uris = extract_uris(file_name) |
| 39 | + |
| 40 | + bad_uri = False |
| 41 | + for uri in uris: |
| 42 | + # A FQDN is a valid internal FQDN if it contains .dcos or ends with .mesos. |
| 43 | + if not (".dcos" in uri or uri.endswith(".mesos")): |
| 44 | + print("Found a bad URI:", uri, "in:", file_name) |
| 45 | + bad_uri = True |
| 46 | + |
| 47 | + return not bad_uri |
| 48 | + |
| 49 | + |
| 50 | +def get_files_to_check_for_uris(framework_directory): |
| 51 | + # There's a set of files that will always be present. |
| 52 | + files = [os.path.join(framework_directory, "universe", "config.json"), |
| 53 | + os.path.join(framework_directory, "universe", "marathon.json.mustache")] |
| 54 | + |
| 55 | + # Always check every file in the `dist` directory of the scheduler. |
| 56 | + dist_dir = os.path.join(framework_directory, "src", "main", "dist") |
| 57 | + |
| 58 | + for dp, dn, filenames in os.walk(dist_dir): |
| 59 | + for file in filenames: |
| 60 | + files.append(os.path.join(dp, file)) |
| 61 | + |
| 62 | + return files |
| 63 | + |
| 64 | + |
| 65 | +def validate_all_uris(framework_directory): |
| 66 | + bad_file = False |
| 67 | + files = get_files_to_check_for_uris(framework_directory) |
| 68 | + for file in files: |
| 69 | + if not validate_uris_in(file): |
| 70 | + bad_file = True |
| 71 | + |
| 72 | + return not bad_file |
| 73 | + |
| 74 | + |
| 75 | +def validate_images(framework_directory): |
| 76 | + svc_yml = os.path.join(framework_directory, "src", "main", "dist", "svc.yml") |
| 77 | + |
| 78 | + with open(svc_yml, "r") as file: |
| 79 | + lines = file.readlines() |
| 80 | + |
| 81 | + bad_image = False |
| 82 | + for line in lines: |
| 83 | + line = line.strip() |
| 84 | + if "image:" in line: |
| 85 | + image_matcher = re.compile("image:\s?(.*)$", re.IGNORECASE) |
| 86 | + match = image_matcher.match(line) |
| 87 | + image_path = match.group(1) |
| 88 | + env_var_matcher = re.compile("\{\{[A-Z0-9_]*\}\}") |
| 89 | + if not env_var_matcher.match(image_path): |
| 90 | + print("Bad image found in svc.yml. It is a direct reference instead of a templated reference:", image_path) |
| 91 | + bad_image = True |
| 92 | + |
| 93 | + return not bad_image |
| 94 | + |
| 95 | + |
| 96 | +def print_help(): |
| 97 | + print("""Scans a framework for any airgap issues. Checks all files for external URIs, |
| 98 | +and docker images for direct references |
| 99 | +
|
| 100 | +usage: python airgap_linter.py <framework-directory>""") |
| 101 | + |
| 102 | + |
| 103 | +def main(argv): |
| 104 | + if len(argv) < 2: |
| 105 | + print_help() |
| 106 | + sys.exit(0) |
| 107 | + |
| 108 | + framework_directory = argv[1] |
| 109 | + |
| 110 | + if not os.path.isdir(framework_directory): |
| 111 | + print("Supplied framework directory", framework_directory, "does not exist or is not a directory.") |
| 112 | + |
| 113 | + uris_valid = validate_all_uris(framework_directory) |
| 114 | + images_valid = validate_images(framework_directory) |
| 115 | + |
| 116 | + invalid = False |
| 117 | + if not uris_valid: |
| 118 | + invalid = True |
| 119 | + |
| 120 | + if not images_valid: |
| 121 | + invalid = True |
| 122 | + |
| 123 | + if invalid: |
| 124 | + print("Airgap check FAILED. This framework will NOT work in an airgap. Fix the detected issues.") |
| 125 | + sys.exit(1) |
| 126 | + |
| 127 | + print("Airgap check complete. This framework will _probably_ work in an airgap, but for the love of everything test that.") |
| 128 | + sys.exit(0) |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == '__main__': |
| 132 | + sys.exit(main(sys.argv)) |
0 commit comments