Some checks failed
go: build dev binaries / cleanup (push) Successful in 4s
Codespell / Check for spelling errors (push) Failing after 3s
docker: build dev containers / build-rust-binaries (amd64, x86_64-unknown-linux-musl) (push) Failing after 3s
docker: build dev containers / build-dev-containers (push) Has been skipped
docker: build dev containers / build-rust-binaries (arm64, true, aarch64-unknown-linux-musl) (push) Failing after 3s
End to End / FUSE Mount (push) Failing after 2s
EC Integration Tests / EC Integration Tests (push) Failing after 30s
FUSE DLM Integration Tests / FUSE DLM Integration Tests (push) Failing after 3s
FUSE Volume Server Failover Tests / FUSE Volume Server Failover (push) Failing after 3s
FUSE Integration Tests / FUSE Integration Testing (push) Failing after 2s
FUSE P2P Peer Chunk Sharing Integration Tests / FUSE P2P Peer Chunk Sharing (push) Failing after 3s
go: build binary / Go Vet (push) Failing after 2s
go: build binary / Go Vet 32-bit (push) Failing after 3s
go: build binary / Build (push) Failing after 4s
go: build binary / Build cross-platform (freebsd/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (darwin/arm64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/arm64) (push) Failing after 3s
go: build binary / Test (push) Failing after 2s
go: build binary / Test 32-bit (push) Failing after 3s
helm: lint and test charts / lint-test (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (11) (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (17) (push) Failing after 3s
go: build dev binaries / build_dev_linux_windows (amd64, linux) (push) Failing after 2s
go: build dev binaries / build_dev_linux_windows (amd64, windows) (push) Failing after 3s
go: build dev binaries / build_dev_darwin (amd64, darwin) (push) Has been skipped
go: build dev binaries / build_dev_darwin (arm64, darwin) (push) Has been skipped
Java Client Unit Tests / Java Unit Tests (11) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (17) (push) Failing after 2s
Java Client Unit Tests / Java Unit Tests (21) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (8) (push) Failing after 3s
Kafka Quick Test (Load Test with Schema Registry) / Kafka Client Load Test (Quick) (push) Failing after 3s
Kafka Gateway Tests / Kafka Unit Tests (unit-tests-1) (push) Failing after 11s
Kafka Gateway Tests / Kafka Integration Tests (Critical) (integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka End-to-End Tests (with SMQ) (e2e-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Consumer Group Tests (Highly Isolated) (consumer-group-1) (push) Failing after 2s
Kafka Gateway Tests / Kafka Client Compatibility (with SMQ) (client-compat-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka SMQ Integration Tests (Full Stack) (smq-integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Protocol Tests (Isolated) (protocol-1) (push) Failing after 3s
KMS Tests / KMS Provider Integration Tests (push) Failing after 2s
KMS Tests / S3 KMS End-to-End Tests (push) Failing after 3s
Master Cold Start Tests / Master Cold Start Tests (push) Failing after 4s
Metadata Subscribe Integration Tests / Metadata Subscribe Integration Tests (push) Failing after 3s
Multi-Master Tests / Multi-Master Failover Tests (push) Failing after 3s
Performance / CPU and Heap Profile (push) Failing after 2s
Performance / S3 Read/Write Benchmark (rust volume) (push) Failing after 3s
pjdfstest POSIX Compliance / pjdfstest (push) Failing after 2s
Plugin Worker Integration Tests / Plugin Worker: erasure_coding (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: vacuum (push) Failing after 3s
Rust Volume Server Tests / Rust Integration Tests (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 1) (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 1) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 3) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Distributed Tests (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Performance Tests (push) Failing after 3s
Performance / Throughput Benchmark (go volume) (push) Failing after 3s
Performance / Throughput Benchmark (rust volume) (push) Failing after 2s
Performance / S3 Read/Write Benchmark (go volume) (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: volume_balance (push) Failing after 3s
PostgreSQL Gateway Tests / PostgreSQL Basic Tests (push) Failing after 3s
Rust Volume Server Tests / Rust Unit Tests (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 3) (push) Failing after 3s
Rust Plugin Worker Tests / Rust Plugin Worker Build and Unit Tests (push) Failing after 3s
rust: build dev volume server binaries / cleanup (push) Successful in 3s
S3 ETag and ACL Tests / S3 ETag + ACL Integration Tests (push) Failing after 3s
S3 IAM Integration Tests / IAM Unit Tests (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (advanced) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (basic) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (group) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (policy-enforcement) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (sts) (push) Failing after 4s
S3 Keycloak Integration Tests / S3 Keycloak Integration Tests (push) Failing after 3s
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.11) (push) Failing after 3s
S3 PyArrow Parquet Tests / Go Unit Tests (Implicit Directory) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.12) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-amd64, x86_64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-arm64, aarch64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-linux (linux-amd64, x86_64-unknown-linux-gnu) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.9) (push) Has been cancelled
S3 Proxy Signature Tests / S3 Proxy Signature Verification Tests (push) Has been cancelled
S3 SDK V2 Route Disambiguation Tests / S3 SDK V2 Routing Tests (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (quick) (push) Has been cancelled
S3 SSE Tests / S3 SSE Compatibility Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Metadata Persistence Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Copy Operations Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Multipart Upload Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Performance Test (push) Has been cancelled
S3 SSE Tests / S3 Volume Encryption Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (comprehensive) (push) Has been cancelled
Spark Integration Tests / Spark Integration Tests (push) Failing after 2s
Telemetry Integration Tests / telemetry-integration-test (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 1) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 1) (push) Failing after 3s
Ceph S3 tests / Basic S3 tests (KV store) (push) Has been cancelled
Ceph S3 tests / S3 Versioning & Object Lock tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Unit Tests (push) Failing after 3s
Ceph S3 tests / S3 CORS tests (push) Has been cancelled
Ceph S3 tests / SeaweedFS Custom S3 Copy tests (push) Has been cancelled
Ceph S3 tests / Basic S3 tests (SQL store) (push) Has been cancelled
test s3 over https using aws-cli / awscli-tests (push) Has been cancelled
TLS Rotation Integration Tests / TLS Rotation Integration Tests (push) Has been cancelled
Vacuum Integration Tests / Vacuum Integration Tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Variables Integration Tests (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (advanced-policy) (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (basic-policy) (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (contextual-policy) (push) Failing after 4s
S3 Policy Integration Tests / Trusted Proxy Detection Tests (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Shell Integration Tests (push) Failing after 3s
Samba on FUSE Integration / samba-integration (push) Failing after 2s
SFTP Integration Tests / SFTP Integration Testing (push) Failing after 3s
terraform: validate and test modules / fmt, validate, plan-level tests (push) Failing after 3s
terraform: validate and test modules / local cluster smoke test (real weed) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 3) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 2) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 2) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 3) (push) Failing after 5s
mount: windows conformance / WinFsp conformance (push) Has been cancelled
mount: windows / Mount on Windows (push) Has been cancelled
532 lines
15 KiB
Go
532 lines
15 KiB
Go
package policy
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/credential"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
// MemoryPolicyStore implements PolicyStore using in-memory storage
|
|
type MemoryPolicyStore struct {
|
|
policies map[string]*PolicyDocument
|
|
mutex sync.RWMutex
|
|
}
|
|
|
|
// NewMemoryPolicyStore creates a new memory-based policy store
|
|
func NewMemoryPolicyStore() *MemoryPolicyStore {
|
|
return &MemoryPolicyStore{
|
|
policies: make(map[string]*PolicyDocument),
|
|
}
|
|
}
|
|
|
|
// StorePolicy stores a policy document in memory (filerAddress ignored for memory store)
|
|
func (s *MemoryPolicyStore) StorePolicy(ctx context.Context, filerAddress string, name string, policy *PolicyDocument) error {
|
|
if name == "" {
|
|
return fmt.Errorf("policy name cannot be empty")
|
|
}
|
|
|
|
if policy == nil {
|
|
return fmt.Errorf("policy cannot be nil")
|
|
}
|
|
|
|
s.mutex.Lock()
|
|
defer s.mutex.Unlock()
|
|
|
|
// Deep copy the policy to prevent external modifications
|
|
s.policies[name] = copyPolicyDocument(policy)
|
|
return nil
|
|
}
|
|
|
|
// GetPolicy retrieves a policy document from memory (filerAddress ignored for memory store)
|
|
func (s *MemoryPolicyStore) GetPolicy(ctx context.Context, filerAddress string, name string) (*PolicyDocument, error) {
|
|
if name == "" {
|
|
return nil, fmt.Errorf("policy name cannot be empty")
|
|
}
|
|
|
|
s.mutex.RLock()
|
|
defer s.mutex.RUnlock()
|
|
|
|
policy, exists := s.policies[name]
|
|
if !exists {
|
|
return nil, fmt.Errorf("policy not found: %s", name)
|
|
}
|
|
|
|
// Return a copy to prevent external modifications
|
|
return copyPolicyDocument(policy), nil
|
|
}
|
|
|
|
// DeletePolicy deletes a policy document from memory (filerAddress ignored for memory store)
|
|
func (s *MemoryPolicyStore) DeletePolicy(ctx context.Context, filerAddress string, name string) error {
|
|
if name == "" {
|
|
return fmt.Errorf("policy name cannot be empty")
|
|
}
|
|
|
|
s.mutex.Lock()
|
|
defer s.mutex.Unlock()
|
|
|
|
delete(s.policies, name)
|
|
return nil
|
|
}
|
|
|
|
// ListPolicies lists all policy names in memory (filerAddress ignored for memory store)
|
|
func (s *MemoryPolicyStore) ListPolicies(ctx context.Context, filerAddress string) ([]string, error) {
|
|
s.mutex.RLock()
|
|
defer s.mutex.RUnlock()
|
|
|
|
names := make([]string, 0, len(s.policies))
|
|
for name := range s.policies {
|
|
names = append(names, name)
|
|
}
|
|
|
|
return names, nil
|
|
}
|
|
|
|
// copyPolicyDocument creates a deep copy of a policy document
|
|
func copyPolicyDocument(original *PolicyDocument) *PolicyDocument {
|
|
if original == nil {
|
|
return nil
|
|
}
|
|
|
|
copied := &PolicyDocument{
|
|
Version: original.Version,
|
|
Id: original.Id,
|
|
}
|
|
|
|
// Copy statements
|
|
copied.Statement = make([]Statement, len(original.Statement))
|
|
for i, stmt := range original.Statement {
|
|
copied.Statement[i] = Statement{
|
|
Sid: stmt.Sid,
|
|
Effect: stmt.Effect,
|
|
Principal: stmt.Principal,
|
|
NotPrincipal: stmt.NotPrincipal,
|
|
}
|
|
|
|
// Copy action slice
|
|
if stmt.Action != nil {
|
|
copied.Statement[i].Action = make([]string, len(stmt.Action))
|
|
copy(copied.Statement[i].Action, stmt.Action)
|
|
}
|
|
|
|
// Copy NotAction slice
|
|
if stmt.NotAction != nil {
|
|
copied.Statement[i].NotAction = make([]string, len(stmt.NotAction))
|
|
copy(copied.Statement[i].NotAction, stmt.NotAction)
|
|
}
|
|
|
|
// Copy resource slice
|
|
if stmt.Resource != nil {
|
|
copied.Statement[i].Resource = make([]string, len(stmt.Resource))
|
|
copy(copied.Statement[i].Resource, stmt.Resource)
|
|
}
|
|
|
|
// Copy NotResource slice
|
|
if stmt.NotResource != nil {
|
|
copied.Statement[i].NotResource = make([]string, len(stmt.NotResource))
|
|
copy(copied.Statement[i].NotResource, stmt.NotResource)
|
|
}
|
|
|
|
// Copy condition map
|
|
if stmt.Condition != nil {
|
|
copied.Statement[i].Condition = make(map[string]map[string]interface{})
|
|
for conditionType, conditionValues := range stmt.Condition {
|
|
copiedConditionValues := make(map[string]interface{}, len(conditionValues))
|
|
for conditionKey, conditionValue := range conditionValues {
|
|
copiedConditionValues[conditionKey] = copyPolicyConditionValue(conditionValue)
|
|
}
|
|
copied.Statement[i].Condition[conditionType] = copiedConditionValues
|
|
}
|
|
}
|
|
}
|
|
|
|
return copied
|
|
}
|
|
|
|
func copyPolicyConditionValue(value interface{}) interface{} {
|
|
switch v := value.(type) {
|
|
case []string:
|
|
copied := make([]string, len(v))
|
|
copy(copied, v)
|
|
return copied
|
|
case []interface{}:
|
|
copied := make([]interface{}, len(v))
|
|
for i := range v {
|
|
copied[i] = copyPolicyConditionValue(v[i])
|
|
}
|
|
return copied
|
|
case map[string]interface{}:
|
|
copied := make(map[string]interface{}, len(v))
|
|
for key, nestedValue := range v {
|
|
copied[key] = copyPolicyConditionValue(nestedValue)
|
|
}
|
|
return copied
|
|
default:
|
|
return v
|
|
}
|
|
}
|
|
|
|
// FilerPolicyStore implements PolicyStore using SeaweedFS filer
|
|
type FilerPolicyStore struct {
|
|
grpcDialOption grpc.DialOption
|
|
basePath string
|
|
filerAddressProvider func() string
|
|
}
|
|
|
|
// NewFilerPolicyStore creates a new filer-based policy store
|
|
func NewFilerPolicyStore(config map[string]interface{}, filerAddressProvider func() string) (*FilerPolicyStore, error) {
|
|
store := &FilerPolicyStore{
|
|
basePath: "/etc/iam/policies", // Default path for policy storage - aligned with /etc/ convention
|
|
filerAddressProvider: filerAddressProvider,
|
|
}
|
|
|
|
// Parse configuration - only basePath and other settings, NOT filerAddress
|
|
if config != nil {
|
|
if basePath, ok := config["basePath"].(string); ok && basePath != "" {
|
|
store.basePath = strings.TrimSuffix(basePath, "/")
|
|
}
|
|
}
|
|
|
|
glog.V(2).Infof("Initialized FilerPolicyStore with basePath %s", store.basePath)
|
|
|
|
return store, nil
|
|
}
|
|
|
|
// StorePolicy stores a policy document in filer
|
|
func (s *FilerPolicyStore) StorePolicy(ctx context.Context, filerAddress string, name string, policy *PolicyDocument) error {
|
|
// Use provider function if filerAddress is not provided
|
|
if filerAddress == "" && s.filerAddressProvider != nil {
|
|
filerAddress = s.filerAddressProvider()
|
|
}
|
|
if filerAddress == "" {
|
|
return fmt.Errorf("filer address is required for FilerPolicyStore")
|
|
}
|
|
if name == "" {
|
|
return fmt.Errorf("policy name cannot be empty")
|
|
}
|
|
if policy == nil {
|
|
return fmt.Errorf("policy cannot be nil")
|
|
}
|
|
|
|
// Serialize policy to JSON
|
|
policyData, err := json.MarshalIndent(policy, "", " ")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to serialize policy: %v", err)
|
|
}
|
|
|
|
policyPath := s.getPolicyPath(name)
|
|
|
|
// Store in filer
|
|
return s.withFilerClient(filerAddress, func(client filer_pb.SeaweedFilerClient) error {
|
|
glog.V(3).Infof("Storing policy %s at %s", name, policyPath)
|
|
if err := s.savePolicyFile(ctx, client, s.getPolicyFileName(name), policyData); err != nil {
|
|
return fmt.Errorf("failed to store policy %s: %v", name, err)
|
|
}
|
|
if err := s.deleteLegacyPolicyFileIfPresent(ctx, client, name); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// GetPolicy retrieves a policy document from filer
|
|
func (s *FilerPolicyStore) GetPolicy(ctx context.Context, filerAddress string, name string) (*PolicyDocument, error) {
|
|
// Use provider function if filerAddress is not provided
|
|
if filerAddress == "" && s.filerAddressProvider != nil {
|
|
filerAddress = s.filerAddressProvider()
|
|
}
|
|
if filerAddress == "" {
|
|
return nil, fmt.Errorf("filer address is required for FilerPolicyStore")
|
|
}
|
|
if name == "" {
|
|
return nil, fmt.Errorf("policy name cannot be empty")
|
|
}
|
|
|
|
var policyData []byte
|
|
err := s.withFilerClient(filerAddress, func(client filer_pb.SeaweedFilerClient) error {
|
|
for _, fileName := range s.getPolicyLookupFileNames(name) {
|
|
request := &filer_pb.LookupDirectoryEntryRequest{
|
|
Directory: s.basePath,
|
|
Name: fileName,
|
|
}
|
|
|
|
glog.V(3).Infof("Looking up policy %s as %s", name, fileName)
|
|
response, err := client.LookupDirectoryEntry(ctx, request)
|
|
if err != nil {
|
|
if isNotFoundPolicyStoreError(err) {
|
|
continue
|
|
}
|
|
return fmt.Errorf("policy lookup failed: %v", err)
|
|
}
|
|
|
|
if response.Entry == nil {
|
|
continue
|
|
}
|
|
|
|
policyData = response.Entry.Content
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("policy not found")
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Deserialize policy from JSON
|
|
var policy PolicyDocument
|
|
if err := json.Unmarshal(policyData, &policy); err != nil {
|
|
return nil, fmt.Errorf("failed to deserialize policy: %v", err)
|
|
}
|
|
|
|
return &policy, nil
|
|
}
|
|
|
|
// DeletePolicy deletes a policy document from filer
|
|
func (s *FilerPolicyStore) DeletePolicy(ctx context.Context, filerAddress string, name string) error {
|
|
// Use provider function if filerAddress is not provided
|
|
if filerAddress == "" && s.filerAddressProvider != nil {
|
|
filerAddress = s.filerAddressProvider()
|
|
}
|
|
if filerAddress == "" {
|
|
return fmt.Errorf("filer address is required for FilerPolicyStore")
|
|
}
|
|
if name == "" {
|
|
return fmt.Errorf("policy name cannot be empty")
|
|
}
|
|
|
|
return s.withFilerClient(filerAddress, func(client filer_pb.SeaweedFilerClient) error {
|
|
for _, fileName := range s.getPolicyLookupFileNames(name) {
|
|
request := &filer_pb.DeleteEntryRequest{
|
|
Directory: s.basePath,
|
|
Name: fileName,
|
|
IsDeleteData: true,
|
|
IsRecursive: false,
|
|
IgnoreRecursiveError: false,
|
|
}
|
|
|
|
glog.V(3).Infof("Deleting policy %s as %s", name, fileName)
|
|
resp, err := client.DeleteEntry(ctx, request)
|
|
if err != nil {
|
|
if isNotFoundPolicyStoreError(err) {
|
|
continue
|
|
}
|
|
return fmt.Errorf("failed to delete policy %s: %v", name, err)
|
|
}
|
|
|
|
if resp.Error != "" {
|
|
return fmt.Errorf("failed to delete policy %s: %s", name, resp.Error)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// ListPolicies lists all policy names in filer
|
|
func (s *FilerPolicyStore) ListPolicies(ctx context.Context, filerAddress string) ([]string, error) {
|
|
// Use provider function if filerAddress is not provided
|
|
if filerAddress == "" && s.filerAddressProvider != nil {
|
|
filerAddress = s.filerAddressProvider()
|
|
}
|
|
if filerAddress == "" {
|
|
return nil, fmt.Errorf("filer address is required for FilerPolicyStore")
|
|
}
|
|
|
|
var policyNames []string
|
|
|
|
err := s.withFilerClient(filerAddress, func(client filer_pb.SeaweedFilerClient) error {
|
|
// List all entries in the policy directory
|
|
request := &filer_pb.ListEntriesRequest{
|
|
Directory: s.basePath,
|
|
Prefix: "",
|
|
StartFromFileName: "",
|
|
InclusiveStartFrom: false,
|
|
Limit: 1000, // Process in batches of 1000
|
|
}
|
|
|
|
stream, err := client.ListEntries(ctx, request)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to list policies: %v", err)
|
|
}
|
|
|
|
for {
|
|
resp, err := stream.Recv()
|
|
if err != nil {
|
|
break // End of stream or error
|
|
}
|
|
|
|
if resp.Entry == nil || resp.Entry.IsDirectory {
|
|
continue
|
|
}
|
|
|
|
if policyName, ok := s.policyNameFromFileName(resp.Entry.Name); ok {
|
|
policyNames = append(policyNames, policyName)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
uniquePolicyNames := make([]string, 0, len(policyNames))
|
|
seen := make(map[string]struct{}, len(policyNames))
|
|
for _, policyName := range policyNames {
|
|
if _, found := seen[policyName]; found {
|
|
continue
|
|
}
|
|
seen[policyName] = struct{}{}
|
|
uniquePolicyNames = append(uniquePolicyNames, policyName)
|
|
}
|
|
|
|
return uniquePolicyNames, nil
|
|
}
|
|
|
|
// Helper methods
|
|
|
|
// withFilerClient executes a function with a filer client
|
|
func (s *FilerPolicyStore) withFilerClient(filerAddress string, fn func(client filer_pb.SeaweedFilerClient) error) error {
|
|
if filerAddress == "" {
|
|
return fmt.Errorf("filer address is required for FilerPolicyStore")
|
|
}
|
|
|
|
// Use the pb.WithGrpcFilerClient helper similar to existing SeaweedFS code
|
|
return pb.WithGrpcFilerClient(false, 0, pb.ServerAddress(filerAddress), s.grpcDialOption, fn)
|
|
}
|
|
|
|
// getPolicyPath returns the full path for a policy
|
|
func (s *FilerPolicyStore) getPolicyPath(policyName string) string {
|
|
return s.basePath + "/" + s.getPolicyFileName(policyName)
|
|
}
|
|
|
|
// getPolicyFileName returns the filename for a policy
|
|
func (s *FilerPolicyStore) getPolicyFileName(policyName string) string {
|
|
return s.getCanonicalPolicyFileName(policyName)
|
|
}
|
|
|
|
func (s *FilerPolicyStore) getLegacyPolicyFileName(policyName string) string {
|
|
return "policy_" + policyName + ".json"
|
|
}
|
|
|
|
func (s *FilerPolicyStore) getCanonicalPolicyFileName(policyName string) string {
|
|
return policyName + ".json"
|
|
}
|
|
|
|
func (s *FilerPolicyStore) getPolicyLookupFileNames(policyName string) []string {
|
|
return []string{
|
|
s.getCanonicalPolicyFileName(policyName),
|
|
s.getLegacyPolicyFileName(policyName),
|
|
}
|
|
}
|
|
|
|
func (s *FilerPolicyStore) policyNameFromFileName(fileName string) (string, bool) {
|
|
if !strings.HasSuffix(fileName, ".json") {
|
|
return "", false
|
|
}
|
|
policyName := strings.TrimSuffix(fileName, ".json")
|
|
if strings.HasPrefix(fileName, "policy_") {
|
|
policyName = strings.TrimPrefix(policyName, "policy_")
|
|
}
|
|
if s.isSupportedPolicyName(policyName) {
|
|
return policyName, true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
func (s *FilerPolicyStore) isSupportedPolicyName(policyName string) bool {
|
|
if policyName == "" {
|
|
return false
|
|
}
|
|
// Bucket policies are stored alongside IAM policies but use the internal
|
|
// "bucket-policy:<bucket>" naming scheme, which is intentionally outside the
|
|
// public IAM policy-name validator.
|
|
if strings.HasPrefix(policyName, "bucket-policy:") {
|
|
return len(policyName) > len("bucket-policy:")
|
|
}
|
|
return credential.ValidatePolicyName(policyName) == nil
|
|
}
|
|
|
|
func (s *FilerPolicyStore) deleteLegacyPolicyFileIfPresent(ctx context.Context, client filer_pb.SeaweedFilerClient, policyName string) error {
|
|
legacyFileName := s.getLegacyPolicyFileName(policyName)
|
|
response, err := client.DeleteEntry(ctx, &filer_pb.DeleteEntryRequest{
|
|
Directory: s.basePath,
|
|
Name: legacyFileName,
|
|
IsDeleteData: true,
|
|
IsRecursive: false,
|
|
IgnoreRecursiveError: false,
|
|
})
|
|
if err != nil {
|
|
if isNotFoundPolicyStoreError(err) {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("failed to delete legacy policy %s: %v", policyName, err)
|
|
}
|
|
if response.Error != "" {
|
|
return fmt.Errorf("failed to delete legacy policy %s: %s", policyName, response.Error)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *FilerPolicyStore) savePolicyFile(ctx context.Context, client filer_pb.SeaweedFilerClient, fileName string, content []byte) error {
|
|
now := time.Now().Unix()
|
|
entry := &filer_pb.Entry{
|
|
Name: fileName,
|
|
IsDirectory: false,
|
|
Attributes: &filer_pb.FuseAttributes{
|
|
Mtime: now,
|
|
Crtime: now,
|
|
FileMode: uint32(0600),
|
|
Uid: uint32(0),
|
|
Gid: uint32(0),
|
|
FileSize: uint64(len(content)),
|
|
},
|
|
Content: content,
|
|
}
|
|
|
|
createRequest := &filer_pb.CreateEntryRequest{
|
|
Directory: s.basePath,
|
|
Entry: entry,
|
|
}
|
|
|
|
if err := filer_pb.CreateEntry(ctx, client, createRequest); err == nil {
|
|
return nil
|
|
} else if !isAlreadyExistsPolicyStoreError(err) {
|
|
return err
|
|
}
|
|
|
|
return filer_pb.UpdateEntry(ctx, client, &filer_pb.UpdateEntryRequest{
|
|
Directory: s.basePath,
|
|
Entry: entry,
|
|
})
|
|
}
|
|
|
|
func isNotFoundPolicyStoreError(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
return errors.Is(err, filer_pb.ErrNotFound) || status.Code(err) == codes.NotFound
|
|
}
|
|
|
|
func isAlreadyExistsPolicyStoreError(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
return status.Code(err) == codes.AlreadyExists
|
|
}
|