mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(codeartifact): add CodeArtifact control-plane support (#3713)
Floci had zero CodeArtifact support: no service package, no docs, nothing. Any terraform apply touching aws_codeartifact_domain, aws_codeartifact_repository, or either permissions-policy resource failed outright. This adds the full CodeArtifact control plane: domain and repository CRUD, resource policies with optimistic-locking revisions, tags, and the fixed set of real AWS-hosted external (upstream) connections (public:npmjs, public:pypi, public:maven-central, etc.). Package publish/fetch endpoints per format (npm, PyPI, Maven, NuGet, generic) are intentionally out of scope here: this closes the terraform apply gap, not real package serving. ListTagsForResource (POST /v1/tags?resourceArn=) is CodeArtifact's one tag operation that addresses the resource with a query parameter on the bare path rather than a path segment, unlike every other V1TagsController/SharedTagsController handler. That method lives on V1TagsController itself rather than the shared AbstractTagsController base: SharedTagsController (mounted at /tags) already declares its own bare POST (tagResourceByBody, for TagResource), so a bare POST on the shared base collides with it and Quarkus refuses to start. CodeArtifact's handler is @V1Tags-qualified and only ever registered against V1TagsController, so the method belongs there. core/common stays service-free: CodeArtifactTagHandler registers itself as a bean the same way MskTagHandler does, and TagHandler gained a default for the lowercase key/value entry names CodeArtifact uses instead of AWS's usual capitalized Key/Value. Known, documented limitations (see docs/services/codeartifact.md): no cycle detection across upstream chains, no referential-integrity check when deleting a repository other repositories reference as an upstream, and no authorization check on cross-account domainOwner addressing (consistent with Floci's opt-in IAM enforcement elsewhere). Verified against the real botocore codeartifact service-2.json (protocol, exact querystring vs. body placement per operation, exact ARN format, epoch-seconds timestamps, real error shapes) and the live AWS API docs for behavioral rules not in the wire model (DeleteDomain blocked while repositories exist, the real fixed external-connection name/format list, PutDomainPermissionsPolicy's all-body field placement vs. the sibling repository operation's querystring placement). Live-verified three ways: - Unit tests including a concurrent-create race test proving the synchronized guard actually holds under contention, plus full-stack QuarkusTest/RestAssured integration tests. - A real terraform init/apply/plan/destroy cycle against a running Floci instance, independently confirming destroy actually tore the domain down via a follow-up describe-domain call. - A live AWS SDK v2 Java test exercising the real CodeartifactClient against a live instance.
This commit is contained in:
1 parent
f83341e013
commit
66bcdaf675
26 files changed
+2410
-6
No files matched your search
@@ -789,9 +789,15 @@ public interface EmulatorConfig {
|
||||
LakeFormationServiceConfig lakeformation();
|
||||
EfsServiceConfig efs();
|
||||
CodeGuruReviewerServiceConfig codegurureviewer();
|
||||
CodeArtifactServiceConfig codeartifact();
|
||||
MarketplaceServiceConfig marketplace();
|
||||
}
|
||||
|
||||
interface CodeArtifactServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface ConnectServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
|
||||
@@ -709,6 +709,11 @@ public class ResolvedServiceCatalog {
|
||||
protocols(ServiceProtocol.REST_JSON),
|
||||
Set.of(), Set.of("codeguru-reviewer"), Set.of(),
|
||||
Set.of(io.github.hectorvent.floci.services.codegurureviewer.CodeGuruReviewerController.class)),
|
||||
descriptor("codeartifact", "codeartifact", config.services().codeartifact().enabled(), true,
|
||||
"codeartifact", config.storage().mode(), 5000L, null, ServiceProtocol.REST_JSON,
|
||||
protocols(ServiceProtocol.REST_JSON),
|
||||
Set.of(), Set.of("codeartifact"), Set.of(),
|
||||
Set.of(io.github.hectorvent.floci.services.codeartifact.CodeArtifactController.class)),
|
||||
descriptor("verifiedpermissions", "verifiedpermissions", config.services().verifiedpermissions().enabled(), true,
|
||||
"verifiedpermissions", config.storage().mode(), 5000L, null, ServiceProtocol.JSON,
|
||||
protocols(ServiceProtocol.JSON),
|
||||
|
||||
@@ -113,8 +113,8 @@ public class TagDispatcher {
|
||||
ArrayNode arr = root.putArray(key);
|
||||
tags.forEach((k, v) -> {
|
||||
ObjectNode entry = arr.addObject();
|
||||
entry.put("Key", k);
|
||||
entry.put("Value", v);
|
||||
entry.put(handler.tagEntryKeyName(), k);
|
||||
entry.put(handler.tagEntryValueName(), v);
|
||||
});
|
||||
} else {
|
||||
ObjectNode tagsNode = root.putObject(key);
|
||||
@@ -147,8 +147,8 @@ public class TagDispatcher {
|
||||
return tags;
|
||||
}
|
||||
for (JsonNode entry : tagNode) {
|
||||
JsonNode k = entry.get("Key");
|
||||
JsonNode v = entry.get("Value");
|
||||
JsonNode k = entry.get(handler.tagEntryKeyName());
|
||||
JsonNode v = entry.get(handler.tagEntryValueName());
|
||||
if (k == null || k.isNull() || v == null || v.isNull()) {
|
||||
if (handler.strictTagValidation()) {
|
||||
throw new AwsException("ValidationException",
|
||||
|
||||
@@ -106,6 +106,24 @@ public interface TagHandler {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON member name for a tag entry's key when {@link #tagsBodyIsList()} is {@code true}.
|
||||
* Defaults to {@code "Key"}, matching the great majority of list-shaped AWS services.
|
||||
* Override to {@code "key"} for services that lowercase entry members (CodeArtifact).
|
||||
*/
|
||||
default String tagEntryKeyName() {
|
||||
return "Key";
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON member name for a tag entry's value when {@link #tagsBodyIsList()} is {@code true}.
|
||||
* Defaults to {@code "Value"}, matching the great majority of list-shaped AWS services.
|
||||
* Override to {@code "value"} for services that lowercase entry members (CodeArtifact).
|
||||
*/
|
||||
default String tagEntryValueName() {
|
||||
return "Value";
|
||||
}
|
||||
|
||||
/** HTTP status for successful path-based TagResource requests. */
|
||||
default int tagResourceSuccessStatus() {
|
||||
return 204;
|
||||
|
||||
@@ -3,13 +3,20 @@ package io.github.hectorvent.floci.core.common;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.QueryParam;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
/**
|
||||
* Dispatcher for the AWS services that put their tag endpoints on {@code /v1/tags/{resourceArn}}
|
||||
* (AppSync, MSK).
|
||||
* (AppSync, MSK) or, for CodeArtifact, on the bare {@code /v1/tags} path with {@code resourceArn}
|
||||
* as a query parameter.
|
||||
*
|
||||
* <p>Same problem and same resolution as {@link SharedTagsController}, one path up: AWS tells
|
||||
* these services apart by hostname, floci serves them all on one port, so the owning service is
|
||||
@@ -26,4 +33,20 @@ public class V1TagsController extends AbstractTagsController {
|
||||
ObjectMapper objectMapper) {
|
||||
super(new TagDispatcher(handlers, regionResolver, objectMapper));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code ListTagsForResource} for services that address the resource with a query
|
||||
* parameter on the bare path ({@code POST /v1/tags?resourceArn=...}) rather than a path
|
||||
* segment. CodeArtifact is the only registered handler on this shape today.
|
||||
*
|
||||
* <p>This lives here rather than on {@link AbstractTagsController} because
|
||||
* {@link SharedTagsController} already declares its own bare {@code POST} ({@code
|
||||
* tagResourceByBody}), and a bare {@code POST} on the shared base would collide with it.
|
||||
*/
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response listTagsForResourceByQuery(@Context HttpHeaders headers,
|
||||
@QueryParam("resourceArn") String resourceArn) {
|
||||
return dispatcher().listTagsForArn(headers, resourceArn);
|
||||
}
|
||||
}
|
||||
+437
@@ -0,0 +1,437 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils;
|
||||
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.ResourcePolicy;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.CodeArtifactRepository;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.ExternalConnection;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.DELETE;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.PUT;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.QueryParam;
|
||||
import jakarta.ws.rs.WebApplicationException;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Path("/")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class CodeArtifactController {
|
||||
|
||||
private final CodeArtifactService service;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public CodeArtifactController(CodeArtifactService service, RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper) {
|
||||
this.service = service;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- domains
|
||||
|
||||
@POST
|
||||
@Path("/v1/domain")
|
||||
public Response createDomain(@Context HttpHeaders headers, @QueryParam("domain") String domain, String body) {
|
||||
JsonNode req = readTree(body);
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
DomainView view = service.createDomain(region, domain, text(req, "encryptionKey"), readTagList(req.get("tags")));
|
||||
return ok(single("domain", domainDescription(view)));
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/v1/domain")
|
||||
public Response deleteDomain(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
DomainView view = service.deleteDomain(region, domain, domainOwner);
|
||||
return ok(single("domain", domainDescription(view)));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/v1/domain")
|
||||
public Response describeDomain(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
DomainView view = service.describeDomain(region, domain, domainOwner);
|
||||
return ok(single("domain", domainDescription(view)));
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/v1/domains")
|
||||
public Response listDomains(@Context HttpHeaders headers, String body) {
|
||||
JsonNode req = readTree(body);
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
Integer maxResults = req.hasNonNull("maxResults") ? req.get("maxResults").asInt() : null;
|
||||
PaginatedResult<DomainView> page = service.listDomains(region, maxResults, text(req, "nextToken"));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode items = response.putArray("domains");
|
||||
page.items().forEach(view -> items.add(domainSummary(view)));
|
||||
if (page.nextToken() != null) {
|
||||
response.put("nextToken", page.nextToken());
|
||||
}
|
||||
return ok(response);
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/v1/domain/permissions/policy")
|
||||
public Response putDomainPermissionsPolicy(@Context HttpHeaders headers, String body) {
|
||||
JsonNode req = readTree(body);
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
ResourcePolicy policy = service.putDomainPermissionsPolicy(region, text(req, "domain"),
|
||||
text(req, "domainOwner"), text(req, "policyDocument"), text(req, "policyRevision"));
|
||||
return ok(single("policy", resourcePolicy(policy)));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/v1/domain/permissions/policy")
|
||||
public Response getDomainPermissionsPolicy(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
ResourcePolicy policy = service.getDomainPermissionsPolicy(region, domain, domainOwner);
|
||||
return ok(single("policy", resourcePolicy(policy)));
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/v1/domain/permissions/policy")
|
||||
public Response deleteDomainPermissionsPolicy(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("policy-revision") String policyRevision) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
ResourcePolicy policy = service.deleteDomainPermissionsPolicy(region, domain, domainOwner, policyRevision);
|
||||
return ok(single("policy", resourcePolicy(policy)));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ repositories
|
||||
|
||||
@POST
|
||||
@Path("/v1/repository")
|
||||
public Response createRepository(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("repository") String repository, String body) {
|
||||
JsonNode req = readTree(body);
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
CodeArtifactRepository r = service.createRepository(region, domain, domainOwner, repository,
|
||||
text(req, "description"), readUpstreams(req.get("upstreams")), readTagList(req.get("tags")));
|
||||
return ok(single("repository", repositoryDescription(r)));
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/v1/repository")
|
||||
public Response deleteRepository(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("repository") String repository) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
CodeArtifactRepository r = service.deleteRepository(region, domain, domainOwner, repository);
|
||||
return ok(single("repository", repositoryDescription(r)));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/v1/repository")
|
||||
public Response describeRepository(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("repository") String repository) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
CodeArtifactRepository r = service.describeRepository(region, domain, domainOwner, repository);
|
||||
return ok(single("repository", repositoryDescription(r)));
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/v1/repository")
|
||||
public Response updateRepository(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("repository") String repository, String body) {
|
||||
JsonNode req = readTree(body);
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
CodeArtifactRepository r = service.updateRepository(region, domain, domainOwner, repository,
|
||||
text(req, "description"), req.has("upstreams") ? readUpstreams(req.get("upstreams")) : null);
|
||||
return ok(single("repository", repositoryDescription(r)));
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/v1/repositories")
|
||||
public Response listRepositories(@Context HttpHeaders headers,
|
||||
@QueryParam("repository-prefix") String repositoryPrefix,
|
||||
@QueryParam("max-results") String maxResults,
|
||||
@QueryParam("next-token") String nextToken) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
PaginatedResult<CodeArtifactRepository> page = service.listRepositories(region, repositoryPrefix,
|
||||
Pagination.parseMaxResults(maxResults, "ValidationException"), nextToken);
|
||||
return ok(repositorySummaryList(page));
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/v1/domain/repositories")
|
||||
public Response listRepositoriesInDomain(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("administrator-account") String administratorAccount,
|
||||
@QueryParam("repository-prefix") String repositoryPrefix,
|
||||
@QueryParam("max-results") String maxResults,
|
||||
@QueryParam("next-token") String nextToken) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
PaginatedResult<CodeArtifactRepository> page = service.listRepositoriesInDomain(region, domain, domainOwner,
|
||||
administratorAccount, repositoryPrefix, Pagination.parseMaxResults(maxResults, "ValidationException"),
|
||||
nextToken);
|
||||
return ok(repositorySummaryList(page));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/v1/repository/endpoint")
|
||||
public Response getRepositoryEndpoint(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("repository") String repository,
|
||||
@QueryParam("format") String format,
|
||||
@QueryParam("endpointType") String endpointType) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
String endpoint = service.getRepositoryEndpoint(region, domain, domainOwner, repository, format,
|
||||
endpointType);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("repositoryEndpoint", endpoint);
|
||||
return ok(response);
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/v1/repository/permissions/policy")
|
||||
public Response putRepositoryPermissionsPolicy(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("repository") String repository, String body) {
|
||||
JsonNode req = readTree(body);
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
ResourcePolicy policy = service.putRepositoryPermissionsPolicy(region, domain, domainOwner, repository,
|
||||
text(req, "policyDocument"), text(req, "policyRevision"));
|
||||
return ok(single("policy", resourcePolicy(policy)));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/v1/repository/permissions/policy")
|
||||
public Response getRepositoryPermissionsPolicy(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("repository") String repository) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
ResourcePolicy policy = service.getRepositoryPermissionsPolicy(region, domain, domainOwner, repository);
|
||||
return ok(single("policy", resourcePolicy(policy)));
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/v1/repository/permissions/policies")
|
||||
public Response deleteRepositoryPermissionsPolicy(@Context HttpHeaders headers,
|
||||
@QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("repository") String repository,
|
||||
@QueryParam("policy-revision") String policyRevision) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
ResourcePolicy policy = service.deleteRepositoryPermissionsPolicy(region, domain, domainOwner, repository,
|
||||
policyRevision);
|
||||
return ok(single("policy", resourcePolicy(policy)));
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/v1/repository/external-connection")
|
||||
public Response associateExternalConnection(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("repository") String repository,
|
||||
@QueryParam("external-connection") String externalConnection) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
CodeArtifactRepository r = service.associateExternalConnection(region, domain, domainOwner, repository,
|
||||
externalConnection);
|
||||
return ok(single("repository", repositoryDescription(r)));
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/v1/repository/external-connection")
|
||||
public Response disassociateExternalConnection(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@QueryParam("domain-owner") String domainOwner,
|
||||
@QueryParam("repository") String repository,
|
||||
@QueryParam("external-connection") String externalConnection) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
CodeArtifactRepository r = service.disassociateExternalConnection(region, domain, domainOwner, repository,
|
||||
externalConnection);
|
||||
return ok(single("repository", repositoryDescription(r)));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------- tags
|
||||
|
||||
@POST
|
||||
@Path("/v1/tag")
|
||||
public Response tagResource(@QueryParam("resourceArn") String resourceArn, String body) {
|
||||
JsonNode req = readTree(body);
|
||||
service.tagResource(resourceArn, readTagList(req.get("tags")));
|
||||
return ok(objectMapper.createObjectNode());
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/v1/untag")
|
||||
public Response untagResource(@QueryParam("resourceArn") String resourceArn, String body) {
|
||||
JsonNode req = readTree(body);
|
||||
List<String> keys = new ArrayList<>();
|
||||
if (req.hasNonNull("tagKeys") && req.get("tagKeys").isArray()) {
|
||||
req.get("tagKeys").forEach(n -> keys.add(n.asText()));
|
||||
}
|
||||
service.untagResource(resourceArn, keys);
|
||||
return ok(objectMapper.createObjectNode());
|
||||
}
|
||||
|
||||
// ListTagsForResource (POST /v1/tags?resourceArn=) is handled by V1TagsController via
|
||||
// CodeArtifactTagHandler; see their javadoc.
|
||||
|
||||
// ----------------------------------------------------------------- helpers
|
||||
|
||||
private ObjectNode domainDescription(DomainView view) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("arn", view.domain().getArn());
|
||||
node.put("assetSizeBytes", 0L);
|
||||
node.put("createdTime", view.domain().getCreatedTime());
|
||||
node.put("encryptionKey", view.domain().getEncryptionKey());
|
||||
node.put("name", view.domain().getName());
|
||||
node.put("owner", view.domain().getOwner());
|
||||
node.put("repositoryCount", view.repositoryCount());
|
||||
// AWS's internal per-domain bucket naming is not publicly documented; this is a synthesized placeholder.
|
||||
node.put("s3BucketArn", "arn:aws:s3:::codeartifact-" + view.domain().getRegion() + "-" + view.domain().getOwner());
|
||||
node.put("status", "Active");
|
||||
return node;
|
||||
}
|
||||
|
||||
private ObjectNode domainSummary(DomainView view) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("name", view.domain().getName());
|
||||
node.put("owner", view.domain().getOwner());
|
||||
node.put("arn", view.domain().getArn());
|
||||
node.put("status", "Active");
|
||||
node.put("createdTime", view.domain().getCreatedTime());
|
||||
node.put("encryptionKey", view.domain().getEncryptionKey());
|
||||
return node;
|
||||
}
|
||||
|
||||
private ObjectNode repositoryDescription(CodeArtifactRepository r) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("administratorAccount", r.getAdministratorAccount());
|
||||
node.put("arn", r.getArn());
|
||||
node.put("createdTime", r.getCreatedTime());
|
||||
if (r.getDescription() != null) {
|
||||
node.put("description", r.getDescription());
|
||||
}
|
||||
node.put("domainName", r.getDomainName());
|
||||
node.put("domainOwner", r.getDomainOwner());
|
||||
ArrayNode connections = node.putArray("externalConnections");
|
||||
for (ExternalConnection ec : r.getExternalConnections()) {
|
||||
ObjectNode c = objectMapper.createObjectNode();
|
||||
c.put("externalConnectionName", ec.getExternalConnectionName());
|
||||
c.put("packageFormat", ec.getPackageFormat());
|
||||
c.put("status", ec.getStatus());
|
||||
connections.add(c);
|
||||
}
|
||||
node.put("name", r.getName());
|
||||
ArrayNode upstreams = node.putArray("upstreams");
|
||||
for (String upstream : r.getUpstreams()) {
|
||||
ObjectNode u = objectMapper.createObjectNode();
|
||||
u.put("repositoryName", upstream);
|
||||
upstreams.add(u);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private ObjectNode repositorySummary(CodeArtifactRepository r) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("name", r.getName());
|
||||
node.put("administratorAccount", r.getAdministratorAccount());
|
||||
node.put("domainName", r.getDomainName());
|
||||
node.put("domainOwner", r.getDomainOwner());
|
||||
node.put("arn", r.getArn());
|
||||
if (r.getDescription() != null) {
|
||||
node.put("description", r.getDescription());
|
||||
}
|
||||
node.put("createdTime", r.getCreatedTime());
|
||||
return node;
|
||||
}
|
||||
|
||||
private ObjectNode repositorySummaryList(PaginatedResult<CodeArtifactRepository> page) {
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode items = response.putArray("repositories");
|
||||
page.items().forEach(r -> items.add(repositorySummary(r)));
|
||||
if (page.nextToken() != null) {
|
||||
response.put("nextToken", page.nextToken());
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
private ObjectNode resourcePolicy(ResourcePolicy policy) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("resourceArn", policy.resourceArn());
|
||||
node.put("revision", policy.revision());
|
||||
node.put("document", policy.document());
|
||||
return node;
|
||||
}
|
||||
|
||||
private ObjectNode single(String field, ObjectNode value) {
|
||||
ObjectNode wrapper = objectMapper.createObjectNode();
|
||||
wrapper.set(field, value);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
private Response ok(ObjectNode body) {
|
||||
return Response.ok(body).build();
|
||||
}
|
||||
|
||||
private Map<String, String> readTagList(JsonNode tagsArray) {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
if (tagsArray == null || tagsArray.isNull()) {
|
||||
return tags;
|
||||
}
|
||||
if (!tagsArray.isArray()) {
|
||||
throw new AwsException("ValidationException", "tags must be an array of {key, value} objects.", 400);
|
||||
}
|
||||
tagsArray.forEach(n -> tags.put(text(n, "key"), text(n, "value")));
|
||||
return tags;
|
||||
}
|
||||
|
||||
private List<String> readUpstreams(JsonNode upstreamsArray) {
|
||||
List<String> upstreams = new ArrayList<>();
|
||||
if (upstreamsArray == null || upstreamsArray.isNull()) {
|
||||
return upstreams;
|
||||
}
|
||||
if (!upstreamsArray.isArray()) {
|
||||
throw new AwsException("ValidationException", "upstreams must be an array of {repositoryName} objects.",
|
||||
400);
|
||||
}
|
||||
upstreamsArray.forEach(n -> upstreams.add(text(n, "repositoryName")));
|
||||
return upstreams;
|
||||
}
|
||||
|
||||
private static String text(JsonNode node, String field) {
|
||||
JsonNode value = node == null ? null : node.get(field);
|
||||
return value != null && value.isTextual() ? value.textValue() : null;
|
||||
}
|
||||
|
||||
private JsonNode readTree(String body) {
|
||||
try {
|
||||
return objectMapper.reader()
|
||||
.with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
|
||||
.readTree(body == null || body.isBlank() ? "{}" : body);
|
||||
} catch (Exception e) {
|
||||
throw new WebApplicationException(JsonErrorResponseUtils.createSerializationErrorResponse());
|
||||
}
|
||||
}
|
||||
}
|
||||
+544
@@ -0,0 +1,544 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
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.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.CodeArtifactRepository;
|
||||
import io.github.hectorvent.floci.services.codeartifact.model.ExternalConnection;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
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.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
public class CodeArtifactService implements Resettable {
|
||||
|
||||
/** name -> packageFormat, the fixed real set of AWS-hosted public upstream connections. */
|
||||
private static final Map<String, String> EXTERNAL_CONNECTIONS = Map.ofEntries(
|
||||
Map.entry("public:maven-clojars", "maven"),
|
||||
Map.entry("public:maven-commonsware", "maven"),
|
||||
Map.entry("public:maven-googleandroid", "maven"),
|
||||
Map.entry("public:maven-gradleplugins", "maven"),
|
||||
Map.entry("public:maven-central", "maven"),
|
||||
Map.entry("public:npmjs", "npm"),
|
||||
Map.entry("public:nuget-org", "nuget"),
|
||||
Map.entry("public:pypi", "pypi"),
|
||||
Map.entry("public:ruby-gems-org", "ruby"),
|
||||
Map.entry("public:crates-io", "cargo"));
|
||||
|
||||
private static final Set<String> PACKAGE_FORMATS =
|
||||
Set.of("npm", "pypi", "maven", "nuget", "generic", "ruby", "swift", "cargo");
|
||||
private static final Set<String> ENDPOINT_TYPES = Set.of("dualstack", "ipv4");
|
||||
|
||||
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}");
|
||||
|
||||
public record DomainView(CodeArtifactDomain domain, int repositoryCount) {}
|
||||
public record ResourcePolicy(String resourceArn, String revision, String document) {}
|
||||
|
||||
private final AccountAwareStorageBackend<CodeArtifactDomain> domains;
|
||||
private final AccountAwareStorageBackend<CodeArtifactRepository> repositories;
|
||||
private final RegionResolver regionResolver;
|
||||
private final EmulatorConfig config;
|
||||
|
||||
@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>>() {});
|
||||
this.regionResolver = regionResolver;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- domains
|
||||
|
||||
public synchronized DomainView createDomain(String region, String domain, String encryptionKey,
|
||||
Map<String, String> tags) {
|
||||
validateDomainName(domain);
|
||||
String owner = regionResolver.getAccountId();
|
||||
String key = domainKey(region, domain);
|
||||
if (domains.getForAccount(owner, key).isPresent()) {
|
||||
throw conflict("Domain with name '" + domain + "' already exists.");
|
||||
}
|
||||
CodeArtifactDomain d = new CodeArtifactDomain();
|
||||
d.setName(domain);
|
||||
d.setOwner(owner);
|
||||
d.setRegion(region);
|
||||
d.setArn(regionResolver.buildArn("codeartifact", region, "domain/" + domain));
|
||||
d.setEncryptionKey(encryptionKey != null ? encryptionKey
|
||||
: regionResolver.buildArn("kms", region, "alias/aws/codeartifact"));
|
||||
d.setCreatedTime(Instant.now().getEpochSecond());
|
||||
d.setTags(validateTags(tags, Map.of()));
|
||||
domains.putForAccount(owner, key, d);
|
||||
return new DomainView(d, 0);
|
||||
}
|
||||
|
||||
public synchronized DomainView deleteDomain(String region, String domain, String domainOwner) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
String key = domainKey(region, domain);
|
||||
CodeArtifactDomain d = requireDomain(owner, key);
|
||||
int repoCount = repositoryCountForDomain(owner, region, domain);
|
||||
if (repoCount > 0) {
|
||||
throw conflict("Domain '" + domain + "' contains repositories and cannot be deleted "
|
||||
+ "until they are deleted.");
|
||||
}
|
||||
domains.deleteForAccount(owner, key);
|
||||
return new DomainView(d, 0);
|
||||
}
|
||||
|
||||
public DomainView describeDomain(String region, String domain, String domainOwner) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
CodeArtifactDomain d = requireDomain(owner, domainKey(region, domain));
|
||||
return new DomainView(d, repositoryCountForDomain(owner, region, domain));
|
||||
}
|
||||
|
||||
public PaginatedResult<DomainView> listDomains(String region, Integer maxResults, String nextToken) {
|
||||
String owner = regionResolver.getAccountId();
|
||||
List<CodeArtifactDomain> matching = domains.scanForAccount(owner, k -> k.startsWith(region + "::"));
|
||||
PaginatedResult<CodeArtifactDomain> page = Pagination.paginate(matching, CodeArtifactDomain::getName,
|
||||
maxResults, nextToken, 100, 1000, "ValidationException");
|
||||
List<DomainView> views = page.items().stream()
|
||||
.map(d -> new DomainView(d, repositoryCountForDomain(owner, region, d.getName())))
|
||||
.toList();
|
||||
return new PaginatedResult<>(views, page.nextToken());
|
||||
}
|
||||
|
||||
public synchronized ResourcePolicy putDomainPermissionsPolicy(String region, String domain, String domainOwner,
|
||||
String policyDocument, String policyRevision) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
String key = domainKey(region, domain);
|
||||
CodeArtifactDomain d = requireDomain(owner, key);
|
||||
checkRevision(d.getPolicyRevision(), policyRevision);
|
||||
validatePolicyDocument(policyDocument);
|
||||
d.setPolicyDocument(policyDocument);
|
||||
d.setPolicyRevision(newRevision());
|
||||
domains.putForAccount(owner, key, d);
|
||||
return new ResourcePolicy(d.getArn(), d.getPolicyRevision(), d.getPolicyDocument());
|
||||
}
|
||||
|
||||
public ResourcePolicy getDomainPermissionsPolicy(String region, String domain, String domainOwner) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
CodeArtifactDomain d = requireDomain(owner, domainKey(region, domain));
|
||||
if (d.getPolicyDocument() == null) {
|
||||
throw notFound("No resource policy is associated with domain '" + domain + "'.");
|
||||
}
|
||||
return new ResourcePolicy(d.getArn(), d.getPolicyRevision(), d.getPolicyDocument());
|
||||
}
|
||||
|
||||
public synchronized ResourcePolicy deleteDomainPermissionsPolicy(String region, String domain, String domainOwner,
|
||||
String policyRevision) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
String key = domainKey(region, domain);
|
||||
CodeArtifactDomain d = requireDomain(owner, key);
|
||||
if (d.getPolicyDocument() == null) {
|
||||
throw notFound("No resource policy is associated with domain '" + domain + "'.");
|
||||
}
|
||||
checkRevision(d.getPolicyRevision(), policyRevision);
|
||||
ResourcePolicy removed = new ResourcePolicy(d.getArn(), d.getPolicyRevision(), d.getPolicyDocument());
|
||||
d.setPolicyDocument(null);
|
||||
d.setPolicyRevision(null);
|
||||
domains.putForAccount(owner, key, d);
|
||||
return removed;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ repositories
|
||||
|
||||
public synchronized CodeArtifactRepository createRepository(String region, String domain, String domainOwner,
|
||||
String repository, String description,
|
||||
List<String> upstreams, Map<String, String> tags) {
|
||||
validateRepositoryName(repository);
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
requireDomain(owner, domainKey(region, domain));
|
||||
String key = repositoryKey(region, domain, repository);
|
||||
if (repositories.getForAccount(owner, key).isPresent()) {
|
||||
throw conflict("Repository with name '" + repository + "' already exists in domain '" + domain + "'.");
|
||||
}
|
||||
validateUpstreams(owner, region, domain, repository, upstreams);
|
||||
validateDescription(description);
|
||||
|
||||
CodeArtifactRepository r = new CodeArtifactRepository();
|
||||
r.setName(repository);
|
||||
r.setDomainName(domain);
|
||||
r.setDomainOwner(owner);
|
||||
r.setAdministratorAccount(regionResolver.getAccountId());
|
||||
r.setRegion(region);
|
||||
r.setArn(regionResolver.buildArn("codeartifact", region, "repository/" + domain + "/" + repository));
|
||||
r.setDescription(description);
|
||||
r.setUpstreams(upstreams != null ? new ArrayList<>(upstreams) : new ArrayList<>());
|
||||
r.setCreatedTime(Instant.now().getEpochSecond());
|
||||
r.setTags(validateTags(tags, Map.of()));
|
||||
repositories.putForAccount(owner, key, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
public synchronized CodeArtifactRepository deleteRepository(String region, String domain, String domainOwner,
|
||||
String repository) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
String key = repositoryKey(region, domain, repository);
|
||||
CodeArtifactRepository r = requireRepository(owner, key);
|
||||
repositories.deleteForAccount(owner, key);
|
||||
return r;
|
||||
}
|
||||
|
||||
public CodeArtifactRepository describeRepository(String region, String domain, String domainOwner,
|
||||
String repository) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
return requireRepository(owner, repositoryKey(region, domain, repository));
|
||||
}
|
||||
|
||||
public synchronized CodeArtifactRepository updateRepository(String region, String domain, String domainOwner,
|
||||
String repository, String description,
|
||||
List<String> upstreams) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
String key = repositoryKey(region, domain, repository);
|
||||
CodeArtifactRepository r = requireRepository(owner, key);
|
||||
if (upstreams != null) {
|
||||
if (!upstreams.isEmpty() && !r.getExternalConnections().isEmpty()) {
|
||||
throw conflict("Repository '" + repository + "' has an external connection; "
|
||||
+ "a repository cannot have both an external connection and upstream repositories.");
|
||||
}
|
||||
validateUpstreams(owner, region, domain, repository, upstreams);
|
||||
r.setUpstreams(new ArrayList<>(upstreams));
|
||||
}
|
||||
if (description != null) {
|
||||
validateDescription(description);
|
||||
r.setDescription(description);
|
||||
}
|
||||
repositories.putForAccount(owner, key, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
public PaginatedResult<CodeArtifactRepository> listRepositories(String region, String repositoryPrefix,
|
||||
Integer maxResults, String nextToken) {
|
||||
String owner = regionResolver.getAccountId();
|
||||
List<CodeArtifactRepository> matching = repositories.scanForAccount(owner, k -> k.startsWith(region + "::"))
|
||||
.stream()
|
||||
.filter(r -> repositoryPrefix == null || r.getName().startsWith(repositoryPrefix))
|
||||
.toList();
|
||||
return Pagination.paginate(matching, r -> r.getDomainName() + "::" + r.getName(),
|
||||
maxResults, nextToken, 100, 1000, "ValidationException");
|
||||
}
|
||||
|
||||
public PaginatedResult<CodeArtifactRepository> listRepositoriesInDomain(String region, String domain,
|
||||
String domainOwner,
|
||||
String administratorAccount,
|
||||
String repositoryPrefix,
|
||||
Integer maxResults, String nextToken) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
requireDomain(owner, domainKey(region, domain));
|
||||
List<CodeArtifactRepository> matching = repositories
|
||||
.scanForAccount(owner, k -> k.startsWith(region + "::" + domain + "::"))
|
||||
.stream()
|
||||
.filter(r -> repositoryPrefix == null || r.getName().startsWith(repositoryPrefix))
|
||||
.filter(r -> administratorAccount == null || administratorAccount.equals(r.getAdministratorAccount()))
|
||||
.toList();
|
||||
return Pagination.paginate(matching, CodeArtifactRepository::getName,
|
||||
maxResults, nextToken, 100, 1000, "ValidationException");
|
||||
}
|
||||
|
||||
public String getRepositoryEndpoint(String region, String domain, String domainOwner, String repository,
|
||||
String format, String endpointType) {
|
||||
if (format == null || !PACKAGE_FORMATS.contains(format)) {
|
||||
throw validation("format must be one of " + PACKAGE_FORMATS + ".");
|
||||
}
|
||||
if (endpointType != null && !ENDPOINT_TYPES.contains(endpointType)) {
|
||||
throw validation("endpointType must be one of " + ENDPOINT_TYPES + ".");
|
||||
}
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
requireRepository(owner, repositoryKey(region, domain, repository));
|
||||
return config.effectiveBaseUrl() + "/codeartifact/" + format + "/" + domain + "/" + repository + "/";
|
||||
}
|
||||
|
||||
public synchronized ResourcePolicy putRepositoryPermissionsPolicy(String region, String domain,
|
||||
String domainOwner, String repository,
|
||||
String policyDocument, String policyRevision) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
String key = repositoryKey(region, domain, repository);
|
||||
CodeArtifactRepository r = requireRepository(owner, key);
|
||||
checkRevision(r.getPolicyRevision(), policyRevision);
|
||||
validatePolicyDocument(policyDocument);
|
||||
r.setPolicyDocument(policyDocument);
|
||||
r.setPolicyRevision(newRevision());
|
||||
repositories.putForAccount(owner, key, r);
|
||||
return new ResourcePolicy(r.getArn(), r.getPolicyRevision(), r.getPolicyDocument());
|
||||
}
|
||||
|
||||
public ResourcePolicy getRepositoryPermissionsPolicy(String region, String domain, String domainOwner,
|
||||
String repository) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
CodeArtifactRepository r = requireRepository(owner, repositoryKey(region, domain, repository));
|
||||
if (r.getPolicyDocument() == null) {
|
||||
throw notFound("No resource policy is associated with repository '" + repository + "'.");
|
||||
}
|
||||
return new ResourcePolicy(r.getArn(), r.getPolicyRevision(), r.getPolicyDocument());
|
||||
}
|
||||
|
||||
public synchronized ResourcePolicy deleteRepositoryPermissionsPolicy(String region, String domain,
|
||||
String domainOwner, String repository,
|
||||
String policyRevision) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
String key = repositoryKey(region, domain, repository);
|
||||
CodeArtifactRepository r = requireRepository(owner, key);
|
||||
if (r.getPolicyDocument() == null) {
|
||||
throw notFound("No resource policy is associated with repository '" + repository + "'.");
|
||||
}
|
||||
checkRevision(r.getPolicyRevision(), policyRevision);
|
||||
ResourcePolicy removed = new ResourcePolicy(r.getArn(), r.getPolicyRevision(), r.getPolicyDocument());
|
||||
r.setPolicyDocument(null);
|
||||
r.setPolicyRevision(null);
|
||||
repositories.putForAccount(owner, key, r);
|
||||
return removed;
|
||||
}
|
||||
|
||||
public synchronized CodeArtifactRepository associateExternalConnection(String region, String domain,
|
||||
String domainOwner, String repository,
|
||||
String externalConnection) {
|
||||
String format = EXTERNAL_CONNECTIONS.get(externalConnection);
|
||||
if (format == null) {
|
||||
throw validation("externalConnection must be one of " + EXTERNAL_CONNECTIONS.keySet() + ".");
|
||||
}
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
String key = repositoryKey(region, domain, repository);
|
||||
CodeArtifactRepository r = requireRepository(owner, key);
|
||||
if (!r.getExternalConnections().isEmpty()) {
|
||||
throw conflict("Repository '" + repository + "' already has an external connection; "
|
||||
+ "a repository can only have one.");
|
||||
}
|
||||
if (!r.getUpstreams().isEmpty()) {
|
||||
throw conflict("Repository '" + repository + "' has upstream repositories; "
|
||||
+ "a repository cannot have both an external connection and upstream repositories.");
|
||||
}
|
||||
List<ExternalConnection> connections = new ArrayList<>(r.getExternalConnections());
|
||||
connections.add(new ExternalConnection(externalConnection, format, "Available"));
|
||||
r.setExternalConnections(connections);
|
||||
repositories.putForAccount(owner, key, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
public synchronized CodeArtifactRepository disassociateExternalConnection(String region, String domain,
|
||||
String domainOwner, String repository,
|
||||
String externalConnection) {
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
String key = repositoryKey(region, domain, repository);
|
||||
CodeArtifactRepository r = requireRepository(owner, key);
|
||||
List<ExternalConnection> connections = new ArrayList<>(r.getExternalConnections());
|
||||
boolean removed = connections.removeIf(ec -> ec.getExternalConnectionName().equals(externalConnection));
|
||||
if (!removed) {
|
||||
throw notFound("Repository '" + repository + "' has no external connection named '"
|
||||
+ externalConnection + "'.");
|
||||
}
|
||||
r.setExternalConnections(connections);
|
||||
repositories.putForAccount(owner, key, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------- tags
|
||||
|
||||
public synchronized void tagResource(String resourceArn, Map<String, String> newTags) {
|
||||
ResourceRef ref = parseResourceArn(resourceArn);
|
||||
if (ref.repository() == null) {
|
||||
String key = domainKey(ref.region(), ref.domain());
|
||||
CodeArtifactDomain d = requireDomain(ref.owner(), key);
|
||||
d.setTags(validateTags(newTags, d.getTags()));
|
||||
domains.putForAccount(ref.owner(), key, d);
|
||||
} else {
|
||||
String key = repositoryKey(ref.region(), ref.domain(), ref.repository());
|
||||
CodeArtifactRepository r = requireRepository(ref.owner(), key);
|
||||
r.setTags(validateTags(newTags, r.getTags()));
|
||||
repositories.putForAccount(ref.owner(), key, r);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void untagResource(String resourceArn, List<String> tagKeys) {
|
||||
ResourceRef ref = parseResourceArn(resourceArn);
|
||||
if (ref.repository() == null) {
|
||||
String key = domainKey(ref.region(), ref.domain());
|
||||
CodeArtifactDomain d = requireDomain(ref.owner(), key);
|
||||
Map<String, String> tags = new LinkedHashMap<>(d.getTags());
|
||||
tagKeys.forEach(tags::remove);
|
||||
d.setTags(tags);
|
||||
domains.putForAccount(ref.owner(), key, d);
|
||||
} else {
|
||||
String key = repositoryKey(ref.region(), ref.domain(), ref.repository());
|
||||
CodeArtifactRepository r = requireRepository(ref.owner(), key);
|
||||
Map<String, String> tags = new LinkedHashMap<>(r.getTags());
|
||||
tagKeys.forEach(tags::remove);
|
||||
r.setTags(tags);
|
||||
repositories.putForAccount(ref.owner(), key, r);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> listTagsForResource(String resourceArn) {
|
||||
ResourceRef ref = parseResourceArn(resourceArn);
|
||||
if (ref.repository() == null) {
|
||||
return requireDomain(ref.owner(), domainKey(ref.region(), ref.domain())).getTags();
|
||||
}
|
||||
return requireRepository(ref.owner(), repositoryKey(ref.region(), ref.domain(), ref.repository())).getTags();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
domains.clear();
|
||||
repositories.clear();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------- helpers
|
||||
|
||||
private record ResourceRef(String type, String region, String owner, String domain, String repository) {}
|
||||
|
||||
private ResourceRef parseResourceArn(String resourceArn) {
|
||||
AwsArnUtils.Arn arn;
|
||||
try {
|
||||
arn = AwsArnUtils.parse(resourceArn);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw validation("resourceArn is not a valid ARN.");
|
||||
}
|
||||
if (!"codeartifact".equals(arn.service())) {
|
||||
throw validation("resourceArn must be a CodeArtifact ARN.");
|
||||
}
|
||||
String[] parts = arn.resource().split("/");
|
||||
if (parts.length == 2 && "domain".equals(parts[0])) {
|
||||
return new ResourceRef("domain", arn.region(), arn.accountId(), parts[1], null);
|
||||
}
|
||||
if (parts.length == 3 && "repository".equals(parts[0])) {
|
||||
return new ResourceRef("repository", arn.region(), arn.accountId(), parts[1], parts[2]);
|
||||
}
|
||||
throw validation("resourceArn must reference a domain or a repository.");
|
||||
}
|
||||
|
||||
private String effectiveOwner(String domainOwner) {
|
||||
if (domainOwner == null || domainOwner.isBlank()) {
|
||||
return regionResolver.getAccountId();
|
||||
}
|
||||
if (!ACCOUNT_ID.matcher(domainOwner).matches()) {
|
||||
throw validation("domainOwner must be a 12-digit account ID.");
|
||||
}
|
||||
return domainOwner;
|
||||
}
|
||||
|
||||
private int repositoryCountForDomain(String owner, String region, String domain) {
|
||||
return repositories.scanForAccount(owner, k -> k.startsWith(region + "::" + domain + "::")).size();
|
||||
}
|
||||
|
||||
private void validateUpstreams(String owner, String region, String domain, String repository,
|
||||
List<String> upstreams) {
|
||||
if (upstreams == null) {
|
||||
return;
|
||||
}
|
||||
if (upstreams.size() > 10) {
|
||||
throw new AwsException("ServiceQuotaExceededException",
|
||||
"A repository can have a maximum of 10 direct upstream repositories.", 402);
|
||||
}
|
||||
for (String upstream : upstreams) {
|
||||
if (upstream.equals(repository)) {
|
||||
throw validation("A repository cannot be its own upstream.");
|
||||
}
|
||||
if (repositories.getForAccount(owner, repositoryKey(region, domain, upstream)).isEmpty()) {
|
||||
throw notFound("Upstream repository '" + upstream + "' was not found in domain '" + domain + "'.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private CodeArtifactDomain requireDomain(String owner, String key) {
|
||||
return domains.getForAccount(owner, key)
|
||||
.orElseThrow(() -> notFound("Domain not found."));
|
||||
}
|
||||
|
||||
private CodeArtifactRepository requireRepository(String owner, String key) {
|
||||
return repositories.getForAccount(owner, key)
|
||||
.orElseThrow(() -> notFound("Repository not found."));
|
||||
}
|
||||
|
||||
private void checkRevision(String currentRevision, String requestedRevision) {
|
||||
if (requestedRevision != null && !requestedRevision.equals(currentRevision)) {
|
||||
throw conflict("The policy revision does not match the current policy revision.");
|
||||
}
|
||||
}
|
||||
|
||||
private static String newRevision() {
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
private static void validatePolicyDocument(String policyDocument) {
|
||||
if (policyDocument == null || policyDocument.isBlank() || policyDocument.length() > 7168) {
|
||||
throw validation("policyDocument must be 1-7168 characters.");
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateDescription(String description) {
|
||||
if (description != null && description.length() > 1000) {
|
||||
throw validation("description must be at most 1000 characters.");
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateDomainName(String domain) {
|
||||
if (domain == null || !DOMAIN_NAME.matcher(domain).matches()) {
|
||||
throw validation("domain must be 2-50 characters and match [a-z][a-z0-9-]*[a-z0-9].");
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateRepositoryName(String repository) {
|
||||
if (repository == null || !REPOSITORY_NAME.matcher(repository).matches()) {
|
||||
throw validation("repository must be 2-100 characters and match [A-Za-z0-9][A-Za-z0-9._-]*.");
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, String> validateTags(Map<String, String> newTags, Map<String, String> existing) {
|
||||
Map<String, String> merged = new LinkedHashMap<>(existing);
|
||||
if (newTags == null) {
|
||||
return merged;
|
||||
}
|
||||
newTags.forEach((k, v) -> {
|
||||
if (k == null || k.isBlank() || k.length() > 128 || k.startsWith("aws:")) {
|
||||
throw validation("Tag key '" + k + "' is invalid.");
|
||||
}
|
||||
if (v == null || v.length() > 256) {
|
||||
throw validation("Tag value for key '" + k + "' is invalid.");
|
||||
}
|
||||
merged.put(k, v);
|
||||
});
|
||||
if (merged.size() > 200) {
|
||||
throw new AwsException("ServiceQuotaExceededException",
|
||||
"The maximum number of tags (200) for this resource has been exceeded.", 402);
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
private static String domainKey(String region, String domain) {
|
||||
return region + "::" + domain;
|
||||
}
|
||||
|
||||
private static String repositoryKey(String region, String domain, String repository) {
|
||||
return region + "::" + domain + "::" + repository;
|
||||
}
|
||||
|
||||
private static AwsException validation(String message) {
|
||||
return new AwsException("ValidationException", message, 400);
|
||||
}
|
||||
|
||||
private static AwsException conflict(String message) {
|
||||
return new AwsException("ConflictException", message, 409);
|
||||
}
|
||||
|
||||
private static AwsException notFound(String message) {
|
||||
return new AwsException("ResourceNotFoundException", message, 404);
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.TagHandler;
|
||||
import io.github.hectorvent.floci.core.common.V1Tags;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* CodeArtifact's {@code ListTagsForResource}, which AWS puts on the bare
|
||||
* {@code /v1/tags} path with {@code resourceArn} as a query parameter rather than a path
|
||||
* segment. {@code TagResource}/{@code UntagResource} don't collide with anything and stay on
|
||||
* {@code CodeArtifactController}'s own {@code /v1/tag} and {@code /v1/untag} routes; only the
|
||||
* list operation is registered here so {@code V1TagsController} can resolve it.
|
||||
*
|
||||
* <p>A separate bean rather than {@code CodeArtifactService} implementing {@link TagHandler}
|
||||
* itself, for the same reason as {@code MskTagHandler}: a class-level {@link V1Tags} would
|
||||
* strip the service's {@code @Default} qualifier and break plain {@code @Inject
|
||||
* CodeArtifactService} injection points.
|
||||
*
|
||||
* <p>CodeArtifact is the one {@link TagHandler} on this dispatcher with a list-shaped tag body
|
||||
* using lowercase {@code key}/{@code value} entry members rather than the AWS-typical
|
||||
* capitalized {@code Key}/{@code Value}.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
@V1Tags
|
||||
public class CodeArtifactTagHandler implements TagHandler {
|
||||
|
||||
private final CodeArtifactService codeArtifactService;
|
||||
|
||||
@Inject
|
||||
public CodeArtifactTagHandler(CodeArtifactService codeArtifactService) {
|
||||
this.codeArtifactService = codeArtifactService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serviceKey() {
|
||||
return "codeartifact";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tagsBodyIsList() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String tagEntryKeyName() {
|
||||
return "key";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String tagEntryValueName() {
|
||||
return "value";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> listTags(String region, String arn) {
|
||||
return codeArtifactService.listTagsForResource(arn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tagResource(String region, String arn, Map<String, String> tags) {
|
||||
codeArtifactService.tagResource(arn, tags);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void untagResource(String region, String arn, List<String> tagKeys) {
|
||||
codeArtifactService.untagResource(arn, tagKeys);
|
||||
}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
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 CodeArtifactDomain {
|
||||
private String name;
|
||||
private String owner;
|
||||
private String region;
|
||||
private String arn;
|
||||
private String encryptionKey;
|
||||
private long createdTime;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
private String policyDocument;
|
||||
private String policyRevision;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(String owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
public String getRegion() {
|
||||
return region;
|
||||
}
|
||||
|
||||
public void setRegion(String region) {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public String getArn() {
|
||||
return arn;
|
||||
}
|
||||
|
||||
public void setArn(String arn) {
|
||||
this.arn = arn;
|
||||
}
|
||||
|
||||
public String getEncryptionKey() {
|
||||
return encryptionKey;
|
||||
}
|
||||
|
||||
public void setEncryptionKey(String encryptionKey) {
|
||||
this.encryptionKey = encryptionKey;
|
||||
}
|
||||
|
||||
public long getCreatedTime() {
|
||||
return createdTime;
|
||||
}
|
||||
|
||||
public void setCreatedTime(long createdTime) {
|
||||
this.createdTime = createdTime;
|
||||
}
|
||||
|
||||
public Map<String, String> getTags() {
|
||||
return Collections.unmodifiableMap(tags);
|
||||
}
|
||||
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
public String getPolicyDocument() {
|
||||
return policyDocument;
|
||||
}
|
||||
|
||||
public void setPolicyDocument(String policyDocument) {
|
||||
this.policyDocument = policyDocument;
|
||||
}
|
||||
|
||||
public String getPolicyRevision() {
|
||||
return policyRevision;
|
||||
}
|
||||
|
||||
public void setPolicyRevision(String policyRevision) {
|
||||
this.policyRevision = policyRevision;
|
||||
}
|
||||
}
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class CodeArtifactRepository {
|
||||
private String name;
|
||||
private String domainName;
|
||||
private String domainOwner;
|
||||
private String administratorAccount;
|
||||
private String region;
|
||||
private String arn;
|
||||
private String description;
|
||||
private List<String> upstreams = new ArrayList<>();
|
||||
private List<ExternalConnection> externalConnections = new ArrayList<>();
|
||||
private long createdTime;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
private String policyDocument;
|
||||
private String policyRevision;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
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 getAdministratorAccount() {
|
||||
return administratorAccount;
|
||||
}
|
||||
|
||||
public void setAdministratorAccount(String administratorAccount) {
|
||||
this.administratorAccount = administratorAccount;
|
||||
}
|
||||
|
||||
public String getRegion() {
|
||||
return region;
|
||||
}
|
||||
|
||||
public void setRegion(String region) {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public String getArn() {
|
||||
return arn;
|
||||
}
|
||||
|
||||
public void setArn(String arn) {
|
||||
this.arn = arn;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public List<String> getUpstreams() {
|
||||
return Collections.unmodifiableList(upstreams);
|
||||
}
|
||||
|
||||
public void setUpstreams(List<String> upstreams) {
|
||||
this.upstreams = upstreams;
|
||||
}
|
||||
|
||||
public List<ExternalConnection> getExternalConnections() {
|
||||
return Collections.unmodifiableList(externalConnections);
|
||||
}
|
||||
|
||||
public void setExternalConnections(List<ExternalConnection> externalConnections) {
|
||||
this.externalConnections = externalConnections;
|
||||
}
|
||||
|
||||
public long getCreatedTime() {
|
||||
return createdTime;
|
||||
}
|
||||
|
||||
public void setCreatedTime(long createdTime) {
|
||||
this.createdTime = createdTime;
|
||||
}
|
||||
|
||||
public Map<String, String> getTags() {
|
||||
return Collections.unmodifiableMap(tags);
|
||||
}
|
||||
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
public String getPolicyDocument() {
|
||||
return policyDocument;
|
||||
}
|
||||
|
||||
public void setPolicyDocument(String policyDocument) {
|
||||
this.policyDocument = policyDocument;
|
||||
}
|
||||
|
||||
public String getPolicyRevision() {
|
||||
return policyRevision;
|
||||
}
|
||||
|
||||
public void setPolicyRevision(String policyRevision) {
|
||||
this.policyRevision = policyRevision;
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package io.github.hectorvent.floci.services.codeartifact.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class ExternalConnection {
|
||||
private String externalConnectionName;
|
||||
private String packageFormat;
|
||||
private String status;
|
||||
|
||||
public ExternalConnection() {
|
||||
}
|
||||
|
||||
public ExternalConnection(String externalConnectionName, String packageFormat, String status) {
|
||||
this.externalConnectionName = externalConnectionName;
|
||||
this.packageFormat = packageFormat;
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getExternalConnectionName() {
|
||||
return externalConnectionName;
|
||||
}
|
||||
|
||||
public void setExternalConnectionName(String externalConnectionName) {
|
||||
this.externalConnectionName = externalConnectionName;
|
||||
}
|
||||
|
||||
public String getPackageFormat() {
|
||||
return packageFormat;
|
||||
}
|
||||
|
||||
public void setPackageFormat(String packageFormat) {
|
||||
this.packageFormat = packageFormat;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user