Search in sources :

Example 1 with InvalidArtifactUriException

use of com.aws.greengrass.componentmanager.exceptions.InvalidArtifactUriException in project aws-greengrass-nucleus by aws-greengrass.

the class DockerImageArtifactParser method getImage.

/**
 * Extract and image instance with image and registry details from an artifact URI, also validates the URI.
 *
 * @param artifact component artifact
 * @return Image instance with image and registry details
 * @throws InvalidArtifactUriException If URI validation against docker defined specification fails
 */
public static Image getImage(ComponentArtifact artifact) throws InvalidArtifactUriException {
    String uriString = artifact.getArtifactUri().toString();
    // Eliminate scheme, e.g. docker:ubuntu@sha256:c4ffb8 -> ubuntu@sha256:c4ffb8
    uriString = uriString.substring(uriString.indexOf(':') + 1);
    // Extract and validate registry
    // If no registry specified, the default is docker hub's registry server
    // e.g. ubuntu == library/ubuntu == docker.io/library/ubuntu == registry.hub.docker.com/library/ubuntu
    String registryEndpoint = "registry.hub.docker.com/library";
    if (uriString.contains("/")) {
        int index = uriString.indexOf('/');
        registryEndpoint = uriString.substring(0, index);
        if (index >= uriString.length() - 1) {
            throw new InvalidArtifactUriException(INVALID_DOCKER_ARTIFACT_URI_MESSAGE);
        }
        if (!DOMAIN_PATTERN.matcher(registryEndpoint).find()) {
            throw new InvalidArtifactUriException(INVALID_DOCKER_ARTIFACT_URI_MESSAGE);
        }
        uriString = uriString.substring(index + 1);
    }
    // Extract image name and tag | digest
    String imageName;
    String imageTag = null;
    String imageDigest = null;
    // Only one of digest or tag should be present
    if (uriString.contains("@")) {
        // Extract and validate image digest
        int index = uriString.indexOf('@');
        imageName = uriString.substring(0, index);
        if (index == uriString.length() - 1) {
            throw new InvalidArtifactUriException(INVALID_DOCKER_ARTIFACT_URI_MESSAGE);
        }
        imageDigest = uriString.substring(index + 1);
        if (!DIGEST_REGEX_PATTERN.matcher(imageDigest).find()) {
            throw new InvalidArtifactUriException(INVALID_DOCKER_ARTIFACT_URI_MESSAGE);
        }
    } else if (uriString.contains(":")) {
        // Extract and validate image tag
        int index = uriString.indexOf(':');
        imageName = uriString.substring(0, index);
        if (index == uriString.length() - 1) {
            throw new InvalidArtifactUriException(INVALID_DOCKER_ARTIFACT_URI_MESSAGE);
        }
        imageTag = uriString.substring(index + 1);
        if (!IMAGE_TAG_REGEX_PATTERN.matcher(imageTag).find()) {
            throw new InvalidArtifactUriException(INVALID_DOCKER_ARTIFACT_URI_MESSAGE);
        }
    } else {
        imageName = uriString;
    }
    // Validate imageName
    Matcher imageNameMatcher = PATH_PATTERN.matcher(imageName);
    if (!imageNameMatcher.find()) {
        throw new InvalidArtifactUriException(INVALID_DOCKER_ARTIFACT_URI_MESSAGE);
    }
    if (Utils.isEmpty(imageTag) && Utils.isEmpty(imageDigest)) {
        // No digest/tag specified, docker engine will pull the latest image
        logger.atWarn().kv("artifact-uri", artifact.getArtifactUri()).log("An image version is not present. Specify an image version via an image tag or digest to" + " ensure that the component is immutable and that the deployment will consistently " + "deliver the same artifacts");
        imageTag = "latest";
    }
    return new Image(getRegistryFromArtifact(registryEndpoint), imageName, imageTag, imageDigest, artifact.getArtifactUri());
}
Also used : InvalidArtifactUriException(com.aws.greengrass.componentmanager.exceptions.InvalidArtifactUriException) Matcher(java.util.regex.Matcher)

Example 2 with InvalidArtifactUriException

use of com.aws.greengrass.componentmanager.exceptions.InvalidArtifactUriException in project aws-greengrass-nucleus by aws-greengrass.

the class S3Downloader method getS3PathForURI.

private S3ObjectPath getS3PathForURI(URI artifactURI) throws InvalidArtifactUriException {
    Matcher s3PathMatcher = S3_PATH_REGEX.matcher(artifactURI.toString());
    if (!s3PathMatcher.matches()) {
        // Bad URI
        throw new InvalidArtifactUriException(getErrorString("Invalid artifact URI " + artifactURI.toString()));
    }
    // Parse artifact path
    String bucket = s3PathMatcher.group(1);
    String key = s3PathMatcher.group(2);
    return new S3ObjectPath(bucket, key);
}
Also used : InvalidArtifactUriException(com.aws.greengrass.componentmanager.exceptions.InvalidArtifactUriException) Matcher(java.util.regex.Matcher)

Example 3 with InvalidArtifactUriException

use of com.aws.greengrass.componentmanager.exceptions.InvalidArtifactUriException in project aws-greengrass-nucleus by aws-greengrass.

the class DockerImageDownloader method download.

@Override
public File download() throws PackageDownloadException, IOException, InterruptedException {
    logger.atDebug().log("Downloading artifact");
    checkDownloadPrerequisites();
    Image image;
    try {
        image = Image.fromArtifactUri(artifact);
    } catch (InvalidArtifactUriException e) {
        throw new PackageDownloadException("Failed to download due to bad artifact URI", e);
    }
    return performDownloadSteps(image);
}
Also used : InvalidArtifactUriException(com.aws.greengrass.componentmanager.exceptions.InvalidArtifactUriException) PackageDownloadException(com.aws.greengrass.componentmanager.exceptions.PackageDownloadException)

Aggregations

InvalidArtifactUriException (com.aws.greengrass.componentmanager.exceptions.InvalidArtifactUriException)3 Matcher (java.util.regex.Matcher)2 PackageDownloadException (com.aws.greengrass.componentmanager.exceptions.PackageDownloadException)1