-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes-sa-pod-creator.sh
145 lines (129 loc) · 3.96 KB
/
kubernetes-sa-pod-creator.sh
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
# Created by Riyaz Walikar @Appsecco
# Copyright Appsecco Inc. 2024
GREEN='\033[0;32m'
COLOR_OFF='\033[0m'
echo "Appsecco Script to generate an admin kubeconfig for a specific namespace called k8s-security-assessment"
echo "Creates resources and saves the kubeconfig-sa-pod-creator.yml that needs to be shared with Appsecco"
echo
read -p "Press enter to continue ...."
# Setup of Kubernetes resources from here
echo -e "${GREEN}Create a namespace called k8s-security-assessment"
cat <<EOF1 | kubectl apply -f -
apiVersion: v1
kind: Namespace
metadata:
name: k8s-security-assessment
EOF1
echo -e "${GREEN}Create a role with admin cap called 'appsecco-pod-creator-role'${COLOR_OFF}"
# Create a role with admin capabilities to a specific namespace.
cat <<EOF2 | kubectl apply -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: appsecco-pod-creator-role
namespace: k8s-security-assessment
rules:
- apiGroups:
- ""
resources:
- '*'
verbs:
- '*'
- apiGroups:
- extensions
resources:
- '*'
verbs:
- '*'
- apiGroups:
- apps
resources:
- '*'
verbs:
- '*'
- apiGroups:
- "*"
resources:
- '*'
verbs:
- '*'
EOF2
echo -e "${GREEN}Create a rolebinding called 'appsecco-pod-creator-role-binding' to bind the admin role to a service account${COLOR_OFF}"
# Create a rolebinding to bind the role to a service account
cat <<EOF3 | kubectl apply -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: appsecco-pod-creator-role-binding
namespace: k8s-security-assessment
subjects:
- kind: ServiceAccount
name: pod-creator-sa
namespace: k8s-security-assessment
roleRef:
kind: Role
name: appsecco-pod-creator-role
apiGroup: rbac.authorization.k8s.io
EOF3
echo -e "${GREEN}Add a service account called 'pod-creator-sa'${COLOR_OFF}"
# Add a service account
cat <<EOF4 | kubectl apply -f -
apiVersion: v1
kind: ServiceAccount
metadata:
name: pod-creator-sa
namespace: k8s-security-assessment
secrets:
- name: pod-creator-sa-secret-token
EOF4
echo -e "${GREEN}Create a secret called 'pod-creator-sa-secret-token', new in Kubernetes > v1.24${COLOR_OFF}"
# Create a secret, new after v1.24
cat <<EOF5 | kubectl apply -f -
apiVersion: v1
kind: Secret
metadata:
name: pod-creator-sa-secret-token
namespace: k8s-security-assessment
annotations:
kubernetes.io/service-account.name: pod-creator-sa
type: kubernetes.io/service-account-token
EOF5
# Generate config manifest for the cluster
echo
echo
export foldername="appsecco-k8s-assessment-kubeconfigs"
if [ ! -d "$foldername" ]; then
mkdir $foldername
fi
export suffix="$(date +%d-%m-%Y-%H-%M-%S)"
echo -e "${GREEN}Generating kubeconfig in folder $foldername ${COLOR_OFF}"
export T=$TERM
export TERM=dumb
export CLUSTER_NAME=$(kubectl config current-context)
export CLUSTER_SERVER=$(kubectl cluster-info | grep --color=never "control plane" | awk '{print $NF}')
export CLUSTER_SA_SECRET_NAME=$(kubectl -n k8s-security-assessment get sa pod-creator-sa -o jsonpath='{ $.secrets[0].name }')
export CLUSTER_SA_TOKEN_NAME=$(kubectl -n k8s-security-assessment get secret | grep --color=never $CLUSTER_SA_SECRET_NAME | awk '{print $1}')
export CLUSTER_SA_TOKEN=$(kubectl -n k8s-security-assessment get secret $CLUSTER_SA_TOKEN_NAME -o "jsonpath={.data.token}" | base64 -d)
export CLUSTER_SA_CRT=$(kubectl -n k8s-security-assessment get secret $CLUSTER_SA_TOKEN_NAME -o "jsonpath={.data['ca\.crt']}")
export TERM=$T
cat <<EOF5 > $foldername/kubeconfig-sa-pod-creator-$suffix.yml
apiVersion: v1
kind: Config
users:
- name: appsecco-ns-pod-creator
user:
token: $CLUSTER_SA_TOKEN
clusters:
- cluster:
certificate-authority-data: $CLUSTER_SA_CRT
server: $CLUSTER_SERVER
name: $CLUSTER_NAME
contexts:
- context:
cluster: $CLUSTER_NAME
user: appsecco-ns-pod-creator
name: k8s-security-assessment-pod-crud
current-context: k8s-security-assessment-pod-crud
EOF5
echo -e "All done! $foldername/kubeconfig-sa-pod-creator-$suffix.yml generated. Share this file with Appsecco."