feat: adapt new logging to uploader, deployer and notifier providers
This commit is contained in:
@@ -8,16 +8,17 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
xerrors "github.com/pkg/errors"
|
||||
usdk "github.com/ucloud/ucloud-sdk-go/ucloud"
|
||||
uAuth "github.com/ucloud/ucloud-sdk-go/ucloud/auth"
|
||||
"github.com/ucloud/ucloud-sdk-go/ucloud"
|
||||
ucloudauth "github.com/ucloud/ucloud-sdk-go/ucloud/auth"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/core/uploader"
|
||||
"github.com/usual2970/certimate/internal/pkg/utils/certs"
|
||||
usdkSsl "github.com/usual2970/certimate/internal/pkg/vendors/ucloud-sdk/ussl"
|
||||
usslsdk "github.com/usual2970/certimate/internal/pkg/vendors/ucloud-sdk/ussl"
|
||||
)
|
||||
|
||||
type UploaderConfig struct {
|
||||
@@ -31,7 +32,8 @@ type UploaderConfig struct {
|
||||
|
||||
type UploaderProvider struct {
|
||||
config *UploaderConfig
|
||||
sdkClient *usdkSsl.USSLClient
|
||||
logger *slog.Logger
|
||||
sdkClient *usslsdk.USSLClient
|
||||
}
|
||||
|
||||
var _ uploader.Uploader = (*UploaderProvider)(nil)
|
||||
@@ -48,10 +50,20 @@ func NewUploader(config *UploaderConfig) (*UploaderProvider, error) {
|
||||
|
||||
return &UploaderProvider{
|
||||
config: config,
|
||||
logger: slog.Default(),
|
||||
sdkClient: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (u *UploaderProvider) WithLogger(logger *slog.Logger) uploader.Uploader {
|
||||
if logger == nil {
|
||||
u.logger = slog.Default()
|
||||
} else {
|
||||
u.logger = logger
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
func (u *UploaderProvider) Upload(ctx context.Context, certPem string, privkeyPem string) (res *uploader.UploadResult, err error) {
|
||||
// 生成新证书名(需符合优刻得命名规则)
|
||||
var certId, certName string
|
||||
@@ -66,14 +78,15 @@ func (u *UploaderProvider) Upload(ctx context.Context, certPem string, privkeyPe
|
||||
// 上传托管证书
|
||||
// REF: https://docs.ucloud.cn/api/usslcertificate-api/upload_normal_certificate
|
||||
uploadNormalCertificateReq := u.sdkClient.NewUploadNormalCertificateRequest()
|
||||
uploadNormalCertificateReq.CertificateName = usdk.String(certName)
|
||||
uploadNormalCertificateReq.SslPublicKey = usdk.String(certPemBase64)
|
||||
uploadNormalCertificateReq.SslPrivateKey = usdk.String(privkeyPemBase64)
|
||||
uploadNormalCertificateReq.SslMD5 = usdk.String(certMd5Hex)
|
||||
uploadNormalCertificateReq.CertificateName = ucloud.String(certName)
|
||||
uploadNormalCertificateReq.SslPublicKey = ucloud.String(certPemBase64)
|
||||
uploadNormalCertificateReq.SslPrivateKey = ucloud.String(privkeyPemBase64)
|
||||
uploadNormalCertificateReq.SslMD5 = ucloud.String(certMd5Hex)
|
||||
if u.config.ProjectId != "" {
|
||||
uploadNormalCertificateReq.ProjectId = usdk.String(u.config.ProjectId)
|
||||
uploadNormalCertificateReq.ProjectId = ucloud.String(u.config.ProjectId)
|
||||
}
|
||||
uploadNormalCertificateResp, err := u.sdkClient.UploadNormalCertificate(uploadNormalCertificateReq)
|
||||
u.logger.Debug("sdk request 'ussl.UploadNormalCertificate'", slog.Any("request", uploadNormalCertificateReq), slog.Any("response", uploadNormalCertificateResp))
|
||||
if err != nil {
|
||||
if uploadNormalCertificateResp != nil && uploadNormalCertificateResp.GetRetCode() == 80035 {
|
||||
if res, err := u.getExistCert(ctx, certPem); err != nil {
|
||||
@@ -81,6 +94,7 @@ func (u *UploaderProvider) Upload(ctx context.Context, certPem string, privkeyPe
|
||||
} else if res == nil {
|
||||
return nil, errors.New("no certificate found")
|
||||
} else {
|
||||
u.logger.Info("ssl certificate already exists")
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
@@ -92,7 +106,7 @@ func (u *UploaderProvider) Upload(ctx context.Context, certPem string, privkeyPe
|
||||
return &uploader.UploadResult{
|
||||
CertId: certId,
|
||||
CertName: certName,
|
||||
ExtendedData: map[string]interface{}{
|
||||
ExtendedData: map[string]any{
|
||||
"resourceId": uploadNormalCertificateResp.LongResourceID,
|
||||
},
|
||||
}, nil
|
||||
@@ -112,15 +126,16 @@ func (u *UploaderProvider) getExistCert(ctx context.Context, certPem string) (re
|
||||
getCertificateListLimit := int(1000)
|
||||
for {
|
||||
getCertificateListReq := u.sdkClient.NewGetCertificateListRequest()
|
||||
getCertificateListReq.Mode = usdk.String("trust")
|
||||
getCertificateListReq.Domain = usdk.String(certX509.Subject.CommonName)
|
||||
getCertificateListReq.Sort = usdk.String("2")
|
||||
getCertificateListReq.Page = usdk.Int(getCertificateListPage)
|
||||
getCertificateListReq.PageSize = usdk.Int(getCertificateListLimit)
|
||||
getCertificateListReq.Mode = ucloud.String("trust")
|
||||
getCertificateListReq.Domain = ucloud.String(certX509.Subject.CommonName)
|
||||
getCertificateListReq.Sort = ucloud.String("2")
|
||||
getCertificateListReq.Page = ucloud.Int(getCertificateListPage)
|
||||
getCertificateListReq.PageSize = ucloud.Int(getCertificateListLimit)
|
||||
if u.config.ProjectId != "" {
|
||||
getCertificateListReq.ProjectId = usdk.String(u.config.ProjectId)
|
||||
getCertificateListReq.ProjectId = ucloud.String(u.config.ProjectId)
|
||||
}
|
||||
getCertificateListResp, err := u.sdkClient.GetCertificateList(getCertificateListReq)
|
||||
u.logger.Debug("sdk request 'ussl.GetCertificateList'", slog.Any("request", getCertificateListReq), slog.Any("response", getCertificateListResp))
|
||||
if err != nil {
|
||||
return nil, xerrors.Wrap(err, "failed to execute sdk request 'ussl.GetCertificateList'")
|
||||
}
|
||||
@@ -143,9 +158,9 @@ func (u *UploaderProvider) getExistCert(ctx context.Context, certPem string) (re
|
||||
}
|
||||
|
||||
getCertificateDetailInfoReq := u.sdkClient.NewGetCertificateDetailInfoRequest()
|
||||
getCertificateDetailInfoReq.CertificateID = usdk.Int(certInfo.CertificateID)
|
||||
getCertificateDetailInfoReq.CertificateID = ucloud.Int(certInfo.CertificateID)
|
||||
if u.config.ProjectId != "" {
|
||||
getCertificateDetailInfoReq.ProjectId = usdk.String(u.config.ProjectId)
|
||||
getCertificateDetailInfoReq.ProjectId = ucloud.String(u.config.ProjectId)
|
||||
}
|
||||
getCertificateDetailInfoResp, err := u.sdkClient.GetCertificateDetailInfo(getCertificateDetailInfoReq)
|
||||
if err != nil {
|
||||
@@ -197,7 +212,7 @@ func (u *UploaderProvider) getExistCert(ctx context.Context, certPem string) (re
|
||||
return &uploader.UploadResult{
|
||||
CertId: fmt.Sprintf("%d", certInfo.CertificateID),
|
||||
CertName: certInfo.Name,
|
||||
ExtendedData: map[string]interface{}{
|
||||
ExtendedData: map[string]any{
|
||||
"resourceId": certInfo.CertificateSN,
|
||||
},
|
||||
}, nil
|
||||
@@ -214,13 +229,13 @@ func (u *UploaderProvider) getExistCert(ctx context.Context, certPem string) (re
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func createSdkClient(privateKey, publicKey string) (*usdkSsl.USSLClient, error) {
|
||||
cfg := usdk.NewConfig()
|
||||
func createSdkClient(privateKey, publicKey string) (*usslsdk.USSLClient, error) {
|
||||
cfg := ucloud.NewConfig()
|
||||
|
||||
credential := uAuth.NewCredential()
|
||||
credential := ucloudauth.NewCredential()
|
||||
credential.PrivateKey = privateKey
|
||||
credential.PublicKey = publicKey
|
||||
|
||||
client := usdkSsl.NewClient(&cfg, &credential)
|
||||
client := usslsdk.NewClient(&cfg, &credential)
|
||||
return client, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user