Skip to content

Commit 12396ca

Browse files
Merge pull request #6 from devopsabcs-engineering/main
Added more scripts to deploy + made basic pipeline work
2 parents 4020d9c + 6f36ec8 commit 12396ca

15 files changed

+530
-0
lines changed

ENGAGEMENTID.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7

Validate-DefectDojo.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
param (
2+
[Parameter()]
3+
[string]$nameSuffix = "ek002",
4+
[Parameter()]
5+
[string]$deploymentName = "deploy-rg-fnapp-$nameSuffix",
6+
[Parameter()]
7+
[string]$resourceGroupName = "rg-fnapp-$nameSuffix"
8+
)
9+
10+
# echo parameters
11+
Write-Host "deploymentName: $deploymentName"
12+
Write-Host "nameSuffix: $nameSuffix"
13+
Write-Host "resourceGroupName: $resourceGroupName"
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
param (
2+
[Parameter()]
3+
[string]$nameSuffix = "ek002",
4+
[Parameter()]
5+
[string]$deploymentName = "deploy-rg-aks-$nameSuffix",
6+
[Parameter()]
7+
[string]$resourceGroupName = "rg-aks-$nameSuffix",
8+
[Parameter()]
9+
[string]$location = "canadacentral",
10+
[Parameter()]
11+
[string]$templateFile = "main.bicep",
12+
[Parameter()]
13+
[string]$clusterName = "aks-cluster-$nameSuffix",
14+
[Parameter()]
15+
[string]$dnsPrefix = "$nameSuffix",
16+
[Parameter()]
17+
[string]$linuxAdminUsername = "azureuser",
18+
[Parameter()]
19+
[int]$agentCount = 1,
20+
[Parameter()]
21+
[string]$sshKeyPath = "$HOME\.ssh\aks-${nameSuffix}-id_rsa"
22+
)
23+
24+
# echo parameters
25+
Write-Host "deploymentName: $deploymentName"
26+
Write-Host "nameSuffix: $nameSuffix"
27+
Write-Host "resourceGroupName: $resourceGroupName"
28+
Write-Host "location: $location"
29+
Write-Host "templateFile: $templateFile"
30+
Write-Host "clusterName: $clusterName"
31+
Write-Host "dnsPrefix: $dnsPrefix"
32+
Write-Host "linuxAdminUsername: $linuxAdminUsername"
33+
Write-Host "agentCount: $agentCount"
34+
35+
Write-Output "Creating resource group $resourceGroupName in location $location"
36+
37+
# create resource group
38+
az group create --name $resourceGroupName `
39+
--location $location
40+
41+
# generate ssh key pair
42+
Write-Output "Generating ssh key pair at $sshKeyPath"
43+
if (-not (Test-Path $sshKeyPath)) {
44+
ssh-keygen -t rsa -b 2048 -f $sshKeyPath -q -N ""
45+
}
46+
else {
47+
Write-Output "ssh key pair already exists"
48+
}
49+
50+
# echo ssh public key
51+
Write-Output "Public key:"
52+
$sshPublicKey = Get-Content "$sshKeyPath.pub"
53+
Write-Output $sshPublicKey
54+
55+
Write-Output "Deploying AKS cluster $clusterName in resource group $resourceGroupName"
56+
57+
# deploy aks cluster
58+
az deployment group create --resource-group $resourceGroupName `
59+
--name $deploymentName `
60+
--template-file $templateFile `
61+
--parameters clusterName=$clusterName `
62+
--parameters dnsPrefix=$dnsPrefix `
63+
--parameters linuxAdminUsername=$linuxAdminUsername `
64+
--parameters agentCount=$agentCount `
65+
--parameters sshRSAPublicKey="`"$sshPublicKey`""
66+
67+
# output aks cluster fqdn from deployment output
68+
$fqdn = (az deployment group show `
69+
--name $deploymentName `
70+
--resource-group $resourceGroupName `
71+
--query "properties.outputs.fqdn.value" `
72+
--output tsv)
73+
74+
Write-Output "AKS cluster is deployed at $fqdn"
75+
76+
# give instructions to connect to the cluster
77+
Write-Output "To connect to the cluster, run the following command:"
78+
Write-Output "az aks get-credentials --resource-group $resourceGroupName --name $clusterName --overwrite-existing"
79+
80+
# # give instructions on how to ssh into the cluster
81+
# Write-Output "To ssh into the cluster, run the following command:"
82+
# Write-Output "ssh -i $sshKeyPath $linuxAdminUsername@$fqdn"
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
param (
2+
[Parameter()]
3+
[string]$manifestTemplateFolder = "./manifests",
4+
[Parameter()]
5+
[string]$IMAGE = "devopsshield/devsecops-pygoat",
6+
[Parameter()]
7+
[string]$TAG = "latest",
8+
[Parameter()]
9+
[string]$dnsResourceGroupName = "rg-dns-prod",
10+
[Parameter()]
11+
[string]$dnsZoneName = "cad4devops.com",
12+
[Parameter()]
13+
[ValidateSet("", "-dev", "-test")]
14+
[string]$environmentSuffix = "-test", # "-dev", "-test", ""
15+
[Parameter()]
16+
[string]$dnsRecordSetName = "pygoat${environmentSuffix}",
17+
[Parameter()]
18+
[string]$HOSTURL = "${dnsRecordSetName}.${dnsZoneName}",
19+
[Parameter()]
20+
[string]$serviceName = "pygoat-svc",
21+
[Parameter()]
22+
[string]$namespace = "pygoat${environmentSuffix}",
23+
[Parameter()]
24+
[string]$subscriptionId = "Microsoft Azure Sponsorship"
25+
)
26+
# docker pull devopsshield/devsecops-pygoat:latest
27+
28+
# echo parameters
29+
Write-Host "manifestTemplateFolder: $manifestTemplateFolder"
30+
Write-Host "IMAGE: $IMAGE"
31+
Write-Host "TAG: $TAG"
32+
Write-Host "HOSTURL: $HOSTURL"
33+
Write-Host "serviceName: $serviceName"
34+
Write-Host "namespace: $namespace"
35+
Write-Host "dnsResourceGroupName: $dnsResourceGroupName"
36+
Write-Host "dnsZoneName: $dnsZoneName"
37+
Write-Host "dnsRecordSetName: $dnsRecordSetName"
38+
Write-Host "subscriptionId: $subscriptionId"
39+
Write-Host "environmentSuffix: $environmentSuffix"
40+
41+
42+
# create a namespace if it does not exist
43+
Write-Output "Creating namespace $namespace if it does not exist"
44+
kubectl create namespace $namespace --dry-run=client -o yaml | kubectl apply -f -
45+
46+
# deploy k8s manifests
47+
Write-Output "Deploying k8s manifests in folder $manifestTemplateFolder"
48+
49+
# loop through each manifest file in the folder with extension template.yaml
50+
$manifestFiles = Get-ChildItem -Path $manifestTemplateFolder -Filter "*.template.yaml"
51+
foreach ($manifestFile in $manifestFiles) {
52+
Write-Output "Processing manifest file $manifestFile"
53+
$manifestFileContent = Get-Content $manifestFile.FullName
54+
# replace #{image}# with the value of the environment variable IMAGE
55+
$manifestFileContent = $manifestFileContent -replace "#\{image\}#", $IMAGE
56+
# replace #{tag}# with the value of the environment variable TAG
57+
$manifestFileContent = $manifestFileContent -replace "#\{tag\}#", $TAG
58+
# replace #{host}# with the value of the environment variable HOST
59+
$manifestFileContent = $manifestFileContent -replace "#\{host\}#", $HOSTURL
60+
# create a new file with the same name but without the .template extension
61+
$newEnvironmentSuffix = $environmentSuffix -replace "-", "."
62+
$newManifestFile = $manifestFile.FullName -replace ".template", $newEnvironmentSuffix
63+
Write-Output "Writing processed manifest file $newManifestFile"
64+
Set-Content -Path $newManifestFile -Value $manifestFileContent
65+
# apply the manifest file
66+
Write-Output "Applying manifest file $newManifestFile"
67+
kubectl apply -f $manifestFile.FullName --namespace $namespace
68+
}
69+
70+
Write-Output "Finished deploying k8s manifests"
71+
72+
# get the external IP address of the service
73+
$service = kubectl get service $serviceName --namespace $namespace -o json | ConvertFrom-Json
74+
$externalIp = $service.status.loadBalancer.ingress[0].ip
75+
Write-Output "External IP address of the service $serviceName is $externalIp"
76+
77+
# get all pods in the namespace
78+
$pods = kubectl get pods --namespace $namespace
79+
Write-Output "Pods in namespace ${namespace}:"
80+
Write-Output $pods
81+
82+
# now get all
83+
Write-Output "Getting all resources in namespace $namespace"
84+
kubectl get all --namespace $namespace
85+
86+
# give instructions to access the service
87+
Write-Output "To access the service, open a web browser and go to http://$externalIp"
88+
89+
# open a web browser
90+
Write-Output "Opening a web browser to http://$externalIp"
91+
Start-Process "http://$externalIp"
92+
93+
# create a DNS record for the service in Azure DNS
94+
Write-Output "Creating a DNS record for the service in Azure DNS"
95+
96+
# login to Azure
97+
Write-Output "Logging in to Azure"
98+
az login
99+
100+
# set subscription
101+
Write-Output "Setting subscription to $subscriptionId"
102+
az account set --subscription "$subscriptionId"
103+
104+
# show the current subscription
105+
Write-Output "Current subscription:"
106+
az account show
107+
108+
Write-Output "Creating DNS record set $dnsRecordSetName in zone $dnsZoneName in resource group $dnsResourceGroupName"
109+
# delete the existing DNS record set if it exists
110+
Write-Output "Deleting existing DNS record set $dnsRecordSetName in zone $dnsZoneName in resource group $dnsResourceGroupName"
111+
az network dns record-set a delete `
112+
--resource-group $dnsResourceGroupName `
113+
--zone-name $dnsZoneName `
114+
--name $dnsRecordSetName `
115+
--yes
116+
Write-Output "DNS record set $dnsRecordSetName deleted in zone $dnsZoneName in resource group $dnsResourceGroupName"
117+
az network dns record-set a create `
118+
--resource-group $dnsResourceGroupName `
119+
--name $dnsRecordSetName `
120+
--zone-name $dnsZoneName
121+
Write-Output "DNS record set $dnsRecordSetName created in zone $dnsZoneName in resource group $dnsResourceGroupName"
122+
az network dns record-set a add-record `
123+
--resource-group $dnsResourceGroupName `
124+
--zone-name $dnsZoneName `
125+
--record-set-name $dnsRecordSetName `
126+
--ipv4-address $externalIp
127+
128+
Write-Output "DNS record set $dnsRecordSetName created in zone $dnsZoneName in resource group $dnsResourceGroupName"
129+
130+
Write-Output "Finished creating DNS record set"
131+
132+
# test the DNS record
133+
Write-Output "Testing the DNS record"
134+
135+
# open a web browser
136+
Write-Output "Opening a web browser to http://$HOSTURL"
137+
Start-Process "http://$HOSTURL"

infra/k8s-cluster/main.bicep

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
@description('The name of the Managed Cluster resource.')
2+
param clusterName string //= 'aks101cluster'
3+
4+
@description('The location of the Managed Cluster resource.')
5+
param location string = resourceGroup().location
6+
7+
@description('Optional DNS prefix to use with hosted Kubernetes API server FQDN.')
8+
param dnsPrefix string
9+
10+
@description('Disk size (in GB) to provision for each of the agent pool nodes. This value ranges from 0 to 1023. Specifying 0 will apply the default disk size for that agentVMSize.')
11+
@minValue(0)
12+
@maxValue(1023)
13+
param osDiskSizeGB int = 0
14+
15+
@description('The number of nodes for the cluster.')
16+
@minValue(1)
17+
@maxValue(50)
18+
param agentCount int = 3
19+
20+
@description('The size of the Virtual Machine.')
21+
param agentVMSize string = 'standard_d2s_v3'
22+
23+
@description('User name for the Linux Virtual Machines.')
24+
param linuxAdminUsername string
25+
26+
@description('Configure all linux machines with the SSH RSA public key string. Your key should include three parts, for example \'ssh-rsa AAAAB...snip...UcyupgH azureuser@linuxvm\'')
27+
param sshRSAPublicKey string
28+
29+
resource aks 'Microsoft.ContainerService/managedClusters@2024-08-01' = {
30+
name: clusterName
31+
location: location
32+
identity: {
33+
type: 'SystemAssigned'
34+
}
35+
properties: {
36+
dnsPrefix: dnsPrefix
37+
agentPoolProfiles: [
38+
{
39+
name: 'agentpool'
40+
osDiskSizeGB: osDiskSizeGB
41+
count: agentCount
42+
vmSize: agentVMSize
43+
osType: 'Linux'
44+
mode: 'System'
45+
}
46+
]
47+
linuxProfile: {
48+
adminUsername: linuxAdminUsername
49+
ssh: {
50+
publicKeys: [
51+
{
52+
keyData: sshRSAPublicKey
53+
}
54+
]
55+
}
56+
}
57+
}
58+
}
59+
60+
output fqdn string = aks.properties.fqdn
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: pygoat-app
5+
labels:
6+
app: pygoat-app
7+
spec:
8+
replicas: 1
9+
selector:
10+
matchLabels:
11+
app: pygoat-app
12+
template:
13+
metadata:
14+
labels:
15+
app: pygoat-app
16+
spec:
17+
containers:
18+
- image: #{image}#:#{tag}#
19+
name: pygoat-app
20+
ports:
21+
- containerPort: 8000
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: pygoat-app
5+
labels:
6+
app: pygoat-app
7+
spec:
8+
replicas: 1
9+
selector:
10+
matchLabels:
11+
app: pygoat-app
12+
template:
13+
metadata:
14+
labels:
15+
app: pygoat-app
16+
spec:
17+
containers:
18+
- image: devopsshield/devsecops-pygoat:latest
19+
name: pygoat-app
20+
ports:
21+
- containerPort: 8000
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: pygoat-app
5+
labels:
6+
app: pygoat-app
7+
spec:
8+
replicas: 1
9+
selector:
10+
matchLabels:
11+
app: pygoat-app
12+
template:
13+
metadata:
14+
labels:
15+
app: pygoat-app
16+
spec:
17+
containers:
18+
- image: devopsshield/devsecops-pygoat:latest
19+
name: pygoat-app
20+
ports:
21+
- containerPort: 8000
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: Ingress
3+
metadata:
4+
name: pygoat-ingress
5+
spec:
6+
ingressClassName: nginx
7+
rules:
8+
- host: #{host}#
9+
http:
10+
paths:
11+
- backend:
12+
service:
13+
name: pygoat-svc
14+
port:
15+
number: 80
16+
path: /
17+
pathType: Prefix

0 commit comments

Comments
 (0)