CodeSampleX

示例

github.com/caddyserver/certmagic v0.23.0: verify certmagic FileStorage and Storage interface operations

已验证示例 — golang github.com/caddyserver/certmagic v0.23.0: verify certmagic FileStorage and Storage interface operations. contract 在 go 1.26 · linux alpine/x64…

sha256:a58e72a0b3a5261a3d22372971a69cd609879727194b41b2437d7962228ac950

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-28

案例

HOW
目标
verify certmagic FileStorage and Storage interface operations
包
符号
  • github.com/caddyserver/certmagic.FileStorage
  • github.com/caddyserver/certmagic.Storage
创建时间
2026-08-28T01:58:41Z

契约

  1. certmagic.FileStorage implements the certmagic.Storage interface for persistent key-value storage
  2. certmagic.FileStorage.Store persists data and Exists reports true for existing keys and false for missing keys
  3. certmagic.FileStorage.Load retrieves stored byte content and returns fs.ErrNotExist for nonexistent keys
  4. certmagic.FileStorage.Stat returns valid KeyInfo with accurate Size, Modified timestamp, and IsTerminal flag
  5. certmagic.FileStorage.List lists stored keys under a prefix with both non-recursive and recursive traversal
  6. certmagic.FileStorage.Lock acquires an exclusive lock blocking concurrent lock attempts until Unlock
  7. certmagic.FileStorage.Delete removes a stored key and subsequent Exists returns false

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify certmagic FileStorage and Storage interface operations
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/caddyserver/certmagic@v0.23.0
Demonstrate these symbols/APIs:
  - github.com/caddyserver/certmagic.FileStorage
  - github.com/caddyserver/certmagic.Storage

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:f4ee76dbd8417499eb9be3e24af3266fac3dcb22f6eea3167c83c536d26fff2c","contract":["certmagic.FileStorage implements the certmagic.Storage interface for persistent key-value storage","certmagic.FileStorage.Store persists data and Exists reports true for existing keys and false for missing keys","certmagic.FileStorage.Load retrieves stored byte content and returns fs.ErrNotExist for nonexistent keys","certmagic.FileStorage.Stat returns valid KeyInfo with accurate Size, Modified timestamp, and IsTerminal flag","certmagic.FileStorage.List lists stored keys under a prefix with both non-recursive and recursive traversal","certmagic.FileStorage.Lock acquires an exclusive lock blocking concurrent lock attempts until Unlock","certmagic.FileStorage.Delete removes a stored key and subsequent Exists returns false"],"goal":"verify certmagic FileStorage and Storage interface operations","kind":"HOW","packages":["pkg:golang/github.com/caddyserver/certmagic@v0.23.0"],"schemaVersion":1,"symbols":["github.com/caddyserver/certmagic.FileStorage","github.com/caddyserver/certmagic.Storage"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/caddyserver/certmagic@v0.23.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/caddyserver/certmagic@v0.23.0","symbols":["github.com/caddyserver/certmagic.FileStorage","github.com/caddyserver/certmagic.Storage"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.24.0

require github.com/caddyserver/certmagic v0.23.0

require (
	github.com/caddyserver/zerossl v0.1.3 // indirect
	github.com/klauspost/cpuid/v2 v2.2.10 // indirect
	github.com/libdns/libdns v1.0.0-beta.1 // indirect
	github.com/mholt/acmez/v3 v3.1.2 // indirect
	github.com/miekg/dns v1.1.63 // indirect
	github.com/zeebo/blake3 v0.2.4 // indirect
	go.uber.org/multierr v1.11.0 // indirect
	go.uber.org/zap v1.27.0 // indirect
	go.uber.org/zap/exp v0.3.0 // indirect
	golang.org/x/crypto v0.36.0 // indirect
	golang.org/x/mod v0.24.0 // indirect
	golang.org/x/net v0.37.0 // indirect
	golang.org/x/sync v0.12.0 // indirect
	golang.org/x/sys v0.31.0 // indirect
	golang.org/x/text v0.23.0 // indirect
	golang.org/x/tools v0.31.0 // indirect
)
go.sum
github.com/caddyserver/certmagic v0.23.0 h1:CfpZ/50jMfG4+1J/u2LV6piJq4HOfO6ppOnOf7DkFEU=
github.com/caddyserver/certmagic v0.23.0/go.mod h1:9mEZIWqqWoI+Gf+4Trh04MOVPD0tGSxtqsxg87hAIH4=
github.com/caddyserver/zerossl v0.1.3 h1:onS+pxp3M8HnHpN5MMbOMyNjmTheJyWRaZYwn+YTAyA=
github.com/caddyserver/zerossl v0.1.3/go.mod h1:CxA0acn7oEGO6//4rtrRjYgEoa4MFw/XofZnrYwGqG4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/libdns/libdns v1.0.0-beta.1 h1:KIf4wLfsrEpXpZ3vmc/poM8zCATXT2klbdPe6hyOBjQ=
github.com/libdns/libdns v1.0.0-beta.1/go.mod h1:4Bj9+5CQiNMVGf87wjX4CY3HQJypUHRuLvlsfsZqLWQ=
github.com/mholt/acmez/v3 v3.1.2 h1:auob8J/0FhmdClQicvJvuDavgd5ezwLBfKuYmynhYzc=
github.com/mholt/acmez/v3 v3.1.2/go.mod h1:L1wOU06KKvq7tswuMDwKdcHeKpFFgkppZy/y0DFxagQ=
github.com/miekg/dns v1.1.63 h1:8M5aAw6OMZfFXTT7K5V0Eu5YiiL8l7nUAkyN6C9YwaY=
github.com/miekg/dns v1.1.63/go.mod h1:6NGHfjhpmr5lt3XPLuyfDJi5AXbNIPM9PY6H6sF1Nfs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/zeebo/assert v1.1.0 h1:hU1L1vLTHsnO8x8c9KAR5GmM5QscxHg5RNU5z5qbUWY=
github.com/zeebo/assert v1.1.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
github.com/zeebo/blake3 v0.2.4 h1:KYQPkhpRtcqh0ssGYcKLG1JYvddkEA8QwCM/yBqhaZI=
github.com/zeebo/blake3 v0.2.4/go.mod h1:7eeQ6d2iXWRGF6npfaxl2CU+xy2Fjo2gxeyZGCRUjcE=
github.com/zeebo/pcg v1.0.1 h1:lyqfGeWiv4ahac6ttHs+I5hwtH/+1mrhlCtVNQM2kHo=
github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
go.uber.org/zap/exp v0.3.0 h1:6JYzdifzYkGmTdRR59oYH+Ng7k49H9qVpWwNSsGJj3U=
go.uber.org/zap/exp v0.3.0/go.mod h1:5I384qq7XGxYyByIhHm6jg5CHkGY0nsTfbDLgDDlgJQ=
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU=
golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
main.go
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/caddyserver/certmagic"
)

func main() {
	ctx := context.Background()

	// 1. Create a local temporary directory for FileStorage
	tmpDir, err := os.MkdirTemp("", "certmagic-storage-*")
	if err != nil {
		log.Fatalf("Failed to create temp dir: %v", err)
	}
	defer os.RemoveAll(tmpDir)

	// 2. Initialize FileStorage implementing certmagic.Storage
	var storage certmagic.Storage = &certmagic.FileStorage{Path: tmpDir}
	fmt.Printf("Initialized FileStorage at: %s\n", tmpDir)

	// 3. Store data
	key := "certificates/example.com/cert.pem"
	data := []byte("-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----")
	if err := storage.Store(ctx, key, data); err != nil {
		log.Fatalf("Failed to store data: %v", err)
	}

	// 4. Check existence and load data
	if storage.Exists(ctx, key) {
		loaded, err := storage.Load(ctx, key)
		if err != nil {
			log.Fatalf("Failed to load data: %v", err)
		}
		fmt.Printf("Loaded key %s (%d bytes)\n", key, len(loaded))
	}

	// 5. Stat key
	info, err := storage.Stat(ctx, key)
	if err != nil {
		log.Fatalf("Failed to stat key: %v", err)
	}
	fmt.Printf("Key info: Size=%d, Modified=%v, IsTerminal=%v\n", info.Size, info.Modified, info.IsTerminal)

	// 6. List keys under prefix
	keys, err := storage.List(ctx, "certificates", true)
	if err != nil {
		log.Fatalf("Failed to list keys: %v", err)
	}
	fmt.Printf("Found %d key(s) under 'certificates': %v\n", len(keys), keys)

	// 7. Lock and Unlock
	lockKey := "certificates/example.com.lock"
	if err := storage.Lock(ctx, lockKey); err != nil {
		log.Fatalf("Failed to acquire lock: %v", err)
	}
	fmt.Println("Acquired lock successfully")
	if err := storage.Unlock(ctx, lockKey); err != nil {
		log.Fatalf("Failed to release lock: %v", err)
	}
	fmt.Println("Released lock successfully")

	// 8. Delete stored data
	if err := storage.Delete(ctx, key); err != nil {
		log.Fatalf("Failed to delete key: %v", err)
	}
	fmt.Printf("Deleted key %s (Exists: %v)\n", key, storage.Exists(ctx, key))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify certmagic FileStorage and Storage interface operations",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/caddyserver/certmagic@v0.23.0"
  ],
  "symbols": [
    "github.com/caddyserver/certmagic.FileStorage",
    "github.com/caddyserver/certmagic.Storage"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"context"
	"errors"
	"fmt"
	"io/fs"
	"os"
	"time"

	"github.com/caddyserver/certmagic"
)

func main() {
	ctx := context.Background()
	tmpDir, err := os.MkdirTemp("", "certmagic-contract-*")
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to create temp directory: %v\n", err)
		os.Exit(1)
	}
	defer os.RemoveAll(tmpDir)

	// 1. certmagic.FileStorage implements the certmagic.Storage interface for persistent key-value storage
	var storage certmagic.Storage = &certmagic.FileStorage{Path: tmpDir}
	if storage == nil {
		fmt.Fprintf(os.Stderr, "FileStorage does not satisfy Storage interface\n")
		os.Exit(1)
	}

	// 2. certmagic.FileStorage.Store persists data and Exists reports true for existing keys and false for missing keys
	key1 := "certs/example.com/site.crt"
	val1 := []byte("cert-data-123")
	if storage.Exists(ctx, key1) {
		fmt.Fprintf(os.Stderr, "Exists returned true before storing key\n")
		os.Exit(1)
	}
	if err := storage.Store(ctx, key1, val1); err != nil {
		fmt.Fprintf(os.Stderr, "Store failed: %v\n", err)
		os.Exit(1)
	}
	if !storage.Exists(ctx, key1) {
		fmt.Fprintf(os.Stderr, "Exists returned false after storing key\n")
		os.Exit(1)
	}
	if storage.Exists(ctx, "nonexistent/key") {
		fmt.Fprintf(os.Stderr, "Exists returned true for nonexistent key\n")
		os.Exit(1)
	}

	// 3. certmagic.FileStorage.Load retrieves stored byte content and returns fs.ErrNotExist for nonexistent keys
	loadedVal, err := storage.Load(ctx, key1)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Load failed for existing key: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(loadedVal, val1) {
		fmt.Fprintf(os.Stderr, "Load returned corrupted data: got %q, want %q\n", loadedVal, val1)
		os.Exit(1)
	}
	_, errMissing := storage.Load(ctx, "nonexistent/key")
	if errMissing == nil {
		fmt.Fprintf(os.Stderr, "Load for nonexistent key did not return error\n")
		os.Exit(1)
	}
	if !errors.Is(errMissing, fs.ErrNotExist) {
		fmt.Fprintf(os.Stderr, "Load for nonexistent key did not wrap fs.ErrNotExist: %v\n", errMissing)
		os.Exit(1)
	}

	// 4. certmagic.FileStorage.Stat returns valid KeyInfo with accurate Size, Modified timestamp, and IsTerminal flag
	info, err := storage.Stat(ctx, key1)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Stat failed for existing key: %v\n", err)
		os.Exit(1)
	}
	if info.Size != int64(len(val1)) {
		fmt.Fprintf(os.Stderr, "Stat returned wrong size: got %d, want %d\n", info.Size, len(val1))
		os.Exit(1)
	}
	if !info.IsTerminal {
		fmt.Fprintf(os.Stderr, "Stat returned IsTerminal=false for file key\n")
		os.Exit(1)
	}
	if time.Since(info.Modified) > 5*time.Minute {
		fmt.Fprintf(os.Stderr, "Stat returned unrealistic Modified time: %v\n", info.Modified)
		os.Exit(1)
	}

	// 5. certmagic.FileStorage.List lists stored keys under a prefix with both non-recursive and recursive traversal
	key2 := "certs/example.com/site.key"
	key3 := "certs/other.com/site.crt"
	if err := storage.Store(ctx, key2, []byte("key-data-456")); err != nil {
		fmt.Fprintf(os.Stderr, "Store key2 failed: %v\n", err)
		os.Exit(1)
	}
	if err := storage.Store(ctx, key3, []byte("other-cert-789")); err != nil {
		fmt.Fprintf(os.Stderr, "Store key3 failed: %v\n", err)
		os.Exit(1)
	}

	// Non-recursive listing on prefix "certs" returns immediate children
	nonRecKeys, err := storage.List(ctx, "certs", false)
	if err != nil {
		fmt.Fprintf(os.Stderr, "List non-recursive failed: %v\n", err)
		os.Exit(1)
	}
	if len(nonRecKeys) != 2 {
		fmt.Fprintf(os.Stderr, "List non-recursive expected 2 entries, got %d: %v\n", len(nonRecKeys), nonRecKeys)
		os.Exit(1)
	}

	// Recursive listing on prefix "certs" returns directories and file keys
	recKeys, err := storage.List(ctx, "certs", true)
	if err != nil {
		fmt.Fprintf(os.Stderr, "List recursive failed: %v\n", err)
		os.Exit(1)
	}
	if len(recKeys) != 5 {
		fmt.Fprintf(os.Stderr, "List recursive expected 5 entries, got %d: %v\n", len(recKeys), recKeys)
		os.Exit(1)
	}

	// 6. certmagic.FileStorage.Lock acquires an exclusive lock blocking concurrent lock attempts until Unlock
	lockName := "certs/example.com/cert.lock"
	if err := storage.Lock(ctx, lockName); err != nil {
		fmt.Fprintf(os.Stderr, "Initial Lock failed: %v\n", err)
		os.Exit(1)
	}

	// Attempting to lock the same resource with a short timeout should fail/timeout
	timeoutCtx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
	secondLockErr := storage.Lock(timeoutCtx, lockName)
	cancel()
	if secondLockErr == nil {
		fmt.Fprintf(os.Stderr, "Second Lock unexpectedly succeeded while lock was held\n")
		os.Exit(1)
	}

	if err := storage.Unlock(ctx, lockName); err != nil {
		fmt.Fprintf(os.Stderr, "Unlock failed: %v\n", err)
		os.Exit(1)
	}

	// After unlock, locking should succeed again
	if err := storage.Lock(ctx, lockName); err != nil {
		fmt.Fprintf(os.Stderr, "Lock failed after Unlock: %v\n", err)
		os.Exit(1)
	}
	if err := storage.Unlock(ctx, lockName); err != nil {
		fmt.Fprintf(os.Stderr, "Second Unlock failed: %v\n", err)
		os.Exit(1)
	}

	// 7. certmagic.FileStorage.Delete removes a stored key and subsequent Exists returns false
	if err := storage.Delete(ctx, key1); err != nil {
		fmt.Fprintf(os.Stderr, "Delete failed: %v\n", err)
		os.Exit(1)
	}
	if storage.Exists(ctx, key1) {
		fmt.Fprintf(os.Stderr, "Key still exists after Delete\n")
		os.Exit(1)
	}

	fmt.Println("All certmagic FileStorage contract assertions passed.")
}

原始种子者

匿名