s3_account.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package s3account
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  4. "sync"
  5. )
  6. //Predefined Accounts
  7. var (
  8. // AccountAdmin is used as the default account for IAM-Credentials access without Account configured
  9. AccountAdmin = Account{
  10. Name: "admin",
  11. EmailAddress: "admin@example.com",
  12. Id: "admin",
  13. }
  14. // AccountAnonymous is used to represent the account for anonymous access
  15. AccountAnonymous = Account{
  16. Name: "anonymous",
  17. EmailAddress: "anonymous@example.com",
  18. Id: "anonymous",
  19. }
  20. )
  21. //Account represents a system user, a system user can
  22. //configure multiple IAM-Users, IAM-Users can configure
  23. //permissions respectively, and each IAM-User can
  24. //configure multiple security credentials
  25. type Account struct {
  26. //Name is also used to display the "DisplayName" as the owner of the bucket or object
  27. Name string
  28. EmailAddress string
  29. //Id is used to identify an Account when granting cross-account access(ACLs) to buckets and objects
  30. Id string
  31. }
  32. type AccountManager struct {
  33. sync.Mutex
  34. filerClient filer_pb.FilerClient
  35. IdNameMapping map[string]string
  36. EmailIdMapping map[string]string
  37. }
  38. func NewAccountManager(filerClient filer_pb.FilerClient) *AccountManager {
  39. am := &AccountManager{
  40. filerClient: filerClient,
  41. IdNameMapping: make(map[string]string),
  42. EmailIdMapping: make(map[string]string),
  43. }
  44. am.initialize()
  45. return am
  46. }
  47. func (am *AccountManager) GetAccountNameById(canonicalId string) string {
  48. return am.IdNameMapping[canonicalId]
  49. }
  50. func (am *AccountManager) GetAccountIdByEmail(email string) string {
  51. return am.EmailIdMapping[email]
  52. }
  53. func (am *AccountManager) initialize() {
  54. // load predefined Accounts
  55. for _, account := range []Account{AccountAdmin, AccountAnonymous} {
  56. am.IdNameMapping[account.Id] = account.Name
  57. am.EmailIdMapping[account.EmailAddress] = account.Id
  58. }
  59. }