From 740fef9b8779236a9f4b1a3b693ad9b6032ec78c Mon Sep 17 00:00:00 2001 From: yangpeng Date: Fri, 26 May 2023 17:13:30 +0800 Subject: [PATCH] modify bucket referer api --- lib/bucket_referer.go | 140 +++++++++++++++++++++++++++++++++---- lib/bucket_referer_test.go | 101 ++++++++++++++++++++++++++ 2 files changed, 227 insertions(+), 14 deletions(-) diff --git a/lib/bucket_referer.go b/lib/bucket_referer.go index 3c970ed9..ab988d71 100644 --- a/lib/bucket_referer.go +++ b/lib/bucket_referer.go @@ -3,6 +3,8 @@ package lib import ( "encoding/xml" "fmt" + "github.com/aliyun/aliyun-oss-go-sdk/oss" + "io/ioutil" "os" "strings" ) @@ -14,6 +16,7 @@ var specChineseBucketReferer = SpecText{ syntaxText: ` ossutil referer --method put oss://bucket referer [options] + ossutil referer --method put --item raw oss://bucket local_xml_file [options] ossutil referer --method get oss://bucket [local_file] ossuitl referer --method delete oss://bucket `, @@ -26,12 +29,34 @@ var specChineseBucketReferer = SpecText{ 1) ossutil referer --method put oss://bucket referer [options] 这个命令将bucket的referer设置成后面的referer值 referer参数可以连续输入多个 + + 2) ossutil referer --method put --item raw oss://bucket local_xml_file [options] + 这个命令从配置文件local_xml_file中读取referer配置,然后设置bucket的referer + 配置文件是一个xml格式的文件,举例如下 + + + + false + false + + http://www.aliyun.com + https://www.aliyun.com + http://www.*.com + https://www.?.aliyuncs.com + + + http://www.refuse.com + https://*.hack.com + http://ban.*.com + https://www.?.deny.com + + - 2) ossutil referer --method get oss://bucket [local_xml_file] + 3) ossutil referer --method get oss://bucket [local_xml_file] 这个命令查询bucket的referer配置 如果输入参数local_xml_file,referer配置将输出到该文件,否则输出到屏幕上 - 3) ossutil referer --method delete oss://bucket + 4) ossutil referer --method delete oss://bucket 这个命令删除bucket的referer配置 `, sampleText: ` @@ -41,13 +66,16 @@ var specChineseBucketReferer = SpecText{ 2) 设置bucket的referer配置,且不允许referer为空 ossutil referer --method put oss://bucket www.test1.com www.test2.com --disable-empty-referer - 3) 查询bucket的referer配置,结果输出到标准输出 + 3) 设置bucket的referer配置,使用local file 的方式 + ossutil referer --method put --item raw oss://bucket local_xml_file + + 4) 查询bucket的referer配置,结果输出到标准输出 ossutil referer --method get oss://bucket - 4) 查询bucket的referer配置,结果输出到本地文件 + 5) 查询bucket的referer配置,结果输出到本地文件 ossutil referer --method get oss://bucket local_xml_file - 5) 删除bucket的referer配置 + 6) 删除bucket的referer配置 ossutil referer --method delete oss://bucket `, } @@ -59,6 +87,7 @@ var specEnglishBucketReferer = SpecText{ syntaxText: ` ossutil referer --method put oss://bucket referer [options] + ossutil referer --method put --item raw oss://bucket local_xml_file [options] ossutil referer --method get oss://bucket [local_file] ossuitl referer --method delete oss://bucket `, @@ -72,13 +101,35 @@ Usage: 1) ossutil referer --method put oss://bucket referer [options] This command sets the referer of the bucket to the following referer value. You can input many referer parameter. + + 2) ossutil referer --method put --item raw oss://bucket local_xml_file [options] + The command sets the referer configuration of bucket from local file local_xml_file + the local_xml_file is xml format,for example + + + + false + false + + http://www.aliyun.com + https://www.aliyun.com + http://www.*.com + https://www.?.aliyuncs.com + + + http://www.refuse.com + https://*.hack.com + http://ban.*.com + https://www.?.deny.com + + - 2) ossutil referer --method get oss://bucket [local_xml_file] + 3) ossutil referer --method get oss://bucket [local_xml_file] The command gets the referer configuration of bucket If you input parameter local_xml_file,the configuration will be output to local_xml_file If you don't input parameter local_xml_file,the configuration will be output to stdout - 3) ossutil referer --method delete oss://bucket + 4) ossutil referer --method delete oss://bucket The command deletes the referer configuration of bucket `, sampleText: ` @@ -88,13 +139,16 @@ Usage: 2) put bucket referer, empty referer is forbidden ossutil referer --method put oss://bucket www.test1.com www.test2.com --disable-empty-referer - 3) get referer configuration to stdout + 3) put bucket referer, read referer from local xml file + ossutil referer --method put --item raw oss://bucket local_xml_file + + 4) get referer configuration to stdout ossutil referer --method get oss://bucket - 4) get referer configuration to local file + 5) get referer configuration to local file ossutil referer --method get oss://bucket local_xml_file - 5) delete referer configuration + 6) delete referer configuration ossutil referer --method delete oss://bucket `, } @@ -130,6 +184,7 @@ var bucketRefererCommand = BucketRefererCommand{ OptionLogLevel, OptionDisableEmptyReferer, OptionMethod, + OptionItem, OptionPassword, OptionMode, OptionECSRoleName, @@ -165,6 +220,7 @@ func (brc *BucketRefererCommand) Init(args []string, options OptionMapType) erro // RunCommand simulate inheritance, and polymorphism func (brc *BucketRefererCommand) RunCommand() error { strMethod, _ := GetString(OptionMethod, brc.command.options) + strItem, _ := GetString(OptionItem, brc.command.options) if strMethod == "" { return fmt.Errorf("--method value is empty") } @@ -174,6 +230,11 @@ func (brc *BucketRefererCommand) RunCommand() error { return fmt.Errorf("--method value is not in the optional value:put|get|delete") } + strItem = strings.ToLower(strItem) + if strMethod == "put" && strItem != "" && strItem != "raw" { + return fmt.Errorf("--item value is not in the optional value:raw") + } + srcBucketUrL, err := GetCloudUrl(brc.command.args[0], "") if err != nil { return err @@ -182,11 +243,17 @@ func (brc *BucketRefererCommand) RunCommand() error { brc.brOption.bucketName = srcBucketUrL.bucket brc.brOption.disableEmptyRefer, _ = GetBool(OptionDisableEmptyReferer, brc.command.options) - if strMethod == "put" { - err = brc.PutBucketRefer() - } else if strMethod == "get" { + switch strMethod { + case "put": + switch strItem { + case "": + err = brc.PutBucketRefer() + case "raw": + err = brc.SetBucketRefer() + } + case "get": err = brc.GetBucketRefer() - } else if strMethod == "delete" { + case "delete": err = brc.DeleteBucketRefer() } return err @@ -208,6 +275,51 @@ func (brc *BucketRefererCommand) PutBucketRefer() error { return client.SetBucketReferer(brc.brOption.bucketName, referers, !brc.brOption.disableEmptyRefer) } +func (brc *BucketRefererCommand) SetBucketRefer() error { + if len(brc.command.args) < 2 { + return fmt.Errorf("put bucket referer need at least 2 parameters,the local xml file is empty") + } + + xmlFile := brc.command.args[1] + fileInfo, err := os.Stat(xmlFile) + if err != nil { + return err + } + + if fileInfo.IsDir() { + return fmt.Errorf("%s is dir,not the expected file", xmlFile) + } + + if fileInfo.Size() == 0 { + return fmt.Errorf("%s is empty file", xmlFile) + } + + // parsing the xml file + file, err := os.Open(xmlFile) + if err != nil { + return err + } + defer file.Close() + text, err := ioutil.ReadAll(file) + if err != nil { + return err + } + + refererConfig := oss.RefererXML{} + err = xml.Unmarshal(text, &refererConfig) + if err != nil { + return err + } + + // put bucket refer + client, err := brc.command.ossClient(brc.brOption.bucketName) + if err != nil { + return err + } + + return client.SetBucketRefererV2(brc.brOption.bucketName, refererConfig) +} + func (brc *BucketRefererCommand) confirm(str string) bool { var val string fmt.Printf(getClearStr(fmt.Sprintf("bucket referer: overwrite \"%s\"(y or N)? ", str))) diff --git a/lib/bucket_referer_test.go b/lib/bucket_referer_test.go index 8dbb08f8..d6792dbb 100644 --- a/lib/bucket_referer_test.go +++ b/lib/bucket_referer_test.go @@ -69,6 +69,107 @@ func (s *OssutilCommandSuite) TestBucketRefererPutSuccess(c *C) { s.removeBucket(bucketName, true, c) } +func (s *OssutilCommandSuite) TestBucketRefererPutSuccessV2(c *C) { + // put referer + bucketName := bucketNamePrefix + randLowStr(12) + s.putBucket(bucketName, c) + + // referer command test + var str string + strMethod := "put" + item := "raw" + options := OptionMapType{ + "endpoint": &str, + "accessKeyID": &str, + "accessKeySecret": &str, + "stsToken": &str, + "configFile": &configFile, + "method": &strMethod, + "item": &item, + } + + refer1 := "http://www.aliyun.com" + + refer2 := "https://www.?.aliyuncs.com" + + blackRefer1 := "http://www.refuse.com" + + blackRefer2 := "http://ban.*.com" + + setXml := ` + + false + false + + ` + refer1 + ` + https://www.aliyun.com + http://www.*.com + ` + refer2 + ` + + + ` + blackRefer1 + ` + https://*.hack.com + ` + blackRefer2 + ` + https://www.?.deny.com + +` + + setFileName := "test-setfile-" + randLowStr(5) + s.createFile(setFileName, setXml, c) + + refererArgs := []string{CloudURLToString(bucketName, ""), setFileName} + _, err := cm.RunCommand("referer", refererArgs, options) + c.Assert(err, IsNil) + os.Remove(setFileName) + + // check,get referer + refererDownName := randLowStr(12) + "-referer-down" + strMethod = "get" + options[OptionMethod] = &strMethod + + refererArgs = []string{CloudURLToString(bucketName, ""), refererDownName} + _, err = cm.RunCommand("referer", refererArgs, options) + c.Assert(err, IsNil) + + // check referer + _, err = os.Stat(refererDownName) + c.Assert(err, IsNil) + + refererBody := s.readFile(refererDownName, c) + referXml := oss.GetBucketRefererResult{} + err = xml.Unmarshal([]byte(refererBody), &referXml) + + c.Assert(err, IsNil) + c.Assert(referXml.AllowEmptyReferer, Equals, false) + c.Assert(*referXml.AllowTruncateQueryString, Equals, false) + c.Assert(len(referXml.RefererList), Equals, 4) + c.Assert(referXml.RefererList[0], Equals, refer1) + c.Assert(referXml.RefererList[3], Equals, refer2) + c.Assert(len(referXml.RefererBlacklist.Referer), Equals, 4) + c.Assert(referXml.RefererBlacklist.Referer[0], Equals, blackRefer1) + c.Assert(referXml.RefererBlacklist.Referer[2], Equals, blackRefer2) + + strMethod = "put" + options[OptionMethod] = &strMethod + + setXml = ` + + true + + +` + + setFileName = "test-setfile-" + randLowStr(5) + s.createFile(setFileName, setXml, c) + refererArgs = []string{CloudURLToString(bucketName, ""), setFileName} + _, err = cm.RunCommand("referer", refererArgs, options) + c.Assert(err, IsNil) + os.Remove(setFileName) + + os.Remove(refererDownName) + s.removeBucket(bucketName, true, c) +} + func (s *OssutilCommandSuite) TestBucketRefererDisableEmpty(c *C) { // put referer bucketName := bucketNamePrefix + randLowStr(12)