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

Commit eb8ecce

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 728a215 commit eb8ecce

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
@@ -958,7 +958,12 @@ func createServer(w http.ResponseWriter, r *http.Request, context *controller) {
958958

959959
type instanceAction func(string) error
960960

961+
// tenantServersAction will apply the operation sent in POST (as os-start, os-stop, os-delete)
962+
// to all servers of a tenant or if ServersID size is greater than zero it will be applied
963+
// only to the subset provided that also belongs to the tenant
961964
func tenantServersAction(w http.ResponseWriter, r *http.Request, context *controller) {
965+
vars := mux.Vars(r)
966+
tenant := vars["tenant"]
962967
var servers payloads.CiaoServersAction
963968
var actionFunc instanceAction
964969
var statusFilter string
@@ -999,14 +1004,22 @@ func tenantServersAction(w http.ResponseWriter, r *http.Request, context *contro
9991004
}
10001005

10011006
if len(servers.ServerIDs) > 0 {
1002-
/* TODO Check that instance belongs to the right tenant */
1003-
for _, instance := range servers.ServerIDs {
1004-
actionFunc(instance)
1007+
for _, instanceID := range servers.ServerIDs {
1008+
// make sure the instance belongs to the tenant
1009+
instance, err := context.ds.GetInstance(instanceID)
1010+
1011+
if err != nil {
1012+
returnErrorCode(w, http.StatusNotFound, "Instance %s could not be found", instanceID)
1013+
return
1014+
}
1015+
1016+
if instance.TenantID != tenant {
1017+
returnErrorCode(w, http.StatusNotFound, "Instance %s does not belong to tenant %s", instanceID, tenant)
1018+
return
1019+
}
1020+
actionFunc(instanceID)
10051021
}
10061022
} else {
1007-
vars := mux.Vars(r)
1008-
tenant := vars["tenant"]
1009-
10101023
/* We want to act on all relevant instances */
10111024
instances, err := context.ds.GetAllInstancesFromTenant(tenant)
10121025
if err != nil {

0 commit comments

Comments
 (0)