|
|
@@ -37,6 +37,7 @@ var (
|
|
|
DockerDir = "/var/lib/docker/volumes/"
|
|
|
// version server check
|
|
|
checkInterval = 5 * time.Minute
|
|
|
+ confPath = filepath.Join(BasePath, "settings", "system.json")
|
|
|
confMutex sync.Mutex
|
|
|
contMutex sync.Mutex
|
|
|
versMutex sync.Mutex
|
|
|
@@ -118,41 +119,70 @@ func UpdateConf(values map[string]interface{}) error {
|
|
|
// mutex lock to avoid race conditions
|
|
|
confMutex.Lock()
|
|
|
defer confMutex.Unlock()
|
|
|
- confPath := filepath.Join(BasePath, "settings", "system.json")
|
|
|
file, err := ioutil.ReadFile(confPath)
|
|
|
if err != nil {
|
|
|
- errmsg := fmt.Sprintf("Unable to load config: %v", err)
|
|
|
- logger.Error(errmsg)
|
|
|
- return err
|
|
|
+ return fmt.Errorf("Unable to load config: %v", err)
|
|
|
}
|
|
|
// unmarshal the config to struct
|
|
|
var configMap map[string]interface{}
|
|
|
if err := json.Unmarshal(file, &configMap); err != nil {
|
|
|
- errmsg := fmt.Sprintf("Error decoding JSON: %v", err)
|
|
|
- logger.Error(errmsg)
|
|
|
- return err
|
|
|
+ return fmt.Errorf("Error decoding JSON: %v", err)
|
|
|
}
|
|
|
// update our unmarshaled struct
|
|
|
for key, value := range values {
|
|
|
configMap[key] = value
|
|
|
}
|
|
|
+ if err = persistConf(configMap); err != nil {
|
|
|
+ return fmt.Errorf("Unable to persist config update: %v", err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// remove a tokenid from the session map if present
|
|
|
+func RemoveSession(sessionID string, fromAuthorized bool) error {
|
|
|
+ confMutex.Lock()
|
|
|
+ defer confMutex.Unlock()
|
|
|
+ confPath := filepath.Join(BasePath, "settings", "system.json")
|
|
|
+ file, err := ioutil.ReadFile(confPath)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("Unable to load config: %v", err)
|
|
|
+ }
|
|
|
+ var configMap map[string]interface{}
|
|
|
+ if err := json.Unmarshal(file, &configMap); err != nil {
|
|
|
+ return fmt.Errorf("Error decoding JSON: %v", err)
|
|
|
+ }
|
|
|
+ sessions, ok := configMap["sessions"].(map[string]interface{})
|
|
|
+ if !ok {
|
|
|
+ return fmt.Errorf("Unexpected format for sessions in config")
|
|
|
+ }
|
|
|
+ targetMapName := "unauthorized"
|
|
|
+ if fromAuthorized {
|
|
|
+ targetMapName = "authorized"
|
|
|
+ }
|
|
|
+ targetMap, ok := sessions[targetMapName].(map[string]interface{})
|
|
|
+ if !ok {
|
|
|
+ return fmt.Errorf("Unexpected format for %s in sessions", targetMapName)
|
|
|
+ }
|
|
|
+ delete(targetMap, sessionID)
|
|
|
+ if err = persistConf(configMap); err != nil {
|
|
|
+ return fmt.Errorf("Unable to persist config update: %v", err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func persistConf(configMap map[string]interface{}) error {
|
|
|
// marshal and persist it
|
|
|
updatedJSON, err := json.MarshalIndent(configMap, "", " ")
|
|
|
if err != nil {
|
|
|
- errmsg := fmt.Sprintf("Error encoding JSON: %v", err)
|
|
|
- logger.Error(errmsg)
|
|
|
- return err
|
|
|
+ return fmt.Errorf("Error encoding JSON: %v", err)
|
|
|
}
|
|
|
// update the globalConfig var
|
|
|
if err := json.Unmarshal(updatedJSON, &globalConfig); err != nil {
|
|
|
- errmsg := fmt.Sprintf("Error updating global config: %v", err)
|
|
|
- logger.Error(errmsg)
|
|
|
- return err
|
|
|
+ return fmt.Errorf("Error updating global config: %v", err)
|
|
|
}
|
|
|
+ // write to disk
|
|
|
if err := ioutil.WriteFile(confPath, updatedJSON, 0644); err != nil {
|
|
|
- errmsg := fmt.Sprintf("Error writing to file: %v", err)
|
|
|
- logger.Error(errmsg)
|
|
|
- return err
|
|
|
+ return fmt.Errorf("Error writing to file: %v", err)
|
|
|
}
|
|
|
return nil
|
|
|
}
|