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.

145 lines
4.4 KiB

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. "net/http"
  8. "github.com/gorilla/mux"
  9. "github.com/seaweedfs/seaweedfs/weed/filer"
  10. "github.com/seaweedfs/seaweedfs/weed/pb"
  11. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  12. "github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
  13. "github.com/seaweedfs/seaweedfs/weed/s3api"
  14. . "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  15. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  16. "github.com/seaweedfs/seaweedfs/weed/util"
  17. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  18. "google.golang.org/grpc"
  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 map[string]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, "", "iam", "", "", "", 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(false, 0, iam.option.Filer, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  67. if err = filer.ReadEntry(iam.masterClient, client, filer.IamConfigDirectory, 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(false, 0, iam.option.Filer, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  88. err = util.Retry("saveIamIdentity", func() error {
  89. return filer.SaveInsideFiler(client, filer.IamConfigDirectory, filer.IamIdentityFile, buf.Bytes())
  90. })
  91. if err != nil {
  92. return err
  93. }
  94. return nil
  95. })
  96. }
  97. func (iam IamS3ApiConfigure) GetPolicies(policies *Policies) (err error) {
  98. var buf bytes.Buffer
  99. err = pb.WithGrpcFilerClient(false, 0, iam.option.Filer, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  100. if err = filer.ReadEntry(iam.masterClient, client, filer.IamConfigDirectory, filer.IamPoliciesFile, &buf); err != nil {
  101. return err
  102. }
  103. return nil
  104. })
  105. if err != nil && err != filer_pb.ErrNotFound {
  106. return err
  107. }
  108. if err == filer_pb.ErrNotFound || buf.Len() == 0 {
  109. policies.Policies = make(map[string]PolicyDocument)
  110. return nil
  111. }
  112. if err := json.Unmarshal(buf.Bytes(), policies); err != nil {
  113. return err
  114. }
  115. return nil
  116. }
  117. func (iam IamS3ApiConfigure) PutPolicies(policies *Policies) (err error) {
  118. var b []byte
  119. if b, err = json.Marshal(policies); err != nil {
  120. return err
  121. }
  122. return pb.WithGrpcFilerClient(false, 0, iam.option.Filer, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  123. if err := filer.SaveInsideFiler(client, filer.IamConfigDirectory, filer.IamPoliciesFile, b); err != nil {
  124. return err
  125. }
  126. return nil
  127. })
  128. }