iamapi_server.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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", "", "", "", *pb.NewServiceDiscoveryFromMap(option.Masters)),
  45. }
  46. s3Option := s3api.S3ApiServerOption{
  47. Filer: option.Filer,
  48. GrpcDialOption: option.GrpcDialOption,
  49. }
  50. iamApiServer = &IamApiServer{
  51. s3ApiConfig: s3ApiConfigure,
  52. iam: s3api.NewIdentityAccessManagement(&s3Option),
  53. }
  54. iamApiServer.registerRouter(router)
  55. return iamApiServer, nil
  56. }
  57. func (iama *IamApiServer) registerRouter(router *mux.Router) {
  58. // API Router
  59. apiRouter := router.PathPrefix("/").Subrouter()
  60. // ListBuckets
  61. // apiRouter.Methods("GET").Path("/").HandlerFunc(track(s3a.iam.Auth(s3a.ListBucketsHandler, ACTION_ADMIN), "LIST"))
  62. apiRouter.Methods(http.MethodPost).Path("/").HandlerFunc(iama.iam.Auth(iama.DoActions, ACTION_ADMIN))
  63. //
  64. // NotFound
  65. apiRouter.NotFoundHandler = http.HandlerFunc(s3err.NotFoundHandler)
  66. }
  67. func (iam IamS3ApiConfigure) GetS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error) {
  68. var buf bytes.Buffer
  69. err = pb.WithGrpcFilerClient(false, 0, iam.option.Filer, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  70. if err = filer.ReadEntry(iam.masterClient, client, filer.IamConfigDirectory, filer.IamIdentityFile, &buf); err != nil {
  71. return err
  72. }
  73. return nil
  74. })
  75. if err != nil {
  76. return err
  77. }
  78. if buf.Len() > 0 {
  79. if err = filer.ParseS3ConfigurationFromBytes(buf.Bytes(), s3cfg); err != nil {
  80. return err
  81. }
  82. }
  83. return nil
  84. }
  85. func (iam IamS3ApiConfigure) PutS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error) {
  86. buf := bytes.Buffer{}
  87. if err := filer.ProtoToText(&buf, s3cfg); err != nil {
  88. return fmt.Errorf("ProtoToText: %s", err)
  89. }
  90. return pb.WithGrpcFilerClient(false, 0, iam.option.Filer, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  91. err = util.Retry("saveIamIdentity", func() error {
  92. return filer.SaveInsideFiler(client, filer.IamConfigDirectory, filer.IamIdentityFile, buf.Bytes())
  93. })
  94. if err != nil {
  95. return err
  96. }
  97. return nil
  98. })
  99. }
  100. func (iam IamS3ApiConfigure) GetPolicies(policies *Policies) (err error) {
  101. var buf bytes.Buffer
  102. err = pb.WithGrpcFilerClient(false, 0, iam.option.Filer, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  103. if err = filer.ReadEntry(iam.masterClient, client, filer.IamConfigDirectory, filer.IamPoliciesFile, &buf); err != nil {
  104. return err
  105. }
  106. return nil
  107. })
  108. if err != nil && err != filer_pb.ErrNotFound {
  109. return err
  110. }
  111. if err == filer_pb.ErrNotFound || buf.Len() == 0 {
  112. policies.Policies = make(map[string]PolicyDocument)
  113. return nil
  114. }
  115. if err := json.Unmarshal(buf.Bytes(), policies); err != nil {
  116. return err
  117. }
  118. return nil
  119. }
  120. func (iam IamS3ApiConfigure) PutPolicies(policies *Policies) (err error) {
  121. var b []byte
  122. if b, err = json.Marshal(policies); err != nil {
  123. return err
  124. }
  125. return pb.WithGrpcFilerClient(false, 0, iam.option.Filer, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  126. if err := filer.SaveInsideFiler(client, filer.IamConfigDirectory, filer.IamPoliciesFile, b); err != nil {
  127. return err
  128. }
  129. return nil
  130. })
  131. }