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.

152 lines
4.4 KiB

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