mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-19 02:15:02 +01:00
* kms/openbao: refresh the AppRole token before it expires The provider logged in with AppRole once, when it was created, and never renewed the token. After the token's TTL every SSE-KMS upload, and every read of an SSE-KMS object whose data key was not cached, failed with "permission denied" (HTTP 500 to the client) until the S3 server was restarted. With AppRole authentication the provider now logs in again in the background once two thirds of the token's TTL have passed, and retries a failed login every 5 s. Each login is bounded by the client's timeout, or by 30 s when request_timeout is not positive. Close stops the refresh. A static token, or an AppRole token without a TTL, is left as it is. * kms: keep the OpenBao token refresh across configuration reloads - AddKMSProvider and InitializeGlobalKMS create a provider instance of their own with the new NewProvider instead of the registry's cached one: a reload closed the cached instance and then got it back, which now stopped its token refresh. The new instance is created before the old one is closed, so a configuration that fails leaves the current provider working - openbao: time each refresh from the start of the login that issued the token, since its TTL runs from then rather than from the response - tests: a reload gives a new instance and closes the old one, a failed reload keeps the old one; the refresh survives a reload; a slow login response * kms: create a replacement provider outside the manager lock AddKMSProvider ran the provider factory, which for OpenBao is a login that may wait for its timeout, while holding the manager lock, so every lookup, also of unrelated providers, waited for it. The new provider is now created before the lock is taken; the lock covers only the swap, and the replaced provider is closed after it. Test: a lookup while another provider is being created.
113 lines
3.2 KiB
Go
113 lines
3.2 KiB
Go
package kms
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
// closeTracker is a KMSProvider that records whether it was closed.
|
|
type closeTracker struct {
|
|
KMSProvider
|
|
closed bool
|
|
}
|
|
|
|
func (c *closeTracker) Close() error {
|
|
c.closed = true
|
|
return nil
|
|
}
|
|
|
|
// TestAddKMSProviderReplacesTheInstance checks a configuration reload: the provider gets a
|
|
// new instance of its own and the old one is closed, while a reload that fails keeps the
|
|
// current instance open and in use.
|
|
func TestAddKMSProviderReplacesTheInstance(t *testing.T) {
|
|
var created []*closeTracker
|
|
fail := false
|
|
RegisterProvider("replace-test", func(util.Configuration) (KMSProvider, error) {
|
|
if fail {
|
|
return nil, errors.New("bad configuration")
|
|
}
|
|
p := &closeTracker{KMSProvider: newCountingKMSProvider()}
|
|
created = append(created, p)
|
|
return p, nil
|
|
})
|
|
mgr := &KMSManager{
|
|
providers: make(map[string]KMSProvider),
|
|
configs: make(map[string]*KMSConfig),
|
|
bucketKMS: make(map[string]string),
|
|
}
|
|
add := func() error { return mgr.AddKMSProvider("p", &KMSConfig{Provider: "replace-test"}) }
|
|
current := func() KMSProvider {
|
|
p, err := mgr.GetKMSProviderByName("p")
|
|
if err != nil {
|
|
t.Fatalf("GetKMSProviderByName: %v", err)
|
|
}
|
|
return p
|
|
}
|
|
|
|
if err := add(); err != nil {
|
|
t.Fatalf("AddKMSProvider: %v", err)
|
|
}
|
|
if err := add(); err != nil {
|
|
t.Fatalf("AddKMSProvider again: %v", err)
|
|
}
|
|
if len(created) != 2 || !created[0].closed || created[1].closed || current() != created[1] {
|
|
t.Fatalf("after a reload: %d instances, first closed %v, second closed %v; want a new open instance in use",
|
|
len(created), created[0].closed, created[len(created)-1].closed)
|
|
}
|
|
|
|
fail = true
|
|
if err := add(); err == nil {
|
|
t.Fatal("AddKMSProvider with a failing configuration succeeded")
|
|
}
|
|
if created[1].closed || current() != created[1] {
|
|
t.Fatal("a failed reload closed or replaced the provider in use")
|
|
}
|
|
}
|
|
|
|
// TestAddKMSProviderDoesNotBlockLookups checks that creating a provider, which may wait on
|
|
// a remote login, does not hold up lookups of the providers already configured.
|
|
func TestAddKMSProviderDoesNotBlockLookups(t *testing.T) {
|
|
RegisterProvider("lookup-test", func(util.Configuration) (KMSProvider, error) {
|
|
return newCountingKMSProvider(), nil
|
|
})
|
|
started, release := make(chan struct{}), make(chan struct{})
|
|
RegisterProvider("slow-test", func(util.Configuration) (KMSProvider, error) {
|
|
close(started)
|
|
<-release
|
|
return newCountingKMSProvider(), nil
|
|
})
|
|
mgr := &KMSManager{
|
|
providers: make(map[string]KMSProvider),
|
|
configs: make(map[string]*KMSConfig),
|
|
bucketKMS: make(map[string]string),
|
|
}
|
|
if err := mgr.AddKMSProvider("ready", &KMSConfig{Provider: "lookup-test"}); err != nil {
|
|
t.Fatalf("AddKMSProvider: %v", err)
|
|
}
|
|
|
|
added := make(chan error, 1)
|
|
go func() { added <- mgr.AddKMSProvider("slow", &KMSConfig{Provider: "slow-test"}) }()
|
|
<-started
|
|
looked := make(chan error, 1)
|
|
go func() {
|
|
_, err := mgr.GetKMSProviderByName("ready")
|
|
looked <- err
|
|
}()
|
|
select {
|
|
case err := <-looked:
|
|
if err != nil {
|
|
t.Fatalf("GetKMSProviderByName: %v", err)
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatal("a lookup waited for a provider being created")
|
|
}
|
|
|
|
close(release)
|
|
if err := <-added; err != nil {
|
|
t.Fatalf("AddKMSProvider (slow): %v", err)
|
|
}
|
|
}
|