mirror of
https://github.com/floci-io/floci.git
synced 2026-12-03 02:07:58 -05:00
feat(codeartifact): add DescribePackage for all package formats (#5308)
DescribePackage returns a package's format, namespace, name, and origin controls, and answers ResourceNotFoundException for a package that does not exist. Existence is checked where the package actually lives: stored generic-format package versions for generic, and each format's sidecar for maven, npm, and pypi. A package whose name merely starts with another's name does not match it. Origin controls are reported as publish ALLOW and upstream BLOCK, the default CodeArtifact assigns to a package whose first version was published directly. Floci never ingests packages from upstream and does not store origin controls, so these values are fixed. For maven, npm, and pypi the existence check starts that repository's sidecar if it is not already running, the same side effect the asset bridge has. RepositorySidecarManager gains packageExists, implemented by each sidecar client.
This commit is contained in:
1 parent
5a8694e97a
commit
e452ce8671
14 files changed
+299
-4
No files matched your search
+27
@@ -328,6 +328,20 @@ public class CodeArtifactController {
|
||||
return ok(publishPackageVersionResponse(result));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/v1/package")
|
||||
public Response describePackage(@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) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
CodeArtifactService.PackageDescription described = service.describePackage(region, domain, domainOwner,
|
||||
repository, format, namespace, packageName);
|
||||
return ok(single("package", packageDescription(described)));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/v1/package/version")
|
||||
public Response describePackageVersion(@Context HttpHeaders headers, @QueryParam("domain") String domain,
|
||||
@@ -484,6 +498,19 @@ public class CodeArtifactController {
|
||||
return node;
|
||||
}
|
||||
|
||||
private ObjectNode packageDescription(CodeArtifactService.PackageDescription described) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("format", described.format());
|
||||
if (described.namespace() != null) {
|
||||
node.put("namespace", described.namespace());
|
||||
}
|
||||
node.put("name", described.packageName());
|
||||
ObjectNode restrictions = node.putObject("originConfiguration").putObject("restrictions");
|
||||
restrictions.put("publish", described.publishRestriction());
|
||||
restrictions.put("upstream", described.upstreamRestriction());
|
||||
return node;
|
||||
}
|
||||
|
||||
private ObjectNode packageVersionDescription(CodeArtifactPackageVersion pv) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("format", pv.getFormat());
|
||||
|
||||
+58
@@ -662,6 +662,64 @@ public class CodeArtifactService implements Resettable {
|
||||
+ "' was not found.", version, "package-version"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Floci never ingests packages from an upstream source, so every package it holds was first
|
||||
* published directly. That is the one case where CodeArtifact's default origin controls are
|
||||
* publish ALLOW and upstream BLOCK.
|
||||
*/
|
||||
public record PackageDescription(String format, String namespace, String packageName,
|
||||
String publishRestriction, String upstreamRestriction) {}
|
||||
|
||||
public PackageDescription describePackage(String region, String domain, String domainOwner, String repository,
|
||||
String format, String namespace, String packageName) {
|
||||
if (format == null || !PACKAGE_FORMATS.contains(format)) {
|
||||
throw validation("format must be one of " + PACKAGE_FORMATS + ".");
|
||||
}
|
||||
validatePackageToken("package", packageName);
|
||||
if (namespace != null) {
|
||||
validatePackageToken("namespace", namespace);
|
||||
}
|
||||
// The API reference's own namespace parameter doc for this action: "The namespace is
|
||||
// required when requesting packages of the following formats: Maven, Swift, generic."
|
||||
// pypi, nuget, ruby, and cargo packages have no namespace at all, so a supplied one can
|
||||
// never be real; echoing it back in the response would misrepresent a package that way.
|
||||
if (("maven".equals(format) || "generic".equals(format)) && (namespace == null || namespace.isBlank())) {
|
||||
throw validation("namespace is required when describing a " + format + " package.");
|
||||
}
|
||||
if ("pypi".equals(format) && namespace != null) {
|
||||
throw validation("pypi packages do not have a namespace.");
|
||||
}
|
||||
requireNonBlank(domain, "domain");
|
||||
requireNonBlank(repository, "repository");
|
||||
String owner = effectiveOwner(domainOwner);
|
||||
requireRepository(owner, repositoryKey(region, domain, repository), repository);
|
||||
boolean exists = CONTAINER_BACKED_FORMATS.contains(format)
|
||||
? containerBackedPackageExists(region, domain, owner, repository, format, namespace, packageName)
|
||||
: genericPackageExists(region, domain, owner, repository, format, namespace, packageName);
|
||||
if (!exists) {
|
||||
throw notFound("Package '" + packageName + "' was not found.", packageName, "package");
|
||||
}
|
||||
return new PackageDescription(format, namespace, packageName, "ALLOW", "BLOCK");
|
||||
}
|
||||
|
||||
private boolean containerBackedPackageExists(String region, String domain, String owner, String repository,
|
||||
String format, String namespace, String packageName) {
|
||||
String repoId = ensureFormatContainerId(format, region, domain, owner, repository);
|
||||
return sidecarRegistry.forFormat(format)
|
||||
.map(manager -> manager.packageExists(repoId, domain, repository, namespace, packageName))
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
private boolean genericPackageExists(String region, String domain, String owner, String repository,
|
||||
String format, String namespace, String packageName) {
|
||||
String prefix = packageVersionKey(region, domain, repository, format, namespace, packageName, "");
|
||||
String wantedNamespace = namespace == null ? "" : namespace;
|
||||
return packageVersions.scanForAccount(owner, key -> key.startsWith(prefix)).stream()
|
||||
.anyMatch(pv -> format.equals(pv.getFormat())
|
||||
&& packageName.equals(pv.getPackageName())
|
||||
&& wantedNamespace.equals(pv.getNamespace() == null ? "" : pv.getNamespace()));
|
||||
}
|
||||
|
||||
public PackageVersionAssetResult getPackageVersionAsset(String region, String domain, String domainOwner,
|
||||
String repository, String format, String namespace, String packageName, String version,
|
||||
String assetName, String packageVersionRevision) {
|
||||
|
||||
+18
-3
@@ -105,7 +105,14 @@ public class PypiserverSidecarClient implements RepositorySidecarManager {
|
||||
return packageName.toLowerCase(Locale.ROOT).replaceAll("[-_.]+", "-");
|
||||
}
|
||||
|
||||
private boolean packageIndexListsAsset(String baseUrl, String packageName, String assetName) {
|
||||
@Override
|
||||
public boolean packageExists(String repositoryContainerId, String domain, String repository, String namespace,
|
||||
String packageName) {
|
||||
String baseUrl = ensureReady(repositoryContainerId, null);
|
||||
return simpleIndex(baseUrl, packageName).isPresent();
|
||||
}
|
||||
|
||||
private Optional<String> simpleIndex(String baseUrl, String packageName) {
|
||||
String normalized = normalizePackageName(packageName);
|
||||
URI uri = SidecarUriUtils.combine(URI.create(baseUrl), "/simple/" + SidecarUriUtils.encodeSegment(normalized)
|
||||
+ "/");
|
||||
@@ -117,15 +124,23 @@ public class PypiserverSidecarClient implements RepositorySidecarManager {
|
||||
throw new IllegalStateException("Could not reach the pypiserver sidecar to look up " + packageName, e);
|
||||
}
|
||||
if (response.statusCode() == 404) {
|
||||
return false;
|
||||
return Optional.empty();
|
||||
}
|
||||
if (response.statusCode() != 200) {
|
||||
throw new IllegalStateException("Could not look up " + packageName + " on the pypiserver sidecar: "
|
||||
+ "upstream returned " + response.statusCode());
|
||||
}
|
||||
return Optional.of(response.body());
|
||||
}
|
||||
|
||||
private boolean packageIndexListsAsset(String baseUrl, String packageName, String assetName) {
|
||||
Optional<String> index = simpleIndex(baseUrl, packageName);
|
||||
if (index.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
// An exact entry match, not a substring search: a shorter real filename that happens to
|
||||
// be a substring of a different, longer real filename must not pass this check.
|
||||
return SIMPLE_INDEX_HREF.matcher(response.body()).results()
|
||||
return SIMPLE_INDEX_HREF.matcher(index.get()).results()
|
||||
.map(match -> {
|
||||
String href = match.group(1);
|
||||
String filename = href.substring(href.lastIndexOf('/') + 1);
|
||||
|
||||
+31
@@ -121,6 +121,37 @@ public class ReposiliteSidecarClient implements RepositorySidecarManager {
|
||||
return Optional.of(response.body());
|
||||
}
|
||||
|
||||
/**
|
||||
* Only a confirmed 404 means absent; confirmed live (a {@code GET} on a missing directory, or a
|
||||
* never-provisioned repository, both answer a clean 404). Anything else, an auth failure or a
|
||||
* server error, throws instead of collapsing to "not found": a Reposilite outage must not read
|
||||
* as every Maven package having been deleted.
|
||||
*/
|
||||
@Override
|
||||
public boolean packageExists(String repositoryContainerId, String domain, String repository, String namespace,
|
||||
String packageName) {
|
||||
String baseUrl = manager.ensureReady();
|
||||
StringBuilder path = new StringBuilder("/").append(SidecarUriUtils.encodeSegment(repositoryContainerId));
|
||||
for (String namespacePart : namespace.split("\\.", -1)) {
|
||||
path.append('/').append(SidecarUriUtils.encodeSegment(namespacePart));
|
||||
}
|
||||
path.append('/').append(SidecarUriUtils.encodeSegment(packageName)).append('/');
|
||||
HttpRequest request = HttpRequest.newBuilder(SidecarUriUtils.combine(URI.create(baseUrl), path.toString()))
|
||||
.timeout(Duration.ofSeconds(30))
|
||||
.header("Authorization", manager.basicAuthHeader())
|
||||
.GET()
|
||||
.build();
|
||||
int status = send(request, BodyHandlers.discarding()).statusCode();
|
||||
if (status == 404) {
|
||||
return false;
|
||||
}
|
||||
if (status != 200) {
|
||||
throw new IllegalStateException("Could not look up " + packageName + " on the Reposilite sidecar: "
|
||||
+ "upstream returned " + status);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Ensures a Reposilite repository named {@code repoId} exists, creating it if not. */
|
||||
public void ensureRepository(String repoId) {
|
||||
String baseUrl = manager.ensureReady();
|
||||
|
||||
+8
@@ -56,4 +56,12 @@ public interface RepositorySidecarManager {
|
||||
*/
|
||||
Optional<byte[]> fetchPackageVersionAsset(String repositoryContainerId, String domain, String repository,
|
||||
String namespace, String packageName, String version, String assetName);
|
||||
|
||||
/**
|
||||
* {@code true} when this sidecar holds at least one version of the named package. {@code namespace}
|
||||
* follows the same convention as {@link #fetchPackageVersionAsset}: {@code null} for a format that
|
||||
* has none. A repository that was never started for this format holds nothing, so it is {@code false}.
|
||||
*/
|
||||
boolean packageExists(String repositoryContainerId, String domain, String repository, String namespace,
|
||||
String packageName);
|
||||
}
|
||||
+7
@@ -93,6 +93,13 @@ public class VerdaccioSidecarClient implements RepositorySidecarManager {
|
||||
return packageDocument(baseUrl, packagePath(namespace, packageName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean packageExists(String repositoryContainerId, String domain, String repository, String namespace,
|
||||
String packageName) {
|
||||
String baseUrl = ensureReady(repositoryContainerId, publicUrl(domain, repository));
|
||||
return fetchPackageDocument(baseUrl, namespace, packageName).isPresent();
|
||||
}
|
||||
|
||||
/** One tarball by its filename, from a backend already made ready by {@link #ensureReady}. */
|
||||
public Optional<byte[]> fetchTarball(String baseUrl, String namespace, String packageName, String assetName) {
|
||||
HttpRequest request = tarballRequest(baseUrl, namespace, packageName, assetName);
|
||||
|
||||
Reference in new issue
Block a user