|
| 1 | +import os |
| 2 | +import re |
| 3 | +import argparse |
| 4 | +from datetime import datetime, timezone |
| 5 | +from common import updateCatalogAttributes, extract_image_and_tag, fetch_service_path |
| 6 | +import sys |
| 7 | +import requests |
| 8 | +import yaml |
| 9 | +import docker |
| 10 | +import subprocess |
| 11 | + |
| 12 | + |
| 13 | +def determine_image_language(dockerfile_path: str, fromstages: list): |
| 14 | + """ |
| 15 | + Determine the language from the matching stages |
| 16 | + :param dockerfile_path (str): Path to the Dockerfile |
| 17 | + :param fromstages (list): list of strings to filter |
| 18 | + :return: Boolean if crypto program executed successfully |
| 19 | + """ |
| 20 | + for stage in fromstages: |
| 21 | + image, tag = extract_image_and_tag(dockerfile_path, stage) |
| 22 | + print(f" stage: {stage} image: {image} tag: {tag}") |
| 23 | + # return first match |
| 24 | + if image != None and image.startswith("datarobotdev/platform-base-python"): |
| 25 | + return "python", image, tag |
| 26 | + if image != None and image.startswith("datarobotdev/platform-base-go"): |
| 27 | + return "go", image, tag |
| 28 | + if image != None and image.startswith("datarobotdev/platform-base-node"): |
| 29 | + return "nodejs", image, tag |
| 30 | + if image != None and image.startswith("datarobotdev/platform-base-java"): |
| 31 | + return "java", image, tag |
| 32 | + |
| 33 | + return None, None, None |
| 34 | + |
| 35 | + |
| 36 | +def fips_build_check(language: str, image: str, tag: str, base_path: str): |
| 37 | + """ |
| 38 | + An example to build docker in docker to run crypto test |
| 39 | + :param language (str): Programming language (go, nodejs, python) |
| 40 | + :param image (str): The image to test |
| 41 | + :param tag (str): The tag to pull |
| 42 | + :param base_path (str): The basedirectory holding the harness-infra repo |
| 43 | + :return: Boolean if crypto program executed successfully |
| 44 | + """ |
| 45 | + client = docker.from_env() |
| 46 | + image_pull = f"{image}:{tag}" |
| 47 | + try: |
| 48 | + # pull the image |
| 49 | + image = client.images.pull(image_pull) |
| 50 | + print(f"Successfully pulled {image_pull}") |
| 51 | + |
| 52 | + # grab image config and lables |
| 53 | + image = client.images.get(image_pull) |
| 54 | + labels = image.attrs.get("Config", {}).get("Labels", {}) |
| 55 | + configs = image.attrs.get("Config", {}) |
| 56 | + print(f"Configs: {configs}") |
| 57 | + print(f"Labels: {labels}") |
| 58 | + |
| 59 | + # execute building image with test program |
| 60 | + print("Running docker build...") |
| 61 | + docker_command = [ |
| 62 | + "docker", |
| 63 | + "build", |
| 64 | + "--build-arg", |
| 65 | + f"BASE_IMAGE={image_pull}", |
| 66 | + "-t", |
| 67 | + "fips-test", |
| 68 | + f"{base_path}/harness-infra/idp/fips/{language}/.", |
| 69 | + ] |
| 70 | + exit_code = subprocess.call(docker_command) |
| 71 | + if exit_code == 0: |
| 72 | + print("Docker build succeeded.") |
| 73 | + else: |
| 74 | + print(f"Error occurred during Docker build. Exit code: {exit_code}") |
| 75 | + |
| 76 | + # attempt to execute the built image |
| 77 | + print("Running docker container...") |
| 78 | + docker_command_run = ["docker", "run", "--rm", "fips-test"] |
| 79 | + exit_code = subprocess.call(docker_command_run) |
| 80 | + print(f"Docker run exited with code {exit_code}") |
| 81 | + |
| 82 | + if exit_code == 0: |
| 83 | + print("The container ran successfully.") |
| 84 | + return True |
| 85 | + |
| 86 | + print(f"An error occurred during Docker run. Exit code: {exit_code}") |
| 87 | + return False |
| 88 | + |
| 89 | + except docker.errors.APIError as e: |
| 90 | + print(f"Error pulling image: {e}") |
| 91 | + except docker.errors.ImageNotFound: |
| 92 | + print(f"Image '{image_pull}' not found.") |
| 93 | + except Exception as e: |
| 94 | + print(f"An error occurred while retrieving image labels: {e}") |
| 95 | + |
| 96 | + |
| 97 | +def main(): |
| 98 | + |
| 99 | + parser = argparse.ArgumentParser( |
| 100 | + description="Process a Dockerfile for FIPS checks." |
| 101 | + ) |
| 102 | + parser.add_argument( |
| 103 | + "--dockerfile_path", |
| 104 | + type=str, |
| 105 | + help="Absolute path to the Default docker path file (usually root dir of repo)", |
| 106 | + ) |
| 107 | + |
| 108 | + parser.add_argument( |
| 109 | + "--catalog_entity", type=str, help="example: component:default/demo-catalog-svc" |
| 110 | + ) |
| 111 | + |
| 112 | + parser.add_argument("--harness_account_id", type=str, help="harness account id") |
| 113 | + |
| 114 | + parser.add_argument("--harness_api_token", type=str, help="API Token") |
| 115 | + |
| 116 | + parser.add_argument( |
| 117 | + "--base_repo_path", |
| 118 | + type=str, |
| 119 | + help="Path to base folder holding repos", |
| 120 | + default="/harness", |
| 121 | + ) |
| 122 | + |
| 123 | + args = parser.parse_args() |
| 124 | + base_path = args.base_repo_path |
| 125 | + catalog_entity = args.catalog_entity |
| 126 | + dockerfile_path = args.dockerfile_path |
| 127 | + service_components = catalog_entity.split("/") |
| 128 | + catalog_svc = service_components[1] |
| 129 | + |
| 130 | + # check for custom configure path in catalog-info.yaml |
| 131 | + print(f"BASE PATH: {base_path} for SERVICE : {catalog_svc}") |
| 132 | + relative_path, language_tag = fetch_service_path(base_path, catalog_svc) |
| 133 | + dockerfile_path_cfg = f"/{base_path}/{catalog_svc}/{relative_path}" |
| 134 | + |
| 135 | + # if the configure path exists for dockerfile use it instead |
| 136 | + if relative_path != None and os.path.exists(dockerfile_path_cfg): |
| 137 | + dockerfile_path = dockerfile_path_cfg |
| 138 | + else: |
| 139 | + print(f" configurepath not found using default instead") |
| 140 | + |
| 141 | + print(f"catalog_entity: {catalog_entity} dockerfilePath: {dockerfile_path}") |
| 142 | + |
| 143 | + # determine the language based on docker base tag |
| 144 | + language, image, tag = determine_image_language( |
| 145 | + dockerfile_path, ["build-release-stage", "runtime-stage", "runner", "base"] |
| 146 | + ) |
| 147 | + |
| 148 | + print(f" Image supporting language {language} {image} {tag}") |
| 149 | + |
| 150 | + # test the image with crypto test |
| 151 | + fips_compliance = fips_build_check(language, image, tag, base_path) |
| 152 | + |
| 153 | + # use for local testing |
| 154 | + # fips_compliance = fips_build_check ("python", "python", "slim-bullseye", base_path) |
| 155 | + |
| 156 | + # prepare to update the catalog |
| 157 | + harness_account_id = args.harness_account_id |
| 158 | + harness_api_token = args.harness_api_token |
| 159 | + properties_array = [ |
| 160 | + {"property": "metadata.fips.compliance", "value": fips_compliance} |
| 161 | + ] |
| 162 | + |
| 163 | + # update the catalog |
| 164 | + # updateCatalogAttributes (harness_account_id, |
| 165 | + # harness_api_token, |
| 166 | + # catalog_entity, |
| 167 | + # "metadata.fips.compliance", |
| 168 | + # properties_array) |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + main() |
0 commit comments