mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(codeartifact): support publishing and reading generic-format package versions (#3839)
Adds PublishPackageVersion, DescribePackageVersion, and GetPackageVersionAsset for CodeArtifact's generic package format, matching AWS's own wire restriction that PublishPackageVersion accepts only the generic format. Includes real asset hashing (MD5, SHA-1, SHA-256, SHA-512), SHA-256 integrity verification against the caller's x-amz-content-sha256 header, correct Unfinished/Published status transitions, and the documented AWS quotas for this action (350 assets per version, 5 GB max asset size), all returning ServiceQuotaExceededException when exceeded. Asset bytes are kept out of the JSON/WAL-backed package-version store (@JsonIgnore on PackageAsset.content) and instead go through a dedicated asset store, a disk file in persistent/hybrid/wal mode or a side map in memory mode, the same split S3Object/S3Service already uses for object bytes. Without this, a publish rewrote every asset of every package version on disk each time, growing quadratically under this action's own 350-asset/5GB quotas. The asset name is also validated against the wire model's AssetName pattern (no control characters), not just its length, since it is echoed back verbatim in the X-AssetName response header on GetPackageVersionAsset. Verified against the real botocore service-2.json wire model and the AWS CodeArtifact User Guide (including its PDF for the quotas table, since the HTML quotas page is JS-rendered). Live-verified with the real AWS CLI and the AWS SDK for Java v2 against a running Floci instance, including an exact byte-for-byte asset round-trip and the real ConflictException when republishing an already-Published version.
This commit is contained in:
1 parent
e190f7d340
commit
bbaf3568c5
10 files changed
+1158
-19
No files matched your search
+105
@@ -11,13 +11,18 @@ import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.DomainView;
|
||||
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.PackageVersionAssetResult;
|
||||
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.PublishPackageVersionResult;
|
||||
import io.github.hectorvent.floci.services.codeartifact.CodeArtifactService.ResourcePolicy;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactPackageVersion;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactRepository;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.ExternalConnection;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.PackageAsset;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.DELETE;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.HeaderParam;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.PUT;
|
||||
import jakarta.ws.rs.Path;
|
||||
@@ -272,6 +277,64 @@ public class CodeArtifactController {
|
||||
return ok(single("repository", repositoryDescription(r)));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------- package versions
|
||||
|
||||
@POST
|
||||
@Path("/v1/package/version/publish")
|
||||
@Consumes(MediaType.WILDCARD)
|
||||
public Response publishPackageVersion(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("repository") String repository,
|
||||
@QueryParam("format") String format,
|
||||
@QueryParam("namespace") String namespace,
|
||||
@QueryParam("package") String packageName,
|
||||
@QueryParam("version") String version,
|
||||
@QueryParam("asset") String assetName,
|
||||
@QueryParam("unfinished") String unfinished,
|
||||
@HeaderParam("x-amz-content-sha256") String assetSha256,
|
||||
byte[] body) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
PublishPackageVersionResult result = service.publishPackageVersion(region, domain, domainOwner, repository,
|
||||
format, namespace, packageName, version, assetName, assetSha256, unfinished, body);
|
||||
return ok(publishPackageVersionResponse(result));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/v1/package/version")
|
||||
public Response describePackageVersion(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("repository") String repository,
|
||||
@QueryParam("format") String format,
|
||||
@QueryParam("namespace") String namespace,
|
||||
@QueryParam("package") String packageName,
|
||||
@QueryParam("version") String version) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
CodeArtifactPackageVersion pv = service.describePackageVersion(region, domain, domainOwner, repository,
|
||||
format, namespace, packageName, version);
|
||||
return ok(single("packageVersion", packageVersionDescription(pv)));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/v1/package/version/asset")
|
||||
public Response getPackageVersionAsset(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("repository") String repository,
|
||||
@QueryParam("format") String format,
|
||||
@QueryParam("namespace") String namespace,
|
||||
@QueryParam("package") String packageName,
|
||||
@QueryParam("version") String version,
|
||||
@QueryParam("asset") String assetName,
|
||||
@QueryParam("revision") String revision) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
PackageVersionAssetResult result = service.getPackageVersionAsset(region, domain, domainOwner, repository,
|
||||
format, namespace, packageName, version, assetName, revision);
|
||||
return Response.ok(result.asset().getContent(), MediaType.APPLICATION_OCTET_STREAM)
|
||||
.header("X-AssetName", result.asset().getName())
|
||||
.header("X-PackageVersion", version)
|
||||
.header("X-PackageVersionRevision", result.packageVersionRevision())
|
||||
.build();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------- tags
|
||||
|
||||
@POST
|
||||
@@ -377,6 +440,48 @@ public class CodeArtifactController {
|
||||
return response;
|
||||
}
|
||||
|
||||
private ObjectNode publishPackageVersionResponse(PublishPackageVersionResult result) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("format", result.packageVersion().getFormat());
|
||||
if (result.packageVersion().getNamespace() != null) {
|
||||
node.put("namespace", result.packageVersion().getNamespace());
|
||||
}
|
||||
node.put("package", result.packageVersion().getPackageName());
|
||||
node.put("version", result.packageVersion().getVersion());
|
||||
node.put("versionRevision", result.packageVersion().getRevision());
|
||||
node.put("status", result.packageVersion().getStatus());
|
||||
node.set("asset", assetSummary(result.asset()));
|
||||
return node;
|
||||
}
|
||||
|
||||
private ObjectNode packageVersionDescription(CodeArtifactPackageVersion pv) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("format", pv.getFormat());
|
||||
if (pv.getNamespace() != null) {
|
||||
node.put("namespace", pv.getNamespace());
|
||||
}
|
||||
node.put("packageName", pv.getPackageName());
|
||||
node.put("version", pv.getVersion());
|
||||
node.put("revision", pv.getRevision());
|
||||
node.put("status", pv.getStatus());
|
||||
if (pv.getPublishedTime() != null) {
|
||||
node.put("publishedTime", pv.getPublishedTime());
|
||||
}
|
||||
ObjectNode origin = node.putObject("origin");
|
||||
origin.put("originType", "INTERNAL");
|
||||
node.putArray("licenses");
|
||||
return node;
|
||||
}
|
||||
|
||||
private ObjectNode assetSummary(PackageAsset asset) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("name", asset.getName());
|
||||
node.put("size", asset.getSize());
|
||||
ObjectNode hashes = node.putObject("hashes");
|
||||
asset.getHashes().forEach(hashes::put);
|
||||
return node;
|
||||
}
|
||||
|
||||
private ObjectNode resourcePolicy(ResourcePolicy policy) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("resourceArn", policy.resourceArn());
|
||||
|
||||
+314
-5
@@ -8,23 +8,40 @@ import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import io.github.hectorvent.floci.core.common.ServiceConfigAccess;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactDomain;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactPackageVersion;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactRepository;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.ExternalConnection;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.PackageAsset;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.AtomicMoveNotSupportedException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@ApplicationScoped
|
||||
public class CodeArtifactService implements Resettable {
|
||||
@@ -46,26 +63,73 @@ public class CodeArtifactService implements Resettable {
|
||||
Set.of("npm", "pypi", "maven", "nuget", "generic", "ruby", "swift", "cargo");
|
||||
private static final Set<String> ENDPOINT_TYPES = Set.of("dualstack", "ipv4");
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CodeArtifactService.class);
|
||||
|
||||
private static final Pattern DOMAIN_NAME = Pattern.compile("[a-z][a-z0-9\\-]{0,48}[a-z0-9]");
|
||||
private static final Pattern REPOSITORY_NAME = Pattern.compile("[A-Za-z0-9][A-Za-z0-9._\\-]{1,99}");
|
||||
private static final Pattern ACCOUNT_ID = Pattern.compile("[0-9]{12}");
|
||||
private static final Pattern PACKAGE_TOKEN = Pattern.compile("[^#/\\s]+");
|
||||
// Matches the wire model's AssetName pattern exactly: any non-empty string with no
|
||||
// control/format/unassigned/surrogate/private-use characters. Without this, an asset name
|
||||
// containing '\r'/'\n' would be echoed back verbatim in the X-AssetName response header.
|
||||
private static final Pattern ASSET_NAME = Pattern.compile("\\P{C}+");
|
||||
private static final List<String> ASSET_HASH_ALGORITHMS = List.of("MD5", "SHA-1", "SHA-256", "SHA-512");
|
||||
private static final long MAX_ASSET_FILE_SIZE_BYTES = 5L * 1024 * 1024 * 1024;
|
||||
private static final int MAX_ASSETS_PER_PACKAGE_VERSION = 350;
|
||||
private static final String ASSET_STORAGE_DIR = "codeartifact-assets";
|
||||
|
||||
public record DomainView(CodeArtifactDomain domain, int repositoryCount) {}
|
||||
public record ResourcePolicy(String resourceArn, String revision, String document) {}
|
||||
public record PublishPackageVersionResult(CodeArtifactPackageVersion packageVersion, PackageAsset asset) {}
|
||||
public record PackageVersionAssetResult(PackageAsset asset, String packageVersionRevision) {}
|
||||
|
||||
private final AccountAwareStorageBackend<CodeArtifactDomain> domains;
|
||||
private final AccountAwareStorageBackend<CodeArtifactRepository> repositories;
|
||||
private final AccountAwareStorageBackend<CodeArtifactPackageVersion> packageVersions;
|
||||
private final RegionResolver regionResolver;
|
||||
private final EmulatorConfig config;
|
||||
// Asset bytes never go through the JSON/WAL-backed `packageVersions` store (see PackageAsset's
|
||||
// @JsonIgnore content field): they're kept here instead, on disk in persistent/hybrid/wal mode
|
||||
// or in this map in memory mode, so a publish only ever rewrites metadata.
|
||||
private final boolean inMemory;
|
||||
private final Path assetRoot;
|
||||
private final ConcurrentHashMap<String, byte[]> memoryAssetStore = new ConcurrentHashMap<>();
|
||||
|
||||
@Inject
|
||||
public CodeArtifactService(StorageFactory storageFactory, RegionResolver regionResolver, EmulatorConfig config) {
|
||||
this.domains = storageFactory.create("codeartifact", "codeartifact-domains.json",
|
||||
new TypeReference<Map<String, CodeArtifactDomain>>() {});
|
||||
this.repositories = storageFactory.create("codeartifact", "codeartifact-repositories.json",
|
||||
new TypeReference<Map<String, CodeArtifactRepository>>() {});
|
||||
public CodeArtifactService(StorageFactory storageFactory, RegionResolver regionResolver, EmulatorConfig config,
|
||||
ServiceConfigAccess serviceConfigAccess) {
|
||||
this(
|
||||
storageFactory.create("codeartifact", "codeartifact-domains.json",
|
||||
new TypeReference<Map<String, CodeArtifactDomain>>() {}),
|
||||
storageFactory.create("codeartifact", "codeartifact-repositories.json",
|
||||
new TypeReference<Map<String, CodeArtifactRepository>>() {}),
|
||||
storageFactory.create("codeartifact", "codeartifact-package-versions.json",
|
||||
new TypeReference<Map<String, CodeArtifactPackageVersion>>() {}),
|
||||
regionResolver, config,
|
||||
"memory".equals(serviceConfigAccess.storageMode("codeartifact")),
|
||||
Path.of(config.storage().persistentPath()).resolve(ASSET_STORAGE_DIR));
|
||||
}
|
||||
|
||||
/** Package-private constructor for testing. */
|
||||
CodeArtifactService(AccountAwareStorageBackend<CodeArtifactDomain> domains,
|
||||
AccountAwareStorageBackend<CodeArtifactRepository> repositories,
|
||||
AccountAwareStorageBackend<CodeArtifactPackageVersion> packageVersions,
|
||||
RegionResolver regionResolver, EmulatorConfig config,
|
||||
boolean inMemory, Path assetRoot) {
|
||||
this.domains = domains;
|
||||
this.repositories = repositories;
|
||||
this.packageVersions = packageVersions;
|
||||
this.regionResolver = regionResolver;
|
||||
this.config = config;
|
||||
this.inMemory = inMemory;
|
||||
this.assetRoot = assetRoot;
|
||||
if (!inMemory) {
|
||||
try {
|
||||
Files.createDirectories(assetRoot);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("Failed to create CodeArtifact asset data directory: " + assetRoot, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- domains
|
||||
@@ -350,6 +414,120 @@ public class CodeArtifactService implements Resettable {
|
||||
return r;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------- package versions
|
||||
|
||||
public synchronized PublishPackageVersionResult publishPackageVersion(String region, String domain,
|
||||
String domainOwner, String repository, String format, String namespace, String packageName,
|
||||
String version, String assetName, String assetSha256, String unfinishedParam, byte[] content) {
|
||||
if (!"generic".equals(format)) {
|
||||
throw validation("format must be 'generic'; PublishPackageVersion only supports the generic "
|
||||
+ "package format.");
|
||||
}
|
||||
boolean unfinished = parseBoolean(unfinishedParam);
|
||||
validatePackageToken("package", packageName);
|
||||
validatePackageToken("packageVersion", version);
|
||||
if (namespace != null) {
|
||||
validatePackageToken("namespace", namespace);
|
||||
}
|
||||
validateAssetName(assetName);
|
||||
if (assetSha256 == null || !SigV4RequestValidator.isSha256Hex(assetSha256)) {
|
||||
throw validation("assetSHA256 is required and must be a 64-character SHA-256 hex digest.");
|
||||
}
|
||||
byte[] assetContent = content != null ? content : new byte[0];
|
||||
if (assetContent.length > MAX_ASSET_FILE_SIZE_BYTES) {
|
||||
throw new AwsException("ServiceQuotaExceededException",
|
||||
"The maximum asset file size is 5 Gigabytes.", 402);
|
||||
}
|
||||
String actualSha256 = sha256Hex(assetContent);
|
||||
if (!assetSha256.equalsIgnoreCase(actualSha256)) {
|
||||
throw validation("assetSHA256 does not match the SHA-256 hash of the uploaded content.");
|
||||
}
|
||||
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
requireRepository(owner, repositoryKey(region, domain, repository));
|
||||
String key = packageVersionKey(region, domain, repository, format, namespace, packageName, version);
|
||||
|
||||
CodeArtifactPackageVersion pv = packageVersions.getForAccount(owner, key).orElse(null);
|
||||
if (pv != null && "Published".equals(pv.getStatus())) {
|
||||
throw conflict("Package version '" + version + "' of package '" + packageName + "' is already "
|
||||
+ "Published; no additional assets can be uploaded to it.");
|
||||
}
|
||||
if (pv == null) {
|
||||
pv = new CodeArtifactPackageVersion();
|
||||
pv.setDomainName(domain);
|
||||
pv.setDomainOwner(owner);
|
||||
pv.setRegion(region);
|
||||
pv.setRepositoryName(repository);
|
||||
pv.setFormat(format);
|
||||
pv.setNamespace(namespace);
|
||||
pv.setPackageName(packageName);
|
||||
pv.setVersion(version);
|
||||
}
|
||||
|
||||
Map<String, PackageAsset> assets = new LinkedHashMap<>(pv.getAssets());
|
||||
if (!assets.containsKey(assetName) && assets.size() >= MAX_ASSETS_PER_PACKAGE_VERSION) {
|
||||
throw new AwsException("ServiceQuotaExceededException",
|
||||
"A package version can have a maximum of " + MAX_ASSETS_PER_PACKAGE_VERSION + " assets.", 402);
|
||||
}
|
||||
|
||||
PackageAsset asset = new PackageAsset();
|
||||
asset.setName(assetName);
|
||||
asset.setSize(assetContent.length);
|
||||
asset.setContent(assetContent);
|
||||
asset.setHashes(computeHashes(assetContent));
|
||||
writeAssetContent(owner, key, assetName, assetContent);
|
||||
assets.put(assetName, asset);
|
||||
pv.setAssets(assets);
|
||||
|
||||
pv.setStatus(unfinished ? "Unfinished" : "Published");
|
||||
if (!unfinished && pv.getPublishedTime() == null) {
|
||||
pv.setPublishedTime(Instant.now().getEpochSecond());
|
||||
}
|
||||
pv.setRevision(newRevision());
|
||||
|
||||
packageVersions.putForAccount(owner, key, pv);
|
||||
return new PublishPackageVersionResult(pv, asset);
|
||||
}
|
||||
|
||||
public CodeArtifactPackageVersion describePackageVersion(String region, String domain, String domainOwner,
|
||||
String repository, String format, String namespace, String packageName, String version) {
|
||||
if (format == null || !PACKAGE_FORMATS.contains(format)) {
|
||||
throw validation("format must be one of " + PACKAGE_FORMATS + ".");
|
||||
}
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
String key = packageVersionKey(region, domain, repository, format, namespace, packageName, version);
|
||||
return packageVersions.getForAccount(owner, key)
|
||||
.orElseThrow(() -> notFound("Package version '" + version + "' of package '" + packageName
|
||||
+ "' was not found."));
|
||||
}
|
||||
|
||||
public PackageVersionAssetResult getPackageVersionAsset(String region, String domain, String domainOwner,
|
||||
String repository, String format, String namespace, String packageName, String version,
|
||||
String assetName, String packageVersionRevision) {
|
||||
if (format == null || !PACKAGE_FORMATS.contains(format)) {
|
||||
throw validation("format must be one of " + PACKAGE_FORMATS + ".");
|
||||
}
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
String key = packageVersionKey(region, domain, repository, format, namespace, packageName, version);
|
||||
CodeArtifactPackageVersion pv = packageVersions.getForAccount(owner, key)
|
||||
.orElseThrow(() -> notFound("Package version '" + version + "' of package '" + packageName
|
||||
+ "' was not found."));
|
||||
if (packageVersionRevision != null && !packageVersionRevision.equals(pv.getRevision())) {
|
||||
throw notFound("Package version '" + version + "' was not found at revision '"
|
||||
+ packageVersionRevision + "'.");
|
||||
}
|
||||
PackageAsset asset = pv.getAssets().get(assetName);
|
||||
if (asset == null) {
|
||||
throw notFound("Asset '" + assetName + "' was not found on package version '" + version + "'.");
|
||||
}
|
||||
// The live object's content may already be populated (same JVM as the publish that set
|
||||
// it), but it isn't guaranteed to be: a reload from the persisted store never restores it,
|
||||
// since it's excluded from that JSON/WAL. Always re-read from the asset store itself
|
||||
// rather than trusting whatever happens to already be on the object.
|
||||
asset.setContent(readAssetContent(owner, key, assetName));
|
||||
return new PackageVersionAssetResult(asset, pv.getRevision());
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------- tags
|
||||
|
||||
public synchronized void tagResource(String resourceArn, Map<String, String> newTags) {
|
||||
@@ -398,6 +576,12 @@ public class CodeArtifactService implements Resettable {
|
||||
public void clear() {
|
||||
domains.clear();
|
||||
repositories.clear();
|
||||
packageVersions.clear();
|
||||
if (inMemory) {
|
||||
memoryAssetStore.clear();
|
||||
} else {
|
||||
deleteAssetRoot();
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------- helpers
|
||||
@@ -522,6 +706,125 @@ public class CodeArtifactService implements Resettable {
|
||||
return merged;
|
||||
}
|
||||
|
||||
private static void validatePackageToken(String field, String value) {
|
||||
if (value == null || value.isEmpty() || value.length() > 255 || !PACKAGE_TOKEN.matcher(value).matches()) {
|
||||
throw validation(field + " must be 1-255 characters with no '#', '/', or whitespace.");
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateAssetName(String assetName) {
|
||||
if (assetName == null || assetName.isEmpty() || assetName.length() > 255
|
||||
|| !ASSET_NAME.matcher(assetName).matches()) {
|
||||
throw validation("asset must be 1-255 characters with no control characters.");
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------- asset bytes
|
||||
|
||||
private void writeAssetContent(String owner, String packageVersionKey, String assetName, byte[] content) {
|
||||
if (inMemory) {
|
||||
memoryAssetStore.put(assetStoreKey(owner, packageVersionKey, assetName), content);
|
||||
return;
|
||||
}
|
||||
Path filePath = resolveAssetPath(owner, packageVersionKey, assetName);
|
||||
try {
|
||||
Files.createDirectories(filePath.getParent());
|
||||
Path tmp = filePath.resolveSibling(filePath.getFileName() + ".tmp-" + UUID.randomUUID());
|
||||
try {
|
||||
Files.write(tmp, content);
|
||||
try {
|
||||
Files.move(tmp, filePath, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (AtomicMoveNotSupportedException e) {
|
||||
Files.move(tmp, filePath, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
} finally {
|
||||
Files.deleteIfExists(tmp);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("Failed to write CodeArtifact asset file: " + filePath, e);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] readAssetContent(String owner, String packageVersionKey, String assetName) {
|
||||
if (inMemory) {
|
||||
byte[] content = memoryAssetStore.get(assetStoreKey(owner, packageVersionKey, assetName));
|
||||
return content != null ? content : new byte[0];
|
||||
}
|
||||
Path filePath = resolveAssetPath(owner, packageVersionKey, assetName);
|
||||
try {
|
||||
return Files.readAllBytes(filePath);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("Failed to read CodeArtifact asset file: " + filePath, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String assetStoreKey(String owner, String packageVersionKey, String assetName) {
|
||||
return owner + "::" + packageVersionKey + "::" + assetName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Both path segments are SHA-256 hex digests, never the raw names: an asset name may contain
|
||||
* '/', '.', '..' or run long, and the version key can exceed a filesystem's filename limit, so
|
||||
* using either directly would let distinct legal names collide or fail. Digests are fixed
|
||||
* length, path-safe, and cannot escape the directory.
|
||||
*/
|
||||
private Path resolveAssetPath(String owner, String packageVersionKey, String assetName) {
|
||||
return assetRoot.resolve(owner)
|
||||
.resolve(sha256Hex(packageVersionKey.getBytes(StandardCharsets.UTF_8)))
|
||||
.resolve(sha256Hex(assetName.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
private void deleteAssetRoot() {
|
||||
if (!Files.isDirectory(assetRoot)) {
|
||||
return;
|
||||
}
|
||||
try (Stream<Path> paths = Files.walk(assetRoot)) {
|
||||
paths.sorted(Comparator.reverseOrder()).forEach(p -> {
|
||||
try {
|
||||
Files.deleteIfExists(p);
|
||||
} catch (IOException e) {
|
||||
LOG.errorv(e, "Failed to delete CodeArtifact asset file: {0}", p);
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
LOG.errorv(e, "Failed to reset CodeArtifact asset storage under {0}", assetRoot);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean parseBoolean(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {
|
||||
return Boolean.parseBoolean(value);
|
||||
}
|
||||
throw validation("unfinished must be 'true' or 'false'.");
|
||||
}
|
||||
|
||||
private static Map<String, String> computeHashes(byte[] content) {
|
||||
Map<String, String> hashes = new LinkedHashMap<>();
|
||||
for (String algorithm : ASSET_HASH_ALGORITHMS) {
|
||||
hashes.put(algorithm, SigV4RequestValidator.hexEncode(digest(algorithm, content)));
|
||||
}
|
||||
return hashes;
|
||||
}
|
||||
|
||||
private static String sha256Hex(byte[] content) {
|
||||
try {
|
||||
return SigV4RequestValidator.sha256Hex(content);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("JVM does not support SHA-256", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] digest(String algorithm, byte[] content) {
|
||||
try {
|
||||
return MessageDigest.getInstance(algorithm).digest(content);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException("JVM does not support " + algorithm, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String domainKey(String region, String domain) {
|
||||
return region + "::" + domain;
|
||||
}
|
||||
@@ -530,6 +833,12 @@ public class CodeArtifactService implements Resettable {
|
||||
return region + "::" + domain + "::" + repository;
|
||||
}
|
||||
|
||||
private static String packageVersionKey(String region, String domain, String repository, String format,
|
||||
String namespace, String packageName, String version) {
|
||||
return region + "::" + domain + "::" + repository + "::" + format + "::"
|
||||
+ (namespace == null ? "" : namespace) + "::" + packageName + "::" + version;
|
||||
}
|
||||
|
||||
private static AwsException validation(String message) {
|
||||
return new AwsException("ValidationException", message, 400);
|
||||
}
|
||||
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class CodeArtifactPackageVersion {
|
||||
private String domainName;
|
||||
private String domainOwner;
|
||||
private String region;
|
||||
private String repositoryName;
|
||||
private String format;
|
||||
private String namespace;
|
||||
private String packageName;
|
||||
private String version;
|
||||
private String status;
|
||||
private String revision;
|
||||
private Long publishedTime;
|
||||
private Map<String, PackageAsset> assets = new LinkedHashMap<>();
|
||||
|
||||
public String getDomainName() {
|
||||
return domainName;
|
||||
}
|
||||
|
||||
public void setDomainName(String domainName) {
|
||||
this.domainName = domainName;
|
||||
}
|
||||
|
||||
public String getDomainOwner() {
|
||||
return domainOwner;
|
||||
}
|
||||
|
||||
public void setDomainOwner(String domainOwner) {
|
||||
this.domainOwner = domainOwner;
|
||||
}
|
||||
|
||||
public String getRegion() {
|
||||
return region;
|
||||
}
|
||||
|
||||
public void setRegion(String region) {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public String getRepositoryName() {
|
||||
return repositoryName;
|
||||
}
|
||||
|
||||
public void setRepositoryName(String repositoryName) {
|
||||
this.repositoryName = repositoryName;
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public void setFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
public void setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
public String getPackageName() {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
public void setPackageName(String packageName) {
|
||||
this.packageName = packageName;
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(String version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getRevision() {
|
||||
return revision;
|
||||
}
|
||||
|
||||
public void setRevision(String revision) {
|
||||
this.revision = revision;
|
||||
}
|
||||
|
||||
public Long getPublishedTime() {
|
||||
return publishedTime;
|
||||
}
|
||||
|
||||
public void setPublishedTime(Long publishedTime) {
|
||||
this.publishedTime = publishedTime;
|
||||
}
|
||||
|
||||
public Map<String, PackageAsset> getAssets() {
|
||||
return Collections.unmodifiableMap(assets);
|
||||
}
|
||||
|
||||
public void setAssets(Map<String, PackageAsset> assets) {
|
||||
this.assets = assets == null ? new LinkedHashMap<>() : new LinkedHashMap<>(assets);
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class PackageAsset {
|
||||
private String name;
|
||||
private long size;
|
||||
// Kept out of the persisted package-version JSON/WAL: CodeArtifactService writes and reads
|
||||
// these bytes through its own asset store (disk file in persistent/hybrid/wal mode, a side
|
||||
// map in memory mode) instead, so a publish never rewrites every asset of every package
|
||||
// version just to append one more.
|
||||
@JsonIgnore
|
||||
private byte[] content = new byte[0];
|
||||
private Map<String, String> hashes = new LinkedHashMap<>();
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(long size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public byte[] getContent() {
|
||||
return content.clone();
|
||||
}
|
||||
|
||||
public void setContent(byte[] content) {
|
||||
this.content = content == null ? new byte[0] : content.clone();
|
||||
}
|
||||
|
||||
public Map<String, String> getHashes() {
|
||||
return Collections.unmodifiableMap(hashes);
|
||||
}
|
||||
|
||||
public void setHashes(Map<String, String> hashes) {
|
||||
this.hashes = hashes == null ? new LinkedHashMap<>() : new LinkedHashMap<>(hashes);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user