-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
api_secureContainer.go
70 lines (57 loc) · 1.89 KB
/
api_secureContainer.go
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
package docker
import (
"net/http"
"encoding/json"
"os"
"github.com/azukaar/cosmos-server/src/utils"
"github.com/gorilla/mux"
)
func SecureContainerRoute(w http.ResponseWriter, req *http.Request) {
if utils.AdminOnly(w, req) != nil {
return
}
vars := mux.Vars(req)
containerName := utils.SanitizeSafe(vars["containerId"])
status := utils.Sanitize(vars["status"])
if os.Getenv("HOSTNAME") != "" && containerName == os.Getenv("HOSTNAME") {
utils.Error("SecureContainerRoute - Container cannot update itself", nil)
utils.HTTPError(w, "Container cannot update itself", http.StatusBadRequest, "DS003")
return
}
if(req.Method == "GET") {
container, err := DockerClient.ContainerInspect(DockerContext, containerName)
if err != nil {
utils.Error("ContainerSecureInscpect", err)
utils.HTTPError(w, "Internal server error: " + err.Error(), http.StatusInternalServerError, "DS002")
return
}
AddLabels(container, map[string]string{
"cosmos-force-network-secured": status,
});
// change network mode to bridge in case it was set to container
container.HostConfig.NetworkMode = "bridge"
utils.Log("API: Set Force network secured "+status+" : " + containerName)
_, errEdit := EditContainer(container.ID, container, false)
if errEdit != nil {
utils.Error("ContainerSecureEdit", errEdit)
utils.HTTPError(w, "Internal server error: " + errEdit.Error(), http.StatusInternalServerError, "DS003")
return
}
utils.TriggerEvent(
"cosmos.docker.isolate",
"Container network isolation changed",
"success",
"container@"+containerName,
map[string]interface{}{
"container": containerName,
"status": status,
})
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
})
} else {
utils.Error("UserList: Method not allowed" + req.Method, nil)
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}