"""Docker image info The DockerImageInfo class is designed to interact with AWS Elastic Container Registry (ECR) to retrieve specific metadata about Docker images stored in the registry. It encapsulates the functionality required to authenticate with AWS ECR, fetch image manifests, and extract information such as the image's ENTRYPOINT and CMD directives. """ import base64 import requests import boto3 class DockerImageInfo: def __init__(self, repository_name, image_tag, ecr_account_id, region='us-east-1'): """Initialize the DockerImageInfo object Initialize the DockerImageInfo object with AWS region, repository name, and image tag. Args: repository_name (str): Name of the ECR repository. image_tag (str): Tag of the Docker image. ecr_account_id (str): AWS account ID that is associated with a registry region (str): AWS region where the ECR repository is located. """ self.repository_name = repository_name self.image_tag = image_tag self.region = region self.registry_ids = [ecr_account_id] def _get_docker_credentials(self): """Authenticate with AWS ECR and retrieve the Docker registry credentials. Returns: (tuple): A tuple containing the endpoint URL, username, and password. """ client = boto3.client('ecr', region_name=self.region) response = client.get_authorization_token(registryIds=self.registry_ids) token = response['authorizationData'][0]['authorizationToken'] endpoint = response['authorizationData'][0]['proxyEndpoint'] username, password = base64.b64decode(token).decode().split(':') return endpoint, username, password def _get_image_manifest(self, endpoint, username, password): """Fetch the image manifest from the Docker registry. Args: endpoint (str): The Docker registry endpoint URL. username (str): The username for Docker registry authentication. password (str): The password for Docker registry authentication. Returns: dict: A dictionary representing the image manifest. """ response = requests.get(f"{endpoint}/v2/{self.repository_name}/manifests/{self.image_tag}", auth=(username, password)) response.raise_for_status() return response.json() def _get_config_blob(self, endpoint, config_digest, username, password): """Fetch the config blob of the Docker image using its digest. Args: endpoint (str): The Docker registry endpoint URL. config_digest (str): The digest of the config blob. username (str): The username for Docker registry authentication. password (str): The password for Docker registry authentication. Returns: dict: A dictionary representing the config blob. """ response = requests.get(f"{endpoint}/v2/{self.repository_name}/blobs/{config_digest}", auth=(username, password)) response.raise_for_status() return response.json() def get_entrypoint_and_cmd(self): """Extract the ENTRYPOINT and CMD from the config blob. Returns: (tuple): A tuple containing the ENTRYPOINT and CMD. """ endpoint, username, password = self._get_docker_credentials() manifest = self._get_image_manifest(endpoint, username, password) config_digest = manifest['config']['digest'] config_blob = self._get_config_blob(endpoint, config_digest, username, password) entrypoint = config_blob.get('config', {}).get('Entrypoint', []) cmd = config_blob.get('config', {}).get('Cmd', []) return entrypoint, cmd