Skip to content
This repository was archived by the owner on Jul 16, 2020. It is now read-only.

Commit 2b4f857

Browse files
author
Leoswaldo Macias
committed
ciao-controller: check instances in list belong to tenant
tenantServerAction function was acting on instances when a list of servers was provided, the function now validates that if a servers list is provided then ensure it belongs to the tenant specified before proceeding to the action. Fixes #98 Signed-off-by: Leoswaldo Macias <[email protected]>
1 parent c97b38e commit 2b4f857

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

ciao-controller/compute.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,12 @@ func createServer(w http.ResponseWriter, r *http.Request, context *controller) {
953953

954954
type instanceAction func(string) error
955955

956+
// tenantServersAction will apply the operation sent in POST (as os-start, os-stop, os-delete)
957+
// to all servers of a tenant or if ServersID size is greater than zero it will be applied
958+
// only to the subset provided that also belongs to the tenant
956959
func tenantServersAction(w http.ResponseWriter, r *http.Request, context *controller) {
960+
vars := mux.Vars(r)
961+
tenant := vars["tenant"]
957962
var servers payloads.CiaoServersAction
958963
var actionFunc instanceAction
959964
var statusFilter string
@@ -994,14 +999,22 @@ func tenantServersAction(w http.ResponseWriter, r *http.Request, context *contro
994999
}
9951000

9961001
if len(servers.ServerIDs) > 0 {
997-
/* TODO Check that instance belongs to the right tenant */
998-
for _, instance := range servers.ServerIDs {
999-
actionFunc(instance)
1002+
for _, instanceID := range servers.ServerIDs {
1003+
// make sure the instance belongs to the tenant
1004+
instance, err := context.ds.GetInstance(instanceID)
1005+
1006+
if err != nil {
1007+
returnErrorCode(w, http.StatusNotFound, "Instance %s could not be found", instanceID)
1008+
return
1009+
}
1010+
1011+
if instance.TenantID != tenant {
1012+
returnErrorCode(w, http.StatusNotFound, "Instance %s does not belong to tenant %s", instanceID, tenant)
1013+
return
1014+
}
1015+
actionFunc(instanceID)
10001016
}
10011017
} else {
1002-
vars := mux.Vars(r)
1003-
tenant := vars["tenant"]
1004-
10051018
/* We want to act on all relevant instances */
10061019
instances, err := context.ds.GetAllInstancesFromTenant(tenant)
10071020
if err != nil {

0 commit comments

Comments
 (0)