mirror of
https://github.com/usual2970/certimate.git
synced 2025-10-05 05:54:53 +00:00
refactor: clean code
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
aliyunCas "github.com/alibabacloud-go/cas-20200407/v3/client"
|
||||
aliyunOpen "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
xerrors "github.com/pkg/errors"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/utils/x509"
|
||||
)
|
||||
@@ -31,7 +32,7 @@ func NewAliyunCASUploader(config *AliyunCASUploaderConfig) (Uploader, error) {
|
||||
config.Region,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create sdk client: %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to create sdk client")
|
||||
}
|
||||
|
||||
return &AliyunCASUploader{
|
||||
@@ -60,7 +61,7 @@ func (u *AliyunCASUploader) Upload(ctx context.Context, certPem string, privkeyP
|
||||
}
|
||||
listUserCertificateOrderResp, err := u.sdkClient.ListUserCertificateOrder(listUserCertificateOrderReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'cas.ListUserCertificateOrder': %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to execute sdk request 'cas.ListUserCertificateOrder'")
|
||||
}
|
||||
|
||||
if listUserCertificateOrderResp.Body.CertificateOrderList != nil {
|
||||
@@ -71,7 +72,7 @@ func (u *AliyunCASUploader) Upload(ctx context.Context, certPem string, privkeyP
|
||||
}
|
||||
getUserCertificateDetailResp, err := u.sdkClient.GetUserCertificateDetail(getUserCertificateDetailReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'cas.GetUserCertificateDetail': %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to execute sdk request 'cas.GetUserCertificateDetail'")
|
||||
}
|
||||
|
||||
var isSameCert bool
|
||||
@@ -120,7 +121,7 @@ func (u *AliyunCASUploader) Upload(ctx context.Context, certPem string, privkeyP
|
||||
}
|
||||
uploadUserCertificateResp, err := u.sdkClient.UploadUserCertificate(uploadUserCertificateReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'cas.UploadUserCertificate': %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to execute sdk request 'cas.UploadUserCertificate'")
|
||||
}
|
||||
|
||||
certId = fmt.Sprintf("%d", tea.Int64Value(uploadUserCertificateResp.Body.CertId))
|
||||
|
@@ -11,6 +11,7 @@ import (
|
||||
aliyunOpen "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||||
aliyunSlb "github.com/alibabacloud-go/slb-20140515/v4/client"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
xerrors "github.com/pkg/errors"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/utils/x509"
|
||||
)
|
||||
@@ -33,7 +34,7 @@ func NewAliyunSLBUploader(config *AliyunSLBUploaderConfig) (Uploader, error) {
|
||||
config.Region,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create sdk client: %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to create sdk client")
|
||||
}
|
||||
|
||||
return &AliyunSLBUploader{
|
||||
@@ -56,7 +57,7 @@ func (u *AliyunSLBUploader) Upload(ctx context.Context, certPem string, privkeyP
|
||||
}
|
||||
describeServerCertificatesResp, err := u.sdkClient.DescribeServerCertificates(describeServerCertificatesReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'slb.DescribeServerCertificates': %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to execute sdk request 'slb.DescribeServerCertificates'")
|
||||
}
|
||||
|
||||
if describeServerCertificatesResp.Body.ServerCertificates != nil && describeServerCertificatesResp.Body.ServerCertificates.ServerCertificate != nil {
|
||||
@@ -90,7 +91,7 @@ func (u *AliyunSLBUploader) Upload(ctx context.Context, certPem string, privkeyP
|
||||
}
|
||||
uploadServerCertificateResp, err := u.sdkClient.UploadServerCertificate(uploadServerCertificateReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'slb.UploadServerCertificate': %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to execute sdk request 'slb.UploadServerCertificate'")
|
||||
}
|
||||
|
||||
certId = *uploadServerCertificateResp.Body.ServerCertificateId
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
hcIam "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iam/v3"
|
||||
hcIamModel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iam/v3/model"
|
||||
hcIamRegion "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iam/v3/region"
|
||||
xerrors "github.com/pkg/errors"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/utils/cast"
|
||||
"github.com/usual2970/certimate/internal/pkg/utils/x509"
|
||||
@@ -36,7 +38,7 @@ func NewHuaweiCloudELBUploader(config *HuaweiCloudELBUploaderConfig) (Uploader,
|
||||
config.Region,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create sdk client: %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to create sdk client: %w")
|
||||
}
|
||||
|
||||
return &HuaweiCloudELBUploader{
|
||||
@@ -65,7 +67,7 @@ func (u *HuaweiCloudELBUploader) Upload(ctx context.Context, certPem string, pri
|
||||
}
|
||||
listCertificatesResp, err := u.sdkClient.ListCertificates(listCertificatesReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'elb.ListCertificates': %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to execute sdk request 'elb.ListCertificates'")
|
||||
}
|
||||
|
||||
if listCertificatesResp.Certificates != nil {
|
||||
@@ -107,7 +109,7 @@ func (u *HuaweiCloudELBUploader) Upload(ctx context.Context, certPem string, pri
|
||||
// REF: https://support.huaweicloud.com/api-iam/iam_06_0001.html
|
||||
projectId, err := u.getSdkProjectId(u.config.Region, u.config.AccessKeyId, u.config.SecretAccessKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get SDK project id: %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to get SDK project id")
|
||||
}
|
||||
|
||||
// 生成新证书名(需符合华为云命名规则)
|
||||
@@ -128,7 +130,7 @@ func (u *HuaweiCloudELBUploader) Upload(ctx context.Context, certPem string, pri
|
||||
}
|
||||
createCertificateResp, err := u.sdkClient.CreateCertificate(createCertificateReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'elb.CreateCertificate': %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to execute sdk request 'elb.CreateCertificate'")
|
||||
}
|
||||
|
||||
certId = createCertificateResp.Certificate.Id
|
||||
@@ -207,7 +209,7 @@ func (u *HuaweiCloudELBUploader) getSdkProjectId(accessKeyId, secretAccessKey, r
|
||||
if err != nil {
|
||||
return "", err
|
||||
} else if response.Projects == nil || len(*response.Projects) == 0 {
|
||||
return "", fmt.Errorf("no project found")
|
||||
return "", errors.New("no project found")
|
||||
}
|
||||
|
||||
return (*response.Projects)[0].Id, nil
|
||||
|
@@ -9,6 +9,7 @@ import (
|
||||
hcScm "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/scm/v3"
|
||||
hcScmModel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/scm/v3/model"
|
||||
hcScmRegion "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/scm/v3/region"
|
||||
xerrors "github.com/pkg/errors"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/utils/cast"
|
||||
"github.com/usual2970/certimate/internal/pkg/utils/x509"
|
||||
@@ -32,7 +33,7 @@ func NewHuaweiCloudSCMUploader(config *HuaweiCloudSCMUploaderConfig) (Uploader,
|
||||
config.Region,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create sdk client: %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to create sdk client")
|
||||
}
|
||||
|
||||
return &HuaweiCloudSCMUploader{
|
||||
@@ -63,7 +64,7 @@ func (u *HuaweiCloudSCMUploader) Upload(ctx context.Context, certPem string, pri
|
||||
}
|
||||
listCertificatesResp, err := u.sdkClient.ListCertificates(listCertificatesReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'scm.ListCertificates': %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to execute sdk request 'scm.ListCertificates'")
|
||||
}
|
||||
|
||||
if listCertificatesResp.Certificates != nil {
|
||||
@@ -76,7 +77,7 @@ func (u *HuaweiCloudSCMUploader) Upload(ctx context.Context, certPem string, pri
|
||||
if exportCertificateResp != nil && exportCertificateResp.HttpStatusCode == 404 {
|
||||
continue
|
||||
}
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'scm.ExportCertificate': %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to execute sdk request 'scm.ExportCertificate'")
|
||||
}
|
||||
|
||||
var isSameCert bool
|
||||
@@ -127,7 +128,7 @@ func (u *HuaweiCloudSCMUploader) Upload(ctx context.Context, certPem string, pri
|
||||
}
|
||||
importCertificateResp, err := u.sdkClient.ImportCertificate(importCertificateReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'scm.ImportCertificate': %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to execute sdk request 'scm.ImportCertificate'")
|
||||
}
|
||||
|
||||
certId = *importCertificateResp.CertificateId
|
||||
|
@@ -5,15 +5,13 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
xerrors "github.com/pkg/errors"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
||||
tcSsl "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssl/v20191205"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/utils/cast"
|
||||
)
|
||||
|
||||
type TencentCloudSSLUploaderConfig struct {
|
||||
Region string `json:"region"`
|
||||
SecretId string `json:"secretId"`
|
||||
SecretKey string `json:"secretKey"`
|
||||
}
|
||||
@@ -25,12 +23,11 @@ type TencentCloudSSLUploader struct {
|
||||
|
||||
func NewTencentCloudSSLUploader(config *TencentCloudSSLUploaderConfig) (Uploader, error) {
|
||||
client, err := (&TencentCloudSSLUploader{}).createSdkClient(
|
||||
config.Region,
|
||||
config.SecretId,
|
||||
config.SecretKey,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create sdk client: %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to create sdk client")
|
||||
}
|
||||
|
||||
return &TencentCloudSSLUploader{
|
||||
@@ -40,33 +37,38 @@ func NewTencentCloudSSLUploader(config *TencentCloudSSLUploaderConfig) (Uploader
|
||||
}
|
||||
|
||||
func (u *TencentCloudSSLUploader) Upload(ctx context.Context, certPem string, privkeyPem string) (res *UploadResult, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Printf("Recovered from panic: %+v", r)
|
||||
fmt.Println()
|
||||
}
|
||||
}()
|
||||
|
||||
// 生成新证书名(需符合腾讯云命名规则)
|
||||
var certId, certName string
|
||||
certName = fmt.Sprintf("certimate-%d", time.Now().UnixMilli())
|
||||
|
||||
// 上传新证书
|
||||
// REF: https://cloud.tencent.com/document/product/400/41665
|
||||
uploadCertificateReq := &tcSsl.UploadCertificateRequest{
|
||||
Alias: cast.StringPtr(certName),
|
||||
CertificatePublicKey: cast.StringPtr(certPem),
|
||||
CertificatePrivateKey: cast.StringPtr(privkeyPem),
|
||||
Repeatable: cast.BoolPtr(false),
|
||||
}
|
||||
uploadCertificateReq := tcSsl.NewUploadCertificateRequest()
|
||||
uploadCertificateReq.Alias = common.StringPtr(certName)
|
||||
uploadCertificateReq.CertificatePublicKey = common.StringPtr(certPem)
|
||||
uploadCertificateReq.CertificatePrivateKey = common.StringPtr(privkeyPem)
|
||||
uploadCertificateReq.Repeatable = common.BoolPtr(false)
|
||||
uploadCertificateResp, err := u.sdkClient.UploadCertificate(uploadCertificateReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'ssl.UploadCertificate': %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to execute sdk request 'ssl.UploadCertificate'")
|
||||
}
|
||||
|
||||
// 获取证书详情
|
||||
// REF: https://cloud.tencent.com/document/api/400/41673
|
||||
//
|
||||
// P.S. 上传重复证书会返回上一次的证书 ID,这里需要重新获取一遍证书名(https://github.com/usual2970/certimate/pull/227)
|
||||
describeCertificateDetailReq := &tcSsl.DescribeCertificateDetailRequest{
|
||||
CertificateId: uploadCertificateResp.Response.CertificateId,
|
||||
}
|
||||
describeCertificateDetailReq := tcSsl.NewDescribeCertificateDetailRequest()
|
||||
describeCertificateDetailReq.CertificateId = uploadCertificateResp.Response.CertificateId
|
||||
describeCertificateDetailResp, err := u.sdkClient.DescribeCertificateDetail(describeCertificateDetailReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'ssl.DescribeCertificateDetail': %w", err)
|
||||
return nil, xerrors.Wrap(err, "failed to execute sdk request 'ssl.DescribeCertificateDetail'")
|
||||
}
|
||||
|
||||
certId = *describeCertificateDetailResp.Response.CertificateId
|
||||
@@ -77,13 +79,9 @@ func (u *TencentCloudSSLUploader) Upload(ctx context.Context, certPem string, pr
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (u *TencentCloudSSLUploader) createSdkClient(region, secretId, secretKey string) (*tcSsl.Client, error) {
|
||||
if region == "" {
|
||||
region = "ap-guangzhou" // SSL 服务默认区域:广州
|
||||
}
|
||||
|
||||
func (u *TencentCloudSSLUploader) createSdkClient(secretId, secretKey string) (*tcSsl.Client, error) {
|
||||
credential := common.NewCredential(secretId, secretKey)
|
||||
client, err := tcSsl.NewClient(credential, region, profile.NewClientProfile())
|
||||
client, err := tcSsl.NewClient(credential, "", profile.NewClientProfile())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user