|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | +"encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "log" |
6 | 7 | "net/http" |
@@ -216,3 +217,179 @@ func apiEnvRemoveHandler(w http.ResponseWriter, r *http.Request) { |
216 | 217 | utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, env) |
217 | 218 | incMetric(metricAPIEnvsOK) |
218 | 219 | } |
| 220 | + |
| 221 | +// POST Handler to perform actions (extend, expire) in enroll values |
| 222 | +func apiEnvEnrollActionsHandler(w http.ResponseWriter, r *http.Request) { |
| 223 | +incMetric(metricAPIQueriesReq) |
| 224 | +utils.DebugHTTPDump(r, settingsmgr.DebugHTTP(settings.ServiceAPI, settings.NoEnvironmentID), false) |
| 225 | +// Extract environment |
| 226 | +envVar := r.PathValue("env") |
| 227 | +if envVar == "" { |
| 228 | +apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil) |
| 229 | +incMetric(metricAPIQueriesErr) |
| 230 | +return |
| 231 | +} |
| 232 | +// Get environment |
| 233 | +env, err := envs.Get(envVar) |
| 234 | +if err != nil { |
| 235 | +apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil) |
| 236 | +incMetric(metricAPIQueriesErr) |
| 237 | +return |
| 238 | +} |
| 239 | +// Get context data and check access |
| 240 | +ctx := r.Context().Value(contextKey(contextAPI)).(contextValue) |
| 241 | +if !apiUsers.CheckPermissions(ctx[ctxUser], users.AdminLevel, env.UUID) { |
| 242 | +apiErrorResponse(w, "no access", http.StatusForbidden, fmt.Errorf("attempt to use API by user %s", ctx[ctxUser])) |
| 243 | +incMetric(metricAPIQueriesErr) |
| 244 | +return |
| 245 | +} |
| 246 | +// Extract action |
| 247 | +actionVar := r.PathValue("action") |
| 248 | +if actionVar == "" { |
| 249 | +apiErrorResponse(w, "error getting action", http.StatusInternalServerError, nil) |
| 250 | +incMetric(metricAPIEnvsErr) |
| 251 | +return |
| 252 | +} |
| 253 | +var e types.ApiActionsRequest |
| 254 | +// Parse request JSON body |
| 255 | +if err := json.NewDecoder(r.Body).Decode(&e); err != nil { |
| 256 | +apiErrorResponse(w, "error parsing POST body", http.StatusInternalServerError, err) |
| 257 | +incMetric(metricAPIEnvsErr) |
| 258 | +return |
| 259 | +} |
| 260 | +var msgReturn string |
| 261 | +switch actionVar { |
| 262 | +case settings.ActionExtend: |
| 263 | +if err := envs.ExtendEnroll(env.UUID); err != nil { |
| 264 | +apiErrorResponse(w, "error extending enrollment", http.StatusInternalServerError, err) |
| 265 | +incMetric(metricAPIEnvsErr) |
| 266 | +return |
| 267 | +} |
| 268 | +msgReturn = "enrollment extended successfully" |
| 269 | +case settings.ActionExpire: |
| 270 | +if err := envs.ExpireEnroll(env.UUID); err != nil { |
| 271 | +apiErrorResponse(w, "error expiring enrollment", http.StatusInternalServerError, err) |
| 272 | +incMetric(metricAPIEnvsErr) |
| 273 | +return |
| 274 | +} |
| 275 | +case settings.ActionRotate: |
| 276 | +if err := envs.RotateEnroll(env.UUID); err != nil { |
| 277 | +apiErrorResponse(w, "error rotating enrollment", http.StatusInternalServerError, err) |
| 278 | +incMetric(metricAPIEnvsErr) |
| 279 | +return |
| 280 | +} |
| 281 | +msgReturn = "enrollment rotated successfully" |
| 282 | +case settings.ActionNotexpire: |
| 283 | +if err := envs.NotExpireEnroll(env.UUID); err != nil { |
| 284 | +apiErrorResponse(w, "error setting no expiration", http.StatusInternalServerError, err) |
| 285 | +incMetric(metricAPIEnvsErr) |
| 286 | +return |
| 287 | +} |
| 288 | +msgReturn = "enrollment set to not expire" |
| 289 | +case settings.SetMacPackage: |
| 290 | +if err := envs.UpdatePkgPackage(env.UUID, e.MacPkgURL); err != nil { |
| 291 | +apiErrorResponse(w, "error setting PKG", http.StatusInternalServerError, err) |
| 292 | +incMetric(metricAPIEnvsErr) |
| 293 | +return |
| 294 | +} |
| 295 | +msgReturn = "PKG updated successfully" |
| 296 | +case settings.SetMsiPackage: |
| 297 | +if err := envs.UpdateMsiPackage(env.UUID, e.MsiPkgURL); err != nil { |
| 298 | +apiErrorResponse(w, "error setting MSI", http.StatusInternalServerError, err) |
| 299 | +incMetric(metricAPIEnvsErr) |
| 300 | +return |
| 301 | +} |
| 302 | +msgReturn = "MSI updated successfully" |
| 303 | +case settings.SetDebPackage: |
| 304 | +if err := envs.UpdateDebPackage(env.UUID, e.DebPkgURL); err != nil { |
| 305 | +apiErrorResponse(w, "error setting DEB", http.StatusInternalServerError, err) |
| 306 | +incMetric(metricAPIEnvsErr) |
| 307 | +return |
| 308 | +} |
| 309 | +msgReturn = "DEB updated successfully" |
| 310 | +case settings.SetRpmPackage: |
| 311 | +if err := envs.UpdateRpmPackage(env.UUID, e.RpmPkgURL); err != nil { |
| 312 | +apiErrorResponse(w, "error setting RPM", http.StatusInternalServerError, err) |
| 313 | +incMetric(metricAPIEnvsErr) |
| 314 | +return |
| 315 | +} |
| 316 | +msgReturn = "RPM updated successfully" |
| 317 | +} |
| 318 | +// Return query name as serialized response |
| 319 | +utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, types.ApiGenericResponse{Message: msgReturn}) |
| 320 | +incMetric(metricAPIEnvsOK) |
| 321 | +} |
| 322 | + |
| 323 | +// POST Handler to perform actions (extend, expire) in remove values |
| 324 | +func apiEnvRemoveActionsHandler(w http.ResponseWriter, r *http.Request) { |
| 325 | +incMetric(metricAPIQueriesReq) |
| 326 | +utils.DebugHTTPDump(r, settingsmgr.DebugHTTP(settings.ServiceAPI, settings.NoEnvironmentID), false) |
| 327 | +// Extract environment |
| 328 | +envVar := r.PathValue("env") |
| 329 | +if envVar == "" { |
| 330 | +apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil) |
| 331 | +incMetric(metricAPIQueriesErr) |
| 332 | +return |
| 333 | +} |
| 334 | +// Get environment |
| 335 | +env, err := envs.Get(envVar) |
| 336 | +if err != nil { |
| 337 | +apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil) |
| 338 | +incMetric(metricAPIQueriesErr) |
| 339 | +return |
| 340 | +} |
| 341 | +// Get context data and check access |
| 342 | +ctx := r.Context().Value(contextKey(contextAPI)).(contextValue) |
| 343 | +if !apiUsers.CheckPermissions(ctx[ctxUser], users.AdminLevel, env.UUID) { |
| 344 | +apiErrorResponse(w, "no access", http.StatusForbidden, fmt.Errorf("attempt to use API by user %s", ctx[ctxUser])) |
| 345 | +incMetric(metricAPIQueriesErr) |
| 346 | +return |
| 347 | +} |
| 348 | +// Extract action |
| 349 | +actionVar := r.PathValue("action") |
| 350 | +if actionVar == "" { |
| 351 | +apiErrorResponse(w, "error getting action", http.StatusInternalServerError, nil) |
| 352 | +incMetric(metricAPIEnvsErr) |
| 353 | +return |
| 354 | +} |
| 355 | +var e types.ApiActionsRequest |
| 356 | +// Parse request JSON body |
| 357 | +if err := json.NewDecoder(r.Body).Decode(&e); err != nil { |
| 358 | +apiErrorResponse(w, "error parsing POST body", http.StatusInternalServerError, err) |
| 359 | +incMetric(metricAPIEnvsErr) |
| 360 | +return |
| 361 | +} |
| 362 | +var msgReturn string |
| 363 | +switch actionVar { |
| 364 | +case settings.ActionExtend: |
| 365 | +if err := envs.ExtendEnroll(env.UUID); err != nil { |
| 366 | +apiErrorResponse(w, "error extending remove", http.StatusInternalServerError, err) |
| 367 | +incMetric(metricAPIEnvsErr) |
| 368 | +return |
| 369 | +} |
| 370 | +msgReturn = "remove extended successfully" |
| 371 | +case settings.ActionExpire: |
| 372 | +if err := envs.ExpireEnroll(env.UUID); err != nil { |
| 373 | +apiErrorResponse(w, "error expiring remove", http.StatusInternalServerError, err) |
| 374 | +incMetric(metricAPIEnvsErr) |
| 375 | +return |
| 376 | +} |
| 377 | +case settings.ActionRotate: |
| 378 | +if err := envs.RotateEnroll(env.UUID); err != nil { |
| 379 | +apiErrorResponse(w, "error rotating remove", http.StatusInternalServerError, err) |
| 380 | +incMetric(metricAPIEnvsErr) |
| 381 | +return |
| 382 | +} |
| 383 | +msgReturn = "remove rotated successfully" |
| 384 | +case settings.ActionNotexpire: |
| 385 | +if err := envs.NotExpireEnroll(env.UUID); err != nil { |
| 386 | +apiErrorResponse(w, "error setting no remove", http.StatusInternalServerError, err) |
| 387 | +incMetric(metricAPIEnvsErr) |
| 388 | +return |
| 389 | +} |
| 390 | +msgReturn = "remove set to not expire" |
| 391 | +} |
| 392 | +// Return query name as serialized response |
| 393 | +utils.HTTPResponse(w, utils.JSONApplicationUTF8, http.StatusOK, types.ApiGenericResponse{Message: msgReturn}) |
| 394 | +incMetric(metricAPIEnvsOK) |
| 395 | +} |
0 commit comments