/usr/share/gocode/src/github.com/ctdk/goiardi/roles.go is in golang-github-ctdk-goiardi-dev 0.11.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | /* Role functions */
/*
* Copyright (c) 2013-2016, Jeremy Bingham (<jeremy@goiardi.gl>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"encoding/json"
"github.com/ctdk/goiardi/actor"
"github.com/ctdk/goiardi/environment"
"github.com/ctdk/goiardi/loginfo"
"github.com/ctdk/goiardi/role"
"github.com/ctdk/goiardi/util"
"net/http"
)
func roleHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
opUser, oerr := actor.GetReqUser(r.Header.Get("X-OPS-USERID"))
if oerr != nil {
jsonErrorReport(w, r, oerr.Error(), oerr.Status())
return
}
/* Roles are bit weird in that there's /roles/NAME, but also
* /roles/NAME/environments and /roles/NAME/environments/NAME, so we'll
* split up the whole path to get those values. */
pathArray := splitPath(r.URL.Path)
roleName := pathArray[1]
chefRole, err := role.Get(roleName)
if err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusNotFound)
return
}
if len(pathArray) == 2 {
/* Normal /roles/NAME case */
switch r.Method {
case "GET", "DELETE":
if opUser.IsValidator() || (!opUser.IsAdmin() && r.Method == "DELETE") {
jsonErrorReport(w, r, "You are not allowed to perform this action", http.StatusForbidden)
return
}
enc := json.NewEncoder(w)
if err = enc.Encode(&chefRole); err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
if r.Method == "DELETE" {
err = chefRole.Delete()
if err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
if lerr := loginfo.LogEvent(opUser, chefRole, "delete"); lerr != nil {
jsonErrorReport(w, r, lerr.Error(), http.StatusInternalServerError)
return
}
}
case "PUT":
if !opUser.IsAdmin() {
jsonErrorReport(w, r, "You are not allowed to perform this action", http.StatusForbidden)
return
}
roleData, jerr := parseObjJSON(r.Body)
if jerr != nil {
jsonErrorReport(w, r, jerr.Error(), http.StatusBadRequest)
return
}
if _, ok := roleData["name"]; !ok {
roleData["name"] = roleName
}
jsonName, sterr := util.ValidateAsString(roleData["name"])
if sterr != nil {
jsonErrorReport(w, r, sterr.Error(), sterr.Status())
return
}
if roleName != roleData["name"].(string) {
jsonErrorReport(w, r, "Role name mismatch", http.StatusBadRequest)
return
}
if jsonName == "" {
roleData["name"] = roleName
}
nerr := chefRole.UpdateFromJSON(roleData)
if nerr != nil {
jsonErrorReport(w, r, nerr.Error(), nerr.Status())
return
}
err = chefRole.Save()
if err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
if lerr := loginfo.LogEvent(opUser, chefRole, "modify"); lerr != nil {
jsonErrorReport(w, r, lerr.Error(), http.StatusInternalServerError)
return
}
enc := json.NewEncoder(w)
if err = enc.Encode(&chefRole); err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
}
default:
jsonErrorReport(w, r, "Unrecognized method!", http.StatusMethodNotAllowed)
}
} else {
var environmentName string
if len(pathArray) == 4 {
environmentName = pathArray[3]
if _, err := environment.Get(environmentName); err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusNotFound)
return
}
}
/* only method for the /roles/NAME/environment stuff is GET */
switch r.Method {
case "GET":
/* If we have an environment name, return the
* environment specific run_list. Otherwise,
* return the environments we have run lists
* for. Always at least return "_default",
* which refers to run_list. */
if opUser.IsValidator() {
jsonErrorReport(w, r, "You are not allowed to perform this action", http.StatusForbidden)
return
}
enc := json.NewEncoder(w)
if environmentName != "" {
var runList []string
if environmentName == "_default" {
runList = chefRole.RunList
} else {
runList = chefRole.EnvRunLists[environmentName]
}
resp := make(map[string][]string, 1)
resp["run_list"] = runList
if err = enc.Encode(&resp); err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusBadRequest)
}
} else {
roleEnvs := make([]string, len(chefRole.EnvRunLists)+1)
roleEnvs[0] = "_default"
i := 1
for k := range chefRole.EnvRunLists {
roleEnvs[i] = k
i++
}
if err = enc.Encode(&roleEnvs); err != nil {
jsonErrorReport(w, r, err.Error(), http.StatusBadRequest)
}
}
default:
jsonErrorReport(w, r, "Unrecognized method!", http.StatusMethodNotAllowed)
}
}
}
|