From 9f26f2815c756d2125bf35032058ddd0d0efba1d Mon Sep 17 00:00:00 2001 From: Konstantin Lebedev Date: Thu, 10 Dec 2020 17:03:55 +0500 Subject: [PATCH] SaveAs S3 Configuration --- weed/filer/read_write.go | 8 ++++-- weed/iamapi/iamapi_management_handlers.go | 33 +++++++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/weed/filer/read_write.go b/weed/filer/read_write.go index 7a6da3beb..bf9159b41 100644 --- a/weed/filer/read_write.go +++ b/weed/filer/read_write.go @@ -41,8 +41,12 @@ func ReadContent(filerAddress string, dir, name string) ([]byte, error) { } func SaveAs(host string, port int, dir, name string, contentType string, byteBuffer *bytes.Buffer) error { - - target := fmt.Sprintf("http://%s:%d%s/%s", host, port, dir, name) + var target string + if port == 0 { + target = fmt.Sprintf("http://%s%s/%s", host, dir, name) + } else { + target = fmt.Sprintf("http://%s:%d%s/%s", host, port, dir, name) + } // set the HTTP method, url, and request body req, err := http.NewRequest(http.MethodPut, target, byteBuffer) diff --git a/weed/iamapi/iamapi_management_handlers.go b/weed/iamapi/iamapi_management_handlers.go index 22bc8748a..4316f0fd5 100644 --- a/weed/iamapi/iamapi_management_handlers.go +++ b/weed/iamapi/iamapi_management_handlers.go @@ -1,10 +1,12 @@ package iamapi import ( + "bytes" "crypto/sha1" "encoding/json" "encoding/xml" "fmt" + "github.com/chrislusf/seaweedfs/weed/filer" "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/pb/iam_pb" "github.com/chrislusf/seaweedfs/weed/s3api/s3_constants" @@ -213,13 +215,15 @@ func GetActions(policy *PolicyDocument) (actions []string) { for _, resource := range statement.Resource { // Parse "arn:aws:s3:::my-bucket/shared/*" res := strings.Split(resource, ":") - if len(res) != 6 || res[0] != "arn:" || res[1] != "aws" || res[2] != "s3" { + if len(res) != 6 || res[0] != "arn" || res[1] != "aws" || res[2] != "s3" { + glog.Infof("not math resource: %s", res) continue } for _, action := range statement.Action { // Parse "s3:Get*" act := strings.Split(action, ":") if len(act) != 2 || act[0] != "s3" { + glog.Infof("not match action: %s", act) continue } if res[5] == "*" { @@ -229,8 +233,11 @@ func GetActions(policy *PolicyDocument) (actions []string) { // Parse my-bucket/shared/* path := strings.Split(res[5], "/") if len(path) != 2 || path[1] != "*" { - actions = append(actions, fmt.Sprintf("%s:%s", MapAction(act[1]), path[0])) + glog.Infof("not match bucket: %s", path) + continue } + actions = append(actions, fmt.Sprintf("%s:%s", MapAction(act[1]), path[0])) + } } } @@ -312,11 +319,14 @@ func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) { glog.Info("values ", values) var response interface{} + changed := true switch r.Form.Get("Action") { case "ListUsers": response = iama.ListUsers(s3cfg, values) + changed = false case "ListAccessKeys": response = iama.ListAccessKeys(s3cfg, values) + changed = false case "CreateUser": response = iama.CreateUser(s3cfg, values) case "DeleteUser": @@ -343,5 +353,24 @@ func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) { writeErrorResponse(w, s3err.ErrNotImplemented, r.URL) return } + if changed { + buf := bytes.Buffer{} + if err := filer.S3ConfigurationToText(&buf, s3cfg); err != nil { + glog.Error("S3ConfigurationToText: ", err) + writeErrorResponse(w, s3err.ErrInternalError, r.URL) + return + } + if err := filer.SaveAs( + iama.option.Filer, + 0, + filer.IamConfigDirecotry, + filer.IamIdentityFile, + "text/plain; charset=utf-8", + &buf); err != nil { + glog.Error("SaveAs: ", err) + writeErrorResponse(w, s3err.ErrInternalError, r.URL) + return + } + } writeSuccessResponseXML(w, encodeResponse(response)) }