119 lines
3.3 KiB
Go
119 lines
3.3 KiB
Go
package conf
|
||
|
||
import (
|
||
"fmt"
|
||
"os"
|
||
|
||
"github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
|
||
"github.com/joho/godotenv"
|
||
)
|
||
|
||
// Config 配置结构体
|
||
type Config struct {
|
||
// STS登录模式配置
|
||
RAMAccessKeyID string
|
||
RAMAccessKeySecret string
|
||
RAMRoleArn string
|
||
|
||
// 通用配置
|
||
Region string
|
||
Endpoint string
|
||
|
||
// STS临时凭证(运行时获取)
|
||
TempAccessKeyID string
|
||
TempAccessKeySecret string
|
||
SecurityToken string
|
||
}
|
||
|
||
// LoadConfig 加载配置
|
||
func LoadConfig() (*Config, error) {
|
||
// 尝试加载多个可能的配置文件
|
||
configFiles := []string{
|
||
".env", // 根目录的.env文件
|
||
"conf/alibabacloud.env", // 阿里云配置文件
|
||
"alibabacloud.env", // 根目录的阿里云配置文件
|
||
}
|
||
|
||
// 按顺序尝试加载配置文件
|
||
for _, configFile := range configFiles {
|
||
if err := godotenv.Load(configFile); err == nil {
|
||
fmt.Printf("成功加载配置文件: %s\n", configFile)
|
||
break
|
||
}
|
||
}
|
||
|
||
config := &Config{
|
||
RAMAccessKeyID: getEnv("RAM_ACCESS_KEY_ID", ""),
|
||
RAMAccessKeySecret: getEnv("RAM_ACCESS_KEY_SECRET", ""),
|
||
RAMRoleArn: getEnv("RAM_ROLE_ARN", ""),
|
||
Region: getEnv("ALIBABA_CLOUD_REGION", "cn-shanghai"),
|
||
Endpoint: getEnv("ALIBABA_CLOUD_ENDPOINT", "green-v2.cn-shanghai.aliyuncs.com"), // 2.0版本端点
|
||
}
|
||
|
||
return config, nil
|
||
}
|
||
|
||
// LoadConfigFromFile 从指定文件加载配置
|
||
func LoadConfigFromFile(configFile string) (*Config, error) {
|
||
// 加载指定的配置文件
|
||
if err := godotenv.Load(configFile); err != nil {
|
||
return nil, fmt.Errorf("加载配置文件失败 %s: %v", configFile, err)
|
||
}
|
||
|
||
fmt.Printf("成功加载配置文件: %s\n", configFile)
|
||
|
||
config := &Config{
|
||
RAMAccessKeyID: getEnv("RAM_ACCESS_KEY_ID", ""),
|
||
RAMAccessKeySecret: getEnv("RAM_ACCESS_KEY_SECRET", ""),
|
||
RAMRoleArn: getEnv("RAM_ROLE_ARN", ""),
|
||
Region: getEnv("ALIBABA_CLOUD_REGION", "cn-shanghai"),
|
||
Endpoint: getEnv("ALIBABA_CLOUD_ENDPOINT", "green-v2.cn-shanghai.aliyuncs.com"), // 2.0版本端点
|
||
}
|
||
|
||
return config, nil
|
||
}
|
||
|
||
// getEnv 获取环境变量,如果不存在则返回默认值
|
||
func getEnv(key, defaultValue string) string {
|
||
if value := os.Getenv(key); value != "" {
|
||
return value
|
||
}
|
||
return defaultValue
|
||
}
|
||
|
||
// GetSTSToken 获取STS临时凭证
|
||
func (c *Config) GetSTSToken() error {
|
||
|
||
// 创建STS客户端
|
||
stsClient, err := sts.NewClientWithAccessKey(c.Region, c.RAMAccessKeyID, c.RAMAccessKeySecret)
|
||
if err != nil {
|
||
return fmt.Errorf("创建STS客户端失败: %w", err)
|
||
}
|
||
|
||
// 构造AssumeRole请求
|
||
request := sts.CreateAssumeRoleRequest()
|
||
request.Scheme = "https"
|
||
request.RoleArn = c.RAMRoleArn
|
||
request.RoleSessionName = "content-security-session" // 会话名称可自定义
|
||
|
||
// 调用接口获取凭证
|
||
response, err := stsClient.AssumeRole(request)
|
||
if err != nil {
|
||
return fmt.Errorf("获取临时凭证失败: %w", err)
|
||
}
|
||
|
||
// 保存临时凭证
|
||
c.TempAccessKeyID = response.Credentials.AccessKeyId
|
||
c.TempAccessKeySecret = response.Credentials.AccessKeySecret
|
||
c.SecurityToken = response.Credentials.SecurityToken
|
||
|
||
return nil
|
||
}
|
||
|
||
// GetEffectiveCredentials 获取有效的访问凭证
|
||
func (c *Config) GetEffectiveCredentials() (accessKeyID, accessKeySecret, securityToken string) {
|
||
// 使用STS临时凭证
|
||
return c.TempAccessKeyID, c.TempAccessKeySecret, c.SecurityToken
|
||
|
||
}
|