1 Star 0 Fork 0

powerpaas/machine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
awscredentials.go 1.72 KB
一键复制 编辑 原始数据 按行查看 历史
package amazonec2
import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
)
type awsCredentials interface {
Credentials() *credentials.Credentials
}
type ProviderFactory interface {
NewStaticProvider(id, secret, token string) credentials.Provider
}
type defaultAWSCredentials struct {
AccessKey string
SecretKey string
SessionToken string
providerFactory ProviderFactory
fallbackProvider awsCredentials
}
func NewAWSCredentials(id, secret, token string) *defaultAWSCredentials {
creds := defaultAWSCredentials{
AccessKey: id,
SecretKey: secret,
SessionToken: token,
fallbackProvider: &AwsDefaultCredentialsProvider{},
providerFactory: &defaultProviderFactory{},
}
return &creds
}
func (c *defaultAWSCredentials) Credentials() *credentials.Credentials {
providers := []credentials.Provider{}
if c.AccessKey != "" && c.SecretKey != "" {
providers = append(providers, c.providerFactory.NewStaticProvider(c.AccessKey, c.SecretKey, c.SessionToken))
}
if c.fallbackProvider != nil {
fallbackCreds, err := c.fallbackProvider.Credentials().Get()
if err == nil {
providers = append(providers, &credentials.StaticProvider{Value: fallbackCreds})
}
}
return credentials.NewChainCredentials(providers)
}
type AwsDefaultCredentialsProvider struct{}
func (c *AwsDefaultCredentialsProvider) Credentials() *credentials.Credentials {
return session.New().Config.Credentials
}
type defaultProviderFactory struct{}
func (c *defaultProviderFactory) NewStaticProvider(id, secret, token string) credentials.Provider {
return &credentials.StaticProvider{Value: credentials.Value{
AccessKeyID: id,
SecretAccessKey: secret,
SessionToken: token,
}}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/powerpaas/machine.git
git@gitee.com:powerpaas/machine.git
powerpaas
machine
machine
v0.15.0-rancher3

搜索帮助