-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
288 lines (204 loc) · 10.5 KB
/
main.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import os
from os.path import exists
import sys
import subprocess
import typer
def install_deps_windows():
def choco():
status = os.system("powershell -Command Set-ExecutionPolicy Bypass -Scope Process -Force; \
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; \
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))")
if status != 0:
raise("failed to install choco")
def wsl():
status = os.system("choco install wsl2")
if status != 0:
raise("failed to install wsl2")
status = os.system("wsl --set-default-version 2")
if status != 0:
raise("failed to set wsl to version 2")
def enable_windows_features():
status = os.system("powershell -Command if((Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online).State -ne 'Enabled') \
{ Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All}")
if status != 0:
raise("failed to enable hyperv")
status = os.system("powershell -Command dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart")
if status != 0:
raise("failed to enable windows subsystem for linux")
status = os.system("powershell -Command dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart")
if status != 0:
raise("failed to enable virtual machine platform")
def docker():
status = os.system("choco install -y docker-desktop")
if status != 0:
raise("failed to install docker")
def minikube():
status = os.system("powershell -Command New-Item -Path 'c:\\' -Name 'minikube' -ItemType Directory -Force")
if status != 0:
raise("failed to create directory")
status = os.system("powershell -Command Invoke-WebRequest -OutFile 'c:\\minikube\\minikube.exe' \
-Uri 'https://github.com/kubernetes/minikube/releases/latest/download/minikube-windows-amd64.exe' -UseBasicParsing")
if status != 0:
raise("failed to install minikube")
status = os.system("powershell -Command $oldpath = [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine); \
if ($oldpath.Split(';') -inotcontains 'C:\minikube') { [Environment]::SetEnvironmentVariable('Path', $('{0};C:\minikube' -f $oldPath), \
[EnvironmentVariableTarget]::Machine) }")
if status != 0:
raise("failed to add minikube to path")
def helm():
status = os.system("powershell -Command choco install -y kubernetes-helm")
if status != 0:
raise("failed to install helm")
def skaffold():
status = os.system("powershell -Command choco install -y skaffold")
if status != 0:
raise("failed to install skaffold")
def aws_cli():
status = os.system("powershell -Command choco install -y awscli")
if status != 0:
raise("failed to install aws cli")
#checks if choco exists
if exists(r'C:\ProgramData\chocolatey\bin'):
choco()
#enables important features for docker
enable_windows_features()
#set the wsl version to 2
if exists(r'C:\ProgramData\chocolatey\lib\wsl2'):
wsl()
#checks if Docker exists
if exists(r'C:\Program Files\Docker\Docker\resources\bin\docker.exe'):
docker()
#checks if minikube exists
if exists(r'c:\minikube\minikube.exe'):
minikube()
#checks if aws exists
if exists(r'C:\Program Files\Amazon\AWSCLIV2\aws.exe'):
aws_cli()
#checks if helm exists
if exists(r'C:\ProgramData\chocolatey\bin\helm.exe'):
helm()
#checks if skaffold exists
if exists(r'C:\ProgramData\chocolatey\bin\skaffold.exe'):
skaffold()
def install_deps_linux():
def minikube():
status = os.system("cd /tmp && curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 \
&& install minikube-linux-amd64 /usr/local/bin/minikube")
if status != 0:
raise("failed to install minikube")
def kubectl():
status = os.system("cd /tmp && curl -LO https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl \
&& install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl")
if status != 0:
raise("failed to install kubectl")
def helm():
status = os.system("curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash")
if status != 0:
raise("failed to install helm")
def skaffold():
status = os.system("cd /tmp && curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/latest/skaffold-linux-amd64 \
&& install skaffold /usr/local/bin/")
if status != 0:
raise("failed to install skaffold")
def aws_cli():
status = os.system("curl -fqsSL \"https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip\" -o \"/tmp/awscliv2.zip\" \
&& unzip -q -o /tmp/awscliv2.zip -d /tmp && /tmp/aws/install -b /usr/local/bin \
&& chmod +x /usr/local/bin/aws \
&& rm -rf /tmp/aws /tmp/awscliv2.zip")
if status != 0:
raise("failed to install aws cli")
#checks if minikube exists
if not exists('/usr/local/bin/minikube'):
minikube()
#checks if kubectl exists
if not exists('/usr/local/bin/kubectl') :
kubectl()
#checks if helm exists
if not exists('/usr/local/bin/helm'):
helm()
#checks if skaffold exists
if not exists('/usr/local/bin/skaffold'):
skaffold()
#checks if aws exists
if not exists('/usr/local/bin/aws') :
aws_cli()
def deploy_linux(dir, dump_file_path):
def start_minikube():
status = os.system("CHANGE_MINIKUBE_NONE_USER=true minikube start --driver=none")
if status != 0:
raise("failed to start minikube")
def create_namespace():
status = os.system(" kubectl create namespace attack; \
kubectl create namespace cymulate; \
kubectl create namespace recon")
if status != 0:
raise("failed to create namespace")
def ingress_enable():
status = os.system("minikube addons enable ingress \
&& kubectl delete -A ValidatingWebhookConfiguration ingress-nginx-admission")
if status != 0:
raise("failed to add ingresss or deleting ingress-nginx-admission")
def redis_install():
status = os.system(" helm repo add bitnami https://charts.bitnami.com/bitnami; \
helm repo update; \
helm upgrade --install -n cymulate --set cluster.enabled=false --set auth.enabled=false --set master.command=\"\" \
--set image.repository=redis --set image.tag=6.0 --set master.disableCommands="" --set replica.replicaCount=0 \
--set master.containerSecurityContext.runAsUser=0 redis bitnami/redis")
if status != 0:
raise("failed to install redis helm")
def aws_token():
status = os.system(f"echo \"@cym:registry=https://cym-dom-118330362824.d.codeartifact.us-east-1.amazonaws.com/npm/cym-repo/ \
//cym-dom-118330362824.d.codeartifact.us-east-1.amazonaws.com/npm/cym-repo/:always-auth=true \
//cym-dom-118330362824.d.codeartifact.us-east-1.amazonaws.com/npm/cym-repo/:_authToken=$(aws --region us-east-1 codeartifact get-authorization-token \
--domain cym-dom --domain-owner 118330362824 --query authorizationToken --output text)\" > {dir}/Application/.secret/.npmrc")
if status != 0:
raise("failed to find to put the token")
def mongodb_install():
status = os.system(" helm repo add bitnami https://charts.bitnami.com/bitnami; \
helm install --wait -n cymulate mongodb bitnami/mongodb --set auth.enabled=false;")
if status != 0:
raise("failed to install mongodb helm")
def mongo_express_install():
status = os.system(" helm repo add cowboysysop https://cowboysysop.github.io/charts/; \
helm install -n cymulate mongo-express cowboysysop/mongo-express --set mongodbEnableAdmin=true;")
if status != 0:
raise("failed to install mongo-express helm")
def mongo_dump():
status = os.system(f"kubectl cp {dump_file_path} cymulate$(kubectl get pods -o name -n cymulate | grep -i mongodb | sed 's/pod//'):/tmp; \
kubectl exec -n cymulate -t $(kubectl get pods -o name -n cymulate | grep -i mongodb) -- mongorestore -d cymulate /tmp/cymulate; \
kubectl exec -n cymulate -t $(kubectl get pods -o name -n cymulate | grep -i mongodb) -- rm -rf /tmp/cymulate")
if status != 0:
raise("failed to copy dump file")
def verdaccio_install():
status = os.system("helm repo add verdaccio https://charts.verdaccio.org; \
helm repo update; \
helm upgrade --install -f npm-values.yaml -n cymulate npm verdaccio/verdaccio")
if status != 0:
raise("failed to install verdaccio")
def run(cmd):
result = subprocess.run(cmd,stdout=subprocess.PIPE)
return result.stdout.decode()
# if 'Running' not in run(['minikube','status']):
# start_minikube()
if 'attack' and 'recon' and 'cymulate' not in run(['kubectl','get','ns']):
create_namespace()
if "ingress-nginx" not in run(['kubectl', 'get', 'pods', '-n', 'ingress-nginx']):
ingress_enable()
aws_token()
if 'mongodb' not in run(['helm','-n','cymulate','list']):
mongodb_install()
if 'redis' not in run(['helm','-n','cymulate','list']):
redis_install()
if 'mongo-express' not in run(['helm','-n','cymulate','list']):
mongo_express_install()
# mongo_dump()
if 'npm' not in run(['helm','-n','cymulate','list']):
verdaccio_install()
# app = typer.Typer()
# @app.command()
def main():
if sys.platform == "linux":
install_deps_linux()
deploy_linux("~/Documents/cymulate/admin","~/Downloads/cymulate")
if __name__ == '__main__':
main()