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.

141 lines
3.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "sort"
  7. "strings"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/pb/iam_pb"
  10. "github.com/chrislusf/seaweedfs/weed/s3iam"
  11. )
  12. func init() {
  13. Commands = append(Commands, &commandS3Configure{})
  14. }
  15. type commandS3Configure struct {
  16. }
  17. func (c *commandS3Configure) Name() string {
  18. return "s3.configure"
  19. }
  20. func (c *commandS3Configure) Help() string {
  21. return `configure and apply s3 options for each bucket
  22. # see the current configuration file content
  23. s3.configure
  24. `
  25. }
  26. func (c *commandS3Configure) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  27. s3ConfigureCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  28. actions := s3ConfigureCommand.String("actions", "", "actions names")
  29. user := s3ConfigureCommand.String("user", "", "user name")
  30. buckets := s3ConfigureCommand.String("buckets", "", "bucket name")
  31. accessKey := s3ConfigureCommand.String("access_key", "", "specify the access key")
  32. secretKey := s3ConfigureCommand.String("secret_key", "", "specify the secret key")
  33. isDelete := s3ConfigureCommand.Bool("delete", false, "delete users, actions or access keys")
  34. apply := s3ConfigureCommand.Bool("apply", false, "update and apply s3 configuration")
  35. if err = s3ConfigureCommand.Parse(args); err != nil {
  36. return nil
  37. }
  38. s3cfg := &iam_pb.S3ApiConfiguration{}
  39. ifs := &s3iam.IAMFilerStore{}
  40. if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  41. ifs = s3iam.NewIAMFilerStore(&client)
  42. if err := ifs.LoadIAMConfig(s3cfg); err != nil {
  43. return nil
  44. }
  45. return nil
  46. }); err != nil {
  47. return err
  48. }
  49. idx := 0
  50. changed := false
  51. if *user != "" && *buckets != "" {
  52. for i, identity := range s3cfg.Identities {
  53. if *user == identity.Name {
  54. idx = i
  55. changed = true
  56. break
  57. }
  58. }
  59. }
  60. var cmdActions []string
  61. for _, bucket := range strings.Split(*buckets, ",") {
  62. for _, action := range strings.Split(*actions, ",") {
  63. cmdActions = append(cmdActions, fmt.Sprintf("%s:%s", action, bucket))
  64. }
  65. }
  66. if changed {
  67. if *isDelete {
  68. var exists []int
  69. for _, cmdAction := range cmdActions {
  70. for i, currentAction := range s3cfg.Identities[idx].Actions {
  71. if cmdAction == currentAction {
  72. exists = append(exists, i)
  73. }
  74. }
  75. }
  76. sort.Sort(sort.Reverse(sort.IntSlice(exists)))
  77. for _, i := range exists {
  78. s3cfg.Identities[idx].Actions = append(
  79. s3cfg.Identities[idx].Actions[:i],
  80. s3cfg.Identities[idx].Actions[i+1:]...,
  81. )
  82. }
  83. if *accessKey != "" {
  84. exists = []int{}
  85. for i, credential := range s3cfg.Identities[idx].Credentials {
  86. if credential.AccessKey == *accessKey {
  87. exists = append(exists, i)
  88. }
  89. }
  90. sort.Sort(sort.Reverse(sort.IntSlice(exists)))
  91. for _, i := range exists {
  92. s3cfg.Identities[idx].Credentials = append(
  93. s3cfg.Identities[idx].Credentials[:i],
  94. s3cfg.Identities[idx].Credentials[:i+1]...,
  95. )
  96. }
  97. }
  98. if *actions == "" && *accessKey == "" {
  99. s3cfg.Identities = append(s3cfg.Identities[:idx], s3cfg.Identities[idx+1:]...)
  100. }
  101. } else {
  102. s3cfg.Identities[idx].Actions = append(s3cfg.Identities[idx].Actions, cmdActions...)
  103. s3cfg.Identities[idx].Credentials = append(s3cfg.Identities[idx].Credentials, &iam_pb.Credential{
  104. AccessKey: *accessKey,
  105. SecretKey: *secretKey,
  106. })
  107. }
  108. } else {
  109. identity := iam_pb.Identity{
  110. Name: *user,
  111. Actions: cmdActions,
  112. }
  113. identity.Credentials = append(identity.Credentials, &iam_pb.Credential{
  114. AccessKey: *accessKey,
  115. SecretKey: *secretKey,
  116. })
  117. s3cfg.Identities = append(s3cfg.Identities, &identity)
  118. }
  119. fmt.Fprintf(writer, fmt.Sprintf("%+v\n", s3cfg.Identities))
  120. fmt.Fprintln(writer)
  121. if *apply {
  122. if err := ifs.SaveIAMConfig(s3cfg); err != nil {
  123. return err
  124. }
  125. }
  126. return nil
  127. }