credential_store.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package credential
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/s3api/policy_engine"
  8. "github.com/seaweedfs/seaweedfs/weed/util"
  9. )
  10. var (
  11. ErrUserNotFound = errors.New("user not found")
  12. ErrUserAlreadyExists = errors.New("user already exists")
  13. ErrAccessKeyNotFound = errors.New("access key not found")
  14. )
  15. // CredentialStoreTypeName represents the type name of a credential store
  16. type CredentialStoreTypeName string
  17. // Credential store name constants
  18. const (
  19. StoreTypeMemory CredentialStoreTypeName = "memory"
  20. StoreTypeFilerEtc CredentialStoreTypeName = "filer_etc"
  21. StoreTypePostgres CredentialStoreTypeName = "postgres"
  22. )
  23. // CredentialStore defines the interface for user credential storage and retrieval
  24. type CredentialStore interface {
  25. // GetName returns the name of the credential store implementation
  26. GetName() CredentialStoreTypeName
  27. // Initialize initializes the credential store with configuration
  28. Initialize(configuration util.Configuration, prefix string) error
  29. // LoadConfiguration loads the entire S3 API configuration
  30. LoadConfiguration(ctx context.Context) (*iam_pb.S3ApiConfiguration, error)
  31. // SaveConfiguration saves the entire S3 API configuration
  32. SaveConfiguration(ctx context.Context, config *iam_pb.S3ApiConfiguration) error
  33. // CreateUser creates a new user with the given identity
  34. CreateUser(ctx context.Context, identity *iam_pb.Identity) error
  35. // GetUser retrieves a user by username
  36. GetUser(ctx context.Context, username string) (*iam_pb.Identity, error)
  37. // UpdateUser updates an existing user
  38. UpdateUser(ctx context.Context, username string, identity *iam_pb.Identity) error
  39. // DeleteUser removes a user by username
  40. DeleteUser(ctx context.Context, username string) error
  41. // ListUsers returns all usernames
  42. ListUsers(ctx context.Context) ([]string, error)
  43. // GetUserByAccessKey retrieves a user by access key
  44. GetUserByAccessKey(ctx context.Context, accessKey string) (*iam_pb.Identity, error)
  45. // CreateAccessKey creates a new access key for a user
  46. CreateAccessKey(ctx context.Context, username string, credential *iam_pb.Credential) error
  47. // DeleteAccessKey removes an access key for a user
  48. DeleteAccessKey(ctx context.Context, username string, accessKey string) error
  49. // Shutdown performs cleanup when the store is being shut down
  50. Shutdown()
  51. }
  52. // AccessKeyInfo represents access key information with metadata
  53. type AccessKeyInfo struct {
  54. AccessKey string `json:"accessKey"`
  55. SecretKey string `json:"secretKey"`
  56. Username string `json:"username"`
  57. CreatedAt time.Time `json:"createdAt"`
  58. }
  59. // UserCredentials represents a user's credentials and metadata
  60. type UserCredentials struct {
  61. Username string `json:"username"`
  62. Email string `json:"email"`
  63. Account *iam_pb.Account `json:"account,omitempty"`
  64. Credentials []*iam_pb.Credential `json:"credentials"`
  65. Actions []string `json:"actions"`
  66. CreatedAt time.Time `json:"createdAt"`
  67. UpdatedAt time.Time `json:"updatedAt"`
  68. }
  69. // PolicyManager interface for managing IAM policies
  70. type PolicyManager interface {
  71. GetPolicies(ctx context.Context) (map[string]policy_engine.PolicyDocument, error)
  72. CreatePolicy(ctx context.Context, name string, document policy_engine.PolicyDocument) error
  73. UpdatePolicy(ctx context.Context, name string, document policy_engine.PolicyDocument) error
  74. DeletePolicy(ctx context.Context, name string) error
  75. GetPolicy(ctx context.Context, name string) (*policy_engine.PolicyDocument, error)
  76. }
  77. // Stores holds all available credential store implementations
  78. var Stores []CredentialStore