mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
feat(ecr): PutImage and the layer API (#5784)
* feat(ecr): PutImage and the layer API PutImage, BatchCheckLayerAvailability, GetDownloadUrlForLayer, InitiateLayerUpload, UploadLayerPart and CompleteLayerUpload now go through the registry:2 container that docker push already writes to, so an image written through the API is the image docker pull and BatchGetImage return. - PutImage writes the manifest under its tag, or under its digest. It answers ImageDigestDoesNotMatchException, ImageAlreadyExistsException, ImageTagAlreadyExistsException and LayersNotFoundException as the model lists them. - Layer uploads map onto registry upload sessions, held in memory. Parts must follow each other, be at most 20 MiB, and all but the last at least 5 MiB. - GetDownloadUrlForLayer returns the blob's address on Floci's registry endpoint. - The immutable-tag guard moves into EcrService, so a docker push and a PutImage of one tag cannot both succeed. Closes #5735 * fix(ecr): refuse mixed layer digests, keep push time on retag, expire idle uploads CompleteLayerUpload refuses a second, different layer digest instead of ignoring it. PutImage keeps an image's first push time when it retags it. A layer upload idle for a day is refused even when no other upload opens, and dropping one cancels its registry upload session. * fix(ecr): check PutImage tags, require a manifest media type, share registry error handling - PutImage checks imageTag against the OCI tag grammar and answers InvalidParameterException otherwise. The tag goes into the registry's manifest URL, so v1?x wrote v1 and a slash wrote into another repository. - A manifest with no mediaType needs imageManifestMediaType, as on AWS, instead of Floci guessing one from the manifest's shape. - PutImage and the layer API reach the registry through one helper, callRegistry, in place of five copies of the same failure handling. * fix(ecr): answer ECR's own message for a tag outside its rule PutImage refuses a tag outside ECR's rule, [a-zA-Z0-9-_.]+ up to 300 characters, with ECR's message. A tag ECR takes but the backing registry cannot hold, one starting with '.' or '-' or longer than 128 characters, is refused with a message naming that limit, which docs/services/ecr.md now notes.
This commit is contained in:
1 parent
d0dac9814f
commit
c6551a47bc
15 files changed
+1563
-7
No files matched your search
@@ -7,11 +7,15 @@ import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import software.amazon.awssdk.core.SdkBytes;
|
||||
import software.amazon.awssdk.services.ecr.EcrClient;
|
||||
import software.amazon.awssdk.services.ecr.model.*;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -238,4 +242,58 @@ class EcrTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(13)
|
||||
@DisplayName("An image pushed through the layer API and PutImage reads back and retags")
|
||||
void pushThroughTheApiAndRetag() throws Exception {
|
||||
String repo = "floci-it/api-push-" + System.nanoTime();
|
||||
ecr.createRepository(b -> b.repositoryName(repo));
|
||||
reposToCleanup.add(repo);
|
||||
|
||||
String config = "{\"architecture\":\"amd64\",\"os\":\"linux\"}";
|
||||
String layer = "floci layer bytes";
|
||||
String configDigest = uploadLayer(repo, config.getBytes(StandardCharsets.UTF_8));
|
||||
String layerDigest = uploadLayer(repo, layer.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
BatchCheckLayerAvailabilityResponse check = ecr.batchCheckLayerAvailability(b -> b
|
||||
.repositoryName(repo).layerDigests(configDigest, layerDigest));
|
||||
assertThat(check.layers()).extracting(Layer::layerAvailability)
|
||||
.containsOnly(LayerAvailability.AVAILABLE);
|
||||
assertThat(check.failures()).isEmpty();
|
||||
|
||||
String manifest = "{\"schemaVersion\":2,\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\","
|
||||
+ "\"config\":{\"mediaType\":\"application/vnd.oci.image.config.v1+json\",\"size\":"
|
||||
+ config.length() + ",\"digest\":\"" + configDigest + "\"},"
|
||||
+ "\"layers\":[{\"mediaType\":\"application/vnd.oci.image.layer.v1.tar\",\"size\":"
|
||||
+ layer.length() + ",\"digest\":\"" + layerDigest + "\"}]}";
|
||||
PutImageResponse put = ecr.putImage(b -> b.repositoryName(repo).imageTag("v1").imageManifest(manifest));
|
||||
assertThat(put.image().imageId().imageDigest()).isEqualTo(sha256(manifest.getBytes(StandardCharsets.UTF_8)));
|
||||
|
||||
String read = ecr.batchGetImage(b -> b.repositoryName(repo)
|
||||
.imageIds(ImageIdentifier.builder().imageTag("v1").build()))
|
||||
.images().get(0).imageManifest();
|
||||
ecr.putImage(b -> b.repositoryName(repo).imageTag("release").imageManifest(read));
|
||||
assertThat(ecr.listImages(b -> b.repositoryName(repo)).imageIds())
|
||||
.extracting(ImageIdentifier::imageTag).contains("v1", "release");
|
||||
assertThatThrownBy(() -> ecr.putImage(b -> b.repositoryName(repo).imageTag("release").imageManifest(read)))
|
||||
.isInstanceOf(ImageAlreadyExistsException.class);
|
||||
|
||||
String url = ecr.getDownloadUrlForLayer(b -> b.repositoryName(repo).layerDigest(layerDigest)).downloadUrl();
|
||||
assertThat(url).endsWith("/v2/" + repo + "/blobs/" + layerDigest);
|
||||
}
|
||||
|
||||
private static String uploadLayer(String repo, byte[] bytes) throws Exception {
|
||||
String uploadId = ecr.initiateLayerUpload(b -> b.repositoryName(repo)).uploadId();
|
||||
UploadLayerPartResponse part = ecr.uploadLayerPart(b -> b.repositoryName(repo).uploadId(uploadId)
|
||||
.partFirstByte(0L).partLastByte((long) bytes.length - 1).layerPartBlob(SdkBytes.fromByteArray(bytes)));
|
||||
assertThat(part.lastByteReceived()).isEqualTo(bytes.length - 1L);
|
||||
String digest = sha256(bytes);
|
||||
return ecr.completeLayerUpload(b -> b.repositoryName(repo).uploadId(uploadId).layerDigests(digest))
|
||||
.layerDigest();
|
||||
}
|
||||
|
||||
private static String sha256(byte[] bytes) throws Exception {
|
||||
return "sha256:" + HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user