mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 02:37:24 +08:00
fix(region): security group name generate for every cloud platform
This commit is contained in:
@@ -124,6 +124,8 @@ type IRegionDriver interface {
|
||||
RequestCacheSecurityGroup(ctx context.Context, userCred mcclient.TokenCredential, region *SCloudregion, vpc *SVpc, secgroup *SSecurityGroup, classic bool, removeProjectId string, task taskman.ITask) error
|
||||
RequestSyncSecurityGroup(ctx context.Context, userCred mcclient.TokenCredential, vpcId string, vpc *SVpc, secgroup *SSecurityGroup, removeProjectId, service string) (string, error)
|
||||
GetDefaultSecurityGroupInRule() cloudprovider.SecurityRule
|
||||
GenerateSecurityGroupName(name string) string
|
||||
IsAllowSecurityGroupNameRepeat() bool
|
||||
GetDefaultSecurityGroupOutRule() cloudprovider.SecurityRule
|
||||
GetSecurityGroupRuleMaxPriority() int
|
||||
GetSecurityGroupRuleMinPriority() int
|
||||
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
@@ -192,10 +191,11 @@ func (self *SSecurityGroupCache) GetIRegion() (cloudprovider.ICloudRegion, error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if region := CloudregionManager.FetchRegionById(self.CloudregionId); region != nil {
|
||||
return provider.GetIRegionById(region.ExternalId)
|
||||
region, err := self.GetRegion()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetRegion")
|
||||
}
|
||||
return nil, fmt.Errorf("failed to find iregion for secgroupcache %s vpc: %s externalId: %s", self.Id, self.VpcId, self.ExternalId)
|
||||
return provider.GetIRegionById(region.ExternalId)
|
||||
}
|
||||
|
||||
func (manager *SSecurityGroupCacheManager) FilterByOwner(q *sqlchemy.SQuery, userCred mcclient.IIdentityProvider, scope rbacutils.TRbacScope) *sqlchemy.SQuery {
|
||||
@@ -711,32 +711,40 @@ func (self *SSecurityGroupCache) CreateISecurityGroup() (cloudprovider.ICloudSec
|
||||
return nil, errors.Wrapf(err, "self.GetIRegion")
|
||||
}
|
||||
|
||||
if strings.ToLower(self.Name) == "default" { //避免有些云不支持default关键字
|
||||
self.Name = "DefaultGroup"
|
||||
regionDriver, err := self.GetRegionDriver()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "GetRegionDriver")
|
||||
}
|
||||
|
||||
self.Name = regionDriver.GenerateSecurityGroupName(self.Name)
|
||||
|
||||
// 避免有的云不支持重名安全组
|
||||
randomString := func(prefix string, length int) string {
|
||||
return fmt.Sprintf("%s-%s", prefix, rand.String(length))
|
||||
}
|
||||
opts := &cloudprovider.SecurityGroupFilterOptions{
|
||||
Name: randomString(self.Name, 1),
|
||||
VpcId: self.VpcId,
|
||||
ProjectId: self.ExternalProjectId,
|
||||
}
|
||||
for i := 2; i < 30; i++ {
|
||||
_, err := iRegion.GetISecurityGroupByName(opts)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotFound {
|
||||
break
|
||||
}
|
||||
if errors.Cause(err) != cloudprovider.ErrDuplicateId {
|
||||
return nil, errors.Wrapf(err, "GetISecurityGroupByName")
|
||||
}
|
||||
if !regionDriver.IsAllowSecurityGroupNameRepeat() {
|
||||
randomString := func(prefix string, length int) string {
|
||||
return fmt.Sprintf("%s-%s", prefix, rand.String(length))
|
||||
}
|
||||
opts.Name = randomString(self.Name, i)
|
||||
opts := &cloudprovider.SecurityGroupFilterOptions{
|
||||
Name: randomString(self.Name, 1),
|
||||
VpcId: self.VpcId,
|
||||
ProjectId: self.ExternalProjectId,
|
||||
}
|
||||
for i := 2; i < 30; i++ {
|
||||
_, err := iRegion.GetISecurityGroupByName(opts)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotFound {
|
||||
break
|
||||
}
|
||||
if errors.Cause(err) != cloudprovider.ErrDuplicateId {
|
||||
return nil, errors.Wrapf(err, "GetISecurityGroupByName")
|
||||
}
|
||||
}
|
||||
opts.Name = randomString(self.Name, i)
|
||||
}
|
||||
self.Name = opts.Name
|
||||
}
|
||||
|
||||
conf := &cloudprovider.SecurityGroupCreateInput{
|
||||
Name: opts.Name,
|
||||
Name: self.Name,
|
||||
Desc: self.Description,
|
||||
VpcId: self.VpcId,
|
||||
ProjectId: self.ExternalProjectId,
|
||||
|
||||
@@ -63,7 +63,6 @@ func init() {
|
||||
),
|
||||
}
|
||||
SecurityGroupManager.NameLength = 128
|
||||
SecurityGroupManager.NameRequireAscii = true
|
||||
SecurityGroupManager.SetVirtualObject(SecurityGroupManager)
|
||||
}
|
||||
|
||||
|
||||
@@ -52,6 +52,14 @@ func init() {
|
||||
models.RegisterRegionDriver(&driver)
|
||||
}
|
||||
|
||||
func (self *SAliyunRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SAliyunRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
func (self *SAliyunRegionDriver) GetDefaultSecurityGroupInRule() cloudprovider.SecurityRule {
|
||||
return cloudprovider.SecurityRule{SecurityRule: *secrules.MustParseSecurityRule("in:deny any")}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/choices"
|
||||
"yunion.io/x/onecloud/pkg/util/pinyinutils"
|
||||
"yunion.io/x/onecloud/pkg/util/rand"
|
||||
"yunion.io/x/onecloud/pkg/util/rbacutils"
|
||||
)
|
||||
@@ -52,6 +53,14 @@ func init() {
|
||||
models.RegisterRegionDriver(&driver)
|
||||
}
|
||||
|
||||
func (self *SAwsRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SAwsRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
return pinyinutils.Text2Pinyin(name)
|
||||
}
|
||||
|
||||
func (self *SAwsRegionDriver) GetDefaultSecurityGroupInRule() cloudprovider.SecurityRule {
|
||||
return cloudprovider.SecurityRule{SecurityRule: *secrules.MustParseSecurityRule("in:deny any")}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/pinyinutils"
|
||||
)
|
||||
|
||||
type SAzureRegionDriver struct {
|
||||
@@ -41,6 +42,14 @@ func (self *SAzureRegionDriver) GetProvider() string {
|
||||
return api.CLOUD_PROVIDER_AZURE
|
||||
}
|
||||
|
||||
func (self *SAzureRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *SAzureRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
return pinyinutils.Text2Pinyin(name)
|
||||
}
|
||||
|
||||
func (self *SAzureRegionDriver) ValidateCreateLoadbalancerData(ctx context.Context, userCred mcclient.TokenCredential, owerId mcclient.IIdentityProvider, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
return nil, httperrors.NewNotImplementedError("%s does not currently support creating loadbalancer", self.GetProvider())
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/billing"
|
||||
"yunion.io/x/onecloud/pkg/util/pinyinutils"
|
||||
"yunion.io/x/onecloud/pkg/util/rbacutils"
|
||||
)
|
||||
|
||||
@@ -222,6 +223,14 @@ func (self *SBaseRegionDriver) RequestDeleteVpc(ctx context.Context, userCred mc
|
||||
return fmt.Errorf("Not implement RequestDeleteVpc")
|
||||
}
|
||||
|
||||
func (self *SBaseRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *SBaseRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
return pinyinutils.Text2Pinyin(name)
|
||||
}
|
||||
|
||||
func (self *SBaseRegionDriver) RequestCacheSecurityGroup(ctx context.Context, userCred mcclient.TokenCredential, region *models.SCloudregion, vpc *models.SVpc, secgroup *models.SSecurityGroup, classic bool, remoteProjectId string, task taskman.ITask) error {
|
||||
return fmt.Errorf("Not Implemented RequestCacheSecurityGroup")
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ package regiondrivers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/util/secrules"
|
||||
@@ -37,6 +38,17 @@ func init() {
|
||||
models.RegisterRegionDriver(&driver)
|
||||
}
|
||||
|
||||
func (self *SCtyunRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SCtyunRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
if strings.ToLower(name) == "default" {
|
||||
return "DefaultGroup"
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func (self *SCtyunRegionDriver) GetDefaultSecurityGroupInRule() cloudprovider.SecurityRule {
|
||||
return cloudprovider.SecurityRule{SecurityRule: *secrules.MustParseSecurityRule("in:deny any")}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package regiondrivers
|
||||
import (
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/util/pinyinutils"
|
||||
)
|
||||
|
||||
type SEcloudRegionDriver struct {
|
||||
@@ -17,3 +18,11 @@ func init() {
|
||||
func (self *SEcloudRegionDriver) GetProvider() string {
|
||||
return api.CLOUD_PROVIDER_ECLOUD
|
||||
}
|
||||
|
||||
func (self *SEcloudRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *SEcloudRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
return pinyinutils.Text2Pinyin(name)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package regiondrivers
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
@@ -32,6 +33,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/pinyinutils"
|
||||
)
|
||||
|
||||
type SGoogleRegionDriver struct {
|
||||
@@ -43,6 +45,24 @@ func init() {
|
||||
models.RegisterRegionDriver(&driver)
|
||||
}
|
||||
|
||||
func (self *SGoogleRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// 名称必须以小写字母开头,后面最多可跟 62 个小写字母、数字或连字符,但不能以连字符结尾
|
||||
func (self *SGoogleRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
ret := ""
|
||||
for _, s := range strings.ToLower(pinyinutils.Text2Pinyin(name)) {
|
||||
if (s >= 'a' && s <= 'z') || (s >= '0' && s <= '9') || (s == '-') {
|
||||
ret = fmt.Sprintf("%s%s", ret, string(s))
|
||||
}
|
||||
}
|
||||
if len(ret) > 0 && (ret[0] < 'a' || ret[0] > 'z') {
|
||||
ret = fmt.Sprintf("sg-%s", ret)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (self *SGoogleRegionDriver) GetDefaultSecurityGroupInRule() cloudprovider.SecurityRule {
|
||||
return cloudprovider.SecurityRule{SecurityRule: *secrules.MustParseSecurityRule("in:deny any")}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,17 @@ func init() {
|
||||
models.RegisterRegionDriver(&driver)
|
||||
}
|
||||
|
||||
func (self *SHuaWeiRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SHuaWeiRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
if strings.ToLower(name) == "default" {
|
||||
return "DefaultGroup"
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func (self *SHuaWeiRegionDriver) GetDefaultSecurityGroupInRule() cloudprovider.SecurityRule {
|
||||
return cloudprovider.SecurityRule{SecurityRule: *secrules.MustParseSecurityRule("in:deny any")}
|
||||
}
|
||||
|
||||
@@ -72,6 +72,14 @@ func (self *SKVMRegionDriver) IsSupportPeerSecgroup() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SKVMRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *SKVMRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
func (self *SKVMRegionDriver) ValidateCreateLoadbalancerData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
|
||||
networkV := validators.NewModelIdOrNameValidator("network", "network", ownerId)
|
||||
addressV := validators.NewIPv4AddrValidator("address")
|
||||
|
||||
@@ -39,6 +39,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/util/billing"
|
||||
"yunion.io/x/onecloud/pkg/util/logclient"
|
||||
"yunion.io/x/onecloud/pkg/util/pinyinutils"
|
||||
"yunion.io/x/onecloud/pkg/util/rand"
|
||||
"yunion.io/x/onecloud/pkg/util/rbacutils"
|
||||
"yunion.io/x/onecloud/pkg/util/seclib2"
|
||||
@@ -48,6 +49,14 @@ type SManagedVirtualizationRegionDriver struct {
|
||||
SVirtualizationRegionDriver
|
||||
}
|
||||
|
||||
func (self *SManagedVirtualizationRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SManagedVirtualizationRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
return pinyinutils.Text2Pinyin(name)
|
||||
}
|
||||
|
||||
func (self *SManagedVirtualizationRegionDriver) IsSupportedElasticcacheSecgroup() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
@@ -48,6 +49,17 @@ func init() {
|
||||
models.RegisterRegionDriver(&driver)
|
||||
}
|
||||
|
||||
func (self *SOpenStackRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SOpenStackRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
if strings.ToLower(name) == "default" {
|
||||
return "DefaultGroup"
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func (self *SOpenStackRegionDriver) GetDefaultSecurityGroupInRule() cloudprovider.SecurityRule {
|
||||
return cloudprovider.SecurityRule{SecurityRule: *secrules.MustParseSecurityRule("in:deny any")}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,14 @@ func init() {
|
||||
models.RegisterRegionDriver(&driver)
|
||||
}
|
||||
|
||||
func (self *SQcloudRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SQcloudRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
func (self *SQcloudRegionDriver) GetDefaultSecurityGroupInRule() cloudprovider.SecurityRule {
|
||||
return cloudprovider.SecurityRule{SecurityRule: *secrules.MustParseSecurityRule("in:deny any")}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,14 @@ func init() {
|
||||
models.RegisterRegionDriver(&driver)
|
||||
}
|
||||
|
||||
func (self *SUcloudRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *SUcloudRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
func (self *SUcloudRegionDriver) GetDefaultSecurityGroupInRule() cloudprovider.SecurityRule {
|
||||
return cloudprovider.SecurityRule{SecurityRule: *secrules.MustParseSecurityRule("in:deny any")}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,14 @@ func init() {
|
||||
models.RegisterRegionDriver(&driver)
|
||||
}
|
||||
|
||||
func (self *SZStackRegionDriver) IsAllowSecurityGroupNameRepeat() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SZStackRegionDriver) GenerateSecurityGroupName(name string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
func (self *SZStackRegionDriver) GetDefaultSecurityGroupInRule() cloudprovider.SecurityRule {
|
||||
return cloudprovider.SecurityRule{SecurityRule: *secrules.MustParseSecurityRule("in:deny any")}
|
||||
}
|
||||
|
||||
@@ -369,9 +369,11 @@ func (self *SRegion) syncSecgroupRules(secgroupId string, rules []cloudprovider.
|
||||
params[fmt.Sprintf("SecurityGroupPolicySet.Ingress.%d.PolicyIndex", idx)] = fmt.Sprintf("%d", rule.PolicyIndex)
|
||||
}
|
||||
}
|
||||
_, err = self.vpcRequest("DeleteSecurityGroupPolicies", params)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "DeleteSecurityGroupPolicies")
|
||||
if len(ruleSet.Egress)+len(ruleSet.Ingress) > 0 {
|
||||
_, err = self.vpcRequest("DeleteSecurityGroupPolicies", params)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "DeleteSecurityGroupPolicies")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user