mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-19 10:54:08 +01:00
filer.sync: sign proxied chunk I/O from the per-side security file
The -a.security / -b.security files were used for gRPC TLS and the HTTPS client but not for jwt.filer_signing, so filer-proxied chunk reads and writes carried a token signed with the process-wide key and failed authorization whenever the two clusters' keys differ. LoadFilerJwtFromFile returns a FilerJwtProvider for each side's file, which FilerSource and FilerSink now accept for proxied chunk reads and writes. With no keys in the file or no flag, both fall back to the process-wide jwt.filer_signing configuration as before. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
8c67b75190
commit
78893d274a
7 files changed
+159
-5
No files matched your search
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
jwt "github.com/golang-jwt/jwt/v5"
|
||||
"github.com/seaweedfs/seaweedfs/weed/glog"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type EncodedJwt string
|
||||
@@ -151,3 +152,38 @@ func DecodeJwt(signingKey SigningKey, tokenString EncodedJwt, claims jwt.Claims)
|
||||
return []byte(signingKey), nil
|
||||
})
|
||||
}
|
||||
|
||||
// FilerJwtProvider signs the credential a filer's HTTP API expects. A nil
|
||||
// provider means the process-wide jwt.filer_signing configuration applies.
|
||||
type FilerJwtProvider func(isWrite bool) EncodedJwt
|
||||
|
||||
// LoadFilerJwtFromFile reads jwt.filer_signing from a security file the way
|
||||
// LoadClientTLSFromFile reads the TLS section. A nil provider means the file
|
||||
// configures no filer signing keys and the process-wide configuration applies.
|
||||
func LoadFilerJwtFromFile(configFile string) (FilerJwtProvider, error) {
|
||||
v := viper.New()
|
||||
v.SetConfigFile(configFile)
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return nil, fmt.Errorf("failed to read security config %s: %v", configFile, err)
|
||||
}
|
||||
|
||||
signingKey := SigningKey(v.GetString("jwt.filer_signing.key"))
|
||||
readSigningKey := SigningKey(v.GetString("jwt.filer_signing.read.key"))
|
||||
if len(signingKey) == 0 && len(readSigningKey) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
signingKeyExpires := v.GetInt("jwt.filer_signing.expires_after_seconds")
|
||||
if signingKeyExpires == 0 {
|
||||
signingKeyExpires = 10
|
||||
}
|
||||
readSigningKeyExpires := v.GetInt("jwt.filer_signing.read.expires_after_seconds")
|
||||
if readSigningKeyExpires == 0 {
|
||||
readSigningKeyExpires = 60
|
||||
}
|
||||
return func(isWrite bool) EncodedJwt {
|
||||
if isWrite {
|
||||
return GenJwtForFilerServer(signingKey, signingKeyExpires)
|
||||
}
|
||||
return GenJwtForFilerServer(readSigningKey, readSigningKeyExpires)
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package security
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoadFilerJwtFromFile(t *testing.T) {
|
||||
configFile := filepath.Join(t.TempDir(), "security.toml")
|
||||
config := `
|
||||
[jwt]
|
||||
[jwt.filer_signing]
|
||||
key = "side-write-key"
|
||||
expires_after_seconds = 30
|
||||
[jwt.filer_signing.read]
|
||||
key = "side-read-key"
|
||||
expires_after_seconds = 90
|
||||
`
|
||||
if err := os.WriteFile(configFile, []byte(config), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
provider, err := LoadFilerJwtFromFile(configFile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if provider == nil {
|
||||
t.Fatal("config with filer signing keys gave a nil provider")
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
isWrite bool
|
||||
signedBy string
|
||||
otherKey string
|
||||
expires int64
|
||||
}{
|
||||
{"read", false, "side-read-key", "side-write-key", 90},
|
||||
{"write", true, "side-write-key", "side-read-key", 30},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
token := provider(tc.isWrite)
|
||||
claims := &SeaweedFilerClaims{}
|
||||
if _, err := DecodeJwt(SigningKey(tc.signedBy), token, claims); err != nil {
|
||||
t.Fatalf("token does not validate against the %s key: %v", tc.name, err)
|
||||
}
|
||||
if claims.ExpiresAt == nil {
|
||||
t.Fatal("token never expires")
|
||||
}
|
||||
if _, err := DecodeJwt(SigningKey(tc.otherKey), token, &SeaweedFilerClaims{}); err == nil {
|
||||
t.Fatal("token also validates against the other access level's key")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFilerJwtFromFileWithoutKeys(t *testing.T) {
|
||||
configFile := filepath.Join(t.TempDir(), "security.toml")
|
||||
if err := os.WriteFile(configFile, []byte("[grpc.client]\n"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
provider, err := LoadFilerJwtFromFile(configFile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if provider != nil {
|
||||
t.Fatal("config without filer signing keys gave a provider")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFilerJwtFromFileMissing(t *testing.T) {
|
||||
if _, err := LoadFilerJwtFromFile(filepath.Join(t.TempDir(), "none.toml")); err == nil {
|
||||
t.Fatal("missing config file loaded without error")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user