mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +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>
237 lines
9.6 KiB
Go
237 lines
9.6 KiB
Go
package iam
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"math/big"
|
|
"sort"
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
// Hash computes a SHA1 hash of the input string.
|
|
func Hash(s *string) string {
|
|
h := util.NewSha1()
|
|
h.Write([]byte(*s))
|
|
return fmt.Sprintf("%x", h.Sum(nil))
|
|
}
|
|
|
|
// UserArn builds an AWS-compatible IAM user ARN.
|
|
func UserArn(userName string) string {
|
|
return fmt.Sprintf("arn:aws:iam::%s:user/%s", DefaultAccountID, userName)
|
|
}
|
|
|
|
// NewUser builds an iam.User for IAM API responses. The Arn must be a real ARN:
|
|
// the terraform aws provider (>= 6.41) reads a user back after creating it and
|
|
// blocks until GetUser returns a value that passes arn.IsARN, so an empty Arn
|
|
// leaves apply hanging until it times out.
|
|
func NewUser(userName string) iam.User {
|
|
arn := UserArn(userName)
|
|
path := "/"
|
|
return iam.User{UserName: &userName, Arn: &arn, Path: &path}
|
|
}
|
|
|
|
// GenerateRandomString generates a cryptographically secure random string.
|
|
// Uses crypto/rand for security-sensitive credential generation.
|
|
func GenerateRandomString(length int, charset string) (string, error) {
|
|
if length <= 0 {
|
|
return "", fmt.Errorf("length must be positive, got %d", length)
|
|
}
|
|
if charset == "" {
|
|
return "", fmt.Errorf("charset must not be empty")
|
|
}
|
|
b := make([]byte, length)
|
|
for i := range b {
|
|
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to generate random index: %w", err)
|
|
}
|
|
b[i] = charset[n.Int64()]
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
// GenerateSecretAccessKey generates a new secret access key.
|
|
func GenerateSecretAccessKey() (string, error) {
|
|
return GenerateRandomString(SecretAccessKeyLength, Charset)
|
|
}
|
|
|
|
// StringSlicesEqual compares two string slices for equality, ignoring order.
|
|
// This is used instead of reflect.DeepEqual to avoid order-dependent comparisons.
|
|
func StringSlicesEqual(a, b []string) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
// Make copies to avoid modifying the originals
|
|
aCopy := make([]string, len(a))
|
|
bCopy := make([]string, len(b))
|
|
copy(aCopy, a)
|
|
copy(bCopy, b)
|
|
sort.Strings(aCopy)
|
|
sort.Strings(bCopy)
|
|
for i := range aCopy {
|
|
if aCopy[i] != bCopy[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// fineGrainedActionMap maps S3 IAM action names to internal S3 action constants.
|
|
// Supports both prefixed (e.g., "s3:DeleteObject") and unprefixed (e.g., "DeleteObject") formats.
|
|
// Populated in init() to avoid duplication of prefixed/unprefixed variants.
|
|
var fineGrainedActionMap = map[string]string{
|
|
// Coarse-grained actions (populated statically)
|
|
StatementActionAdmin: s3_constants.ACTION_ADMIN,
|
|
StatementActionWrite: s3_constants.ACTION_WRITE,
|
|
StatementActionWriteAcp: s3_constants.ACTION_WRITE_ACP,
|
|
StatementActionRead: s3_constants.ACTION_READ,
|
|
StatementActionReadAcp: s3_constants.ACTION_READ_ACP,
|
|
StatementActionList: s3_constants.ACTION_LIST,
|
|
StatementActionTagging: s3_constants.ACTION_TAGGING,
|
|
StatementActionDelete: s3_constants.ACTION_DELETE_BUCKET,
|
|
}
|
|
|
|
// baseS3ActionMap defines the base S3 actions that will be populated with both
|
|
// prefixed (s3:Action) and unprefixed (Action) variants in init().
|
|
var baseS3ActionMap = map[string]string{
|
|
// Object operations
|
|
"DeleteObject": s3_constants.ACTION_WRITE,
|
|
"PutObject": s3_constants.ACTION_WRITE,
|
|
"GetObject": s3_constants.ACTION_READ,
|
|
"DeleteObjectVersion": s3_constants.ACTION_WRITE,
|
|
"GetObjectVersion": s3_constants.ACTION_READ,
|
|
"GetObjectAttributes": s3_constants.ACTION_READ,
|
|
"GetObjectVersionAttributes": s3_constants.ACTION_READ,
|
|
// Tagging operations
|
|
"GetObjectTagging": s3_constants.ACTION_TAGGING,
|
|
"GetObjectVersionTagging": s3_constants.ACTION_TAGGING,
|
|
"PutObjectTagging": s3_constants.ACTION_TAGGING,
|
|
"PutObjectVersionTagging": s3_constants.ACTION_TAGGING,
|
|
"DeleteObjectTagging": s3_constants.ACTION_TAGGING,
|
|
"DeleteObjectVersionTagging": s3_constants.ACTION_TAGGING,
|
|
"GetBucketTagging": s3_constants.ACTION_TAGGING,
|
|
"PutBucketTagging": s3_constants.ACTION_TAGGING,
|
|
"DeleteBucketTagging": s3_constants.ACTION_TAGGING,
|
|
// ACL operations
|
|
"PutObjectAcl": s3_constants.ACTION_WRITE_ACP,
|
|
"PutObjectVersionAcl": s3_constants.ACTION_WRITE_ACP,
|
|
"GetObjectAcl": s3_constants.ACTION_READ_ACP,
|
|
"GetObjectVersionAcl": s3_constants.ACTION_READ_ACP,
|
|
"PutBucketAcl": s3_constants.ACTION_WRITE_ACP,
|
|
"GetBucketAcl": s3_constants.ACTION_READ_ACP,
|
|
// Bucket operations
|
|
"DeleteBucket": s3_constants.ACTION_DELETE_BUCKET,
|
|
// Bucket policy is permissions management: an explicit Allow in one skips
|
|
// the IAM check, so it gets its own actions rather than folding into Write
|
|
// (which any object writer holds) or Admin (which grants everything).
|
|
"GetBucketPolicy": s3_constants.ACTION_READ,
|
|
"PutBucketPolicy": s3_constants.ACTION_PUT_BUCKET_POLICY,
|
|
"DeleteBucketPolicy": s3_constants.ACTION_DELETE_BUCKET_POLICY,
|
|
"ListBucket": s3_constants.ACTION_LIST,
|
|
"ListBucketVersions": s3_constants.ACTION_LIST,
|
|
"ListAllMyBuckets": s3_constants.ACTION_LIST,
|
|
"GetBucketLocation": s3_constants.ACTION_READ,
|
|
"GetBucketVersioning": s3_constants.ACTION_READ,
|
|
"PutBucketVersioning": s3_constants.ACTION_PUT_BUCKET_VERSIONING,
|
|
"GetBucketCors": s3_constants.ACTION_READ,
|
|
"PutBucketCors": s3_constants.ACTION_PUT_BUCKET_CORS,
|
|
"DeleteBucketCors": s3_constants.ACTION_DELETE_BUCKET_CORS,
|
|
"GetBucketNotification": s3_constants.ACTION_READ,
|
|
"PutBucketNotification": s3_constants.ACTION_WRITE,
|
|
"GetBucketObjectLockConfiguration": s3_constants.ACTION_GET_BUCKET_OBJECT_LOCK_CONFIG,
|
|
"PutBucketObjectLockConfiguration": s3_constants.ACTION_PUT_BUCKET_OBJECT_LOCK_CONFIG,
|
|
// Multipart upload operations
|
|
"CreateMultipartUpload": s3_constants.ACTION_WRITE,
|
|
"UploadPart": s3_constants.ACTION_WRITE,
|
|
"CompleteMultipartUpload": s3_constants.ACTION_WRITE,
|
|
"AbortMultipartUpload": s3_constants.ACTION_WRITE,
|
|
"ListMultipartUploads": s3_constants.ACTION_LIST_MULTIPART_UPLOADS,
|
|
"ListParts": s3_constants.ACTION_LIST_PARTS,
|
|
// AWS IAM names for the multipart listing operations get their own
|
|
// action names so a policy grant cannot widen into a general Read
|
|
// or Write grant; VerifyActionPermission matches them against the
|
|
// resolved request action.
|
|
"ListBucketMultipartUploads": s3_constants.ACTION_LIST_MULTIPART_UPLOADS,
|
|
"ListMultipartUploadParts": s3_constants.ACTION_LIST_PARTS,
|
|
// Retention and legal hold operations
|
|
"GetObjectRetention": s3_constants.ACTION_GET_OBJECT_RETENTION,
|
|
"PutObjectRetention": s3_constants.ACTION_PUT_OBJECT_RETENTION,
|
|
"GetObjectLegalHold": s3_constants.ACTION_GET_OBJECT_LEGAL_HOLD,
|
|
"PutObjectLegalHold": s3_constants.ACTION_PUT_OBJECT_LEGAL_HOLD,
|
|
"BypassGovernanceRetention": s3_constants.ACTION_BYPASS_GOVERNANCE_RETENTION,
|
|
}
|
|
|
|
func init() {
|
|
// Populate both prefixed and unprefixed variants for all base S3 actions.
|
|
// This avoids duplication and makes it easy to add new actions in one place.
|
|
for action, constant := range baseS3ActionMap {
|
|
fineGrainedActionMap[action] = constant // unprefixed: "DeleteObject"
|
|
fineGrainedActionMap["s3:"+action] = constant // prefixed: "s3:DeleteObject"
|
|
}
|
|
}
|
|
|
|
// MapToStatementAction converts a policy statement action to an S3 action constant.
|
|
// It handles both coarse-grained statement actions (e.g., "Put*", "Get*") and
|
|
// fine-grained S3 actions (e.g., "s3:DeleteObject", "s3:PutObject") via exact lookup.
|
|
func MapToStatementAction(action string) string {
|
|
if val, ok := fineGrainedActionMap[action]; ok {
|
|
return val
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// MapToIdentitiesAction converts an S3 action constant to a policy statement action.
|
|
func MapToIdentitiesAction(action string) string {
|
|
switch action {
|
|
case s3_constants.ACTION_ADMIN:
|
|
return StatementActionAdmin
|
|
case s3_constants.ACTION_WRITE:
|
|
return StatementActionWrite
|
|
case s3_constants.ACTION_WRITE_ACP:
|
|
return StatementActionWriteAcp
|
|
case s3_constants.ACTION_READ:
|
|
return StatementActionRead
|
|
case s3_constants.ACTION_READ_ACP:
|
|
return StatementActionReadAcp
|
|
case s3_constants.ACTION_LIST:
|
|
return StatementActionList
|
|
case s3_constants.ACTION_TAGGING:
|
|
return StatementActionTagging
|
|
case s3_constants.ACTION_DELETE_BUCKET:
|
|
return StatementActionDelete
|
|
case s3_constants.ACTION_BYPASS_GOVERNANCE_RETENTION:
|
|
return "BypassGovernanceRetention"
|
|
case s3_constants.ACTION_GET_OBJECT_RETENTION:
|
|
return "GetObjectRetention"
|
|
case s3_constants.ACTION_PUT_OBJECT_RETENTION:
|
|
return "PutObjectRetention"
|
|
case s3_constants.ACTION_GET_OBJECT_LEGAL_HOLD:
|
|
return "GetObjectLegalHold"
|
|
case s3_constants.ACTION_PUT_OBJECT_LEGAL_HOLD:
|
|
return "PutObjectLegalHold"
|
|
case s3_constants.ACTION_GET_BUCKET_OBJECT_LOCK_CONFIG:
|
|
return "GetBucketObjectLockConfiguration"
|
|
case s3_constants.ACTION_PUT_BUCKET_OBJECT_LOCK_CONFIG:
|
|
return "PutBucketObjectLockConfiguration"
|
|
case s3_constants.ACTION_PUT_BUCKET_POLICY:
|
|
return "PutBucketPolicy"
|
|
case s3_constants.ACTION_DELETE_BUCKET_POLICY:
|
|
return "DeleteBucketPolicy"
|
|
case s3_constants.ACTION_PUT_BUCKET_CORS:
|
|
return "PutBucketCors"
|
|
case s3_constants.ACTION_DELETE_BUCKET_CORS:
|
|
return "DeleteBucketCors"
|
|
case s3_constants.ACTION_PUT_BUCKET_VERSIONING:
|
|
return "PutBucketVersioning"
|
|
case s3_constants.ACTION_LIST_MULTIPART_UPLOADS:
|
|
return "ListBucketMultipartUploads"
|
|
case s3_constants.ACTION_LIST_PARTS:
|
|
return "ListMultipartUploadParts"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|