forked from caddyserver/certmagic
-
Notifications
You must be signed in to change notification settings - Fork 1
Add capability for gradual rollout of transition mode #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9dee912
Add capability for gradual rollout of transition mode
philippta e600a41
Add note about storage mode race conditions
philippta 54f0627
Use first SAN of CertificateResource for storage mode check
philippta 7737506
Add debug logging for storage mode operations
philippta cfe29c3
Simplify storage mode rollout tests with curated edge cases
philippta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package certmagic | ||
|
|
||
| import ( | ||
| "hash/fnv" | ||
| "os" | ||
| "strconv" | ||
| ) | ||
|
|
||
| const ( | ||
| // Storage mode controls the format in which certificates are stored in `Storage`. | ||
| // | ||
| // Formats: | ||
| // - legacy: Store cert, privkey and meta as three separate storage items (.cert, .key, .json). | ||
| // - bundle: Store cert, privkey and meta as a single, bundled storage item (.bundle). | ||
| // | ||
| // Modes: | ||
| // - legacy: Store and load certificates in legacy format. | ||
| // - transition: Store in legacy and bundle format, load as bundle with fallback to legacy format. | ||
| // - bundle: Store and load certificates in bundle format. | ||
| // | ||
| // In the transition mode, failures around reads and writes of the bundle are soft. | ||
| // They should only log errors and try to work with the legacy format as fallback. | ||
| // Operations on the legacy format are hard-failures, implying that errors should be propagated up. | ||
| // | ||
| // The rollout percentage enables a phased migration by controlling which domains | ||
| // enter the transition phase. If a domain's deterministic bucket (0-99) is below | ||
| // the rollout percentage, it uses 'transition' mode (dual-write, bundle-read). | ||
| // Otherwise, it remains in 'legacy' mode. | ||
| // | ||
| // The logic for selection is: | ||
| // if mode == StorageModeTransition: | ||
| // useTransition = hash(domain)%100 < rollout | ||
| // return useTransition ? StorageModeTransition : StorageModeLegacy | ||
| // | ||
| // The storage mode is controlled via the CERTMAGIC_STORAGE_MODE environment variable | ||
| StorageModeEnv = "CERTMAGIC_STORAGE_MODE" | ||
|
|
||
| StorageModeLegacy = "legacy" | ||
| StorageModeTransition = "transition" | ||
| StorageModeBundle = "bundle" | ||
|
|
||
| // StorageModeRolloutPercentEnv controls the percentage of domains that will use | ||
| // the bundle format when the storage mode is set to "transition". | ||
| // An empty rollout precent is equal to 0%. | ||
| StorageModeRolloutPercentEnv = "CERTMAGIC_STORAGE_MODE_ROLLOUT_PERCENT" | ||
| ) | ||
|
|
||
| var ( | ||
| StorageMode string | ||
| StorageModeRolloutPercent int | ||
| ) | ||
|
|
||
| func ConfigureStorageMode(mode string, rolloutPercent int) { | ||
| // Note: We have no lock protecting these variables. This is a potential race condition, yes. | ||
| // But this function is only called once during init(), before anything else happens. | ||
| StorageMode = mode | ||
| StorageModeRolloutPercent = rolloutPercent | ||
| } | ||
philippta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func init() { | ||
| mode := os.Getenv(StorageModeEnv) | ||
|
|
||
| // rolloutPercent becomes zero if env is unset or malformed | ||
| rolloutPercent, _ := strconv.Atoi(os.Getenv(StorageModeRolloutPercentEnv)) | ||
|
|
||
| ConfigureStorageMode(mode, rolloutPercent) | ||
| } | ||
|
|
||
| func StorageModeForDomain(domain string) string { | ||
| if StorageMode == StorageModeBundle { | ||
| return StorageModeBundle | ||
| } | ||
| if StorageMode != StorageModeTransition { | ||
| return StorageModeLegacy | ||
| } | ||
| if RolloutBucketForDomain(domain) < StorageModeRolloutPercent { | ||
| return StorageModeTransition | ||
| } else { | ||
| return StorageModeLegacy | ||
| } | ||
| } | ||
|
|
||
| func RolloutBucketForDomain(domain string) int { | ||
| h := fnv.New32a() | ||
| h.Write([]byte(domain)) | ||
| return int(h.Sum32() % 100) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package certmagic | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestStorageModeRolloutPercentLegacy(t *testing.T) { | ||
| // In legacy mode, storage mode for all domains must be "legacy", no matter the rollout percent. | ||
| for _, rolloutPercent := range []int{0, 50, 100} { | ||
| ConfigureStorageMode(StorageModeLegacy, rolloutPercent) | ||
|
|
||
| for _, domain := range []string{"cyufsv.com", "lgxeeu.com", "msngsw.com"} { | ||
| if got := StorageModeForDomain(domain); got != StorageModeLegacy { | ||
| t.Errorf("rollout %d%%, StorageModeForDomain(%q) = %q, want %q", | ||
| rolloutPercent, domain, got, StorageModeLegacy) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestStorageModeRolloutPercentBundle(t *testing.T) { | ||
| // In bundle mode, storage mode for all domains must be "bundle", no matter the rollout percent. | ||
| for _, rolloutPercent := range []int{0, 50, 100} { | ||
| ConfigureStorageMode(StorageModeBundle, rolloutPercent) | ||
|
|
||
| for _, domain := range []string{"cyufsv.com", "lgxeeu.com", "msngsw.com"} { | ||
| if got := StorageModeForDomain(domain); got != StorageModeBundle { | ||
| t.Errorf("rollout %d%%, StorageModeForDomain(%q) = %q, want %q", | ||
| rolloutPercent, domain, got, StorageModeBundle) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestStorageModeRolloutPercentTransition(t *testing.T) { | ||
| // In transition mode, storage mode for domains can either be "transition" or "legacy", depending on rollout percent. | ||
| // Domains are assigned to buckets 0-99 based on their hash. A domain enters transition mode | ||
| // if its bucket is below the rollout percent. | ||
| // | ||
| // Test domains and their buckets: | ||
| // "cyufsv.com" -> bucket 0 | ||
| // "wrgmsg.com" -> bucket 1 | ||
| // "cdbbdh.com" -> bucket 49 | ||
| // "lgxeeu.com" -> bucket 50 | ||
| // "hwqhre.com" -> bucket 51 | ||
| // "ckycee.com" -> bucket 98 | ||
| // "msngsw.com" -> bucket 99 | ||
| tests := []struct { | ||
| name string | ||
| rolloutPercent int | ||
| domain string | ||
| domainBucket int | ||
| want string | ||
| }{ | ||
| // 0% rollout: no domains should transition | ||
| {"0% rollout, bucket 0", 0, "cyufsv.com", 0, StorageModeLegacy}, | ||
| {"0% rollout, bucket 50", 0, "lgxeeu.com", 50, StorageModeLegacy}, | ||
| {"0% rollout, bucket 99", 0, "msngsw.com", 99, StorageModeLegacy}, | ||
|
|
||
| // 100% rollout: all domains should transition | ||
| {"100% rollout, bucket 0", 100, "cyufsv.com", 0, StorageModeTransition}, | ||
| {"100% rollout, bucket 50", 100, "lgxeeu.com", 50, StorageModeTransition}, | ||
| {"100% rollout, bucket 99", 100, "msngsw.com", 99, StorageModeTransition}, | ||
|
|
||
| // Edge cases: bucket exactly at rollout boundary | ||
| {"50% rollout, bucket 49 (just below)", 50, "cdbbdh.com", 49, StorageModeTransition}, | ||
| {"50% rollout, bucket 50 (exactly at)", 50, "lgxeeu.com", 50, StorageModeLegacy}, | ||
| {"50% rollout, bucket 51 (just above)", 50, "hwqhre.com", 51, StorageModeLegacy}, | ||
|
|
||
| // Edge cases at boundaries | ||
| {"1% rollout, bucket 0", 1, "cyufsv.com", 0, StorageModeTransition}, | ||
| {"1% rollout, bucket 1", 1, "wrgmsg.com", 1, StorageModeLegacy}, | ||
| {"99% rollout, bucket 98", 99, "ckycee.com", 98, StorageModeTransition}, | ||
| {"99% rollout, bucket 99", 99, "msngsw.com", 99, StorageModeLegacy}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ConfigureStorageMode(StorageModeTransition, tt.rolloutPercent) | ||
| if got := StorageModeForDomain(tt.domain); got != tt.want { | ||
| t.Errorf("StorageModeForDomain(%q) = %q, want %q (bucket %d, rollout %d%%)", | ||
| tt.domain, got, tt.want, tt.domainBucket, tt.rolloutPercent) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.