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.

154 lines
4.4 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
  1. package iamapi
  2. // https://docs.aws.amazon.com/cli/latest/reference/iam/list-roles.html
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/chrislusf/seaweedfs/weed/filer"
  8. "github.com/chrislusf/seaweedfs/weed/pb"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/iam_pb"
  11. "github.com/chrislusf/seaweedfs/weed/s3api"
  12. . "github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
  13. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. "github.com/chrislusf/seaweedfs/weed/wdclient"
  16. "github.com/gorilla/mux"
  17. "google.golang.org/grpc"
  18. "net/http"
  19. "strings"
  20. )
  21. type IamS3ApiConfig interface {
  22. GetS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error)
  23. PutS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error)
  24. GetPolicies(policies *Policies) (err error)
  25. PutPolicies(policies *Policies) (err error)
  26. }
  27. type IamS3ApiConfigure struct {
  28. option *IamServerOption
  29. masterClient *wdclient.MasterClient
  30. }
  31. type IamServerOption struct {
  32. Masters string
  33. Filer string
  34. Port int
  35. FilerGrpcAddress string
  36. GrpcDialOption grpc.DialOption
  37. }
  38. type IamApiServer struct {
  39. s3ApiConfig IamS3ApiConfig
  40. iam *s3api.IdentityAccessManagement
  41. }
  42. var s3ApiConfigure IamS3ApiConfig
  43. func NewIamApiServer(router *mux.Router, option *IamServerOption) (iamApiServer *IamApiServer, err error) {
  44. s3ApiConfigure = IamS3ApiConfigure{
  45. option: option,
  46. masterClient: wdclient.NewMasterClient(option.GrpcDialOption, pb.AdminShellClient, "", 0, "", strings.Split(option.Masters, ",")),
  47. }
  48. s3Option := s3api.S3ApiServerOption{Filer: option.Filer}
  49. iamApiServer = &IamApiServer{
  50. s3ApiConfig: s3ApiConfigure,
  51. iam: s3api.NewIdentityAccessManagement(&s3Option),
  52. }
  53. iamApiServer.registerRouter(router)
  54. return iamApiServer, nil
  55. }
  56. func (iama *IamApiServer) registerRouter(router *mux.Router) {
  57. // API Router
  58. apiRouter := router.PathPrefix("/").Subrouter()
  59. // ListBuckets
  60. // apiRouter.Methods("GET").Path("/").HandlerFunc(track(s3a.iam.Auth(s3a.ListBucketsHandler, ACTION_ADMIN), "LIST"))
  61. apiRouter.Methods("POST").Path("/").HandlerFunc(iama.iam.Auth(iama.DoActions, ACTION_ADMIN))
  62. //
  63. // NotFound
  64. apiRouter.NotFoundHandler = http.HandlerFunc(s3err.NotFoundHandler)
  65. }
  66. func (iam IamS3ApiConfigure) GetS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error) {
  67. var buf bytes.Buffer
  68. err = pb.WithGrpcFilerClient(iam.option.FilerGrpcAddress, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  69. if err = filer.ReadEntry(iam.masterClient, client, filer.IamConfigDirecotry, filer.IamIdentityFile, &buf); err != nil {
  70. return err
  71. }
  72. return nil
  73. })
  74. if err != nil {
  75. return err
  76. }
  77. if buf.Len() > 0 {
  78. if err = filer.ParseS3ConfigurationFromBytes(buf.Bytes(), s3cfg); err != nil {
  79. return err
  80. }
  81. }
  82. return nil
  83. }
  84. func (iam IamS3ApiConfigure) PutS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error) {
  85. buf := bytes.Buffer{}
  86. if err := filer.ProtoToText(&buf, s3cfg); err != nil {
  87. return fmt.Errorf("ProtoToText: %s", err)
  88. }
  89. return pb.WithGrpcFilerClient(
  90. iam.option.FilerGrpcAddress,
  91. iam.option.GrpcDialOption,
  92. func(client filer_pb.SeaweedFilerClient) error {
  93. err = util.Retry("saveIamIdentity", func() error {
  94. return filer.SaveInsideFiler(client, filer.IamConfigDirecotry, filer.IamIdentityFile, buf.Bytes())
  95. })
  96. if err != nil {
  97. return err
  98. }
  99. return nil
  100. },
  101. )
  102. }
  103. func (iam IamS3ApiConfigure) GetPolicies(policies *Policies) (err error) {
  104. var buf bytes.Buffer
  105. err = pb.WithGrpcFilerClient(iam.option.FilerGrpcAddress, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  106. if err = filer.ReadEntry(iam.masterClient, client, filer.IamConfigDirecotry, filer.IamPoliciesFile, &buf); err != nil {
  107. return err
  108. }
  109. return nil
  110. })
  111. if err != nil {
  112. return err
  113. }
  114. if buf.Len() == 0 {
  115. policies.Policies = make(map[string]PolicyDocument)
  116. return nil
  117. }
  118. if err := json.Unmarshal(buf.Bytes(), policies); err != nil {
  119. return err
  120. }
  121. return nil
  122. }
  123. func (iam IamS3ApiConfigure) PutPolicies(policies *Policies) (err error) {
  124. var b []byte
  125. if b, err = json.Marshal(policies); err != nil {
  126. return err
  127. }
  128. return pb.WithGrpcFilerClient(
  129. iam.option.FilerGrpcAddress,
  130. iam.option.GrpcDialOption,
  131. func(client filer_pb.SeaweedFilerClient) error {
  132. if err := filer.SaveInsideFiler(client, filer.IamConfigDirecotry, filer.IamPoliciesFile, b); err != nil {
  133. return err
  134. }
  135. return nil
  136. },
  137. )
  138. }