refactor: clean code

This commit is contained in:
Fu Diwei 2025-11-13 20:54:11 +08:00
parent b18fccfeca
commit 4be788f880
10 changed files with 245 additions and 4 deletions

View File

@ -19,6 +19,7 @@ func init() {
provider, err := qiniukodo.NewSSLDeployerProvider(&qiniukodo.SSLDeployerProviderConfig{
AccessKey: credentials.AccessKey,
SecretKey: credentials.SecretKey,
Bucket: xmaps.GetString(options.ProviderExtendedConfig, "bucket"),
Domain: xmaps.GetString(options.ProviderExtendedConfig, "domain"),
})
return provider, err

View File

@ -5,7 +5,7 @@ import (
"github.com/certimate-go/certimate/internal/domain"
"github.com/certimate-go/certimate/pkg/core"
upyuncdn "github.com/certimate-go/certimate/pkg/core/ssl-deployer/providers/upyun-cdn"
upyunfile "github.com/certimate-go/certimate/pkg/core/ssl-deployer/providers/upyun-file"
xmaps "github.com/certimate-go/certimate/pkg/utils/maps"
)
@ -16,9 +16,10 @@ func init() {
return nil, fmt.Errorf("failed to populate provider access config: %w", err)
}
provider, err := upyuncdn.NewSSLDeployerProvider(&upyuncdn.SSLDeployerProviderConfig{
provider, err := upyunfile.NewSSLDeployerProvider(&upyunfile.SSLDeployerProviderConfig{
Username: credentials.Username,
Password: credentials.Password,
Bucket: xmaps.GetString(options.ProviderExtendedConfig, "bucket"),
Domain: xmaps.GetString(options.ProviderExtendedConfig, "domain"),
})
return provider, err

View File

@ -18,6 +18,8 @@ type SSLDeployerProviderConfig struct {
AccessKey string `json:"accessKey"`
// 七牛云 SecretKey。
SecretKey string `json:"secretKey"`
// 存储桶名。暂时无用。
Bucket string `json:"bucket"`
// 自定义域名(不支持泛域名)。
Domain string `json:"domain"`
}

View File

@ -16,6 +16,7 @@ var (
fInputKeyPath string
fAccessKey string
fSecretKey string
fBucket string
fDomain string
)
@ -26,6 +27,7 @@ func init() {
flag.StringVar(&fInputKeyPath, argsPrefix+"INPUTKEYPATH", "", "")
flag.StringVar(&fAccessKey, argsPrefix+"ACCESSKEY", "", "")
flag.StringVar(&fSecretKey, argsPrefix+"SECRETKEY", "", "")
flag.StringVar(&fBucket, argsPrefix+"BUCKET", "", "")
flag.StringVar(&fDomain, argsPrefix+"DOMAIN", "", "")
}
@ -37,6 +39,7 @@ Shell command to run this test:
--CERTIMATE_SSLDEPLOYER_QINIUKODO_INPUTKEYPATH="/path/to/your-input-key.pem" \
--CERTIMATE_SSLDEPLOYER_QINIUKODO_ACCESSKEY="your-access-key" \
--CERTIMATE_SSLDEPLOYER_QINIUKODO_SECRETKEY="your-secret-key" \
--CERTIMATE_SSLDEPLOYER_QINIUKODO_BUCKET="your-bucket" \
--CERTIMATE_SSLDEPLOYER_QINIUKODO_DOMAIN="example.com"
*/
func TestDeploy(t *testing.T) {
@ -49,12 +52,14 @@ func TestDeploy(t *testing.T) {
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
fmt.Sprintf("ACCESSKEY: %v", fAccessKey),
fmt.Sprintf("SECRETKEY: %v", fSecretKey),
fmt.Sprintf("BUCKET: %v", fBucket),
fmt.Sprintf("DOMAIN: %v", fDomain),
}, "\n"))
deployer, err := provider.NewSSLDeployerProvider(&provider.SSLDeployerProviderConfig{
AccessKey: fAccessKey,
SecretKey: fSecretKey,
Bucket: fBucket,
Domain: fDomain,
})
if err != nil {

View File

@ -0,0 +1,122 @@
package upyunfile
import (
"context"
"errors"
"fmt"
"log/slog"
"github.com/samber/lo"
"github.com/certimate-go/certimate/pkg/core"
sslmgrsp "github.com/certimate-go/certimate/pkg/core/ssl-manager/providers/upyun-ssl"
upyunsdk "github.com/certimate-go/certimate/pkg/sdk3rd/upyun/console"
)
type SSLDeployerProviderConfig struct {
// 又拍云账号用户名。
Username string `json:"username"`
// 又拍云账号密码。
Password string `json:"password"`
// 存储桶名。暂时无用。
Bucket string `json:"bucket"`
// 自定义域名(支持泛域名)。
Domain string `json:"domain"`
}
type SSLDeployerProvider struct {
config *SSLDeployerProviderConfig
logger *slog.Logger
sdkClient *upyunsdk.Client
sslManager core.SSLManager
}
var _ core.SSLDeployer = (*SSLDeployerProvider)(nil)
func NewSSLDeployerProvider(config *SSLDeployerProviderConfig) (*SSLDeployerProvider, error) {
if config == nil {
return nil, errors.New("the configuration of the ssl deployer provider is nil")
}
client, err := createSDKClient(config.Username, config.Password)
if err != nil {
return nil, fmt.Errorf("could not create sdk client: %w", err)
}
sslmgr, err := sslmgrsp.NewSSLManagerProvider(&sslmgrsp.SSLManagerProviderConfig{
Username: config.Username,
Password: config.Password,
})
if err != nil {
return nil, fmt.Errorf("could not create ssl manager: %w", err)
}
return &SSLDeployerProvider{
config: config,
logger: slog.Default(),
sdkClient: client,
sslManager: sslmgr,
}, nil
}
func (d *SSLDeployerProvider) SetLogger(logger *slog.Logger) {
if logger == nil {
d.logger = slog.New(slog.DiscardHandler)
} else {
d.logger = logger
}
d.sslManager.SetLogger(logger)
}
func (d *SSLDeployerProvider) Deploy(ctx context.Context, certPEM string, privkeyPEM string) (*core.SSLDeployResult, error) {
// 上传证书
upres, err := d.sslManager.Upload(ctx, certPEM, privkeyPEM)
if err != nil {
return nil, fmt.Errorf("failed to upload certificate file: %w", err)
} else {
d.logger.Info("ssl certificate uploaded", slog.Any("result", upres))
}
// 获取域名证书配置
getHttpsServiceManagerResp, err := d.sdkClient.GetHttpsServiceManager(d.config.Domain)
d.logger.Debug("sdk request 'console.GetHttpsServiceManager'", slog.String("request.domain", d.config.Domain), slog.Any("response", getHttpsServiceManagerResp))
if err != nil {
return nil, fmt.Errorf("failed to execute sdk request 'console.GetHttpsServiceManager': %w", err)
}
// 判断域名是否已启用 HTTPS
// 如果已启用,迁移域名证书;否则,设置新证书
_, lastCertIndex, _ := lo.FindIndexOf(getHttpsServiceManagerResp.Data.Domains, func(item upyunsdk.HttpsServiceManagerDomain) bool {
return item.Https
})
if lastCertIndex == -1 {
updateHttpsCertificateManagerReq := &upyunsdk.UpdateHttpsCertificateManagerRequest{
CertificateId: upres.CertId,
Domain: d.config.Domain,
Https: true,
ForceHttps: true,
}
updateHttpsCertificateManagerResp, err := d.sdkClient.UpdateHttpsCertificateManager(updateHttpsCertificateManagerReq)
d.logger.Debug("sdk request 'console.EnableDomainHttps'", slog.Any("request", updateHttpsCertificateManagerReq), slog.Any("response", updateHttpsCertificateManagerResp))
if err != nil {
return nil, fmt.Errorf("failed to execute sdk request 'console.UpdateHttpsCertificateManager': %w", err)
}
} else if getHttpsServiceManagerResp.Data.Domains[lastCertIndex].CertificateId != upres.CertId {
migrateHttpsDomainReq := &upyunsdk.MigrateHttpsDomainRequest{
CertificateId: upres.CertId,
Domain: d.config.Domain,
}
migrateHttpsDomainResp, err := d.sdkClient.MigrateHttpsDomain(migrateHttpsDomainReq)
d.logger.Debug("sdk request 'console.MigrateHttpsDomain'", slog.Any("request", migrateHttpsDomainReq), slog.Any("response", migrateHttpsDomainResp))
if err != nil {
return nil, fmt.Errorf("failed to execute sdk request 'console.MigrateHttpsDomain': %w", err)
}
}
return &core.SSLDeployResult{}, nil
}
func createSDKClient(username, password string) (*upyunsdk.Client, error) {
return upyunsdk.NewClient(username, password)
}

View File

@ -0,0 +1,80 @@
package upyunfile_test
import (
"context"
"flag"
"fmt"
"os"
"strings"
"testing"
provider "github.com/certimate-go/certimate/pkg/core/ssl-deployer/providers/upyun-file"
)
var (
fInputCertPath string
fInputKeyPath string
fUsername string
fPassword string
fBucket string
fDomain string
)
func init() {
argsPrefix := "CERTIMATE_SSLDEPLOYER_UPYUNFILE_"
flag.StringVar(&fInputCertPath, argsPrefix+"INPUTCERTPATH", "", "")
flag.StringVar(&fInputKeyPath, argsPrefix+"INPUTKEYPATH", "", "")
flag.StringVar(&fUsername, argsPrefix+"USERNAME", "", "")
flag.StringVar(&fPassword, argsPrefix+"PASSWORD", "", "")
flag.StringVar(&fBucket, argsPrefix+"BUCKET", "", "")
flag.StringVar(&fDomain, argsPrefix+"DOMAIN", "", "")
}
/*
Shell command to run this test:
go test -v ./upyun_file_test.go -args \
--CERTIMATE_SSLDEPLOYER_UPYUNFILE_INPUTCERTPATH="/path/to/your-input-cert.pem" \
--CERTIMATE_SSLDEPLOYER_UPYUNFILE_INPUTKEYPATH="/path/to/your-input-key.pem" \
--CERTIMATE_SSLDEPLOYER_UPYUNFILE_USERNAME="your-username" \
--CERTIMATE_SSLDEPLOYER_UPYUNFILE_PASSWORD="your-password" \
--CERTIMATE_SSLDEPLOYER_UPYUNFILE_BUCKET="your-bucket" \
--CERTIMATE_SSLDEPLOYER_UPYUNFILE_DOMAIN="example.com"
*/
func TestDeploy(t *testing.T) {
flag.Parse()
t.Run("Deploy", func(t *testing.T) {
t.Log(strings.Join([]string{
"args:",
fmt.Sprintf("INPUTCERTPATH: %v", fInputCertPath),
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
fmt.Sprintf("USERNAME: %v", fUsername),
fmt.Sprintf("PASSWORD: %v", fPassword),
fmt.Sprintf("BUCKET: %v", fBucket),
fmt.Sprintf("DOMAIN: %v", fDomain),
}, "\n"))
deployer, err := provider.NewSSLDeployerProvider(&provider.SSLDeployerProviderConfig{
Username: fUsername,
Password: fPassword,
Bucket: fBucket,
Domain: fDomain,
})
if err != nil {
t.Errorf("err: %+v", err)
return
}
fInputCertData, _ := os.ReadFile(fInputCertPath)
fInputKeyData, _ := os.ReadFile(fInputKeyPath)
res, err := deployer.Deploy(context.Background(), string(fInputCertData), string(fInputKeyData))
if err != nil {
t.Errorf("err: %+v", err)
return
}
t.Logf("ok: %v", res)
})
}

View File

@ -19,6 +19,15 @@ const BizDeployNodeConfigFieldsProviderQiniuKodo = () => {
return (
<>
<Form.Item
name={[parentNamePath, "bucket"]}
initialValue={initialValues.domain}
label={t("workflow_node.deploy.form.qiniu_kodo_bucket.label")}
rules={[formRule]}
>
<Input placeholder={t("workflow_node.deploy.form.qiniu_kodo_bucket.placeholder")} />
</Form.Item>
<Form.Item
name={[parentNamePath, "domain"]}
initialValue={initialValues.domain}
@ -33,6 +42,7 @@ const BizDeployNodeConfigFieldsProviderQiniuKodo = () => {
const getInitialValues = (): Nullish<z.infer<ReturnType<typeof getSchema>>> => {
return {
bucket: "",
domain: "",
};
};
@ -41,6 +51,7 @@ const getSchema = ({ i18n = getI18n() }: { i18n?: ReturnType<typeof getI18n> })
const { t } = i18n;
return z.object({
bucket: z.string().nonempty(t("workflow_node.deploy.form.qiniu_kodo_domain.placeholder")),
domain: z.string().refine((v) => validDomainName(v), t("common.errmsg.domain_invalid")),
});
};

View File

@ -24,6 +24,15 @@ const BizDeployNodeConfigFieldsProviderUpyunFile = () => {
<Tips message={<span dangerouslySetInnerHTML={{ __html: t("workflow_node.deploy.form.upyun_file.guide") }}></span>} />
</Form.Item>
<Form.Item
name={[parentNamePath, "bucket"]}
initialValue={initialValues.domain}
label={t("workflow_node.deploy.form.upyun_file_bucket.label")}
rules={[formRule]}
>
<Input placeholder={t("workflow_node.deploy.form.upyun_file_bucket.placeholder")} />
</Form.Item>
<Form.Item
name={[parentNamePath, "domain"]}
initialValue={initialValues.domain}
@ -38,6 +47,7 @@ const BizDeployNodeConfigFieldsProviderUpyunFile = () => {
const getInitialValues = (): Nullish<z.infer<ReturnType<typeof getSchema>>> => {
return {
bucket: "",
domain: "",
};
};
@ -46,6 +56,7 @@ const getSchema = ({ i18n = getI18n() }: { i18n?: ReturnType<typeof getI18n> })
const { t } = i18n;
return z.object({
bucket: z.string().nonempty(t("workflow_node.deploy.form.upyun_file_bucket.placeholder")),
domain: z.string().refine((v) => validDomainName(v), t("common.errmsg.domain_invalid")),
});
};

View File

@ -693,6 +693,8 @@
"workflow_node.deploy.form.proxmoxve_auto_restart.label": "Auto restart Proxmox VE after deployment",
"workflow_node.deploy.form.qiniu_cdn_domain.label": "Qiniu CDN domain",
"workflow_node.deploy.form.qiniu_cdn_domain.placeholder": "Please enter Qiniu CDN domain name",
"workflow_node.deploy.form.qiniu_kodo_bucket.label": "Qiniu Kodo bucket",
"workflow_node.deploy.form.qiniu_kodo_bucket.placeholder": "Please enter Qiniu Kodo bucket name",
"workflow_node.deploy.form.qiniu_kodo_domain.label": "Qiniu Kodo custom domain",
"workflow_node.deploy.form.qiniu_kodo_domain.placeholder": "Please enter Qiniu Kodo bucket custom domain name",
"workflow_node.deploy.form.qiniu_pili_hub.label": "Qiniu Pili hub",
@ -920,6 +922,8 @@
"workflow_node.deploy.form.upyun_cdn_domain.label": "UPYUN CDN domain",
"workflow_node.deploy.form.upyun_cdn_domain.placeholder": "Please enter UPYUN CDN domain name",
"workflow_node.deploy.form.upyun_file.guide": "This uses webpage simulator login and does not guarantee stability. If there are any changes to the UPYUN, please create a GitHub Issue.",
"workflow_node.deploy.form.upyun_file_bucket.label": "UPYUN USS bucket",
"workflow_node.deploy.form.upyun_file_bucket.placeholder": "Please enter UPYUN USS bucket name",
"workflow_node.deploy.form.upyun_file_domain.label": "UPYUN USS custom domain",
"workflow_node.deploy.form.upyun_file_domain.placeholder": "Please enter UPYUN USS bucket custom domain name",
"workflow_node.deploy.form.volcengine_alb_region.label": "VolcEngine ALB region",

View File

@ -691,6 +691,8 @@
"workflow_node.deploy.form.proxmoxve_auto_restart.label": "部署后自动重启 Proxmox VE 服务",
"workflow_node.deploy.form.qiniu_cdn_domain.label": "七牛云 CDN 加速域名",
"workflow_node.deploy.form.qiniu_cdn_domain.placeholder": "请输入七牛云 CDN 加速域名",
"workflow_node.deploy.form.qiniu_kodo_bucket.label": "七牛云对象存储桶名",
"workflow_node.deploy.form.qiniu_kodo_bucket.placeholder": "请输入七牛云对象存储桶名",
"workflow_node.deploy.form.qiniu_kodo_domain.label": "七牛云对象存储自定义域名",
"workflow_node.deploy.form.qiniu_kodo_domain.placeholder": "请输入七牛云对象存储自定义域名",
"workflow_node.deploy.form.qiniu_pili_hub.label": "七牛云视频直播空间名",
@ -918,6 +920,8 @@
"workflow_node.deploy.form.upyun_cdn_domain.label": "又拍云 CDN 加速域名",
"workflow_node.deploy.form.upyun_cdn_domain.placeholder": "请输入又拍云 CDN 加速域名",
"workflow_node.deploy.form.upyun_file.guide": "由于又拍云未公开相关 API这里将使用网页模拟登录方式部署但无法保证稳定性。如遇又拍云接口变更请到 GitHub 发起 Issue 告知。",
"workflow_node.deploy.form.upyun_file_bucket.label": "又拍云云存储桶名",
"workflow_node.deploy.form.upyun_file_bucket.placeholder": "请输入又拍云云存储桶名",
"workflow_node.deploy.form.upyun_file_domain.label": "又拍云云存储自定义域名",
"workflow_node.deploy.form.upyun_file_domain.placeholder": "请输入又拍云云存储自定义域名",
"workflow_node.deploy.form.volcengine_alb_resource_type.label": "证书部署方式",
@ -958,8 +962,8 @@
"workflow_node.deploy.form.volcengine_imagex_region.label": "火山引擎 ImageX 服务地域",
"workflow_node.deploy.form.volcengine_imagex_region.placeholder": "请输入火山引擎 ImageX 服务地域例如cn-north-1",
"workflow_node.deploy.form.volcengine_imagex_region.tooltip": "这是什么?请参阅 <a href=\"https://www.volcengine.com/docs/508/23757\" target=\"_blank\">https://www.volcengine.com/docs/508/23757</a>",
"workflow_node.deploy.form.volcengine_imagex_service_id.label": "火山引擎 TOS 服务 ID",
"workflow_node.deploy.form.volcengine_imagex_service_id.placeholder": "请输入火山引擎 TOS 服务 ID",
"workflow_node.deploy.form.volcengine_imagex_service_id.label": "火山引擎 ImageX 服务 ID",
"workflow_node.deploy.form.volcengine_imagex_service_id.placeholder": "请输入火山引擎 ImageX 服务 ID",
"workflow_node.deploy.form.volcengine_imagex_service_id.tooltip": "这是什么?请参阅 <a href=\"https://console.volcengine.com/imagex\" target=\"_blank\">https://console.volcengine.com/imagex</a>",
"workflow_node.deploy.form.volcengine_imagex_domain.label": "火山引擎 ImageX 绑定域名",
"workflow_node.deploy.form.volcengine_imagex_domain.placeholder": "请输入火山引擎 ImageX 绑定域名",