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.

95 lines
2.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
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.loadIAMConfigFromEntry(resp.Entry, 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. Content: []byte{},
  47. }
  48. err := ifs.saveIAMConfigToEntry(entry, 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) loadIAMConfigFromEntry(entry *filer_pb.Entry, config *iam_pb.S3ApiConfiguration) error {
  74. if err := proto.Unmarshal(entry.Content, config); err != nil {
  75. return err
  76. }
  77. return nil
  78. }
  79. func (ifs *IAMFilerStore) saveIAMConfigToEntry(entry *filer_pb.Entry, config *iam_pb.S3ApiConfiguration) (err error) {
  80. entry.Content, err = proto.Marshal(config)
  81. if err != nil {
  82. return err
  83. }
  84. return nil
  85. }