Skip to content

Commit

Permalink
fix(region): allow disable default policy (#21585)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioito authored Nov 14, 2024
1 parent bdc9d8e commit 8457991
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 30 deletions.
21 changes: 15 additions & 6 deletions pkg/apigateway/service/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ package service

import (
_ "yunion.io/x/onecloud/pkg/cloudevent/policy"
_ "yunion.io/x/onecloud/pkg/compute/policy"
_ "yunion.io/x/onecloud/pkg/image/policy"
_ "yunion.io/x/onecloud/pkg/keystone/policy"
_ "yunion.io/x/onecloud/pkg/logger/policy"
_ "yunion.io/x/onecloud/pkg/notify/policy"
_ "yunion.io/x/onecloud/pkg/yunionconf/policy"
compute "yunion.io/x/onecloud/pkg/compute/policy"
image "yunion.io/x/onecloud/pkg/image/policy"
keystone "yunion.io/x/onecloud/pkg/keystone/policy"
logger "yunion.io/x/onecloud/pkg/logger/policy"
notify "yunion.io/x/onecloud/pkg/notify/policy"
yunionconf "yunion.io/x/onecloud/pkg/yunionconf/policy"
)

func InitDefaultPolicy() {
compute.Init()
image.Init()
keystone.Init()
logger.Init()
notify.Init()
yunionconf.Init()
}
1 change: 1 addition & 0 deletions pkg/apigateway/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func StartService() {
baseOpts := &opts.BaseOptions
commonOpts := &opts.CommonOptions
common_options.ParseOptions(opts, os.Args, "apigateway.conf", api.SERVICE_TYPE)
InitDefaultPolicy()
app_common.InitAuth(commonOpts, func() {
log.Infof("Auth complete.")
})
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/identity/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ var (
// kubeserver blacklist options
// ############################
"running_mode",
"enable_default_policy",
},
}
)
Expand Down
10 changes: 10 additions & 0 deletions pkg/cloudcommon/consts/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ var (
localTaskWorkerCount int

enableChangeOwnerAutoRename = false

enableDefaultPolicy = true
)

func SetDefaultPolicy(enable bool) {
enableDefaultPolicy = enable
}

func IsEnableDefaultPolicy() bool {
return enableDefaultPolicy == true
}

func SetDefaultDB(dialect, connStr string) {
defaultDBDialect = dialect
defaultDBConnectionString = connStr
Expand Down
8 changes: 5 additions & 3 deletions pkg/cloudcommon/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -123,6 +123,7 @@ type BaseOptions struct {
EnableAppProfiling bool `help:"enable profiling API" default:"false"`

EnableChangeOwnerAutoRename bool `help:"Allows renaming when changing names" default:"false"`
EnableDefaultPolicy bool `help:"Enable defualt policies" default:"true"`
}

const (
Expand Down Expand Up @@ -220,7 +221,7 @@ func (opt *EtcdOptions) GetEtcdTLSConfig() (*tls.Config, error) {
opt.EtcdUseTLS = true
}
if opt.EtcdCacert != "" {
data, err := ioutil.ReadFile(opt.EtcdCacert)
data, err := os.ReadFile(opt.EtcdCacert)
if err != nil {
return nil, errors.Wrap(err, "read cacert file")
}
Expand Down Expand Up @@ -377,7 +378,7 @@ func parseOptions(optStruct interface{}, args []string, configFileName string, s
h.Init()
log.DisableColors()
log.Logger().AddHook(h)
log.Logger().Out = ioutil.Discard
log.Logger().Out = io.Discard
atexit.Register(atexit.ExitHandler{
Prio: atexit.PRIO_LOG_CLOSE,
Reason: "deinit log rotate hook",
Expand All @@ -393,6 +394,7 @@ func parseOptions(optStruct interface{}, args []string, configFileName string, s
consts.SetRegion(optionsRef.Region)
}

consts.SetDefaultPolicy(optionsRef.EnableDefaultPolicy)
consts.SetDomainizedNamespace(optionsRef.DomainizedNamespace)

consts.SetTaskWorkerCount(optionsRef.TaskWorkerCount)
Expand Down
7 changes: 5 additions & 2 deletions pkg/cloudid/policy/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"yunion.io/x/pkg/util/rbacscope"

api "yunion.io/x/onecloud/pkg/apis/cloudid"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
common_policy "yunion.io/x/onecloud/pkg/cloudcommon/policy"
"yunion.io/x/onecloud/pkg/util/rbacutils"
)
Expand Down Expand Up @@ -108,6 +109,8 @@ var (
}
)

func init() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
func Init() {
if consts.IsEnableDefaultPolicy() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
}
}
3 changes: 2 additions & 1 deletion pkg/cloudid/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
_ "yunion.io/x/onecloud/pkg/cloudid/drivers"
"yunion.io/x/onecloud/pkg/cloudid/models"
"yunion.io/x/onecloud/pkg/cloudid/options"
_ "yunion.io/x/onecloud/pkg/cloudid/policy"
"yunion.io/x/onecloud/pkg/cloudid/policy"
"yunion.io/x/onecloud/pkg/cloudid/saml"
_ "yunion.io/x/onecloud/pkg/cloudid/tasks"
"yunion.io/x/onecloud/pkg/mcclient/auth"
Expand All @@ -47,6 +47,7 @@ func StartService() {
baseOpts := &opts.BaseOptions
commonOpts := &opts.CommonOptions
common_options.ParseOptions(opts, os.Args, "cloudid.conf", api.SERVICE_TYPE)
policy.Init()

app_common.InitAuth(commonOpts, func() {
log.Infof("Auth complete!!")
Expand Down
7 changes: 5 additions & 2 deletions pkg/compute/policy/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"yunion.io/x/pkg/util/rbacscope"

api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
common_policy "yunion.io/x/onecloud/pkg/cloudcommon/policy"
"yunion.io/x/onecloud/pkg/util/rbacutils"
)
Expand Down Expand Up @@ -403,6 +404,8 @@ var (
}
)

func init() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
func Init() {
if consts.IsEnableDefaultPolicy() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
}
}
3 changes: 2 additions & 1 deletion pkg/compute/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import (
_ "yunion.io/x/onecloud/pkg/compute/hostdrivers"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/compute/options"
_ "yunion.io/x/onecloud/pkg/compute/policy"
"yunion.io/x/onecloud/pkg/compute/policy"
_ "yunion.io/x/onecloud/pkg/compute/regiondrivers"
_ "yunion.io/x/onecloud/pkg/compute/storagedrivers"
"yunion.io/x/onecloud/pkg/compute/tasks"
Expand All @@ -67,6 +67,7 @@ func StartServiceWithJobs(jobs func(cron *cronman.SCronJobManager)) {
baseOpts := &options.Options.BaseOptions
dbOpts := &options.Options.DBOptions
common_options.ParseOptions(opts, os.Args, "region.conf", api.SERVICE_TYPE)
policy.Init()

if opts.PortV2 > 0 {
log.Infof("Port V2 %d is specified, use v2 port", opts.PortV2)
Expand Down
7 changes: 5 additions & 2 deletions pkg/image/policy/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"yunion.io/x/pkg/util/rbacscope"

api "yunion.io/x/onecloud/pkg/apis/image"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
common_policy "yunion.io/x/onecloud/pkg/cloudcommon/policy"
"yunion.io/x/onecloud/pkg/util/rbacutils"
)
Expand Down Expand Up @@ -65,6 +66,8 @@ var (
}
)

func init() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
func Init() {
if consts.IsEnableDefaultPolicy() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
}
}
3 changes: 2 additions & 1 deletion pkg/image/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
"yunion.io/x/onecloud/pkg/image/drivers/s3"
"yunion.io/x/onecloud/pkg/image/models"
"yunion.io/x/onecloud/pkg/image/options"
_ "yunion.io/x/onecloud/pkg/image/policy"
"yunion.io/x/onecloud/pkg/image/policy"
_ "yunion.io/x/onecloud/pkg/image/tasks"
"yunion.io/x/onecloud/pkg/image/torrent"
"yunion.io/x/onecloud/pkg/mcclient/auth"
Expand All @@ -55,6 +55,7 @@ func StartService() {
baseOpts := &opts.BaseOptions
dbOpts := &opts.DBOptions
common_options.ParseOptions(opts, os.Args, "glance-api.conf", api.SERVICE_TYPE)
policy.Init()

// no need to run glance as root any more
// isRoot := sysutils.IsRootPermission()
Expand Down
7 changes: 5 additions & 2 deletions pkg/keystone/policy/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"yunion.io/x/pkg/util/rbacscope"

api "yunion.io/x/onecloud/pkg/apis/identity"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
common_policy "yunion.io/x/onecloud/pkg/cloudcommon/policy"
"yunion.io/x/onecloud/pkg/util/rbacutils"
)
Expand Down Expand Up @@ -191,6 +192,8 @@ var (
}
)

func init() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
func Init() {
if consts.IsEnableDefaultPolicy() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
}
}
3 changes: 2 additions & 1 deletion pkg/keystone/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"yunion.io/x/onecloud/pkg/keystone/cronjobs"
"yunion.io/x/onecloud/pkg/keystone/models"
"yunion.io/x/onecloud/pkg/keystone/options"
_ "yunion.io/x/onecloud/pkg/keystone/policy"
kpolicy "yunion.io/x/onecloud/pkg/keystone/policy"
"yunion.io/x/onecloud/pkg/keystone/saml"
_ "yunion.io/x/onecloud/pkg/keystone/tasks"
"yunion.io/x/onecloud/pkg/keystone/tokens"
Expand Down Expand Up @@ -63,6 +63,7 @@ func StartService() {

opts := &options.Options
common_options.ParseOptions(opts, os.Args, "keystone.conf", api.SERVICE_TYPE)
kpolicy.Init()

if opts.Port == 0 {
opts.Port = 5000 // keystone well-known port
Expand Down
7 changes: 5 additions & 2 deletions pkg/logger/policy/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"yunion.io/x/pkg/util/rbacscope"

api "yunion.io/x/onecloud/pkg/apis/logger"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
common_policy "yunion.io/x/onecloud/pkg/cloudcommon/policy"
"yunion.io/x/onecloud/pkg/util/rbacutils"
)
Expand Down Expand Up @@ -57,6 +58,8 @@ var (
}
)

func init() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
func Init() {
if consts.IsEnableDefaultPolicy() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
}
}
3 changes: 2 additions & 1 deletion pkg/logger/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"yunion.io/x/onecloud/pkg/logger/extern"
"yunion.io/x/onecloud/pkg/logger/models"
"yunion.io/x/onecloud/pkg/logger/options"
_ "yunion.io/x/onecloud/pkg/logger/policy"
"yunion.io/x/onecloud/pkg/logger/policy"
)

func StartService() {
Expand All @@ -42,6 +42,7 @@ func StartService() {
commonOpts := &opts.CommonOptions
dbOpts := &opts.DBOptions
common_options.ParseOptions(opts, os.Args, "log.conf", api.SERVICE_TYPE)
policy.Init()

app_common.InitAuth(commonOpts, func() {
log.Infof("Auth complete!!")
Expand Down
7 changes: 5 additions & 2 deletions pkg/notify/policy/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"yunion.io/x/pkg/util/rbacscope"

api "yunion.io/x/onecloud/pkg/apis/notify"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
common_policy "yunion.io/x/onecloud/pkg/cloudcommon/policy"
"yunion.io/x/onecloud/pkg/util/rbacutils"
)
Expand Down Expand Up @@ -180,6 +181,8 @@ var (
}
)

func init() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
func Init() {
if consts.IsEnableDefaultPolicy() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
}
}
3 changes: 2 additions & 1 deletion pkg/notify/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
"yunion.io/x/onecloud/pkg/notify/models"
"yunion.io/x/onecloud/pkg/notify/options"
_ "yunion.io/x/onecloud/pkg/notify/policy"
"yunion.io/x/onecloud/pkg/notify/policy"
_ "yunion.io/x/onecloud/pkg/notify/sender/smsdriver"
_ "yunion.io/x/onecloud/pkg/notify/tasks"
)
Expand All @@ -42,6 +42,7 @@ func StartService() {
dbOpts := &options.Options.DBOptions
baseOpts := &options.Options.BaseOptions
common_options.ParseOptions(opts, os.Args, "notify.conf", api.SERVICE_TYPE)
policy.Init()

// init auth
app.InitAuth(commonOpts, func() {
Expand Down
7 changes: 5 additions & 2 deletions pkg/yunionconf/policy/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"yunion.io/x/pkg/util/rbacscope"

api "yunion.io/x/onecloud/pkg/apis/yunionconf"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
common_policy "yunion.io/x/onecloud/pkg/cloudcommon/policy"
"yunion.io/x/onecloud/pkg/util/rbacutils"
)
Expand Down Expand Up @@ -95,6 +96,8 @@ var (
}
)

func init() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
func Init() {
if consts.IsEnableDefaultPolicy() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
}
}
3 changes: 2 additions & 1 deletion pkg/yunionconf/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/yunionconf/models"
"yunion.io/x/onecloud/pkg/yunionconf/options"
_ "yunion.io/x/onecloud/pkg/yunionconf/policy"
"yunion.io/x/onecloud/pkg/yunionconf/policy"
)

func StartService() {
Expand All @@ -42,6 +42,7 @@ func StartService() {
commonOpts := &options.Options.CommonOptions
dbOpts := &options.Options.DBOptions
common_options.ParseOptions(opts, os.Args, "yunionconf.conf", api.SERVICE_TYPE)
policy.Init()
app_common.InitAuth(commonOpts, func() {
log.Infof("Auth complete!!")
})
Expand Down

0 comments on commit 8457991

Please sign in to comment.