feat: support template variables in webhook deployment

This commit is contained in:
Fu Diwei
2025-01-03 23:06:02 +08:00
parent e695c8ee5c
commit 90058b2dae
10 changed files with 97 additions and 57 deletions

View File

@@ -367,23 +367,9 @@ func createDeployer(target string, accessConfig string, deployConfig map[string]
return nil, nil, fmt.Errorf("failed to unmarshal access config: %w", err)
}
variables := make(map[string]string)
if deployConfig != nil {
value, ok := deployConfig["variables"]
if ok {
kvs := make([]domain.KV, 0)
bts, _ := json.Marshal(value)
if err := json.Unmarshal(bts, &kvs); err == nil {
for _, kv := range kvs {
variables[kv.Key] = kv.Value
}
}
}
}
deployer, err := providerWebhook.NewWithLogger(&providerWebhook.WebhookDeployerConfig{
Url: access.Url,
Variables: variables,
WebhookUrl: access.Url,
WebhookData: maps.GetValueAsString(deployConfig, "webhookData"),
}, logger)
return deployer, logger, err
}

View File

@@ -17,9 +17,3 @@ type DeployConfig struct {
Type string `json:"type"`
Config map[string]any `json:"config"`
}
// Deprecated: TODO: 即将废弃
type KV struct {
Key string `json:"key"`
Value string `json:"value"`
}

View File

@@ -209,8 +209,8 @@ func execSshCommand(sshCli *ssh.Client, command string) (string, string, error)
if err != nil {
return "", "", err
}
defer session.Close()
var stdoutBuf bytes.Buffer
session.Stdout = &stdoutBuf
var stderrBuf bytes.Buffer

View File

@@ -19,9 +19,9 @@ import (
type WebhookDeployerConfig struct {
// Webhook URL。
Url string `json:"url"`
// Webhook 变量字典
Variables map[string]string `json:"variables,omitempty"`
WebhookUrl string `json:"webhookUrl"`
// Webhook 回调数据JSON 格式)
WebhookData string `json:"webhookData,omitempty"`
}
type WebhookDeployer struct {
@@ -67,19 +67,25 @@ func (d *WebhookDeployer) Deploy(ctx context.Context, certPem string, privkeyPem
return nil, xerrors.Wrap(err, "failed to parse x509")
}
// TODO: 自定义回调数据
reqBody, _ := json.Marshal(&webhookData{
SubjectAltNames: strings.Join(certX509.DNSNames, ","),
Certificate: certPem,
PrivateKey: privkeyPem,
Variables: d.config.Variables,
})
resp, err := d.httpClient.Post(d.config.Url, bytes.NewReader(reqBody), map[string][]string{"Content-Type": {"application/json"}})
var webhookData interface{}
err = json.Unmarshal([]byte(d.config.WebhookData), &webhookData)
if err != nil {
return nil, xerrors.Wrap(err, "failed to unmarshall webhook data")
}
replaceJsonValueRecursively(webhookData, "${DOMAIN}", certX509.Subject.CommonName)
replaceJsonValueRecursively(webhookData, "${DOMAINS}", strings.Join(certX509.DNSNames, ";"))
replaceJsonValueRecursively(webhookData, "${SUBJECT_ALT_NAMES}", strings.Join(certX509.DNSNames, ";"))
replaceJsonValueRecursively(webhookData, "${CERTIFICATE}", certPem)
replaceJsonValueRecursively(webhookData, "${PRIVATE_KEY}", privkeyPem)
reqBody, _ := json.Marshal(&webhookData)
resp, err := d.httpClient.Post(d.config.WebhookUrl, bytes.NewReader(reqBody), map[string][]string{"Content-Type": {"application/json"}})
if err != nil {
return nil, xerrors.Wrap(err, "failed to send webhook request")
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, xerrors.Wrap(err, "failed to read response body")
@@ -93,3 +99,19 @@ func (d *WebhookDeployer) Deploy(ctx context.Context, certPem string, privkeyPem
},
}, nil
}
func replaceJsonValueRecursively(data interface{}, oldStr, newStr string) interface{} {
switch v := data.(type) {
case map[string]interface{}:
for k, val := range v {
v[k] = replaceJsonValueRecursively(val, oldStr, newStr)
}
case []interface{}:
for i, val := range v {
v[i] = replaceJsonValueRecursively(val, oldStr, newStr)
}
case string:
return strings.ReplaceAll(v, oldStr, newStr)
}
return data
}

View File

@@ -14,7 +14,8 @@ import (
var (
fInputCertPath string
fInputKeyPath string
fUrl string
fWebhookUrl string
fWebhookData string
)
func init() {
@@ -22,7 +23,8 @@ func init() {
flag.StringVar(&fInputCertPath, argsPrefix+"INPUTCERTPATH", "", "")
flag.StringVar(&fInputKeyPath, argsPrefix+"INPUTKEYPATH", "", "")
flag.StringVar(&fUrl, argsPrefix+"URL", "", "")
flag.StringVar(&fWebhookUrl, argsPrefix+"URL", "", "")
flag.StringVar(&fWebhookData, argsPrefix+"DATA", "", "")
}
/*
@@ -31,7 +33,8 @@ Shell command to run this test:
go test -v webhook_test.go -args \
--CERTIMATE_DEPLOYER_WEBHOOK_INPUTCERTPATH="/path/to/your-input-cert.pem" \
--CERTIMATE_DEPLOYER_WEBHOOK_INPUTKEYPATH="/path/to/your-input-key.pem" \
--CERTIMATE_DEPLOYER_WEBHOOK_URL="https://example.com/your-webhook-url"
--CERTIMATE_DEPLOYER_WEBHOOK_URL="https://example.com/your-webhook-url" \
--CERTIMATE_DEPLOYER_WEBHOOK_DATA="{\"certificate\":\"${Certificate}\",\"privateKey\":\"${PrivateKey}\"}"
*/
func TestDeploy(t *testing.T) {
flag.Parse()
@@ -41,11 +44,13 @@ func TestDeploy(t *testing.T) {
"args:",
fmt.Sprintf("INPUTCERTPATH: %v", fInputCertPath),
fmt.Sprintf("INPUTKEYPATH: %v", fInputKeyPath),
fmt.Sprintf("URL: %v", fUrl),
fmt.Sprintf("WEBHOOKURL: %v", fWebhookUrl),
fmt.Sprintf("WEBHOOKDATA: %v", fWebhookData),
}, "\n"))
deployer, err := provider.New(&provider.WebhookDeployerConfig{
Url: fUrl,
WebhookUrl: fWebhookUrl,
WebhookData: fWebhookData,
})
if err != nil {
t.Errorf("err: %+v", err)