seaweedfs/weed/iamapi/iamapi_server.go

150 lines
4.3 KiB
Go
Raw Normal View History

2020-12-09 20:11:49 +08:00
package iamapi
// https://docs.aws.amazon.com/cli/latest/reference/iam/list-roles.html
import (
"bytes"
2021-04-11 02:57:45 +08:00
"encoding/json"
2021-04-06 16:43:08 +08:00
"fmt"
2020-12-09 20:11:49 +08:00
"github.com/chrislusf/seaweedfs/weed/filer"
"github.com/chrislusf/seaweedfs/weed/pb"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/pb/iam_pb"
2021-04-08 20:40:47 +08:00
"github.com/chrislusf/seaweedfs/weed/s3api"
. "github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
2020-12-09 20:11:49 +08:00
"github.com/chrislusf/seaweedfs/weed/wdclient"
"github.com/gorilla/mux"
"google.golang.org/grpc"
"net/http"
"strings"
)
2021-04-06 16:43:08 +08:00
type IamS3ApiConfig interface {
GetS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error)
PutS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error)
2021-04-11 02:57:45 +08:00
GetPolicies(policies *Policies) (err error)
PutPolicies(policies *Policies) (err error)
2021-04-06 16:43:08 +08:00
}
type IamS3ApiConfigure struct {
option *IamServerOption
masterClient *wdclient.MasterClient
}
2020-12-09 20:11:49 +08:00
type IamServerOption struct {
Masters string
Filer string
Port int
FilerGrpcAddress string
GrpcDialOption grpc.DialOption
}
type IamApiServer struct {
2021-04-06 16:43:08 +08:00
s3ApiConfig IamS3ApiConfig
2021-04-08 20:40:47 +08:00
iam *s3api.IdentityAccessManagement
2020-12-09 20:11:49 +08:00
}
2021-04-06 16:43:08 +08:00
var s3ApiConfigure IamS3ApiConfig
2020-12-09 20:11:49 +08:00
func NewIamApiServer(router *mux.Router, option *IamServerOption) (iamApiServer *IamApiServer, err error) {
2021-04-06 16:43:08 +08:00
s3ApiConfigure = IamS3ApiConfigure{
2020-12-09 20:11:49 +08:00
option: option,
masterClient: wdclient.NewMasterClient(option.GrpcDialOption, pb.AdminShellClient, "", 0, "", strings.Split(option.Masters, ",")),
}
2021-04-08 20:40:47 +08:00
s3Option := s3api.S3ApiServerOption{Filer: option.Filer}
2021-04-06 16:43:08 +08:00
iamApiServer = &IamApiServer{
s3ApiConfig: s3ApiConfigure,
2021-04-08 20:40:47 +08:00
iam: s3api.NewIdentityAccessManagement(&s3Option),
2021-04-06 16:43:08 +08:00
}
2020-12-09 20:11:49 +08:00
iamApiServer.registerRouter(router)
return iamApiServer, nil
}
func (iama *IamApiServer) registerRouter(router *mux.Router) {
// API Router
apiRouter := router.PathPrefix("/").Subrouter()
// ListBuckets
// apiRouter.Methods("GET").Path("/").HandlerFunc(track(s3a.iam.Auth(s3a.ListBucketsHandler, ACTION_ADMIN), "LIST"))
2021-04-08 20:40:47 +08:00
apiRouter.Methods("POST").Path("/").HandlerFunc(iama.iam.Auth(iama.DoActions, ACTION_ADMIN))
//
2020-12-09 20:11:49 +08:00
// NotFound
apiRouter.NotFoundHandler = http.HandlerFunc(notFoundHandler)
}
2021-04-06 16:43:08 +08:00
func (iam IamS3ApiConfigure) GetS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error) {
2020-12-09 20:11:49 +08:00
var buf bytes.Buffer
2021-04-06 16:43:08 +08:00
err = pb.WithGrpcFilerClient(iam.option.FilerGrpcAddress, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
if err = filer.ReadEntry(iam.masterClient, client, filer.IamConfigDirecotry, filer.IamIdentityFile, &buf); err != nil {
2020-12-09 20:11:49 +08:00
return err
}
return nil
})
if err != nil {
return err
}
if buf.Len() > 0 {
if err = filer.ParseS3ConfigurationFromBytes(buf.Bytes(), s3cfg); err != nil {
return err
}
}
return nil
}
2021-04-06 16:43:08 +08:00
func (iam IamS3ApiConfigure) PutS3ApiConfiguration(s3cfg *iam_pb.S3ApiConfiguration) (err error) {
buf := bytes.Buffer{}
if err := filer.S3ConfigurationToText(&buf, s3cfg); err != nil {
return fmt.Errorf("S3ConfigurationToText: %s", err)
}
return pb.WithGrpcFilerClient(
iam.option.FilerGrpcAddress,
iam.option.GrpcDialOption,
func(client filer_pb.SeaweedFilerClient) error {
if err := filer.SaveInsideFiler(client, filer.IamConfigDirecotry, filer.IamIdentityFile, buf.Bytes()); err != nil {
return err
}
return nil
},
)
}
2021-04-11 02:57:45 +08:00
func (iam IamS3ApiConfigure) GetPolicies(policies *Policies) (err error) {
var buf bytes.Buffer
err = pb.WithGrpcFilerClient(iam.option.FilerGrpcAddress, iam.option.GrpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
if err = filer.ReadEntry(iam.masterClient, client, filer.IamConfigDirecotry, filer.IamPoliciesFile, &buf); err != nil {
return err
}
return nil
})
if err != nil {
return err
}
if buf.Len() == 0 {
policies.Policies = make(map[string]PolicyDocument)
return nil
}
if err := json.Unmarshal(buf.Bytes(), policies); err != nil {
return err
}
return nil
}
func (iam IamS3ApiConfigure) PutPolicies(policies *Policies) (err error) {
var b []byte
if b, err = json.Marshal(policies); err != nil {
return err
}
return pb.WithGrpcFilerClient(
iam.option.FilerGrpcAddress,
iam.option.GrpcDialOption,
func(client filer_pb.SeaweedFilerClient) error {
if err := filer.SaveInsideFiler(client, filer.IamConfigDirecotry, filer.IamPoliciesFile, b); err != nil {
return err
}
return nil
},
)
}