Files
seaweedfs/weed/replication/repl_util/sse_init.go
T
7c41ed0635 kms: swap a reload's providers in as a whole, after S3 parses its identities (#13344)
* kms: build every provider before a reload swaps them in

A reload replaced the providers one at a time, closing each old instance
as its replacement went in, and kept the providers and bucket mappings
the config no longer listed. Between closing the old default instance and
setting the new global provider, S3 could get the closed instance. A
reload now builds every provider first and swaps the whole set in at
once, along with the default, the bucket mappings and the global
provider. The default provider failing still fails the reload, and then
nothing changes. Any other provider that fails keeps its previous
instance. Providers the config no longer lists are dropped, and a bucket
mapped to one falls back to the default. A manager other than the global
one no longer sets the global provider. The manager no longer keeps the
provider configurations, which nothing read. Each load health-checks
every provider once, after the swap, for filer.toml as for the identities
file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3api: parse the identities before loading KMS

Loading the S3 config loaded KMS before parsing the identities, so a file
whose identities didn't parse still replaced the KMS providers. A file
without a kms block left the previous providers in place, and one with a
number out of float64 range, which protojson accepts, skipped KMS without
an error.

The identities are now parsed first, and a file that fails there changes
neither. A file without a kms block drops the providers, so a reload that
took the block out turns SSE-KMS off. A file encoding/json can't read
fails the KMS load with an error that doesn't quote it, as it could quote
key material. A KMS load that fails is logged as an error, not a warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* kms: skip a bucket mapping with an empty bucket name

SetBucketKMSProvider refused an empty bucket name, which the rebuilt
mappings no longer went through.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* kms: check a rejected load leaves the installed provider open

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-12-03 10:34:45 -08:00

54 lines
1.7 KiB
Go

package repl_util
import (
"sync"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/kms"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api"
"github.com/seaweedfs/seaweedfs/weed/util"
)
var (
sseInitMu sync.Mutex
sseInitialized bool
)
// InitializeSSEForReplication sets up SSE-S3 and SSE-KMS decryption so that
// replication sinks can transparently decrypt encrypted objects.
// SSE-S3 is initialized from the filer (KEK stored on filer).
// SSE-KMS is initialized from Viper config (security.toml [kms] section or
// WEED_KMS_* environment variables).
// SSE-C cannot be decrypted (customer key not available) and will error at
// decryption time.
//
// Safe to call multiple times; only the first successful initialization takes
// effect. Failed attempts do not prevent future retries.
func InitializeSSEForReplication(filerSource filer_pb.FilerClient) error {
sseInitMu.Lock()
defer sseInitMu.Unlock()
if sseInitialized {
return nil
}
// Initialize SSE-S3 key manager from filer
if err := s3api.GetSSES3KeyManager().InitializeWithFiler(filerSource); err != nil {
return err
}
// Attempt KMS initialization from Viper config.
// KMS configuration is typically in the S3 config file which the
// replication commands don't load directly. Support loading from
// security.toml [kms] section or WEED_KMS_* environment variables.
loader := kms.NewConfigLoader(util.GetViper())
if err := loader.LoadConfigurations(); err != nil {
glog.Warningf("KMS initialization from config failed: %v (SSE-KMS decryption will not be available)", err)
} else {
glog.V(0).Infof("KMS initialized for replication")
}
sseInitialized = true
return nil
}