mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* util: SHA-256 stands in for MD5 and SHA-1 under fips140=only GODEBUG=fips140=only panics on MD5 and SHA-1. NewMd5 and NewSha1 return those hashes as before, and under fips140=only SHA-256 cut to the same size, so digests keep their length. Md5, HashStringToLong and HashToInt32 use NewMd5. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer stores: hash directory keys with NewMd5 and NewSha1 leveldb2, leveldb3, rocksdb and arangodb hash directory keys with MD5, tikv with SHA-1, which GODEBUG=fips140=only refuses, so the filer panicked at startup. They now use util.NewMd5 and util.NewSha1. Keys made under fips140=only differ from MD5 keys, so leveldb2 and leveldb3 mark an empty database opened under fips140=only and refuse a database whose keys are of the other kind instead of showing it empty. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * operation, volume: no Content-MD5 checks under fips140=only Under GODEBUG=fips140=only the uploader's digest is not an MD5, so it is no longer sent as Content-MD5, which volume servers outside that mode would reject; the digest still becomes the chunk ETag. A Go volume server in that mode keeps a client's Content-MD5 unchecked instead of panicking. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api, filer: ETags and content digests with util.NewMd5 ETags, entry Md5 attributes and chunk digests now use util.Md5 and util.NewMd5, so under GODEBUG=fips140=only they are SHA-256 cut to 16 bytes instead of panicking. That digest cannot check a client's Content-MD5, so in that mode Content-MD5 is accepted unchecked, and the filer stops returning Content-MD5. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api, iam, master: SHA-1 names with util.NewSha1 Multipart upload ids, IAM policy ids, the OIDC provider, audit and session revocation file names, and the master's read-only copy dir hash names with SHA-1. They now use util.NewSha1, which GODEBUG=fips140=only turns into truncated SHA-256. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * postgres, oidc, update: refuse MD5 and SHA-1 checks under fips140=only PostgreSQL md5 authentication, OIDC TLS thumbprints (SHA-1 by definition) and weed update's MD5 check of the download cannot run under GODEBUG=fips140=only. They now fail with an error saying so instead of panicking. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: no empty-body MD5 when Content-MD5 is not checked Under fips140=only a bodyless signed request still reached md5.Sum(nil) before the nil check and panicked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: empty copy part ETag from the digest it stores Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util: skip the native MD5 test under fips140=only Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3sink: no Content-MD5 under fips140=only The entry's Md5 attribute is truncated SHA-256 there, which an S3 destination checking Content-MD5 would reject. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iam: move OIDC provider and revocation records to this mode's name The record names hash the ARN or JTI with util.NewSha1, which differs under GODEBUG=fips140=only. On the first use of a filer, each store renames records whose name does not match the one derived from their content, so lookups, revocation checks and deletes find records written in the other mode. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iam: test the rename with the names each fips140 mode derives Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * iam: rename a record only if it is unchanged, under its new name's lock Two processes renaming after a mode switch could both list a record; if one renamed it and the provider was then deleted, the other wrote it back. The rename is now one filer transaction locked on the new name, like the stores' conditional writes, that requires the old entry to be unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
272 lines
8.6 KiB
Go
272 lines
8.6 KiB
Go
package integration
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
// RevocationEntry is one row in the revocation list. ExpiresAt is set to the
|
|
// session's natural expiration so the store can garbage-collect entries that
|
|
// can no longer be reused.
|
|
type RevocationEntry struct {
|
|
JTI string `json:"jti"`
|
|
RevokedAt time.Time `json:"revokedAt"`
|
|
ExpiresAt time.Time `json:"expiresAt"`
|
|
Reason string `json:"reason,omitempty"`
|
|
}
|
|
|
|
// SessionRevocationStore is the per-deployment blocklist of revoked sessions.
|
|
// Implementations must be safe for concurrent use; the IsRevoked path is hot
|
|
// (checked on every signed request) and must not block on slow IO.
|
|
type SessionRevocationStore interface {
|
|
Revoke(ctx context.Context, filerAddress string, entry *RevocationEntry) error
|
|
IsRevoked(ctx context.Context, filerAddress string, jti string) (bool, error)
|
|
Purge(ctx context.Context, filerAddress string, before time.Time) (int, error)
|
|
}
|
|
|
|
// MemorySessionRevocationStore keeps the blocklist in process memory. Suitable
|
|
// for single-node deployments and tests; for HA, swap in the filer-backed
|
|
// implementation so revocations propagate across `weed` instances.
|
|
type MemorySessionRevocationStore struct {
|
|
mu sync.RWMutex
|
|
entries map[string]*RevocationEntry
|
|
}
|
|
|
|
// NewMemorySessionRevocationStore returns an empty in-memory blocklist.
|
|
func NewMemorySessionRevocationStore() *MemorySessionRevocationStore {
|
|
return &MemorySessionRevocationStore{entries: make(map[string]*RevocationEntry)}
|
|
}
|
|
|
|
func (m *MemorySessionRevocationStore) Revoke(ctx context.Context, _ string, entry *RevocationEntry) error {
|
|
if entry == nil || entry.JTI == "" {
|
|
return fmt.Errorf("entry with JTI is required")
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
cp := *entry
|
|
if cp.RevokedAt.IsZero() {
|
|
cp.RevokedAt = time.Now().UTC()
|
|
}
|
|
m.entries[entry.JTI] = &cp
|
|
return nil
|
|
}
|
|
|
|
func (m *MemorySessionRevocationStore) IsRevoked(ctx context.Context, _ string, jti string) (bool, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
_, ok := m.entries[jti]
|
|
return ok, nil
|
|
}
|
|
|
|
func (m *MemorySessionRevocationStore) Purge(ctx context.Context, _ string, before time.Time) (int, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
count := 0
|
|
for k, v := range m.entries {
|
|
if !v.ExpiresAt.IsZero() && v.ExpiresAt.Before(before) {
|
|
delete(m.entries, k)
|
|
count++
|
|
}
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// FilerSessionRevocationStore persists entries under a filer directory.
|
|
// Files are named after the JTI so IsRevoked is a single LookupDirectoryEntry
|
|
// call — O(1) on a name-indexed filer. Purge enumerates the directory; that
|
|
// cost is operator-controlled (cron-driven) so the hot path stays cheap.
|
|
type FilerSessionRevocationStore struct {
|
|
grpcDialOption grpc.DialOption
|
|
basePath string
|
|
filerAddressProvider func() string
|
|
names *storedNameRekey
|
|
}
|
|
|
|
// NewFilerSessionRevocationStore returns a filer-backed blocklist store.
|
|
// Default basePath `/etc/iam/revoked-sessions` aligns with the other IAM
|
|
// directories and is safe to back up alongside roles + providers.
|
|
func NewFilerSessionRevocationStore(config map[string]interface{}, filerAddressProvider func() string) *FilerSessionRevocationStore {
|
|
store := &FilerSessionRevocationStore{
|
|
basePath: "/etc/iam/revoked-sessions",
|
|
filerAddressProvider: filerAddressProvider,
|
|
}
|
|
if config != nil {
|
|
if bp, ok := config["basePath"].(string); ok && bp != "" {
|
|
store.basePath = strings.TrimSuffix(bp, "/")
|
|
}
|
|
}
|
|
store.names = &storedNameRekey{
|
|
dir: store.basePath,
|
|
name: store.fileName,
|
|
keyOf: func(content []byte) string {
|
|
var record RevocationEntry
|
|
if json.Unmarshal(content, &record) != nil {
|
|
return ""
|
|
}
|
|
return record.JTI
|
|
},
|
|
}
|
|
glog.V(2).Infof("Initialized FilerSessionRevocationStore with basePath %s", store.basePath)
|
|
return store
|
|
}
|
|
|
|
func (f *FilerSessionRevocationStore) resolveFilerAddress(filerAddress string) string {
|
|
if filerAddress != "" {
|
|
return filerAddress
|
|
}
|
|
if f.filerAddressProvider != nil {
|
|
return f.filerAddressProvider()
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// fileName hashes the JTI before using it as a filename. RevokeSession is
|
|
// an exported API that accepts an arbitrary string; even though every
|
|
// SeaweedFS-issued session id is a safe random token, an external caller
|
|
// could pass "../../etc/passwd" or similar. Hashing produces a fixed-width
|
|
// hex name that's both filesystem-safe and stable, so the lookup-on-revoke
|
|
// path still finds the right entry.
|
|
func (f *FilerSessionRevocationStore) fileName(jti string) string {
|
|
h := util.NewSha1()
|
|
h.Write([]byte(jti))
|
|
return hex.EncodeToString(h.Sum(nil)) + ".json"
|
|
}
|
|
|
|
func (f *FilerSessionRevocationStore) Revoke(ctx context.Context, filerAddress string, entry *RevocationEntry) error {
|
|
filerAddress = f.resolveFilerAddress(filerAddress)
|
|
if filerAddress == "" {
|
|
return fmt.Errorf("filer address is required")
|
|
}
|
|
if entry == nil || entry.JTI == "" {
|
|
return fmt.Errorf("entry with JTI is required")
|
|
}
|
|
if entry.RevokedAt.IsZero() {
|
|
entry.RevokedAt = time.Now().UTC()
|
|
}
|
|
data, err := json.MarshalIndent(entry, "", " ")
|
|
if err != nil {
|
|
return fmt.Errorf("marshal revocation entry: %v", err)
|
|
}
|
|
return f.withFilerClient(ctx, filerAddress, func(client filer_pb.SeaweedFilerClient) error {
|
|
err := filer_pb.CreateEntry(ctx, client, &filer_pb.CreateEntryRequest{
|
|
Directory: f.basePath,
|
|
Entry: &filer_pb.Entry{
|
|
Name: f.fileName(entry.JTI),
|
|
IsDirectory: false,
|
|
Attributes: &filer_pb.FuseAttributes{
|
|
Mtime: time.Now().Unix(),
|
|
Crtime: time.Now().Unix(),
|
|
FileMode: uint32(0o600),
|
|
},
|
|
Content: data,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("revoke %s: %v", entry.JTI, err)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (f *FilerSessionRevocationStore) IsRevoked(ctx context.Context, filerAddress string, jti string) (bool, error) {
|
|
filerAddress = f.resolveFilerAddress(filerAddress)
|
|
if filerAddress == "" {
|
|
return false, fmt.Errorf("filer address is required")
|
|
}
|
|
revoked := false
|
|
err := f.withFilerClient(ctx, filerAddress, func(client filer_pb.SeaweedFilerClient) error {
|
|
resp, err := client.LookupDirectoryEntry(ctx, &filer_pb.LookupDirectoryEntryRequest{
|
|
Directory: f.basePath,
|
|
Name: f.fileName(jti),
|
|
})
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "not found") || strings.Contains(err.Error(), "no such") {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
revoked = resp.Entry != nil
|
|
return nil
|
|
})
|
|
return revoked, err
|
|
}
|
|
|
|
func (f *FilerSessionRevocationStore) Purge(ctx context.Context, filerAddress string, before time.Time) (int, error) {
|
|
filerAddress = f.resolveFilerAddress(filerAddress)
|
|
if filerAddress == "" {
|
|
return 0, fmt.Errorf("filer address is required")
|
|
}
|
|
count := 0
|
|
err := f.withFilerClient(ctx, filerAddress, func(client filer_pb.SeaweedFilerClient) error {
|
|
// Stream-paginate the directory: ListEntriesRequest has no built-in
|
|
// "page until done" semantics, so we use StartFromFileName to walk
|
|
// the directory in chunks and avoid the previous hardcoded 10k cap.
|
|
const pageSize = 1000
|
|
startFrom := ""
|
|
for {
|
|
stream, err := client.ListEntries(ctx, &filer_pb.ListEntriesRequest{
|
|
Directory: f.basePath,
|
|
Limit: pageSize,
|
|
StartFromFileName: startFrom,
|
|
InclusiveStartFrom: false,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("list revocation entries: %w", err)
|
|
}
|
|
lastName := ""
|
|
pageCount := 0
|
|
for {
|
|
resp, recvErr := stream.Recv()
|
|
if recvErr != nil {
|
|
if errors.Is(recvErr, io.EOF) {
|
|
break
|
|
}
|
|
return fmt.Errorf("recv revocation entry: %w", recvErr)
|
|
}
|
|
if resp.Entry == nil || resp.Entry.IsDirectory {
|
|
continue
|
|
}
|
|
lastName = resp.Entry.Name
|
|
pageCount++
|
|
var entry RevocationEntry
|
|
if err := json.Unmarshal(resp.Entry.Content, &entry); err != nil {
|
|
continue
|
|
}
|
|
if entry.ExpiresAt.IsZero() || entry.ExpiresAt.After(before) {
|
|
continue
|
|
}
|
|
if err := filer_pb.DoRemove(ctx, client, f.basePath, resp.Entry.Name, true, false, false, false, nil); err == nil {
|
|
count++
|
|
}
|
|
}
|
|
if pageCount < pageSize {
|
|
return nil
|
|
}
|
|
startFrom = lastName
|
|
}
|
|
})
|
|
return count, err
|
|
}
|
|
|
|
func (f *FilerSessionRevocationStore) withFilerClient(ctx context.Context, filerAddress string, fn func(filer_pb.SeaweedFilerClient) error) error {
|
|
return pb.WithGrpcFilerClient(false, 0, pb.ServerAddress(filerAddress), f.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
|
if err := f.names.ensure(ctx, filerAddress, client); err != nil {
|
|
return err
|
|
}
|
|
return fn(client)
|
|
})
|
|
}
|