feat: retain encrypted instance credentials and reissue v2 certificates
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-12 11:41:02 +08:00
parent fad1fba710
commit 9ba8053b5b
21 changed files with 449 additions and 39 deletions

View File

@@ -11,14 +11,15 @@ import (
)
type Config struct {
HTTPAddr string
TrustedProxies []string
ReadHeaderTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ShutdownTimeout time.Duration
SigningPrivateKeyPEM string
HTTPAddr string
TrustedProxies []string
ReadHeaderTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ShutdownTimeout time.Duration
SigningPrivateKeyPEM string
CredentialEncryptionKey []byte
}
func Load(getenv func(string) string) (Config, error) {
@@ -85,6 +86,16 @@ func Load(getenv func(string) string) (Config, error) {
}
c.SigningPrivateKeyPEM = string(data)
}
if path := getenv("LICENSE_CREDENTIAL_ENCRYPTION_KEY_FILE"); path != "" {
if !filepath.IsAbs(path) {
return Config{}, fmt.Errorf("credential encryption key path must be absolute")
}
data, err := os.ReadFile(path)
if err != nil || len(data) != 32 {
return Config{}, fmt.Errorf("credential encryption key must be exactly 32 raw bytes")
}
c.CredentialEncryptionKey = data
}
if root := getenv("PACKAGE_STORAGE_ROOT"); root != "" {
if !filepath.IsAbs(root) {
return Config{}, fmt.Errorf("PACKAGE_STORAGE_ROOT must be absolute")

View File

@@ -62,3 +62,25 @@ func TestLoadRejectsInvalid(t *testing.T) {
})
}
}
func TestCredentialKeyFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "credentials.key")
for _, size := range []int{31, 32, 33} {
if err := os.WriteFile(path, make([]byte, size), 0600); err != nil {
t.Fatal(err)
}
c, err := Load(func(name string) string {
if name == "LICENSE_CREDENTIAL_ENCRYPTION_KEY_FILE" {
return path
}
return ""
})
if size == 32 {
if err != nil || len(c.CredentialEncryptionKey) != 32 {
t.Fatal("valid key rejected")
}
} else if err == nil {
t.Fatal("invalid length accepted")
}
}
}