Skip to content

Commit

Permalink
modify Credentials according to the review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
taowei.wtw committed Jul 10, 2019
1 parent cef4a56 commit 3baf052
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 61 deletions.
2 changes: 1 addition & 1 deletion oss/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
8 changes: 4 additions & 4 deletions oss/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func New(endpoint, accessKeyID, accessKeySecret string, options ...ClientOption)
config.AccessKeyID = accessKeyID
config.AccessKeySecret = accessKeySecret

defAkBuild := &defaultCredentialInfBuild{config: config}
config.UserAKBuild = defAkBuild
defAkBuild := &defaultCredentialsProvider{config: config}
config.CredentialsProvider = defAkBuild

// URL parse
url := &urlMaker{}
Expand Down Expand Up @@ -1280,9 +1280,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
}
}

Expand Down
18 changes: 9 additions & 9 deletions oss/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
74 changes: 37 additions & 37 deletions oss/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions oss/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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() != "" {
Expand Down
15 changes: 8 additions & 7 deletions oss/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (s *OssConnSuite) TestAuth(c *C) {
endpoint := "https://github.com/"

cfg := getDefaultOssConfig()
defAkBuild := &defaultCredentialInfBuild{config: cfg}
cfg.UserAKBuild = defAkBuild
defAkBuild := &defaultCredentialsProvider{config: cfg}
cfg.CredentialsProvider = defAkBuild

um := urlMaker{}
um.Init(endpoint, false, false)
Expand Down Expand Up @@ -151,8 +151,8 @@ func (s *OssConnSuite) TestConnToolFunc(c *C) {

func (s *OssConnSuite) TestSignRtmpURL(c *C) {
cfg := getDefaultOssConfig()
defAkBuild := &defaultCredentialInfBuild{config: cfg}
cfg.UserAKBuild = defAkBuild
defAkBuild := &defaultCredentialsProvider{config: cfg}
cfg.CredentialsProvider = defAkBuild

um := urlMaker{}
um.Init(endpoint, false, false)
Expand Down Expand Up @@ -181,10 +181,11 @@ func (s *OssConnSuite) TestGetRtmpSignedStr(c *C) {
um.Init(endpoint, false, false)
conn := Conn{cfg, &um, nil}

defAkBuild := &defaultCredentialInfBuild{config: cfg}
cfg.UserAKBuild = defAkBuild
defAkBuild := &defaultCredentialsProvider{config: cfg}
cfg.CredentialsProvider = defAkBuild

akIf := conn.config.GetCredentials()

akIf := conn.config.GetCredentialInf()
//Anonymous
channelName := "test-get-rtmp-signed-str"
playlistName := "playlist.m3u8"
Expand Down

0 comments on commit 3baf052

Please sign in to comment.