feat: add safeline deployer
This commit is contained in:
@@ -89,10 +89,10 @@ func (d *HuaweiCloudWAFDeployer) Deploy(ctx context.Context, certPem string, pri
|
||||
upres, err := d.sslUploader.Upload(ctx, certPem, privkeyPem)
|
||||
if err != nil {
|
||||
return nil, xerrors.Wrap(err, "failed to upload certificate file")
|
||||
} else {
|
||||
d.logger.Logt("certificate file uploaded", upres)
|
||||
}
|
||||
|
||||
d.logger.Logt("certificate file uploaded", upres)
|
||||
|
||||
// 根据部署资源类型决定部署方式
|
||||
switch d.config.ResourceType {
|
||||
case RESOURCE_TYPE_CERTIFICATE:
|
||||
|
||||
8
internal/pkg/core/deployer/providers/safeline/consts.go
Normal file
8
internal/pkg/core/deployer/providers/safeline/consts.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package safeline
|
||||
|
||||
type ResourceType string
|
||||
|
||||
const (
|
||||
// 资源类型:替换指定证书。
|
||||
RESOURCE_TYPE_CERTIFICATE = ResourceType("certificate")
|
||||
)
|
||||
102
internal/pkg/core/deployer/providers/safeline/safeline.go
Normal file
102
internal/pkg/core/deployer/providers/safeline/safeline.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package safeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
xerrors "github.com/pkg/errors"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/core/deployer"
|
||||
"github.com/usual2970/certimate/internal/pkg/core/logger"
|
||||
safelinesdk "github.com/usual2970/certimate/internal/pkg/vendors/safeline-sdk"
|
||||
)
|
||||
|
||||
type SafeLineDeployerConfig struct {
|
||||
// 雷池 URL。
|
||||
ApiUrl string `json:"apiUrl"`
|
||||
// 雷池 API Token。
|
||||
ApiToken string `json:"apiToken"`
|
||||
// 部署资源类型。
|
||||
ResourceType ResourceType `json:"resourceType"`
|
||||
// 证书 ID。
|
||||
// 部署资源类型为 [RESOURCE_TYPE_CERTIFICATE] 时必填。
|
||||
CertificateId int32 `json:"certificateId,omitempty"`
|
||||
}
|
||||
|
||||
type SafeLineDeployer struct {
|
||||
config *SafeLineDeployerConfig
|
||||
logger logger.Logger
|
||||
sdkClient *safelinesdk.SafeLineClient
|
||||
}
|
||||
|
||||
var _ deployer.Deployer = (*SafeLineDeployer)(nil)
|
||||
|
||||
func New(config *SafeLineDeployerConfig) (*SafeLineDeployer, error) {
|
||||
return NewWithLogger(config, logger.NewNilLogger())
|
||||
}
|
||||
|
||||
func NewWithLogger(config *SafeLineDeployerConfig, logger logger.Logger) (*SafeLineDeployer, error) {
|
||||
if config == nil {
|
||||
return nil, errors.New("config is nil")
|
||||
}
|
||||
|
||||
if logger == nil {
|
||||
return nil, errors.New("logger is nil")
|
||||
}
|
||||
|
||||
client, err := createSdkClient(config.ApiUrl, config.ApiToken)
|
||||
if err != nil {
|
||||
return nil, xerrors.Wrap(err, "failed to create sdk clients")
|
||||
}
|
||||
|
||||
return &SafeLineDeployer{
|
||||
logger: logger,
|
||||
config: config,
|
||||
sdkClient: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *SafeLineDeployer) Deploy(ctx context.Context, certPem string, privkeyPem string) (*deployer.DeployResult, error) {
|
||||
// 根据部署资源类型决定部署方式
|
||||
switch d.config.ResourceType {
|
||||
case RESOURCE_TYPE_CERTIFICATE:
|
||||
if err := d.deployToCertificate(ctx, certPem, privkeyPem); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported resource type: %s", d.config.ResourceType)
|
||||
}
|
||||
|
||||
return &deployer.DeployResult{}, nil
|
||||
}
|
||||
|
||||
func (d *SafeLineDeployer) deployToCertificate(ctx context.Context, certPem string, privkeyPem string) error {
|
||||
if d.config.CertificateId == 0 {
|
||||
return errors.New("config `certificateId` is required")
|
||||
}
|
||||
|
||||
// 更新证书
|
||||
updateCertificateReq := &safelinesdk.UpdateCertificateRequest{
|
||||
Id: d.config.CertificateId,
|
||||
Type: 2,
|
||||
Manual: &safelinesdk.UpdateCertificateRequestBodyManul{
|
||||
Crt: certPem,
|
||||
Key: privkeyPem,
|
||||
},
|
||||
}
|
||||
updateCertificateResp, err := d.sdkClient.UpdateCertificate(updateCertificateReq)
|
||||
if err != nil {
|
||||
return xerrors.Wrap(err, "failed to execute sdk request 'safeline.UpdateCertificate'")
|
||||
} else {
|
||||
d.logger.Logt("已更新证书", updateCertificateResp)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func createSdkClient(apiUrl, apiToken string) (*safelinesdk.SafeLineClient, error) {
|
||||
client := safelinesdk.NewSafeLineClient(apiUrl, apiToken)
|
||||
return client, nil
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package safeline_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
provider "github.com/usual2970/certimate/internal/pkg/core/deployer/providers/safeline"
|
||||
)
|
||||
|
||||
var (
|
||||
fInputCertPath string
|
||||
fInputKeyPath string
|
||||
fApiUrl string
|
||||
fApiToken string
|
||||
fCertificateId int
|
||||
)
|
||||
|
||||
func init() {
|
||||
argsPrefix := "CERTIMATE_DEPLOYER_SAFELINE_"
|
||||
|
||||
flag.StringVar(&fInputCertPath, argsPrefix+"INPUTCERTPATH", "", "")
|
||||
flag.StringVar(&fInputKeyPath, argsPrefix+"INPUTKEYPATH", "", "")
|
||||
flag.StringVar(&fApiUrl, argsPrefix+"APIURL", "", "")
|
||||
flag.StringVar(&fApiToken, argsPrefix+"APITOKEN", "", "")
|
||||
flag.IntVar(&fCertificateId, argsPrefix+"CERTIFICATEID", 0, "")
|
||||
}
|
||||
|
||||
/*
|
||||
Shell command to run this test:
|
||||
|
||||
go test -v ./safeline_test.go -args \
|
||||
--CERTIMATE_DEPLOYER_SAFELINE_INPUTCERTPATH="/path/to/your-input-cert.pem" \
|
||||
--CERTIMATE_DEPLOYER_SAFELINE_INPUTKEYPATH="/path/to/your-input-key.pem" \
|
||||
--CERTIMATE_DEPLOYER_SAFELINE_APIURL="your-safeline-url" \
|
||||
--CERTIMATE_DEPLOYER_SAFELINE_APITOKEN="your-safeline-api-token" \
|
||||
--CERTIMATE_DEPLOYER_SAFELINE_CERTIFICATEID="your-cerficiate-id"
|
||||
*/
|
||||
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("APIURL: %v", fApiUrl),
|
||||
fmt.Sprintf("APITOKEN: %v", fApiToken),
|
||||
fmt.Sprintf("CERTIFICATEID: %v", fCertificateId),
|
||||
}, "\n"))
|
||||
|
||||
deployer, err := provider.New(&provider.SafeLineDeployerConfig{
|
||||
ApiUrl: fApiUrl,
|
||||
ApiToken: fApiToken,
|
||||
ResourceType: provider.ResourceType("certificate"),
|
||||
CertificateId: fCertificateId,
|
||||
})
|
||||
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)
|
||||
})
|
||||
}
|
||||
8
internal/pkg/vendors/btpanel-sdk/client.go
vendored
8
internal/pkg/vendors/btpanel-sdk/client.go
vendored
@@ -9,8 +9,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/utils/maps"
|
||||
)
|
||||
|
||||
type BaoTaPanelClient struct {
|
||||
@@ -113,11 +111,7 @@ func (c *BaoTaPanelClient) sendRequestWithResult(path string, params map[string]
|
||||
return err
|
||||
}
|
||||
|
||||
jsonResp := make(map[string]any)
|
||||
if err := json.Unmarshal(resp.Body(), &jsonResp); err != nil {
|
||||
return fmt.Errorf("baota: failed to parse response: %w", err)
|
||||
}
|
||||
if err := maps.Populate(jsonResp, &result); err != nil {
|
||||
if err := json.Unmarshal(resp.Body(), &result); err != nil {
|
||||
return fmt.Errorf("baota: failed to parse response: %w", err)
|
||||
}
|
||||
|
||||
|
||||
59
internal/pkg/vendors/gname-sdk/api.go
vendored
59
internal/pkg/vendors/gname-sdk/api.go
vendored
@@ -5,6 +5,19 @@ type BaseResponse interface {
|
||||
GetMsg() string
|
||||
}
|
||||
|
||||
type baseResponse struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
func (r *baseResponse) GetCode() int {
|
||||
return r.Code
|
||||
}
|
||||
|
||||
func (r *baseResponse) GetMsg() string {
|
||||
return r.Msg
|
||||
}
|
||||
|
||||
type AddDomainResolutionRequest struct {
|
||||
ZoneName string `json:"ym"`
|
||||
RecordType string `json:"lx"`
|
||||
@@ -15,17 +28,8 @@ type AddDomainResolutionRequest struct {
|
||||
}
|
||||
|
||||
type AddDomainResolutionResponse struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data int `json:"data"`
|
||||
}
|
||||
|
||||
func (r *AddDomainResolutionResponse) GetCode() int {
|
||||
return r.Code
|
||||
}
|
||||
|
||||
func (r *AddDomainResolutionResponse) GetMsg() string {
|
||||
return r.Msg
|
||||
baseResponse
|
||||
Data int `json:"data"`
|
||||
}
|
||||
|
||||
type ModifyDomainResolutionRequest struct {
|
||||
@@ -39,16 +43,7 @@ type ModifyDomainResolutionRequest struct {
|
||||
}
|
||||
|
||||
type ModifyDomainResolutionResponse struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
func (r *ModifyDomainResolutionResponse) GetCode() int {
|
||||
return r.Code
|
||||
}
|
||||
|
||||
func (r *ModifyDomainResolutionResponse) GetMsg() string {
|
||||
return r.Msg
|
||||
baseResponse
|
||||
}
|
||||
|
||||
type DeleteDomainResolutionRequest struct {
|
||||
@@ -57,16 +52,7 @@ type DeleteDomainResolutionRequest struct {
|
||||
}
|
||||
|
||||
type DeleteDomainResolutionResponse struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
func (r *DeleteDomainResolutionResponse) GetCode() int {
|
||||
return r.Code
|
||||
}
|
||||
|
||||
func (r *DeleteDomainResolutionResponse) GetMsg() string {
|
||||
return r.Msg
|
||||
baseResponse
|
||||
}
|
||||
|
||||
type ListDomainResolutionRequest struct {
|
||||
@@ -76,8 +62,7 @@ type ListDomainResolutionRequest struct {
|
||||
}
|
||||
|
||||
type ListDomainResolutionResponse struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
baseResponse
|
||||
Count int `json:"count"`
|
||||
Data []*ResolutionRecord `json:"data"`
|
||||
Page int `json:"page"`
|
||||
@@ -92,11 +77,3 @@ type ResolutionRecord struct {
|
||||
RecordValue string `json:"jxz"`
|
||||
MX int `json:"mx"`
|
||||
}
|
||||
|
||||
func (r *ListDomainResolutionResponse) GetCode() int {
|
||||
return r.Code
|
||||
}
|
||||
|
||||
func (r *ListDomainResolutionResponse) GetMsg() string {
|
||||
return r.Msg
|
||||
}
|
||||
|
||||
12
internal/pkg/vendors/gname-sdk/client.go
vendored
12
internal/pkg/vendors/gname-sdk/client.go
vendored
@@ -10,8 +10,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/utils/maps"
|
||||
)
|
||||
|
||||
type GnameClient struct {
|
||||
@@ -131,9 +129,7 @@ func (c *GnameClient) sendRequest(path string, params map[string]any) (*resty.Re
|
||||
resp, err := req.Post(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("gname: failed to send request: %w", err)
|
||||
}
|
||||
|
||||
if resp.IsError() {
|
||||
} else if resp.IsError() {
|
||||
return nil, fmt.Errorf("gname: unexpected status code: %d, %s", resp.StatusCode(), resp.Body())
|
||||
}
|
||||
|
||||
@@ -146,11 +142,7 @@ func (c *GnameClient) sendRequestWithResult(path string, params map[string]any,
|
||||
return err
|
||||
}
|
||||
|
||||
jsonResp := make(map[string]any)
|
||||
if err := json.Unmarshal(resp.Body(), &jsonResp); err != nil {
|
||||
return fmt.Errorf("gname: failed to parse response: %w", err)
|
||||
}
|
||||
if err := maps.Populate(jsonResp, &result); err != nil {
|
||||
if err := json.Unmarshal(resp.Body(), &result); err != nil {
|
||||
return fmt.Errorf("gname: failed to parse response: %w", err)
|
||||
}
|
||||
|
||||
|
||||
34
internal/pkg/vendors/safeline-sdk/api.go
vendored
Normal file
34
internal/pkg/vendors/safeline-sdk/api.go
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
package safelinesdk
|
||||
|
||||
type BaseResponse interface {
|
||||
GetErrCode() *string
|
||||
GetErrMsg() *string
|
||||
}
|
||||
|
||||
type baseResponse struct {
|
||||
ErrCode *string `json:"err,omitempty"`
|
||||
ErrMsg *string `json:"msg,omitempty"`
|
||||
}
|
||||
|
||||
func (r *baseResponse) GetErrCode() *string {
|
||||
return r.ErrCode
|
||||
}
|
||||
|
||||
func (r *baseResponse) GetErrMsg() *string {
|
||||
return r.ErrMsg
|
||||
}
|
||||
|
||||
type UpdateCertificateRequest struct {
|
||||
Id int32 `json:"id"`
|
||||
Type int32 `json:"type"`
|
||||
Manual *UpdateCertificateRequestBodyManul `json:"manual"`
|
||||
}
|
||||
|
||||
type UpdateCertificateRequestBodyManul struct {
|
||||
Crt string `json:"crt"`
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
type UpdateCertificateResponse struct {
|
||||
baseResponse
|
||||
}
|
||||
87
internal/pkg/vendors/safeline-sdk/client.go
vendored
Normal file
87
internal/pkg/vendors/safeline-sdk/client.go
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
package safelinesdk
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
type SafeLineClient struct {
|
||||
apiHost string
|
||||
apiToken string
|
||||
client *resty.Client
|
||||
}
|
||||
|
||||
func NewSafeLineClient(apiHost, apiToken string) *SafeLineClient {
|
||||
client := resty.New()
|
||||
|
||||
return &SafeLineClient{
|
||||
apiHost: apiHost,
|
||||
apiToken: apiToken,
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *SafeLineClient) WithTimeout(timeout time.Duration) *SafeLineClient {
|
||||
c.client.SetTimeout(timeout)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *SafeLineClient) UpdateCertificate(req *UpdateCertificateRequest) (*UpdateCertificateResponse, error) {
|
||||
params := make(map[string]any)
|
||||
jsonData, _ := json.Marshal(req)
|
||||
json.Unmarshal(jsonData, ¶ms)
|
||||
|
||||
result := UpdateCertificateResponse{}
|
||||
err := c.sendRequestWithResult("/api/open/cert", params, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (c *SafeLineClient) sendRequest(path string, params map[string]any) (*resty.Response, error) {
|
||||
if params == nil {
|
||||
params = make(map[string]any)
|
||||
}
|
||||
|
||||
url := strings.TrimRight(c.apiHost, "/") + path
|
||||
req := c.client.R().
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetHeader("X-SLCE-API-TOKEN", c.apiToken).
|
||||
SetBody(params)
|
||||
resp, err := req.Post(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("safeline: failed to send request: %w", err)
|
||||
}
|
||||
|
||||
if resp.IsError() {
|
||||
return nil, fmt.Errorf("safeline: unexpected status code: %d, %s", resp.StatusCode(), resp.Body())
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *SafeLineClient) sendRequestWithResult(path string, params map[string]any, result BaseResponse) error {
|
||||
resp, err := c.sendRequest(path, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(resp.Body(), &result); err != nil {
|
||||
return fmt.Errorf("safeline: failed to parse response: %w", err)
|
||||
}
|
||||
|
||||
if result.GetErrCode() != nil && *result.GetErrCode() != "" {
|
||||
if result.GetErrMsg() == nil {
|
||||
return fmt.Errorf("safeline api error: %s", *result.GetErrCode())
|
||||
} else {
|
||||
return fmt.Errorf("safeline api error: %s, %s", *result.GetErrCode(), *result.GetErrMsg())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user