Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/kosli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ Paths the list already matches stay excluded whatever is later added there, so k
downloadConcurrencyFlag = "[optional] The number of S3 objects to fetch at the same time when fingerprinting the bucket. When downloading, the default, each object in flight may hold up to 40 MB of download buffers in memory, on top of the disk the --download-budget allows. With --fingerprint-source metadata it bounds the checksum reads instead, which hold neither, and defaults to 32."
downloadBudgetFlag = "[optional] The maximum total size of the S3 objects downloading at the same time, which caps the temporary disk the snapshot uses. A bare number is megabytes; add K, M, G or T (optionally followed by B) to choose the unit, e.g. 512M or 8G. An object larger than the budget still downloads, on its own. Objects are downloaded to the OS temporary directory. Has no effect with --fingerprint-source metadata, which reads stored checksums and uses no temporary disk."
s3FingerprintSourceFlag = "[defaulted] Where each object's SHA256 comes from when fingerprinting the bucket. Valid sources are: [" + validS3FingerprintSources + "]. 'content' downloads every contributing object and hashes it. 'metadata' reads the SHA256 checksum S3 stores for each object instead, which skips the download but requires every contributing object to have been uploaded with a full-object SHA256 checksum, unless --skip-unusable-checksums is set. Both produce the same fingerprint and need the same permissions."
s3SkipUnusableChecksumsFlag = "[optional] With --fingerprint-source metadata, leave out every object whose SHA256 checksum is missing, multipart (composite) or undecodable instead of failing the snapshot. The skipped objects are named in a warning. The fingerprint then covers the remaining objects only, so it does not match an artifact attested from the full bucket content. When no object is left, an empty snapshot is reported, so the environment shows the bucket as holding no artifact."
s3SkipUnusableChecksumsFlag = "[optional] With --fingerprint-source metadata, leave out every object whose SHA256 checksum is missing, multipart (composite) or undecodable instead of failing the snapshot. The skipped objects are named in a warning. The fingerprint then covers the remaining objects only, so it does not match an artifact attested from the full bucket content. When no object is left, the bucket is reported as an empty bucket, with the fingerprint of an empty directory."
bucketPathsFlag = "[optional] The comma separated list of file and/or directory paths in the S3 bucket to include when fingerprinting. Paths match by literal prefix. Cannot be used together with --exclude or --exclude-regex."
bucketPathsRegexFlag = "[optional] The comma separated list of Go regular expressions matched against object keys in the S3 bucket to include when fingerprinting. Cannot be used together with --exclude or --exclude-regex."
excludeBucketPathsFlag = "[optional] The comma separated list of file and/or directory paths in the S3 bucket to exclude when fingerprinting. Paths match by literal prefix. Cannot be used together with --include or --include-regex."
Expand Down
6 changes: 4 additions & 2 deletions cmd/kosli/snapshotS3.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ const snapshotS3ShortDesc = `Report a snapshot of the content of an AWS S3 bucke

const snapshotS3LongDesc = snapshotS3ShortDesc + awsAuthDesc + `
You can report the entire bucket content, or filter some of the content using ^--include^ / ^--exclude^ (literal prefix match) or ^--include-regex^ / ^--exclude-regex^ (Go regular expressions matched against the full object key).
In all cases, the content is reported as one artifact. If you wish to report separate files/dirs within the same bucket as separate artifacts, you need to run the command twice.
In all cases, the content is reported as one artifact, named after the bucket. If you wish to report separate files/dirs within the same bucket as separate artifacts, you need to run the command twice.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both runs now produce an artefact with the same name, and the sentence does not say they need separate environments.

The fingerprint is the one ^kosli fingerprint^ gives for a local copy of the reported content: ^--artifact-type file^ when a single object is reported, ^--artifact-type dir^ otherwise.
A bucket with nothing to report is not an error. Whether it holds no objects or the filters leave none, it is still reported as one artifact named after the bucket, with the fingerprint of an empty directory (^e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855^) and a timestamp of 0. When filters are what left nothing, a warning is logged, since a mistyped filter has the same effect.
Object keys are never used as local file names: by default each object is downloaded to a temporary file, hashed and removed, and the fingerprint is computed from the keys and the content digests, so any key S3 accepts can be fingerprinted on any operating system.
Keys that cannot form a directory tree are rejected and fail the snapshot, naming every key involved: a key containing a ^..^ segment, two keys that resolve to the same path (such as ^a//b^ and ^a/b^), or an object whose key is also a prefix of other objects (such as ^a^ beside ^a/b^). A legitimate key of that shape can be left out with ^--exclude-regex^ (anchor and escape it, since the pattern is a regular expression matched against the whole key); when ^--include^ or ^--include-regex^ is set, exclude filters are ignored, so narrow the include filter instead.

By default each object's SHA256 comes from downloading the object and hashing it. ^--fingerprint-source metadata^ reads the SHA256 checksum S3 stores for the object instead, which skips the download, the temporary disk and the hashing. Everything else, like the keys, the ^.kosli_ignore^ rules, the way digests combine into the fingerprint, is the same in both modes, so the fingerprint is identical and a snapshot matches the artifact you attested either way. Two conditions apply:
- Every contributing object must carry a full-object SHA256 checksum. S3 only stores one when the upload asked for it, for example ^aws s3api put-object --checksum-algorithm SHA256^. Objects without one fail the snapshot, all named in one run.
- A multipart upload gets a composite SHA256, which hashes the checksums of the parts rather than the object content, so it cannot serve as the object's fingerprint. Such an object can be collapsed into a single part in place with ^aws s3api copy-object --checksum-algorithm SHA256 --copy-source yourBucket/yourKey --bucket yourBucket --key yourKey^.
^--skip-unusable-checksums^ relaxes both conditions: an object whose SHA256 checksum is missing, composite or undecodable is left out with a warning that names it, and the snapshot is reported from the remaining objects, as if the bucket did not hold the skipped ones. Such a snapshot has a different fingerprint from an artifact attested from the full bucket content, so it does not match that artifact in Kosli until every object carries a usable checksum. When no object is left, an empty snapshot is reported, so the environment shows the bucket as holding no artifact.
^--skip-unusable-checksums^ relaxes both conditions: an object whose SHA256 checksum is missing, composite or undecodable is left out with a warning that names it, and the snapshot is reported from the remaining objects, as if the bucket did not hold the skipped ones. Such a snapshot has a different fingerprint from an artifact attested from the full bucket content, so it does not match that artifact in Kosli until every object carries a usable checksum. When no object is left, the bucket is reported as an empty bucket.
A root ^.kosli_ignore^ is still downloaded in this mode, because its rules decide which objects contribute; the objects it excludes are never fetched and need no checksum. Reading a checksum does not need fewer permissions than downloading: AWS requires ^s3:GetObject^ for both, and an SSE-KMS encrypted object additionally needs ^kms:GenerateDataKey^ and ^kms:Decrypt^ either way.

` + kosliIgnoreDescNoExclude
Expand Down
46 changes: 40 additions & 6 deletions cmd/kosli/snapshotS3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,16 @@ func (suite *SnapshotS3TestSuite) TestSnapshotS3Cmd() {
golden: "bucket kosli-cli-public was reported to environment snapshot-s3-env\n",
},
{
wantError: true,
name: "fails when --include does not match any file or dir",
cmd: fmt.Sprintf(`snapshot s3 %s %s --bucket %s --include non-existing.md`, suite.envName, suite.defaultKosliArguments, suite.bucketName),
golden: "Error: no matching file or dirs in bucket: [kosli-cli-public]\n",
name: "reports an empty bucket, with a warning, when --include does not match any file or dir",
cmd: fmt.Sprintf(`snapshot s3 %s %s --bucket %s --include non-existing.md`, suite.envName, suite.defaultKosliArguments, suite.bucketName),
golden: "[warning] no object in bucket [kosli-cli-public] matches the include and exclude filters (or the bucket is empty); reporting it as an empty bucket\n" +
"bucket kosli-cli-public was reported to environment snapshot-s3-env\n",
},
{
name: "reports an empty bucket, with a warning, when --exclude-regex removes every file",
cmd: fmt.Sprintf(`snapshot s3 %s %s --bucket %s --exclude-regex '.*'`, suite.envName, suite.defaultKosliArguments, suite.bucketName),
golden: "[warning] no object in bucket [kosli-cli-public] matches the include and exclude filters (or the bucket is empty); reporting it as an empty bucket\n" +
"bucket kosli-cli-public was reported to environment snapshot-s3-env\n",
},
{
name: "can snapshot entire bucket except a subset of files/dirs using --exclude",
Expand Down Expand Up @@ -179,10 +185,10 @@ func (suite *SnapshotS3TestSuite) TestSnapshotS3Cmd() {
"bucket kosli-cli-public was reported to environment snapshot-s3-env\n",
},
{
name: "--skip-unusable-checksums reports an empty snapshot when no object has a stored checksum",
name: "--skip-unusable-checksums reports an empty bucket when no object has a stored checksum",
cmd: fmt.Sprintf(`snapshot s3 %s %s --bucket %s --include dummy --fingerprint-source metadata --skip-unusable-checksums`, suite.envName, suite.defaultKosliArguments, suite.bucketName),
golden: "[warning] skipped 1 of the 1 objects whose checksum was read in bucket [kosli-cli-public]; they have no usable SHA256 checksum, so the fingerprint does not cover them:\n[dummy/dummy_2/template.yml]\n" +
"[warning] no object in bucket [kosli-cli-public] is left to fingerprint; reporting an empty snapshot\n" +
"[warning] no object in bucket [kosli-cli-public] is left to fingerprint; reporting it as an empty bucket\n" +
"bucket kosli-cli-public was reported to environment snapshot-s3-env\n",
},
{
Expand All @@ -198,6 +204,34 @@ func (suite *SnapshotS3TestSuite) TestSnapshotS3Cmd() {
}
}

// Also confirms the server accepts the empty-directory fingerprint and the
// zero timestamp an empty bucket is reported with.
func (suite *SnapshotS3TestSuite) TestSnapshotS3CmdOnAnEmptyBucket() {
bucketName := suite.bucketName
aws.NewS3ClientFunc = func(_ *aws.AWSStaticCreds) (aws.S3API, error) {
return &aws.FakeS3Client{Bucket: bucketName, Objects: map[string][]byte{"dummy/": nil}}, nil
}

tests := []cmdTestCase{
{
name: "snapshot s3 reports an empty bucket",
cmd: fmt.Sprintf(`snapshot s3 %s %s --bucket %s`, suite.envName, suite.defaultKosliArguments, suite.bucketName),
golden: "bucket [kosli-cli-public] holds no objects; reporting it as an empty bucket\n" +
"bucket kosli-cli-public was reported to environment snapshot-s3-env\n",
},
{
name: "snapshot s3 reports an empty bucket with --fingerprint-source metadata",
cmd: fmt.Sprintf(`snapshot s3 %s %s --bucket %s --fingerprint-source metadata`, suite.envName, suite.defaultKosliArguments, suite.bucketName),
golden: "bucket [kosli-cli-public] holds no objects; reporting it as an empty bucket\n" +
"bucket kosli-cli-public was reported to environment snapshot-s3-env\n",
},
}

for _, t := range tests {
runTestCmd(suite.T(), []cmdTestCase{t})
}
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestSnapshotS3TestSuite(t *testing.T) {
Expand Down
12 changes: 10 additions & 2 deletions docs/adr/20260911-s3-fingerprint-from-virtual-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ The fingerprint format itself is fixed. An S3 snapshot must match the fingerprin

5. **Downloads may run in parallel**, behind a fixed worker pool and a bytes-in-flight budget derived from listing sizes, with results written by index for determinism and a cancellable context so the first transport error stops in-flight multipart downloads. This is a performance property, not a safety one, and was delivered separately from the change this record describes; see #1167. The defaults are eight objects in flight within 512 MB of listed bytes, tunable with `--download-concurrency` and `--download-budget`, so peak temp disk is the budget rather than one object.

6. **The switch is pinned, not argued.** `TestPinnedFingerprints` in `internal/aws` holds fingerprints of representative fake buckets recorded against the key-layout implementation before it was replaced: unusual key shapes, a `.` sorting before a `/`, nested prefixes with folder markers, and a single object with its basename as artifact name. It was green before the switch and must stay green after it, alongside `TestGetS3DataFromClientKeepsTodaysLayoutForUnusualKeys` from #1155.
6. **The switch is pinned, not argued.** `TestPinnedFingerprints` in `internal/aws` holds fingerprints of representative fake buckets recorded against the key-layout implementation before it was replaced: unusual key shapes, a `.` sorting before a `/`, nested prefixes with folder markers, and a single object with its basename as artifact name (named after the bucket since the 2026-11-25 amendment under Guarantees). It was green before the switch and must stay green after it, alongside `TestGetS3DataFromClientKeepsTodaysLayoutForUnusualKeys` from #1155.

## Guarantees

> **Amended 2026-11-25.** The artifact name and the empty case described in this record have since
> changed; the fingerprint calculation has not. An S3 snapshot now always reports exactly one
> artifact, named after the bucket, including when a single object is reported (previously named
> after the object). A bucket with nothing to fingerprint, because it holds no objects or because
> the filters or skipped checksums leave none, is reported with the fingerprint of an empty
> directory and a timestamp of 0 instead of failing. So "keeps its fingerprint and artifact name"
> below now holds for the fingerprint only in the single-object case.

The attest side is unchanged: `kosli attest artifact --artifact-type dir` still runs `DirSha256` on a real directory, and that defines the format. The snapshot side re-derives the same value from the bucket. Three guarantees follow, each with the test that holds it:

- **Snapshot equals attestation.** `VirtualDirSha256` of the bucket's `(path, sha256)` pairs equals `DirSha256` of the directory those objects were uploaded from, and a single object equals `FileSha256` plus its basename. Held by the materialise-then-compare tests in `internal/digest` (`TestVirtualDirTestSuite`, `TestVirtualIgnoreTestSuite`) and by `TestMatchesAttestedDirectory` in `internal/aws`, which hashes a directory, uploads its files to the fake bucket and snapshots it.
- **Snapshot equals attestation.** `VirtualDirSha256` of the bucket's `(path, sha256)` pairs equals `DirSha256` of the directory those objects were uploaded from, and a single object equals `FileSha256` of that object (its artifact name was its basename, and is the bucket name since the amendment above). Held by the materialise-then-compare tests in `internal/digest` (`TestVirtualDirTestSuite`, `TestVirtualIgnoreTestSuite`) and by `TestMatchesAttestedDirectory` in `internal/aws`, which hashes a directory, uploads its files to the fake bucket and snapshots it.
- **Snapshot equals its own history.** Every bucket that snapshotted successfully under the key-layout implementation keeps its fingerprint and artifact name, because the fold rule is what `filepath.Join` did. Held by `TestPinnedFingerprints` and `TestGetS3DataFromClientKeepsTodaysLayoutForUnusualKeys`.
- **No object is lost silently.** Every key S3 accepts is representable, whatever the operating system, because the key never becomes a path. Rejections are of two kinds, and each fails the snapshot rather than shorten the manifest. Keys that cannot form a directory tree at all: a `..` segment, two keys folding onto one path, and an object that is also a prefix. S3 permits those, no filesystem does, so no attested directory could match them, and the error names every key involved. And listings the store returned malformed: an entry with no key, a key listed more than once, and no modification time on any matching object. Real S3 produces none of these; an S3-compatible store might, and each is an error rather than a guess. A path deeper than `globSeparatorsLimit` segments is also rejected, which no S3 key can reach, so the exported digest functions carry the bound for other callers. Held by `TestEveryS3KeyIsRepresentable`, which generates keys over S3's full character set, and `TestDownloadsExactlyTheContributingObjects`, which asserts the downloaded set equals the listed objects minus markers and exclusions.

Expand Down
Loading
Loading