fix: check host sharing status when considering whether a host is assignable

This commit is contained in:
Qiu Jian
2020-09-12 19:21:41 +08:00
parent be55e9e8cd
commit 04f036ce2e
4 changed files with 26 additions and 16 deletions
+3 -4
View File
@@ -559,10 +559,9 @@ func checkAssignHost(userCred mcclient.TokenCredential, preferHost string) error
return httperrors.NewBadRequestError("Host %s not found", preferHost)
}
host := iHost.(*models.SHost)
if db.IsAdminAllowPerform(userCred, host, "assign-host") {
} else if db.IsDomainAllowPerform(userCred, host, "assign-host") && userCred.GetProjectDomainId() == host.DomainId {
} else {
return httperrors.NewNotSufficientPrivilegeError("Only system admin can assign host")
err := host.IsAssignable(userCred)
if err != nil {
return errors.Wrap(err, "IsAssignable")
}
return nil
}
+3 -4
View File
@@ -4417,10 +4417,9 @@ func (manager *SGuestManager) PerformBatchMigrate(ctx context.Context, userCred
host := iHost.(*SHost)
preferHostId = host.Id
if db.IsAdminAllowPerform(userCred, host, "assign-host") {
} else if db.IsDomainAllowPerform(userCred, host, "assign-host") && userCred.GetProjectDomainId() == host.DomainId {
} else {
return nil, httperrors.NewNotSufficientPrivilegeError("Only system admin can assign host")
err := host.IsAssignable(userCred)
if err != nil {
return nil, errors.Wrap(err, "IsAssignable")
}
}
+4 -4
View File
@@ -20,6 +20,7 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
api "yunion.io/x/onecloud/pkg/apis/compute"
@@ -73,10 +74,9 @@ func ValidateScheduleCreateData(ctx context.Context, userCred mcclient.TokenCred
}
baremetal := bmObj.(*SHost)
if db.IsAdminAllowPerform(userCred, baremetal, "assign-host") {
} else if db.IsDomainAllowPerform(userCred, baremetal, "assign-host") && userCred.GetProjectDomainId() == baremetal.DomainId {
} else {
return nil, httperrors.NewNotSufficientPrivilegeError("Only system admin can assign host")
err = baremetal.IsAssignable(userCred)
if err != nil {
return nil, errors.Wrap(err, "IsAssignable")
}
if !baremetal.GetEnabled() {
+16 -4
View File
@@ -4928,10 +4928,9 @@ func (host *SHost) PerformHostMaintenance(ctx context.Context, userCred mcclient
}
host := iHost.(*SHost)
preferHostId = host.Id
if db.IsAdminAllowPerform(userCred, host, "assign-host") {
} else if db.IsDomainAllowPerform(userCred, host, "assign-host") && userCred.GetProjectDomainId() == host.DomainId {
} else {
return nil, httperrors.NewNotSufficientPrivilegeError("Only system admin can assign host")
err := host.IsAssignable(userCred)
if err != nil {
return nil, errors.Wrap(err, "IsAssignable")
}
}
@@ -5479,3 +5478,16 @@ func (manager *SHostManager) FetchHostByExtId(extid string) *SHost {
return &host
}
}
func (host *SHost) IsAssignable(userCred mcclient.TokenCredential) error {
if db.IsAdminAllowPerform(userCred, host, "assign-host") {
return nil
} else if db.IsDomainAllowPerform(userCred, host, "assign-host") &&
(userCred.GetProjectDomainId() == host.DomainId ||
host.PublicScope == string(rbacutils.ScopeSystem) ||
(host.PublicScope == string(rbacutils.ScopeDomain) && utils.IsInStringArray(userCred.GetProjectDomainId(), host.GetSharedDomains()))) {
return nil
} else {
return httperrors.NewNotSufficientPrivilegeError("Only system admin can assign host")
}
}