iamapi_server.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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, "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, 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(false, 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.IamConfigDirecotry, 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, iam.option.Filer, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  100. if err = filer.ReadEntry(iam.masterClient, client, filer.IamConfigDirecotry, filer.IamPoliciesFile, &buf); err != nil {
  101. return err
  102. }
  103. return nil
  104. })
  105. if err != nil {
  106. return err
  107. }
  108. if 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, iam.option.Filer, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  123. if err := filer.SaveInsideFiler(client, filer.IamConfigDirecotry, filer.IamPoliciesFile, b); err != nil {
  124. return err
  125. }
  126. return nil
  127. })
  128. }