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.

104 lines
2.9 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
  1. package iamapi
  2. // https://docs.aws.amazon.com/cli/latest/reference/iam/list-roles.html
  3. import (
  4. "bytes"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/pb"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/pb/iam_pb"
  10. "github.com/chrislusf/seaweedfs/weed/wdclient"
  11. "github.com/gorilla/mux"
  12. "google.golang.org/grpc"
  13. "net/http"
  14. "strings"
  15. )
  16. type IamS3ApiConfig interface {
  17. GetS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error)
  18. PutS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error)
  19. }
  20. type IamS3ApiConfigure struct {
  21. option *IamServerOption
  22. masterClient *wdclient.MasterClient
  23. }
  24. type IamServerOption struct {
  25. Masters string
  26. Filer string
  27. Port int
  28. FilerGrpcAddress string
  29. GrpcDialOption grpc.DialOption
  30. }
  31. type IamApiServer struct {
  32. s3ApiConfig IamS3ApiConfig
  33. filerclient *filer_pb.SeaweedFilerClient
  34. }
  35. var s3ApiConfigure IamS3ApiConfig
  36. func NewIamApiServer(router *mux.Router, option *IamServerOption) (iamApiServer *IamApiServer, err error) {
  37. s3ApiConfigure = IamS3ApiConfigure{
  38. option: option,
  39. masterClient: wdclient.NewMasterClient(option.GrpcDialOption, pb.AdminShellClient, "", 0, "", strings.Split(option.Masters, ",")),
  40. }
  41. iamApiServer = &IamApiServer{
  42. s3ApiConfig: s3ApiConfigure,
  43. }
  44. iamApiServer.registerRouter(router)
  45. return iamApiServer, nil
  46. }
  47. func (iama *IamApiServer) registerRouter(router *mux.Router) {
  48. // API Router
  49. apiRouter := router.PathPrefix("/").Subrouter()
  50. // ListBuckets
  51. // apiRouter.Methods("GET").Path("/").HandlerFunc(track(s3a.iam.Auth(s3a.ListBucketsHandler, ACTION_ADMIN), "LIST"))
  52. apiRouter.Path("/").Methods("POST").HandlerFunc(iama.DoActions)
  53. // NotFound
  54. apiRouter.NotFoundHandler = http.HandlerFunc(notFoundHandler)
  55. }
  56. func (iam IamS3ApiConfigure) GetS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error) {
  57. var buf bytes.Buffer
  58. err = pb.WithGrpcFilerClient(iam.option.FilerGrpcAddress, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  59. if err = filer.ReadEntry(iam.masterClient, client, filer.IamConfigDirecotry, filer.IamIdentityFile, &buf); err != nil {
  60. return err
  61. }
  62. return nil
  63. })
  64. if err != nil {
  65. return err
  66. }
  67. if buf.Len() > 0 {
  68. if err = filer.ParseS3ConfigurationFromBytes(buf.Bytes(), s3cfg); err != nil {
  69. return err
  70. }
  71. }
  72. return nil
  73. }
  74. func (iam IamS3ApiConfigure) PutS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error) {
  75. buf := bytes.Buffer{}
  76. if err := filer.S3ConfigurationToText(&buf, s3cfg); err != nil {
  77. return fmt.Errorf("S3ConfigurationToText: %s", err)
  78. }
  79. return pb.WithGrpcFilerClient(
  80. iam.option.FilerGrpcAddress,
  81. iam.option.GrpcDialOption,
  82. func(client filer_pb.SeaweedFilerClient) error {
  83. if err := filer.SaveInsideFiler(client, filer.IamConfigDirecotry, filer.IamIdentityFile, buf.Bytes()); err != nil {
  84. return err
  85. }
  86. return nil
  87. },
  88. )
  89. }