mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(codeartifact): treat identical-content asset republish as idempotent (#4619)
PublishPackageVersion silently overwrote a generic-format asset's stored
bytes whenever the same asset name was republished to an Unfinished
package version, with no comparison against what was already there. AWS's
own docs ("Overwriting package assets" in the CodeArtifact packages
overview) say a republish is only rejected when the content actually
differs from what's already stored; byte-identical content succeeds
idempotently. Compares the new upload against the existing asset's real
bytes (read the same way GetPackageVersionAsset does, not the in-memory
PackageAsset.content field, which is @JsonIgnore and unreliable after a
reload) and now returns ConflictException on a genuine mismatch instead of
silently replacing the content.
This commit is contained in:
1 parent
7e3ff3c5ee
commit
0232d5d224
4 files changed
+234
-5
No files matched your search
+53
@@ -22,10 +22,12 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.AtomicMoveNotSupportedException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.security.MessageDigest;
|
||||
@@ -33,6 +35,7 @@ import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -581,6 +584,19 @@ public class CodeArtifactService implements Resettable {
|
||||
}
|
||||
|
||||
Map<String, PackageAsset> assets = new LinkedHashMap<>(pv.getAssets());
|
||||
// AWS's own docs ("Overwriting package assets" in CodeArtifact's packages overview): a
|
||||
// republish of an asset name that already exists is only rejected when the new content
|
||||
// actually differs from what's already there; byte-identical content succeeds because the
|
||||
// operation is idempotent, e.g. a client retrying after a dropped response. The in-memory
|
||||
// PackageAsset.content field is @JsonIgnore and not reliable after a reload from persisted
|
||||
// storage, so the real existing bytes have to come from the asset store, the same place
|
||||
// getPackageVersionAsset reads them from. A genuinely missing backing file (metadata says
|
||||
// the asset exists but its bytes don't, e.g. a partial restore) reads the same as "nothing
|
||||
// to conflict with" rather than a hard failure, so a republish can still repair it.
|
||||
if (assets.containsKey(assetName) && !existingAssetMatchesOrIsMissing(owner, key, assetName, assetContent)) {
|
||||
throw conflict("Asset '" + assetName + "' already exists for package version '" + version
|
||||
+ "' of package '" + packageName + "' with different content.", assetName, "asset");
|
||||
}
|
||||
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,
|
||||
@@ -973,6 +989,43 @@ public class CodeArtifactService implements Resettable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code true} when the asset's backing bytes are genuinely missing (e.g. a partial restore
|
||||
* left the metadata but not the file, so there is nothing to conflict with) or match
|
||||
* {@code newContent}; {@code false} only on a real mismatch. Deliberately never materializes
|
||||
* the existing file as a second full byte array: {@code newContent} is already fully resident
|
||||
* (RESTEasy Reactive buffers the whole request body before this method ever runs), so loading a
|
||||
* second full copy of a large existing asset just to compare it would double peak memory for a
|
||||
* same-content retry. Also deliberately separate from {@link #readAssetContent}: a real
|
||||
* download through {@code getPackageVersionAsset} needs a missing file to stay a hard failure,
|
||||
* not silently read as "nothing published yet".
|
||||
*/
|
||||
private boolean existingAssetMatchesOrIsMissing(String owner, String packageVersionKey, String assetName,
|
||||
byte[] newContent) {
|
||||
if (inMemory) {
|
||||
byte[] existing = memoryAssetStore.get(assetStoreKey(owner, packageVersionKey, assetName));
|
||||
return existing == null || Arrays.equals(existing, newContent);
|
||||
}
|
||||
Path filePath = resolveAssetPath(owner, packageVersionKey, assetName);
|
||||
try (InputStream in = Files.newInputStream(filePath)) {
|
||||
byte[] buffer = new byte[8192];
|
||||
int offset = 0;
|
||||
int read;
|
||||
while ((read = in.read(buffer)) != -1) {
|
||||
if (offset + read > newContent.length || !Arrays.equals(buffer, 0, read,
|
||||
newContent, offset, offset + read)) {
|
||||
return false;
|
||||
}
|
||||
offset += read;
|
||||
}
|
||||
return offset == newContent.length;
|
||||
} catch (NoSuchFileException e) {
|
||||
return true;
|
||||
} 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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user