-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret.go
89 lines (73 loc) · 2.89 KB
/
secret.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) Autovia GmbH
// SPDX-License-Identifier: Apache-2.0
package k8s
import (
"context"
"log"
"net/http"
S "github.com/autovia/flightdeck/api/structs"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func SecretHandler(app *S.App, c *S.Client, w http.ResponseWriter, r *http.Request) error {
url := S.GetRequestParams(r, "/api/v1/secret/")
log.Printf("SecretHandler url: %v", url)
secret, err := c.Clientset.CoreV1().Secrets(url.Scope).Get(context.TODO(), url.Resource, metav1.GetOptions{})
if err != nil {
return S.RespondError(err)
}
secret.ObjectMeta.ManagedFields = nil
return S.RespondFormat(r, w, http.StatusOK, secret)
}
func SecretPodListHandler(app *S.App, c *S.Client, w http.ResponseWriter, r *http.Request) error {
url := S.GetRequestParams(r, "/api/v1/graph/secret/")
log.Printf("SecretHandler url: %v", url)
g := S.Graph{Nodes: []S.Node{}, Edges: []S.Edge{}}
secnode := g.AddNode("secret", url.Resource, url.Resource, S.NodeOptions{Namespace: url.Scope, Type: "secret"})
podList, err := c.Clientset.CoreV1().Pods(url.Scope).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return S.RespondError(err)
}
for _, pod := range podList.Items {
for _, volume := range pod.Spec.Volumes {
if volume.Secret != nil {
if volume.Secret.SecretName == url.Resource {
if !g.Includes(pod.ObjectMeta.Name) {
podnode := g.AddNode("pod", string(pod.ObjectMeta.UID), pod.ObjectMeta.Name, S.NodeOptions{Namespace: url.Scope, Type: "pod"})
g.AddEdge(secnode, podnode)
}
}
}
if volume.Projected != nil {
for _, source := range volume.Projected.Sources {
if source.Secret != nil {
if source.Secret.Name == url.Resource {
if !g.Includes(pod.ObjectMeta.Name) {
podnode := g.AddNode("pod", string(pod.ObjectMeta.UID), pod.ObjectMeta.Name, S.NodeOptions{Namespace: url.Scope, Type: "pod"})
g.AddEdge(secnode, podnode)
}
}
}
}
}
}
}
return S.RespondJSON(w, http.StatusOK, g)
}
func NamespaceSecretListHandler(app *S.App, c *S.Client, w http.ResponseWriter, r *http.Request) error {
url := S.GetRequestParams(r, "/api/v1/namespace/secret/")
log.Printf("NamespaceSecretListHandler url: %v", url)
g := S.Graph{Nodes: []S.Node{}, Edges: []S.Edge{}}
ns, err := c.Clientset.CoreV1().Namespaces().Get(context.TODO(), url.Scope, metav1.GetOptions{})
if err != nil {
return S.RespondError(err)
}
node := g.AddNode("ns", string(ns.ObjectMeta.UID), ns.ObjectMeta.Name, S.NodeOptions{Type: "namespace", Group: true})
secList, err := c.Clientset.CoreV1().Secrets(url.Scope).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return S.RespondError(err)
}
for _, cm := range secList.Items {
g.AddNode("secret", string(cm.ObjectMeta.UID), cm.ObjectMeta.Name, S.NodeOptions{Namespace: url.Scope, Type: "secret", ParentNode: node, Extent: "parent"})
}
return S.RespondJSON(w, http.StatusOK, g)
}