Skip to content

Commit cf7d77c

Browse files
now we add defect dojo post install scripts
1 parent 2de1d04 commit cf7d77c

6 files changed

Lines changed: 696 additions & 0 deletions

File tree

New-Deployment.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
param (
2+
[Parameter()]
3+
[string]$nameSuffix = "ek005",
4+
[Parameter()]
5+
[string]$deploymentName = "deploy-rg-fnapp-$nameSuffix",
6+
[Parameter()]
7+
[string]$location = "canadacentral",
8+
[Parameter()]
9+
[string]$templateFile = "infra/main.bicep",
10+
[Parameter()]
11+
[string]$resourceGroupName = "rg-fnapp-$nameSuffix"
12+
)
13+
14+
# echo parameters
15+
Write-Host "deploymentName: $deploymentName"
16+
Write-Host "location: $location"
17+
Write-Host "templateFile: $templateFile"
18+
Write-Host "nameSuffix: $nameSuffix"
19+
Write-Host "resourceGroupName: $resourceGroupName"
20+
21+
# create resource group
22+
az group create --name $resourceGroupName `
23+
--location $location
24+
25+
az deployment group create --name $deploymentName `
26+
--resource-group $resourceGroupName `
27+
--template-file $templateFile `
28+
--parameters nameSuffix="$nameSuffix"

New-GitHubFederatedIdentity.ps1

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
param (
2+
[Parameter()]
3+
[string]
4+
$displayName = "GH__devopsabcs_engineering__WRKSHP_FunctionApps", #"<your-service-principal-name>"
5+
[Parameter()]
6+
[string]
7+
$githubRepo = "devopsabcs-engineering/WRKSHP_FunctionApps", #"<your-github-username>/<your-repo-name>"
8+
[Parameter()]
9+
[string]
10+
$subscriptionId = "64c3d212-40ed-4c6d-a825-6adfbdf25dad", #"<your-subscription-id>"
11+
[Parameter()]
12+
[string]
13+
$tenantId = "aa93b9d9-037d-4f08-a26d-783cff0e2369", #"<your-tenant-id>"
14+
[Parameter()]
15+
[string]
16+
$clientId = ""
17+
)
18+
19+
# echo parameters
20+
Write-Output "displayName: $displayName"
21+
Write-Output "githubRepo: $githubRepo"
22+
Write-Output "subscriptionId: $subscriptionId"
23+
Write-Output "tenantId: $tenantId"
24+
Write-Output "clientId: $clientId"
25+
26+
# create azure credentials for the pipeline in github actions
27+
28+
# Login to Azure
29+
#az login --service-principal -u "<your-service-principal-id>" -p "<your-service-principal-secret>" --tenant $tenantId
30+
az login --tenant $tenantId
31+
32+
# Create the federated service principal
33+
$sp = az ad sp create-for-rbac --name $displayName --role Contributor `
34+
--scopes /subscriptions/$subscriptionId `
35+
--query "{clientId: appId, clientSecret: password}" -o json | ConvertFrom-Json
36+
$clientId = $sp.clientId
37+
38+
# get object id from app registration
39+
$objectId = az ad app show --id $clientId --query id -o tsv
40+
Write-Output "Service principal object ID: $objectId"
41+
42+
43+
Write-Output "Service principal created."
44+
Write-Output "Client ID: $clientId"
45+
46+
# read credentials.json from file
47+
#$credentialRaw = Get-Content -Path "credential.json" -Raw
48+
$credentialRaw =
49+
@'
50+
{
51+
"name": "__CREDENTIAL_NAME__",
52+
"issuer": "https://token.actions.githubusercontent.com",
53+
"subject": "__SUBJECT__",
54+
"audiences": [
55+
"api://AzureADTokenExchange"
56+
]
57+
}
58+
'@
59+
60+
# find and replace the placeholders with the actual values
61+
$credential = $credentialRaw -replace "__CREDENTIAL_NAME__", $displayName -replace "__SUBJECT__", "repo:${githubRepo}:ref:refs/heads/main"
62+
63+
#$appId = "<Your-App-Id>"
64+
$credential = $credential | ConvertFrom-Json
65+
Write-Output "Credential: $credential"
66+
New-AzADAppFederatedCredential -ApplicationObjectId $objectId -Name $credential.name `
67+
-Issuer $credential.issuer -Subject $credential.subject `
68+
-Audience $credential.audiences
69+
70+
gh auth login
71+
72+
# Push secrets to GitHub
73+
$secrets = @{
74+
AZURE_CLIENT_ID = $clientId
75+
AZURE_TENANT_ID = $tenantId
76+
AZURE_SUBSCRIPTION_ID = $subscriptionId
77+
}
78+
79+
# use gh cli to push secrets to github
80+
foreach ($secret in $secrets.GetEnumerator()) {
81+
$value = $secret.Value
82+
$name = $secret.Key
83+
gh secret set $name -b $value -R $githubRepo
84+
}
85+
86+
Write-Output "Federated service principal created and secrets pushed to GitHub."

Validate-Deployment.ps1

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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"
14+
15+
#az account show
16+
# get functionAppName
17+
$functionAppName = az deployment group show --resource-group $resourceGroupName `
18+
--name $deploymentName `
19+
--query properties.outputs.azureFunctionName.value `
20+
-o tsv
21+
Write-Host "functionAppName: $functionAppName"
22+
23+
# get testUrl with function key
24+
$functionKey = az functionapp keys list --name $functionAppName `
25+
--resource-group $resourceGroupName `
26+
--query functionKeys.default `
27+
-o tsv
28+
29+
Write-Host "functionKey: $functionKey"
30+
31+
$addNameParameter = $true
32+
33+
if ($addNameParameter) {
34+
Write-Host "Adding name parameter to testUrl"
35+
$testUrl = "https://$functionAppName.azurewebsites.net/api/HttpExample?name=AzureDevOpsSmokeTest&code=$functionKey"
36+
}
37+
else {
38+
$testUrl = "https://$functionAppName.azurewebsites.net/api/HttpExample?code=$functionKey"
39+
}
40+
41+
Write-Host "testUrl: $testUrl"
42+
43+
# test function
44+
$testResult = Invoke-RestMethod -Uri $testUrl
45+
Write-Host "testResult: $testResult"
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
param (
2+
[Parameter()]
3+
[string]$nameSuffix = "ek005",
4+
[Parameter()]
5+
[string]$deploymentName = "deploy-rg-defectdojo-$nameSuffix",
6+
[Parameter()]
7+
[string]$location = "canadacentral",
8+
[Parameter()]
9+
[string]$templateFile = "main.bicep",
10+
[Parameter()]
11+
[string]$resourceGroupName = "rg-defectdojo-$nameSuffix",
12+
[Parameter()]
13+
[string]$subscriptionId = "IT Test",
14+
[Parameter()]
15+
[string]$sshKeyPath = "$HOME\.ssh\vm-defectdojo-${nameSuffix}-id_rsa"
16+
)
17+
18+
# function to generate random password
19+
function New-Password {
20+
param (
21+
[int]$length = 32
22+
)
23+
24+
$chars = [char[]]('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=[]{}|;:,.<>?')
25+
$password = -join ($chars | Get-Random -Count $length)
26+
return $password
27+
}
28+
29+
# install az cli if not already installed
30+
if (-not (Get-Command az -ErrorAction SilentlyContinue)) {
31+
Write-Output "Installing az cli"
32+
Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://aka.ms/installazurecliwindows')
33+
}
34+
else {
35+
Write-Output "az cli already installed"
36+
}
37+
38+
# login
39+
Write-Output "Logging in to Azure"
40+
az login
41+
42+
# set subscription
43+
Write-Output "Setting subscription to $subscriptionId"
44+
az account set --subscription "$subscriptionId"
45+
46+
# echo parameters
47+
Write-Output "nameSuffix: $nameSuffix"
48+
Write-Output "deploymentName: $deploymentName"
49+
Write-Output "location: $location"
50+
Write-Output "templateFile: $templateFile"
51+
Write-Output "resourceGroupName: $resourceGroupName"
52+
53+
# deploy
54+
# create resource group
55+
Write-Output "Creating resource group $resourceGroupName in location $location"
56+
az group create --name $resourceGroupName `
57+
--location $location
58+
59+
# generate ssh key pair
60+
Write-Output "Generating ssh key pair at $sshKeyPath"
61+
if (-not (Test-Path $sshKeyPath)) {
62+
ssh-keygen -t rsa -b 2048 -f $sshKeyPath -q -N ""
63+
}
64+
else {
65+
Write-Output "ssh key pair already exists"
66+
}
67+
68+
# echo ssh public key
69+
Write-Output "Public key:"
70+
$sshPublicKey = Get-Content "$sshKeyPath.pub"
71+
Write-Output $sshPublicKey
72+
73+
# generate random password for postgresql
74+
$password = New-Password -length 32
75+
Write-Output "Generated password for PostgreSQL: $password"
76+
77+
# deploy bicep
78+
Write-Output "Deploying bicep template $templateFile to resource group $resourceGroupName"
79+
az deployment group create `
80+
--name $deploymentName `
81+
--resource-group $resourceGroupName `
82+
--template-file main.bicep `
83+
--parameters sshPublicKey="`"$sshPublicKey`"" `
84+
--parameters administratorLoginPassword="`"$password`"" `
85+
--parameters nameSuffix="`"$nameSuffix`""
86+
87+
# output vm public ip address from deployment output
88+
$fqdn = (az deployment group show `
89+
--name $deploymentName `
90+
--resource-group $resourceGroupName `
91+
--query "properties.outputs.fqdn.value" `
92+
--output tsv)
93+
94+
Write-Output "DefectDojo is deployed at $fqdn"
95+
96+
# output postgresql fqdn from deployment output
97+
$fullyQualifiedDomainName = (az deployment group show `
98+
--name $deploymentName `
99+
--resource-group $resourceGroupName `
100+
--query "properties.outputs.fullyQualifiedDomainName.value" `
101+
--output tsv)
102+
103+
Write-Output "PostgreSQL is deployed at $fullyQualifiedDomainName"
104+
105+
# output admin username from deployment output
106+
$adminUsername = (az deployment group show `
107+
--name $deploymentName `
108+
--resource-group $resourceGroupName `
109+
--query "properties.outputs.adminUsername.value" `
110+
--output tsv)
111+
112+
Write-Output "Admin username is $adminUsername"
113+
114+
# get psql password from deployment output
115+
$administratorLogin = (az deployment group show `
116+
--name $deploymentName `
117+
--resource-group $resourceGroupName `
118+
--query "properties.outputs.administratorLogin.value" `
119+
--output tsv)
120+
121+
# give ssh instructions
122+
Write-Output "To ssh into the VM, run the following command:"
123+
Write-Output "ssh -i $sshKeyPath $adminUsername@$fqdn"
124+
125+
# give psql instructions
126+
Write-Output "To connect to PostgreSQL, run the following command:"
127+
Write-Output "psql -h $fullyQualifiedDomainName -U $administratorLogin -P $password"

0 commit comments

Comments
 (0)