Skip to content

Commit 9acb905

Browse files
committed
forbid deleting of onlogs container
1 parent 90bad14 commit 9acb905

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

application/backend/app/daemon/daemon.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,17 @@ func GetContainersList() []string {
194194

195195
return names
196196
}
197+
198+
func GetContainerImageNameByContainerID(containerID string) string {
199+
body := string(makeSocketRequest("containers/" + containerID + "/json"))
200+
body = strings.Split(body, "\r\n\r\n")[1]
201+
var result map[string]any
202+
json.Unmarshal([]byte(body), &result)
203+
204+
if result["Config"] == nil {
205+
return ""
206+
}
207+
208+
imageName := fmt.Sprintf("%v", result["Config"].(map[string]any)["Image"])
209+
return imageName
210+
}

application/backend/app/routes/routes.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,9 +713,14 @@ func DeleteContainer(w http.ResponseWriter, req *http.Request) {
713713
decoder := json.NewDecoder(req.Body)
714714
decoder.Decode(&logItem)
715715

716-
if (logItem.Host == "" || logItem.Host == util.GetHost()) && strings.Contains(logItem.Service, "onlogs") {
717-
w.WriteHeader(http.StatusForbidden)
718-
json.NewEncoder(w).Encode(map[string]interface{}{"error": "Can't delete myself!"})
716+
if logItem.Host == "" || logItem.Host == util.GetHost() {
717+
dockerContainerID := util.GetDockerContainerID(logItem.Host, logItem.Service)
718+
dockerImage := daemon.GetContainerImageNameByContainerID(dockerContainerID)
719+
if strings.Contains(dockerImage, "devforth/onlogs") {
720+
w.WriteHeader(http.StatusForbidden)
721+
json.NewEncoder(w).Encode(map[string]interface{}{"error": "Can't delete logs of OnLogs container!"})
722+
return
723+
}
719724
}
720725

721726
go containerdb.DeleteContainer(logItem.Host, logItem.Service, true)

0 commit comments

Comments
 (0)