github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmd/authn/config.go (about) 1 // Package authn is authentication server for AIStore. 2 /* 3 * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package main 6 7 import ( 8 "net/http" 9 10 "github.com/NVIDIA/aistore/api/authn" 11 "github.com/NVIDIA/aistore/cmn" 12 "github.com/NVIDIA/aistore/cmn/jsp" 13 jsoniter "github.com/json-iterator/go" 14 ) 15 16 var Conf = &authn.Config{} 17 18 func configHandler(w http.ResponseWriter, r *http.Request) { 19 switch r.Method { 20 case http.MethodGet: 21 httpConfigGet(w, r) 22 case http.MethodPut: 23 httpConfigPut(w, r) 24 default: 25 cmn.WriteErr405(w, r, http.MethodPut, http.MethodGet) 26 } 27 } 28 29 func httpConfigGet(w http.ResponseWriter, r *http.Request) { 30 if err := validateAdminPerms(w, r); err != nil { 31 return 32 } 33 Conf.RLock() 34 writeJSON(w, Conf, "config") 35 Conf.RUnlock() 36 } 37 38 func httpConfigPut(w http.ResponseWriter, r *http.Request) { 39 if err := validateAdminPerms(w, r); err != nil { 40 return 41 } 42 updateCfg := &authn.ConfigToUpdate{} 43 if err := jsoniter.NewDecoder(r.Body).Decode(updateCfg); err != nil { 44 cmn.WriteErrMsg(w, r, "Invalid request") 45 return 46 } 47 if err := Conf.ApplyUpdate(updateCfg); err != nil { 48 cmn.WriteErr(w, r, err) 49 return 50 } 51 if err := jsp.SaveMeta(configPath, Conf, nil); err != nil { 52 cmn.WriteErr(w, r, err) 53 } 54 }