fixes: idp driver validate config support

This commit is contained in:
Qiu Jian
2020-04-14 14:51:28 +08:00
parent 9c498b2924
commit 57898da085
5 changed files with 29 additions and 18 deletions
+14 -11
View File
@@ -15,16 +15,17 @@
package cas
import (
"context"
"database/sql"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
api "yunion.io/x/onecloud/pkg/apis/identity"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/keystone/driver"
"yunion.io/x/onecloud/pkg/keystone/models"
"yunion.io/x/onecloud/pkg/mcclient"
)
type SCASDriverClass struct{}
@@ -45,36 +46,38 @@ func (self *SCASDriverClass) Name() string {
return api.IdentityDriverCAS
}
func (self *SCASDriverClass) ValidateConfig(tconf api.TConfigs) error {
func (self *SCASDriverClass) ValidateConfig(ctx context.Context, userCred mcclient.TokenCredential, tconf api.TConfigs) (api.TConfigs, error) {
conf := api.SCASIdpConfigOptions{}
confJson := jsonutils.Marshal(tconf["cas"])
log.Debugf("%s %s", tconf, confJson)
err := confJson.Unmarshal(&conf)
if err != nil {
return errors.Wrap(err, "unmarshal config")
return tconf, errors.Wrap(err, "unmarshal config")
}
if len(conf.DefaultCasProjectId) > 0 {
_, err := models.ProjectManager.FetchProjectById(conf.DefaultCasProjectId)
obj, err := models.ProjectManager.FetchByIdOrName(userCred, conf.DefaultCasProjectId)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return errors.Wrapf(httperrors.ErrResourceNotFound, "project %s", conf.DefaultCasProjectId)
return tconf, errors.Wrapf(httperrors.ErrResourceNotFound, "project %s", conf.DefaultCasProjectId)
} else {
return errors.Wrap(err, "FetchProjectById")
return tconf, errors.Wrap(err, "FetchProjectById")
}
}
tconf["cas"]["default_cas_project_id"] = jsonutils.NewString(obj.GetId())
}
if len(conf.DefaultCasRoleId) > 0 {
_, err := models.RoleManager.FetchRoleById(conf.DefaultCasRoleId)
obj, err := models.RoleManager.FetchByIdOrName(userCred, conf.DefaultCasRoleId)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return errors.Wrapf(httperrors.ErrResourceNotFound, "role %s", conf.DefaultCasRoleId)
return tconf, errors.Wrapf(httperrors.ErrResourceNotFound, "role %s", conf.DefaultCasRoleId)
} else {
return errors.Wrap(err, "FetchRoleById")
return tconf, errors.Wrap(err, "FetchRoleById")
}
}
tconf["cas"]["default_cas_role_id"] = jsonutils.NewString(obj.GetId())
}
return nil
return tconf, nil
}
func init() {
+1 -1
View File
@@ -26,7 +26,7 @@ type IIdentityBackendClass interface {
SyncMethod() string
Name() string
NewDriver(idpId, idpName, template, targetDomainId string, autoCreateProject bool, conf api.TConfigs) (IIdentityBackend, error)
ValidateConfig(conf api.TConfigs) error
ValidateConfig(ctx context.Context, userCred mcclient.TokenCredential, conf api.TConfigs) (api.TConfigs, error)
}
type IIdentityBackend interface {
+5 -2
View File
@@ -15,8 +15,11 @@
package ldap
import (
"context"
api "yunion.io/x/onecloud/pkg/apis/identity"
"yunion.io/x/onecloud/pkg/keystone/driver"
"yunion.io/x/onecloud/pkg/mcclient"
)
type SLDAPDriverClass struct{}
@@ -37,8 +40,8 @@ func (self *SLDAPDriverClass) Name() string {
return api.IdentityDriverLDAP
}
func (self *SLDAPDriverClass) ValidateConfig(conf api.TConfigs) error {
return nil
func (self *SLDAPDriverClass) ValidateConfig(ctx context.Context, userCred mcclient.TokenCredential, conf api.TConfigs) (api.TConfigs, error) {
return conf, nil
}
func init() {
+5 -2
View File
@@ -15,8 +15,11 @@
package sql
import (
"context"
api "yunion.io/x/onecloud/pkg/apis/identity"
"yunion.io/x/onecloud/pkg/keystone/driver"
"yunion.io/x/onecloud/pkg/mcclient"
)
type SSQLDriverClass struct{}
@@ -37,8 +40,8 @@ func (self *SSQLDriverClass) Name() string {
return api.IdentityDriverSQL
}
func (self *SSQLDriverClass) ValidateConfig(conf api.TConfigs) error {
return nil
func (self *SSQLDriverClass) ValidateConfig(ctx context.Context, userCred mcclient.TokenCredential, conf api.TConfigs) (api.TConfigs, error) {
return conf, nil
}
func init() {
+4 -2
View File
@@ -237,7 +237,8 @@ func (ident *SIdentityProvider) PerformConfig(ctx context.Context, userCred mccl
return nil, httperrors.NewInvalidStatusError("cannot update config when not idle")
}
err := ident.getDriverClass().ValidateConfig(input.Config)
var err error
input.Config, err = ident.getDriverClass().ValidateConfig(ctx, userCred, input.Config)
if err != nil {
return nil, errors.Wrap(err, "ValidateConfig")
}
@@ -316,7 +317,8 @@ func (manager *SIdentityProviderManager) ValidateCreateData(
input.TargetDomainId = domain.Id
}
err := drvCls.ValidateConfig(input.Config)
var err error
input.Config, err = drvCls.ValidateConfig(ctx, userCred, input.Config)
if err != nil {
return input, errors.Wrap(err, "ValidateConfig")
}