fix(region): support ssl delete (#22911)

This commit is contained in:
屈轩
2025-07-16 19:55:43 +08:00
committed by GitHub
parent 49178a2ed4
commit 63fc981e77
11 changed files with 176 additions and 21 deletions
+2 -2
View File
@@ -96,12 +96,12 @@ require (
k8s.io/cri-api v0.22.17
k8s.io/klog/v2 v2.20.0
moul.io/http2curl/v2 v2.3.0
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20250714101342-5bbfacd83769
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20250716105959-0e3f847829d9
yunion.io/x/executor v0.0.0-20250518005516-5402e9e0bed0
yunion.io/x/jsonutils v1.0.1-0.20250507052344-1abcf4f443b1
yunion.io/x/log v1.0.1-0.20240305175729-7cf2d6cd5a91
yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900
yunion.io/x/pkg v1.10.4-0.20250715023532-99a31ea00303
yunion.io/x/pkg v1.10.4-0.20250715170922-d5b8a92716b6
yunion.io/x/s3cli v0.0.0-20241221171442-1c11599d28e1
yunion.io/x/sqlchemy v1.1.3-0.20250531010554-ce98f840b833
yunion.io/x/structarg v0.0.0-20231017124457-df4d5009457c
+4 -4
View File
@@ -1408,8 +1408,8 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20250714101342-5bbfacd83769 h1:UJ6AQIuM3u/Pdvczws+jvHKhGNyNdKXBcGhy3g4zd3Q=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20250714101342-5bbfacd83769/go.mod h1:FXxAEbdNfWXX9gjME3K2nJhkydHY5EKEUZb+RLEzVwQ=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20250716105959-0e3f847829d9 h1:W5X659Gv39oyKkSY6pp20Z/B1Rmoy3q7E6YWy21HSwU=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20250716105959-0e3f847829d9/go.mod h1:FXxAEbdNfWXX9gjME3K2nJhkydHY5EKEUZb+RLEzVwQ=
yunion.io/x/executor v0.0.0-20250518005516-5402e9e0bed0 h1:msG4SiDSVU7CrXH06WuHlNEZXIooTcmNbfrIGHuIHBU=
yunion.io/x/executor v0.0.0-20250518005516-5402e9e0bed0/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws=
yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634=
@@ -1423,8 +1423,8 @@ yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900 h1:Hu/4ERvoWaN6aiFs4h4/yvVB
yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900/go.mod h1:0vLkNEhlmA64HViPBAnSTUMrx5QP1CLsxXmxDKQ80tc=
yunion.io/x/pkg v0.0.0-20190620104149-945c25821dbf/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E=
yunion.io/x/pkg v0.0.0-20190628082551-f4033ba2ea30/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E=
yunion.io/x/pkg v1.10.4-0.20250715023532-99a31ea00303 h1:gbAWm7tVNr0mZjLMJa1A6P85tHsuEpDEEnUHz0+mBhw=
yunion.io/x/pkg v1.10.4-0.20250715023532-99a31ea00303/go.mod h1:0Bwxqd9MA3ACi119/l02FprY/o9gHahmYC2bsSbnVpM=
yunion.io/x/pkg v1.10.4-0.20250715170922-d5b8a92716b6 h1:bcvKeB+j9oc1wS7/AjonkUlh+Dl6HSw/hfQuFI2JUXw=
yunion.io/x/pkg v1.10.4-0.20250715170922-d5b8a92716b6/go.mod h1:0Bwxqd9MA3ACi119/l02FprY/o9gHahmYC2bsSbnVpM=
yunion.io/x/s3cli v0.0.0-20241221171442-1c11599d28e1 h1:1KJ3YYinydPHpDEQRXdr/T8SYcKZ5Er+m489H+PnaQ4=
yunion.io/x/s3cli v0.0.0-20241221171442-1c11599d28e1/go.mod h1:0iFKpOs1y4lbCxeOmq3Xx/0AcQoewVPwj62eRluioEo=
yunion.io/x/sqlchemy v1.1.3-0.20250531010554-ce98f840b833 h1:XTFC1naKYkciCQDLm9izpzHXfTenmmtYsTpVKrsN5hE=
+46
View File
@@ -331,6 +331,28 @@ func (s *SSSLCertificate) syncRemoveCloudSSLCertificate(ctx context.Context, use
return nil
}
// 删除证书
func (s *SSSLCertificate) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input jsonutils.JSONObject) error {
err := s.StartSSLCertificateDeleteTask(ctx, userCred, nil)
if err != nil {
return errors.Wrapf(err, "StartSSLCertificateDeleteTask")
}
return nil
}
func (s *SSSLCertificate) StartSSLCertificateDeleteTask(ctx context.Context, userCred mcclient.TokenCredential, params *jsonutils.JSONDict) error {
task, err := taskman.TaskManager.NewTask(ctx, "SSLCertificateDeleteTask", s, userCred, params, "", "", nil)
if err != nil {
return errors.Wrapf(err, "NewTask")
}
s.SetStatus(ctx, userCred, apis.STATUS_DELETING, "")
return task.ScheduleRun(nil)
}
func (s *SSSLCertificate) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
return nil
}
func (s *SSSLCertificate) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error {
return s.SVirtualResourceBase.Delete(ctx, userCred)
}
@@ -457,6 +479,30 @@ func (r *SCloudprovider) newFromCloudSSLCertificate(
return &s, nil
}
func (s *SSSLCertificate) GetICloudSSLCertificate(ctx context.Context) (cloudprovider.ICloudSSLCertificate, error) {
if len(s.ExternalId) == 0 {
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "ExternalId is empty")
}
provider := s.GetCloudprovider()
if provider == nil {
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "Cloudprovider is empty")
}
drv, err := provider.GetProvider(ctx)
if err != nil {
return nil, errors.Wrapf(err, "GetProvider")
}
certs, err := drv.GetISSLCertificates()
if err != nil {
return nil, errors.Wrapf(err, "GetICloudSSLCertificate")
}
for i := range certs {
if certs[i].GetGlobalId() == s.ExternalId {
return certs[i], nil
}
}
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "GetICloudSSLCertificate")
}
func (man *SSSLCertificateManager) ListItemExportKeys(ctx context.Context,
q *sqlchemy.SQuery,
userCred mcclient.TokenCredential,
@@ -98,7 +98,7 @@ func (self *SSLCertificateCreateTask) OnInit(ctx context.Context, obj db.IStanda
addr := ""
switch sc.Issuer {
case api.SSL_ISSUER_LETSENCRYPT:
addr = acme.LetsEncryptStaging
addr = acme.LetsEncryptProduction
case api.SSL_ISSUER_ZEROSSL:
addr = acme.ZeroSSLProduction
}
@@ -254,17 +254,19 @@ func (self *SSLCertificateCreateTask) OnInit(ctx context.Context, obj db.IStanda
start, end, country, province, city := time.Time{}, time.Time{}, "", "", ""
var pemData []string
for _, c := range certs {
start = c.NotBefore
end = c.NotAfter
if len(c.Subject.Country) > 0 {
country = c.Subject.Country[0]
}
if len(c.Subject.Province) > 0 {
province = c.Subject.Province[0]
}
if len(c.Subject.Locality) > 0 {
city = c.Subject.Locality[0]
for i, c := range certs {
if i == 0 {
start = c.NotBefore
end = c.NotAfter
if len(c.Subject.Country) > 0 {
country = c.Subject.Country[0]
}
if len(c.Subject.Province) > 0 {
province = c.Subject.Province[0]
}
if len(c.Subject.Locality) > 0 {
city = c.Subject.Locality[0]
}
}
pemData = append(pemData, strings.TrimSpace(string(pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
@@ -294,6 +296,7 @@ func (self *SSLCertificateCreateTask) OnInit(ctx context.Context, obj db.IStanda
return errors.Wrapf(err, "GetProvider")
}
opts := &cloudprovider.SSLCertificateCreateOptions{
Name: sc.Name,
DnsZoneId: zone.ExternalId,
Certificate: sc.Certificate,
PrivateKey: sc.PrivateKey,
@@ -0,0 +1,75 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ssl_certificate
import (
"context"
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/apis"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/util/logclient"
)
type SSLCertificateDeleteTask struct {
taskman.STask
}
func init() {
taskman.RegisterTask(SSLCertificateDeleteTask{})
}
func (self *SSLCertificateDeleteTask) taskFailed(ctx context.Context, cert *models.SSSLCertificate, err error) {
cert.SetStatus(ctx, self.UserCred, apis.STATUS_DELETE_FAILED, err.Error())
logclient.AddActionLogWithStartable(self, cert, logclient.ACT_DELOCATE, err, self.UserCred, false)
self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
}
func (self *SSLCertificateDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
cert := obj.(*models.SSSLCertificate)
iCert, err := cert.GetICloudSSLCertificate(ctx)
if err != nil {
if errors.Cause(err) == cloudprovider.ErrNotFound {
self.taskComplete(ctx, cert)
return
}
self.taskFailed(ctx, cert, errors.Wrapf(err, "cert.GetICloudSSLCertificate"))
return
}
err = iCert.Delete()
if err != nil {
self.taskFailed(ctx, cert, errors.Wrapf(err, "iCert.Delete"))
return
}
self.taskComplete(ctx, cert)
}
func (self *SSLCertificateDeleteTask) taskComplete(ctx context.Context, cert *models.SSSLCertificate) {
cert.RealDelete(ctx, self.GetUserCred())
notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
Obj: cert,
Action: notifyclient.ActionDelete,
})
self.SetStageComplete(ctx, nil)
}
+2 -2
View File
@@ -1852,7 +1852,7 @@ sigs.k8s.io/structured-merge-diff/v4/value
# sigs.k8s.io/yaml v1.2.0
## explicit; go 1.12
sigs.k8s.io/yaml
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20250714101342-5bbfacd83769
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20250716105959-0e3f847829d9
## explicit; go 1.21
yunion.io/x/cloudmux/pkg/apis
yunion.io/x/cloudmux/pkg/apis/billing
@@ -1949,7 +1949,7 @@ yunion.io/x/log/hooks
yunion.io/x/ovsdb/cli_util
yunion.io/x/ovsdb/schema/ovn_nb
yunion.io/x/ovsdb/types
# yunion.io/x/pkg v1.10.4-0.20250715023532-99a31ea00303
# yunion.io/x/pkg v1.10.4-0.20250715170922-d5b8a92716b6
## explicit; go 1.18
yunion.io/x/pkg/appctx
yunion.io/x/pkg/errors
+2
View File
@@ -1822,6 +1822,8 @@ type ICloudSSLCertificate interface {
GetCert() string
GetKey() string
GetDnsZoneId() string
Delete() error
}
type IAiGateway interface {
+1
View File
@@ -20,6 +20,7 @@ const (
)
type SSLCertificateCreateOptions struct {
Name string
DnsZoneId string
Certificate string
PrivateKey string
+9
View File
@@ -14,6 +14,11 @@
package multicloud
import (
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/pkg/errors"
)
type SCertificateBase struct {
SVirtualResourceBase
}
@@ -21,3 +26,7 @@ type SCertificateBase struct {
func (cert *SCertificateBase) GetDnsZoneId() string {
return ""
}
func (cert *SCertificateBase) Delete() error {
return errors.Wrapf(cloudprovider.ErrNotImplemented, "Delete")
}
+1 -1
View File
@@ -129,7 +129,7 @@ func (addr *SAddress) GetAssociationType() string {
return api.EIP_ASSOCIATE_TYPE_SERVER
}
for _, user := range addr.Users {
if strings.HasPrefix(user, "/instances/") {
if strings.Contains(user, "/instances/") {
return api.EIP_ASSOCIATE_TYPE_SERVER
}
if strings.Contains(user, "/forwardingRules/") {
+19
View File
@@ -95,3 +95,22 @@ func ContainsWord(str, w string) bool {
reg := regexp.MustCompile(fmt.Sprintf("\\b%s\\b", w))
return reg.MatchString(str)
}
func byte2hex(b byte) byte {
if b >= 0 && b <= 9 {
return b + 0x30
}
if b >= 10 && b <= 15 {
return b - 10 + 0x61
}
return '?'
}
func Bytes2Str(b []byte) string {
buf := strings.Builder{}
for i := range b {
buf.WriteByte(byte2hex((b[i] & 0xf0) >> 4))
buf.WriteByte(byte2hex(b[i] & 0x0f))
}
return buf.String()
}