chore: move '/internal/pkg' to '/pkg'
This commit is contained in:
81
pkg/core/ssl-deployer/providers/bunny-cdn/bunny_cdn.go
Normal file
81
pkg/core/ssl-deployer/providers/bunny-cdn/bunny_cdn.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package bunnycdn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/usual2970/certimate/pkg/core"
|
||||
bunnysdk "github.com/usual2970/certimate/pkg/sdk3rd/bunny"
|
||||
)
|
||||
|
||||
type SSLDeployerProviderConfig struct {
|
||||
// Bunny API Key。
|
||||
ApiKey string `json:"apiKey"`
|
||||
// Bunny Pull Zone ID。
|
||||
PullZoneId string `json:"pullZoneId"`
|
||||
// Bunny CDN Hostname(支持泛域名)。
|
||||
Hostname string `json:"hostname"`
|
||||
}
|
||||
|
||||
type SSLDeployerProvider struct {
|
||||
config *SSLDeployerProviderConfig
|
||||
logger *slog.Logger
|
||||
sdkClient *bunnysdk.Client
|
||||
}
|
||||
|
||||
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.ApiKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not create sdk client: %w", err)
|
||||
}
|
||||
|
||||
return &SSLDeployerProvider{
|
||||
config: config,
|
||||
logger: slog.Default(),
|
||||
sdkClient: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *SSLDeployerProvider) SetLogger(logger *slog.Logger) {
|
||||
if logger == nil {
|
||||
d.logger = slog.New(slog.DiscardHandler)
|
||||
} else {
|
||||
d.logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
func (d *SSLDeployerProvider) Deploy(ctx context.Context, certPEM string, privkeyPEM string) (*core.SSLDeployResult, error) {
|
||||
if d.config.PullZoneId == "" {
|
||||
return nil, fmt.Errorf("config `pullZoneId` is required")
|
||||
}
|
||||
if d.config.Hostname == "" {
|
||||
return nil, fmt.Errorf("config `hostname` is required")
|
||||
}
|
||||
|
||||
// 上传证书
|
||||
createCertificateReq := &bunnysdk.AddCustomCertificateRequest{
|
||||
Hostname: d.config.Hostname,
|
||||
Certificate: base64.StdEncoding.EncodeToString([]byte(certPEM)),
|
||||
CertificateKey: base64.StdEncoding.EncodeToString([]byte(privkeyPEM)),
|
||||
}
|
||||
err := d.sdkClient.AddCustomCertificate(d.config.PullZoneId, createCertificateReq)
|
||||
d.logger.Debug("sdk request 'bunny.AddCustomCertificate'", slog.Any("request", createCertificateReq))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute sdk request 'bunny.AddCustomCertificate': %w", err)
|
||||
}
|
||||
|
||||
return &core.SSLDeployResult{}, nil
|
||||
}
|
||||
|
||||
func createSDKClient(apiKey string) (*bunnysdk.Client, error) {
|
||||
return bunnysdk.NewClient(apiKey)
|
||||
}
|
||||
75
pkg/core/ssl-deployer/providers/bunny-cdn/bunny_cdn_test.go
Normal file
75
pkg/core/ssl-deployer/providers/bunny-cdn/bunny_cdn_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package bunnycdn_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
provider "github.com/usual2970/certimate/pkg/core/ssl-deployer/providers/bunny-cdn"
|
||||
)
|
||||
|
||||
var (
|
||||
fInputCertPath string
|
||||
fInputKeyPath string
|
||||
fApiKey string
|
||||
fPullZoneId string
|
||||
fHostName string
|
||||
)
|
||||
|
||||
func init() {
|
||||
argsPrefix := "CERTIMATE_SSLDEPLOYER_BUNNYCDN_"
|
||||
|
||||
flag.StringVar(&fInputCertPath, argsPrefix+"INPUTCERTPATH", "", "")
|
||||
flag.StringVar(&fInputKeyPath, argsPrefix+"INPUTKEYPATH", "", "")
|
||||
flag.StringVar(&fApiKey, argsPrefix+"APIKEY", "", "")
|
||||
flag.StringVar(&fPullZoneId, argsPrefix+"PULLZONEID", "", "")
|
||||
flag.StringVar(&fHostName, argsPrefix+"HOSTNAME", "", "")
|
||||
}
|
||||
|
||||
/*
|
||||
Shell command to run this test:
|
||||
|
||||
go test -v ./bunny_cdn_test.go -args \
|
||||
--CERTIMATE_SSLDEPLOYER_BUNNYCDN_INPUTCERTPATH="/path/to/your-input-cert.pem" \
|
||||
--CERTIMATE_SSLDEPLOYER_BUNNYCDN_INPUTKEYPATH="/path/to/your-input-key.pem" \
|
||||
--CERTIMATE_SSLDEPLOYER_BUNNYCDN_APITOKEN="your-api-token" \
|
||||
--CERTIMATE_SSLDEPLOYER_BUNNYCDN_PULLZONEID="your-pull-zone-id" \
|
||||
--CERTIMATE_SSLDEPLOYER_BUNNYCDN_HOSTNAME="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("APIKEY: %v", fApiKey),
|
||||
fmt.Sprintf("PULLZONEID: %v", fPullZoneId),
|
||||
fmt.Sprintf("HOSTNAME: %v", fHostName),
|
||||
}, "\n"))
|
||||
|
||||
deployer, err := provider.NewSSLDeployerProvider(&provider.SSLDeployerProviderConfig{
|
||||
ApiKey: fApiKey,
|
||||
PullZoneId: fPullZoneId,
|
||||
Hostname: fHostName,
|
||||
})
|
||||
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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user