You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.6 KiB

4 years ago
  1. package s3iam
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  4. "github.com/chrislusf/seaweedfs/weed/pb/iam_pb"
  5. "time"
  6. proto "github.com/golang/protobuf/proto"
  7. )
  8. const (
  9. iamConfigPrefix = "/etc/iam"
  10. iamIdentityFile = "identity.json"
  11. )
  12. type IAMFilerStore struct {
  13. client *filer_pb.SeaweedFilerClient
  14. }
  15. func NewIAMFilerStore(client *filer_pb.SeaweedFilerClient) *IAMFilerStore {
  16. return &IAMFilerStore{client: client}
  17. }
  18. func (ifs *IAMFilerStore) getIAMConfigRequest() *filer_pb.LookupDirectoryEntryRequest {
  19. return &filer_pb.LookupDirectoryEntryRequest{
  20. Directory: iamConfigPrefix,
  21. Name: iamIdentityFile,
  22. }
  23. }
  24. func (ifs *IAMFilerStore) LoadIAMConfig(config *iam_pb.S3ApiConfiguration) error {
  25. resp, err := filer_pb.LookupEntry(*ifs.client, ifs.getIAMConfigRequest())
  26. if err != nil {
  27. return err
  28. }
  29. err = ifs.loadIAMConfigFromEntryExtended(&resp.Entry.Extended, config)
  30. if err != nil {
  31. return err
  32. }
  33. return nil
  34. }
  35. func (ifs *IAMFilerStore) SaveIAMConfig(config *iam_pb.S3ApiConfiguration) error {
  36. entry := &filer_pb.Entry{
  37. Name: iamIdentityFile,
  38. IsDirectory: false,
  39. Attributes: &filer_pb.FuseAttributes{
  40. Mtime: time.Now().Unix(),
  41. Crtime: time.Now().Unix(),
  42. FileMode: uint32(0644),
  43. Collection: "",
  44. Replication: "",
  45. },
  46. Extended: make(map[string][]byte),
  47. }
  48. err := ifs.saveIAMConfigToEntryExtended(&entry.Extended, config)
  49. if err != nil {
  50. return err
  51. }
  52. _, err = filer_pb.LookupEntry(*ifs.client, ifs.getIAMConfigRequest())
  53. if err == filer_pb.ErrNotFound {
  54. err = filer_pb.CreateEntry(*ifs.client, &filer_pb.CreateEntryRequest{
  55. Directory: iamConfigPrefix,
  56. Entry: entry,
  57. IsFromOtherCluster: false,
  58. Signatures: nil,
  59. })
  60. } else {
  61. err = filer_pb.UpdateEntry(*ifs.client, &filer_pb.UpdateEntryRequest{
  62. Directory: iamConfigPrefix,
  63. Entry: entry,
  64. IsFromOtherCluster: false,
  65. Signatures: nil,
  66. })
  67. }
  68. if err != nil {
  69. return err
  70. }
  71. return nil
  72. }
  73. func (ifs *IAMFilerStore) loadIAMConfigFromEntryExtended(extended *map[string][]byte, config *iam_pb.S3ApiConfiguration) error {
  74. for _, ident := range *extended {
  75. identity := &iam_pb.Identity{}
  76. if err := proto.Unmarshal(ident, identity); err != nil {
  77. return err
  78. }
  79. config.Identities = append(config.Identities, identity)
  80. }
  81. return nil
  82. }
  83. func (ifs *IAMFilerStore) saveIAMConfigToEntryExtended(extended *map[string][]byte, config *iam_pb.S3ApiConfiguration) error {
  84. for _, identity := range config.Identities {
  85. ident, err := proto.Marshal(identity)
  86. if err != nil {
  87. return err
  88. }
  89. (*extended)[identity.Name] = ident
  90. }
  91. return nil
  92. }