Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify bucket referer api #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 126 additions & 14 deletions lib/bucket_referer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package lib
import (
"encoding/xml"
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"io/ioutil"
"os"
"strings"
)
Expand All @@ -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
`,
Expand All @@ -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格式的文件,举例如下

<?xml version="1.0" encoding="UTF-8"?>
<RefererConfiguration>
<AllowEmptyReferer>false</AllowEmptyReferer>
<AllowTruncateQueryString>false</AllowTruncateQueryString>
<RefererList>
<Referer>http://www.aliyun.com</Referer>
<Referer>https://www.aliyun.com</Referer>
<Referer>http://www.*.com</Referer>
<Referer>https://www.?.aliyuncs.com</Referer>
</RefererList>
<RefererBlacklist>
<Referer>http://www.refuse.com</Referer>
<Referer>https://*.hack.com</Referer>
<Referer>http://ban.*.com</Referer>
<Referer>https://www.?.deny.com</Referer>
</RefererBlacklist>
</RefererConfiguration>

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: `
Expand All @@ -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
`,
}
Expand All @@ -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
`,
Expand All @@ -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

<?xml version="1.0" encoding="UTF-8"?>
<RefererConfiguration>
<AllowEmptyReferer>false</AllowEmptyReferer>
<AllowTruncateQueryString>false</AllowTruncateQueryString>
<RefererList>
<Referer>http://www.aliyun.com</Referer>
<Referer>https://www.aliyun.com</Referer>
<Referer>http://www.*.com</Referer>
<Referer>https://www.?.aliyuncs.com</Referer>
</RefererList>
<RefererBlacklist>
<Referer>http://www.refuse.com</Referer>
<Referer>https://*.hack.com</Referer>
<Referer>http://ban.*.com</Referer>
<Referer>https://www.?.deny.com</Referer>
</RefererBlacklist>
</RefererConfiguration>

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: `
Expand All @@ -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
`,
}
Expand Down Expand Up @@ -130,6 +184,7 @@ var bucketRefererCommand = BucketRefererCommand{
OptionLogLevel,
OptionDisableEmptyReferer,
OptionMethod,
OptionItem,
OptionPassword,
OptionMode,
OptionECSRoleName,
Expand Down Expand Up @@ -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")
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)))
Expand Down
101 changes: 101 additions & 0 deletions lib/bucket_referer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := `<?xml version="1.0" encoding="UTF-8"?>
<RefererConfiguration>
<AllowEmptyReferer>false</AllowEmptyReferer>
<AllowTruncateQueryString>false</AllowTruncateQueryString>
<RefererList>
<Referer>` + refer1 + `</Referer>
<Referer>https://www.aliyun.com</Referer>
<Referer>http://www.*.com</Referer>
<Referer>` + refer2 + `</Referer>
</RefererList>
<RefererBlacklist>
<Referer>` + blackRefer1 + `</Referer>
<Referer>https://*.hack.com</Referer>
<Referer>` + blackRefer2 + `</Referer>
<Referer>https://www.?.deny.com</Referer>
</RefererBlacklist>
</RefererConfiguration>`

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 = `<?xml version="1.0" encoding="UTF-8"?>
<RefererConfiguration>
<AllowEmptyReferer>true</AllowEmptyReferer>
<RefererList>
</RefererList>
</RefererConfiguration>`

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)
Expand Down