Sfoglia il codice sorgente

modify Credentials according to the review comments

taowei.wtw 6 anni fa
parent
commit
475356f935
7 ha cambiato i file con 69 aggiunte e 77 eliminazioni
  1. 1 1
      oss/auth.go
  2. 13 13
      oss/bucket_test.go
  3. 2 5
      oss/client.go
  4. 9 9
      oss/client_test.go
  5. 40 37
      oss/conf.go
  6. 3 3
      oss/conn.go
  7. 1 9
      oss/conn_test.go

+ 1 - 1
oss/auth.go

@@ -23,7 +23,7 @@ type headerSorter struct {
 // signHeader signs the header and sets it as the authorization header.
 func (conn Conn) signHeader(req *http.Request, canonicalizedResource string) {
 
-	akIf := conn.config.GetCredentialInf()
+	akIf := conn.config.GetCredentials()
 
 	// Get the final authorization string
 	authorizationStr := "OSS " + akIf.GetAccessKeyID() + ":" + conn.getSignedStr(req, canonicalizedResource, akIf.GetAccessKeySecret())

+ 13 - 13
oss/bucket_test.go

@@ -4507,12 +4507,14 @@ func (s *OssBucketSuite) TestBucketTrafficLimitObject(c *C) {
 
 	// put object
 	start := time.Now().UnixNano() / 1000 / 1000
-	err = bucket.PutObject(objectName, fd, TrafficLimitHeader(traffic))
+	err = bucket.PutObject(objectName, fd, TrafficLimitHeader(traffic), GetResponseHeader(&respHeader))
 	c.Assert(err, IsNil)
 	endingTime := time.Now().UnixNano() / 1000 / 1000
 	costT := endingTime - start
 	costV := contentLength * 1000 / costT // bit * 1000 / Millisecond = bit/s
 	c.Assert((costV < maxTraffic), Equals, true)
+	qosDelayTime = GetQosDelayTime(respHeader)
+	c.Assert(len(qosDelayTime) > 0, Equals, true)
 
 	// putobject without TrafficLimit
 	//
@@ -4549,12 +4551,17 @@ func (s *OssBucketSuite) TestBucketTrafficLimitObject(c *C) {
 	start = time.Now().UnixNano() / 1000 / 1000
 	nextPos, err = bucket.AppendObject(objectKey, strings.NewReader(randStr(18)), nextPos)
 	c.Assert(err, IsNil)
-	nextPos, err = bucket.AppendObject(objectKey, fd, nextPos, TrafficLimitHeader(traffic))
+
+	var respAppendHeader http.Header
+	nextPos, err = bucket.AppendObject(objectKey, fd, nextPos, TrafficLimitHeader(traffic), GetResponseHeader(&respAppendHeader))
 	c.Assert(err, IsNil)
 	endingTime = time.Now().UnixNano() / 1000 / 1000
 	costT = endingTime - start
 	costV = contentLength * 1000 / costT // bit * 1000 / Millisecond = bit/s
 	c.Assert((costV < maxTraffic), Equals, true)
+	qosDelayTime = GetQosDelayTime(respAppendHeader)
+	c.Assert(len(qosDelayTime) > 0, Equals, true)
+
 	err = bucket.GetObjectToFile(objectKey, newFile, TrafficLimitHeader(traffic))
 	c.Assert(err, IsNil)
 	err = bucket.DeleteObject(objectKey)
@@ -4576,7 +4583,7 @@ func (s *OssBucketSuite) TestBucketTrafficLimitObject(c *C) {
 
 	// get object with url
 	newFile = "test-file-" + randStr(10)
-	strURL, err = bucket.SignURL(objectName, HTTPGet, 60, TrafficLimitParam(traffic), GetResponseHeader(&respHeader))
+	strURL, err = bucket.SignURL(objectName, HTTPGet, 60, TrafficLimitParam(traffic))
 	c.Assert(err, IsNil)
 	start = time.Now().UnixNano() / 1000 / 1000
 	err = bucket.GetObjectToFileWithURL(strURL, newFile)
@@ -4585,22 +4592,16 @@ func (s *OssBucketSuite) TestBucketTrafficLimitObject(c *C) {
 	costT = endingTime - start
 	costV = contentLength * 1000 / costT // bit * 1000 / Millisecond = bit/s
 	c.Assert((costV < maxTraffic), Equals, true)
-	qosDelayTime = GetQosDelayTime(respHeader)
-	c.Assert(len(qosDelayTime) > 0, Equals, true)
 	os.Remove(newFile)
 
 	// copy object
 	destObjectName := objectNamePrefix + randStr(8)
-	_, err = bucket.CopyObject(objectName, destObjectName, TrafficLimitHeader(traffic), GetResponseHeader(&respHeader))
+	_, err = bucket.CopyObject(objectName, destObjectName, TrafficLimitHeader(traffic))
 	c.Assert(err, IsNil)
-	qosDelayTime = GetQosDelayTime(respHeader)
-	c.Assert(len(qosDelayTime) > 0, Equals, true)
 	err = bucket.DeleteObject(destObjectName)
 	c.Assert(err, IsNil)
 
-	err = bucket.DeleteObject(objectName)
-	c.Assert(err, IsNil)
-	bucket.DeleteObject(bucketName)
+	forceDeleteBucket(client, bucketName, c)
 }
 
 func (s *OssBucketSuite) TestBucketTrafficLimitUpload(c *C) {
@@ -4652,6 +4653,5 @@ func (s *OssBucketSuite) TestBucketTrafficLimitUpload(c *C) {
 	c.Assert((costV < maxTraffic), Equals, true)
 	os.Remove(fileName)
 
-	bucket.DeleteObject(objectName)
-	c.Assert(err, IsNil)
+	forceDeleteBucket(client, bucketName, c)
 }

+ 2 - 5
oss/client.go

@@ -46,9 +46,6 @@ func New(endpoint, accessKeyID, accessKeySecret string, options ...ClientOption)
 	config.AccessKeyID = accessKeyID
 	config.AccessKeySecret = accessKeySecret
 
-	defAkBuild := &defaultCredentialInfBuild{config: config}
-	config.UserAKBuild = defAkBuild
-
 	// URL parse
 	url := &urlMaker{}
 	url.Init(config.Endpoint, config.IsCname, config.IsUseProxy)
@@ -1280,9 +1277,9 @@ func SetLogger(Logger *log.Logger) ClientOption {
 
 // SetAKInterface sets funciton for get the user's ak
 //
-func SetCredentialInfBuild(akBuild CredentialInfBuild) ClientOption {
+func SetCredentialsProvider(provider CredentialsProvider) ClientOption {
 	return func(client *Client) {
-		client.Config.UserAKBuild = akBuild
+		client.Config.CredentialsProvider = provider
 	}
 }
 

+ 9 - 9
oss/client_test.go

@@ -2713,32 +2713,32 @@ func struct2string(obj interface{}, c *C) string {
 	return string(str)
 }
 
-type TestCredentialInf struct {
+type TestCredentials struct {
 }
 
-func (testCreInf *TestCredentialInf) GetAccessKeyID() string {
+func (testCreInf *TestCredentials) GetAccessKeyID() string {
 	return os.Getenv("OSS_TEST_ACCESS_KEY_ID")
 }
 
-func (testCreInf *TestCredentialInf) GetAccessKeySecret() string {
+func (testCreInf *TestCredentials) GetAccessKeySecret() string {
 	return os.Getenv("OSS_TEST_ACCESS_KEY_SECRET")
 }
 
-func (testCreInf *TestCredentialInf) GetSecurityToken() string {
+func (testCreInf *TestCredentials) GetSecurityToken() string {
 	return ""
 }
 
-type TestCredentialInfBuild struct {
+type TestCredentialsProvider struct {
 }
 
-func (testInfBuild *TestCredentialInfBuild) GetCredentialInf() CredentialInf {
-	return &TestCredentialInf{}
+func (testInfBuild *TestCredentialsProvider) GetCredentials() Credentials {
+	return &TestCredentials{}
 }
 
 func (s *OssClientSuite) TestClientCredentialInfBuild(c *C) {
 	var bucketNameTest = bucketNamePrefix + randLowStr(6)
-	var defaultBuild TestCredentialInfBuild
-	client, err := New(endpoint, "", "", SetCredentialInfBuild(&defaultBuild))
+	var defaultBuild TestCredentialsProvider
+	client, err := New(endpoint, "", "", SetCredentialsProvider(&defaultBuild))
 	c.Assert(err, IsNil)
 	err = client.CreateBucket(bucketNameTest)
 	c.Assert(err, IsNil)

+ 40 - 37
oss/conf.go

@@ -36,67 +36,67 @@ type HTTPMaxConns struct {
 }
 
 // CredentialInf is interface for get AccessKeyID,AccessKeySecret,SecurityToken
-type CredentialInf interface {
+type Credentials interface {
 	GetAccessKeyID() string
 	GetAccessKeySecret() string
 	GetSecurityToken() string
 }
 
 // CredentialInfBuild is interface for get CredentialInf
-type CredentialInfBuild interface {
-	GetCredentialInf() CredentialInf
+type CredentialsProvider interface {
+	GetCredentials() Credentials
 }
 
-type defaultCredentialInf struct {
+type defaultCredentials struct {
 	config *Config
 }
 
-func (defCre *defaultCredentialInf) GetAccessKeyID() string {
+func (defCre *defaultCredentials) GetAccessKeyID() string {
 	return defCre.config.AccessKeyID
 }
 
-func (defCre *defaultCredentialInf) GetAccessKeySecret() string {
+func (defCre *defaultCredentials) GetAccessKeySecret() string {
 	return defCre.config.AccessKeySecret
 }
 
-func (defCre *defaultCredentialInf) GetSecurityToken() string {
+func (defCre *defaultCredentials) GetSecurityToken() string {
 	return defCre.config.SecurityToken
 }
 
-type defaultCredentialInfBuild struct {
+type defaultCredentialsProvider struct {
 	config *Config
 }
 
-func (defBuild *defaultCredentialInfBuild) GetCredentialInf() CredentialInf {
-	return &defaultCredentialInf{config: defBuild.config}
+func (defBuild *defaultCredentialsProvider) GetCredentials() Credentials {
+	return &defaultCredentials{config: defBuild.config}
 }
 
 // Config defines oss configuration
 type Config struct {
-	Endpoint         string             // OSS endpoint
-	AccessKeyID      string             // AccessId
-	AccessKeySecret  string             // AccessKey
-	RetryTimes       uint               // Retry count by default it's 5.
-	UserAgent        string             // SDK name/version/system information
-	IsDebug          bool               // Enable debug mode. Default is false.
-	Timeout          uint               // Timeout in seconds. By default it's 60.
-	SecurityToken    string             // STS Token
-	IsCname          bool               // If cname is in the endpoint.
-	HTTPTimeout      HTTPTimeout        // HTTP timeout
-	HTTPMaxConns     HTTPMaxConns       // Http max connections
-	IsUseProxy       bool               // Flag of using proxy.
-	ProxyHost        string             // Flag of using proxy host.
-	IsAuthProxy      bool               // Flag of needing authentication.
-	ProxyUser        string             // Proxy user
-	ProxyPassword    string             // Proxy password
-	IsEnableMD5      bool               // Flag of enabling MD5 for upload.
-	MD5Threshold     int64              // Memory footprint threshold for each MD5 computation (16MB is the default), in byte. When the data is more than that, temp file is used.
-	IsEnableCRC      bool               // Flag of enabling CRC for upload.
-	LogLevel         int                // Log level
-	Logger           *log.Logger        // For write log
-	UploadLimitSpeed int                // Upload limit speed:KB/s, 0 is unlimited
-	UploadLimiter    *OssLimiter        // Bandwidth limit reader for upload
-	UserAKBuild      CredentialInfBuild // User provides interface to get AccessKeyID, AccessKeySecret, SecurityToken
+	Endpoint            string              // OSS endpoint
+	AccessKeyID         string              // AccessId
+	AccessKeySecret     string              // AccessKey
+	RetryTimes          uint                // Retry count by default it's 5.
+	UserAgent           string              // SDK name/version/system information
+	IsDebug             bool                // Enable debug mode. Default is false.
+	Timeout             uint                // Timeout in seconds. By default it's 60.
+	SecurityToken       string              // STS Token
+	IsCname             bool                // If cname is in the endpoint.
+	HTTPTimeout         HTTPTimeout         // HTTP timeout
+	HTTPMaxConns        HTTPMaxConns        // Http max connections
+	IsUseProxy          bool                // Flag of using proxy.
+	ProxyHost           string              // Flag of using proxy host.
+	IsAuthProxy         bool                // Flag of needing authentication.
+	ProxyUser           string              // Proxy user
+	ProxyPassword       string              // Proxy password
+	IsEnableMD5         bool                // Flag of enabling MD5 for upload.
+	MD5Threshold        int64               // Memory footprint threshold for each MD5 computation (16MB is the default), in byte. When the data is more than that, temp file is used.
+	IsEnableCRC         bool                // Flag of enabling CRC for upload.
+	LogLevel            int                 // Log level
+	Logger              *log.Logger         // For write log
+	UploadLimitSpeed    int                 // Upload limit speed:KB/s, 0 is unlimited
+	UploadLimiter       *OssLimiter         // Bandwidth limit reader for upload
+	CredentialsProvider CredentialsProvider // User provides interface to get AccessKeyID, AccessKeySecret, SecurityToken
 }
 
 // LimitUploadSpeed uploadSpeed:KB/s, 0 is unlimited,default is 0
@@ -129,9 +129,9 @@ func (config *Config) WriteLog(LogLevel int, format string, a ...interface{}) {
 	config.Logger.Printf("%s", logBuffer.String())
 }
 
-// for get CredentialInfBuild
-func (config *Config) GetCredentialInf() CredentialInf {
-	return config.UserAKBuild.GetCredentialInf()
+// for get Credentials
+func (config *Config) GetCredentials() Credentials {
+	return config.CredentialsProvider.GetCredentials()
 }
 
 // getDefaultOssConfig gets the default configuration.
@@ -169,5 +169,8 @@ func getDefaultOssConfig() *Config {
 	config.LogLevel = LogOff
 	config.Logger = log.New(os.Stdout, "", log.LstdFlags)
 
+	provider := &defaultCredentialsProvider{config: &config}
+	config.CredentialsProvider = provider
+
 	return &config
 }

+ 3 - 3
oss/conn.go

@@ -240,7 +240,7 @@ func (conn Conn) doRequest(method string, uri *url.URL, canonicalizedResource st
 	req.Header.Set(HTTPHeaderHost, conn.config.Endpoint)
 	req.Header.Set(HTTPHeaderUserAgent, conn.config.UserAgent)
 
-	akIf := conn.config.GetCredentialInf()
+	akIf := conn.config.GetCredentials()
 	if akIf.GetSecurityToken() != "" {
 		req.Header.Set(HTTPHeaderOssSecurityToken, akIf.GetSecurityToken())
 	}
@@ -283,7 +283,7 @@ func (conn Conn) doRequest(method string, uri *url.URL, canonicalizedResource st
 }
 
 func (conn Conn) signURL(method HTTPMethod, bucketName, objectName string, expiration int64, params map[string]interface{}, headers map[string]string) string {
-	akIf := conn.config.GetCredentialInf()
+	akIf := conn.config.GetCredentials()
 	if akIf.GetSecurityToken() != "" {
 		params[HTTPParamSecurityToken] = akIf.GetSecurityToken()
 	}
@@ -330,7 +330,7 @@ func (conn Conn) signRtmpURL(bucketName, channelName, playlistName string, expir
 	expireStr := strconv.FormatInt(expiration, 10)
 	params[HTTPParamExpires] = expireStr
 
-	akIf := conn.config.GetCredentialInf()
+	akIf := conn.config.GetCredentials()
 	if akIf.GetAccessKeyID() != "" {
 		params[HTTPParamAccessKeyID] = akIf.GetAccessKeyID()
 		if akIf.GetSecurityToken() != "" {

+ 1 - 9
oss/conn_test.go

@@ -84,11 +84,7 @@ func (s *OssConnSuite) TestURLMarker(c *C) {
 
 func (s *OssConnSuite) TestAuth(c *C) {
 	endpoint := "https://github.com/"
-
 	cfg := getDefaultOssConfig()
-	defAkBuild := &defaultCredentialInfBuild{config: cfg}
-	cfg.UserAKBuild = defAkBuild
-
 	um := urlMaker{}
 	um.Init(endpoint, false, false)
 	conn := Conn{cfg, &um, nil}
@@ -151,8 +147,6 @@ func (s *OssConnSuite) TestConnToolFunc(c *C) {
 
 func (s *OssConnSuite) TestSignRtmpURL(c *C) {
 	cfg := getDefaultOssConfig()
-	defAkBuild := &defaultCredentialInfBuild{config: cfg}
-	cfg.UserAKBuild = defAkBuild
 
 	um := urlMaker{}
 	um.Init(endpoint, false, false)
@@ -181,10 +175,8 @@ func (s *OssConnSuite) TestGetRtmpSignedStr(c *C) {
 	um.Init(endpoint, false, false)
 	conn := Conn{cfg, &um, nil}
 
-	defAkBuild := &defaultCredentialInfBuild{config: cfg}
-	cfg.UserAKBuild = defAkBuild
+	akIf := conn.config.GetCredentials()
 
-	akIf := conn.config.GetCredentialInf()
 	//Anonymous
 	channelName := "test-get-rtmp-signed-str"
 	playlistName := "playlist.m3u8"