-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
319 lines (301 loc) · 13.8 KB
/
Jenkinsfile
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
pipeline {
agent any
environment {
TF_IN_AUTOMATION = 'true'
TF_CLI_CONFIG_FILE = credentials('terraform_creds')
AZURE = credentials('Azure_Service_Principal')
AWS_CREDENTIALS_ID = 'aws-credential' // The ID for AWS credentials stored in Jenkins
//GCP = credentials('GCP_Credentials')
EC2_SSH_KEY = credentials('ec2_ssh')
NGROK_TOKEN = credentials('ngrok_token')
}
stages {
stage('Select Cloud Providers') {
steps {
script {
def selectedClouds = input message: 'Select Cloud Providers to deploy to:', parameters: [
booleanParam(defaultValue: true, description: 'Deploy to Azure', name: 'Azure'),
booleanParam(defaultValue: true, description: 'Deploy to AWS', name: 'AWS'),
booleanParam(defaultValue: true, description: 'Deploy to GCP', name: 'GCP')
]
// Store the selected options in variables
env.DEPLOY_AZURE = selectedClouds['Azure'] ? 'true' : 'false'
env.DEPLOY_AWS = selectedClouds['AWS'] ? 'true' : 'false'
env.DEPLOY_GCP = selectedClouds['GCP'] ? 'true' : 'false'
echo "Selected Cloud Providers: Azure=${env.DEPLOY_AZURE}, AWS=${env.DEPLOY_AWS}, GCP=${env.DEPLOY_GCP}"
}
}
}
stage('Terraform: Init') {
parallel {
stage('Init Azure') {
when {
expression { env.DEPLOY_AZURE == 'true' }
}
steps {
dir('Azure') {
sh 'terraform init -no-color'
}
}
}
stage('Init AWS') {
when {
expression { env.DEPLOY_AWS == 'true' }
}
steps {
dir('AWS') {
withAWS(credentials: "${env.AWS_CREDENTIALS_ID}") {
sh 'terraform init -no-color'
}
}
}
}
stage('Init GCP') {
when {
expression { env.DEPLOY_GCP == 'true' }
}
steps {
echo "GCP Init stage - Dummy"
// Add GCP initialization commands here in the future
}
}
}
}
stage('Terraform: Plan') {
parallel {
stage('Plan Azure') {
when {
expression { env.DEPLOY_AZURE == 'true' }
}
steps {
dir('Azure') {
sh 'az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID'
sh 'terraform plan -no-color -var "AZURE_CLIENT_ID=${AZURE_CLIENT_ID}" -var "AZURE_CLIENT_SECRET=${AZURE_CLIENT_SECRET}" -var "AZURE_TENANT_ID=${AZURE_TENANT_ID}" -var "AZURE_SUBSCRIPTION_ID=${AZURE_SUBSCRIPTION_ID}"'
}
}
}
stage('Plan AWS') {
when {
expression { env.DEPLOY_AWS == 'true' }
}
steps {
dir('AWS') {
withAWS(credentials: "${env.AWS_CREDENTIALS_ID}") {
sh 'terraform plan -no-color'
}
}
}
}
stage('Plan GCP') {
when {
expression { env.DEPLOY_GCP == 'true' }
}
steps {
echo "GCP Plan stage - Dummy"
// Add GCP plan commands here in the future
}
}
}
}
stage('Terraform: Apply') {
parallel {
stage('Apply Azure') {
when {
expression { env.DEPLOY_AZURE == 'true' }
}
steps {
dir('Azure') {
sh 'terraform apply -auto-approve -no-color -var "AZURE_CLIENT_ID=${AZURE_CLIENT_ID}" -var "AZURE_CLIENT_SECRET=${AZURE_CLIENT_SECRET}" -var "AZURE_TENANT_ID=${AZURE_TENANT_ID}" -var "AZURE_SUBSCRIPTION_ID=${AZURE_SUBSCRIPTION_ID}"'
script {
waflab_vm_ip_address = sh(script: "terraform output waflab_vm_ip_address", returnStdout: true).trim()
waflab_appgw_url = sh(script: "terraform output waflab_appgw_url", returnStdout: true).trim()
}
}
}
}
stage('Apply AWS') {
when {
expression { env.DEPLOY_AWS == 'true' }
}
steps {
dir('AWS') {
withAWS(credentials: "${env.AWS_CREDENTIALS_ID}") {
sh 'terraform apply -auto-approve -no-color'
script {
aws_instance_ip = sh(script: "terraform output ec2_public_ip", returnStdout: true).trim()
aws_lb_dns = sh(script: "terraform output load_balancer_dns", returnStdout: true).trim()
}
}
}
}
}
stage('Apply GCP') {
when {
expression { env.DEPLOY_GCP == 'true' }
}
steps {
echo "GCP Apply stage - Dummy"
// Add GCP apply commands here in the future
}
}
}
}
stage('Ansible: Deploy OWASP JuiceShop (Azure only)') {
when {
expression { env.DEPLOY_AZURE == 'true' }
}
steps {
dir('Azure') {
sh "ansible-playbook ./deploy-owasp-juiceshop.yml -u adminuser --private-key ${EC2_SSH_KEY} --extra-vars 'waflab_vm_ip_address=${waflab_vm_ip_address}'"
}
}
}
stage('Decide on GoTestWAF Execution') {
when {
expression { return env.DEPLOY_AZURE == 'true' || env.DEPLOY_AWS == 'true' || env.DEPLOY_GCP == 'true' }
}
steps {
script {
def runGoTestWAF_Azure = false
def runGoTestWAF_AWS = false
def runGoTestWAF_GCP = false
if (env.DEPLOY_AZURE == 'true') {
runGoTestWAF_Azure = input(message: 'Run GoTestWAF on Azure?', ok: 'Proceed', parameters: [booleanParam(defaultValue: true, description: 'Run GoTestWAF on Azure?', name: 'RunGoTestWAF_Azure')])
}
if (env.DEPLOY_AWS == 'true') {
runGoTestWAF_AWS = input(message: 'Run GoTestWAF on AWS?', ok: 'Proceed', parameters: [booleanParam(defaultValue: true, description: 'Run GoTestWAF on AWS?', name: 'RunGoTestWAF_AWS')])
}
if (env.DEPLOY_GCP == 'true') {
runGoTestWAF_GCP = input(message: 'Run GoTestWAF on GCP?', ok: 'Proceed', parameters: [booleanParam(defaultValue: true, description: 'Run GoTestWAF on GCP?', name: 'RunGoTestWAF_GCP')])
}
env.RUN_GOTESTWAF_AZURE = runGoTestWAF_Azure ? 'true' : 'false'
env.RUN_GOTESTWAF_AWS = runGoTestWAF_AWS ? 'true' : 'false'
env.RUN_GOTESTWAF_GCP = runGoTestWAF_GCP ? 'true' : 'false'
}
}
}
stage('Run GoTestWAF Report') {
parallel {
stage('Test Azure WAF') {
when {
expression { env.DEPLOY_AZURE == 'true' && env.RUN_GOTESTWAF_AZURE == 'true' }
}
steps {
dir('Azure') { // Use Azure directory for the Azure GoTestWAF report
sh "docker pull wallarm/gotestwaf:latest"
sh "docker run --user root --rm --network='host' -v /var/lib/jenkins/reports/Azure:/app/reports wallarm/gotestwaf --reportFormat=html --includePayloads=true --skipWAFIdentification --noEmailReport --url ${waflab_appgw_url}/#/"
}
}
}
stage('Test AWS WAF') {
when {
expression { env.DEPLOY_AWS == 'true' && env.RUN_GOTESTWAF_AWS == 'true' }
}
steps {
dir('AWS') { // Use AWS directory for the AWS GoTestWAF report
sh "docker pull wallarm/gotestwaf:latest"
sh "docker run --user root --rm --network='host' -v /var/lib/jenkins/reports/AWS:/app/reports wallarm/gotestwaf --reportFormat=html --includePayloads=true --skipWAFIdentification --noEmailReport --url http://${aws_lb_dns}/#/"
}
}
}
stage('Test GCP WAF') {
when {
expression { env.DEPLOY_GCP == 'true' && env.RUN_GOTESTWAF_GCP == 'true' }
}
steps {
dir('GCP') { // Use GCP directory for the GCP GoTestWAF report
echo "GCP GoTestWAF stage - Dummy"
// Add GCP GoTestWAF commands here in the future
}
}
}
}
}
stage('Start HTTP Server and ngrok') {
when {
expression { return env.DEPLOY_AZURE == 'true' || env.DEPLOY_AWS == 'true' || env.DEPLOY_GCP == 'true' }
}
steps {
script {
def ngrokToken = env.NGROK_TOKEN
sh "ansible-playbook ./deploy-http-ngrok.yml -e 'ngrok_token=${ngrokToken}'"
sleep 5
}
}
}
stage('Get ngrok URL') {
when {
expression { return env.DEPLOY_AZURE == 'true' || env.DEPLOY_AWS == 'true' || env.DEPLOY_GCP == 'true' }
}
steps {
script {
def ngrokInfo = sh(script: 'curl -s http://localhost:4040/api/tunnels', returnStdout: true).trim()
def url = readJSON text: ngrokInfo
echo "ngrok URL: ${url.tunnels[0]?.public_url}"
}
}
}
stage('Prompt for Destroy Confirmation') {
when {
expression { return env.DEPLOY_AZURE == 'true' || env.DEPLOY_AWS == 'true' || env.DEPLOY_GCP == 'true' }
}
steps {
script {
def destroyAzure = false
def destroyAWS = false
def destroyGCP = false
if (env.DEPLOY_AZURE == 'true') {
destroyAzure = input(message: 'Destroy Azure Resources?', parameters: [booleanParam(defaultValue: false, name: 'confirm')])
}
if (env.DEPLOY_AWS == 'true') {
destroyAWS = input(message: 'Destroy AWS Resources?', parameters: [booleanParam(defaultValue: false, name: 'confirm')])
}
if (env.DEPLOY_GCP == 'true') {
destroyGCP = input(message: 'Destroy GCP Resources?', parameters: [booleanParam(defaultValue: false, name: 'confirm')])
}
env.DESTROY_AZURE = destroyAzure ? 'true' : 'false'
env.DESTROY_AWS = destroyAWS ? 'true' : 'false'
env.DESTROY_GCP = destroyGCP ? 'true' : 'false'
}
}
}
stage('Terraform: Destroy') {
when {
expression { env.DESTROY_AZURE == 'true' || env.DESTROY_AWS == 'true' || env.DESTROY_GCP == 'true' }
}
parallel {
stage('Destroy Azure') {
when {
expression { env.DESTROY_AZURE == 'true' }
}
steps {
dir('Azure') {
sh 'terraform destroy -auto-approve -no-color -var "AZURE_CLIENT_ID=${AZURE_CLIENT_ID}" -var "AZURE_CLIENT_SECRET=${AZURE_CLIENT_SECRET}" -var "AZURE_TENANT_ID=${AZURE_TENANT_ID}" -var "AZURE_SUBSCRIPTION_ID=${AZURE_SUBSCRIPTION_ID}"'
}
}
}
stage('Destroy AWS') {
when {
expression { env.DESTROY_AWS == 'true' }
}
steps {
dir('AWS') {
withAWS(credentials: "${env.AWS_CREDENTIALS_ID}") {
sh 'terraform destroy -auto-approve -no-color'
}
}
}
}
stage('Destroy GCP') {
when {
expression { env.DESTROY_GCP == 'true' }
}
steps {
echo "GCP Destroy stage - Dummy"
// Add GCP destroy commands here in the future
}
}
}
}
}
}