-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreds.go
49 lines (41 loc) · 1.26 KB
/
creds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package clients
import (
"fmt"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
)
// client chainedCreds for Cli
func ChainedCredsToCli(secretId, secretKey, token string) (common.CredentialIface, error) {
providerChain := []common.Provider{
NewConfigurationCredentialProvider(&Configuration{secretId, secretKey, token}),
DefaultEnvProvider(),
common.DefaultCvmRoleProvider(),
}
return common.NewProviderChain(providerChain).GetCredential()
}
// Configuration
type Configuration struct {
SecretId string
SecretKey string
Token string
}
// NewConfigurationCredentialProvider
func NewConfigurationCredentialProvider(configuration *Configuration) common.Provider {
return &ConfigurationProvider{
Configuration: configuration,
}
}
// ConfigurationProvider
type ConfigurationProvider struct {
Configuration *Configuration
}
// GetCredential
func (p *ConfigurationProvider) GetCredential() (common.CredentialIface, error) {
if p.Configuration.SecretId != "" && p.Configuration.SecretKey != "" {
return common.NewTokenCredential(p.Configuration.SecretId, p.Configuration.SecretKey, p.Configuration.Token), nil
} else {
return nil, ErrNoValidCredentialsFound
}
}
var (
ErrNoValidCredentialsFound = fmt.Errorf("no valid credentials were found")
)