diff --git a/.gitignore b/.gitignore index 75f22e3..5589a25 100644 --- a/.gitignore +++ b/.gitignore @@ -478,3 +478,6 @@ $RECYCLE.BIN/ # Vim temporary swap files *.swp + +# IDEA files +/.idea/ diff --git a/.idea/.idea.skillSphere/.idea/.gitignore b/.idea/.idea.skillSphere/.idea/.gitignore deleted file mode 100644 index 6163235..0000000 --- a/.idea/.idea.skillSphere/.idea/.gitignore +++ /dev/null @@ -1,13 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Rider ignored files -/projectSettingsUpdater.xml -/.idea.skillSphere.iml -/contentModel.xml -/modules.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/.idea.skillSphere/.idea/dataSources.xml b/.idea/.idea.skillSphere/.idea/dataSources.xml deleted file mode 100644 index 5483c5b..0000000 --- a/.idea/.idea.skillSphere/.idea/dataSources.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - sqlserver.jb - true - com.jetbrains.jdbc.sqlserver.SqlServerDriver - Server=localhost,1433 - $ProjectFileDir$ - - - \ No newline at end of file diff --git a/.idea/.idea.skillSphere/.idea/encodings.xml b/.idea/.idea.skillSphere/.idea/encodings.xml deleted file mode 100644 index df87cf9..0000000 --- a/.idea/.idea.skillSphere/.idea/encodings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/.idea.skillSphere/.idea/indexLayout.xml b/.idea/.idea.skillSphere/.idea/indexLayout.xml deleted file mode 100644 index 7b08163..0000000 --- a/.idea/.idea.skillSphere/.idea/indexLayout.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/.idea.skillSphere/.idea/vcs.xml b/.idea/.idea.skillSphere/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/.idea.skillSphere/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.scripts/checks.ps1 b/.scripts/checks.ps1 deleted file mode 100644 index 5dbd5df..0000000 --- a/.scripts/checks.ps1 +++ /dev/null @@ -1,29 +0,0 @@ -# Check if Azure CLI is installed -$azPath = (Get-Command az -ErrorAction SilentlyContinue).Source -if (-not $azPath) { - throw "Azure CLI (az) is not installed. Please install it and try again." -} - -# Check if Azure CLI is authenticated -az account show --output none -if ($LASTEXITCODE -ne 0) { - throw "Azure CLI (az) is not authenticated. Please authenticate with Azure CLI and try again." -} - -# Check if GitHub CLI is installed -$ghPath = (Get-Command gh -ErrorAction SilentlyContinue).Source -if (-not $ghPath) { - throw "GitHub CLI (gh) is not installed. Please install it and try again." -} - -# Check if GitHub CLI is authenticated -gh auth status | Out-Null -if ($LASTEXITCODE -ne 0) { - throw "GitHub CLI (gh) is not authenticated. Please authenticate with GitHub CLI and try again." -} - -# Check if Git repo is initialised -git status | Out-Null -if ($LASTEXITCODE -ne 0) { - throw "The Git repository has not been initialised. Please create a new GitHub repository and try again." -} \ No newline at end of file diff --git a/.scripts/cleanup.ps1 b/.scripts/cleanup.ps1 deleted file mode 100644 index 824909d..0000000 --- a/.scripts/cleanup.ps1 +++ /dev/null @@ -1,78 +0,0 @@ -Param( - [String]$ProjectName -) - -$scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path -$checksScript = Join-Path $scriptRoot "checks.ps1" - -try { - . $checksScript -} catch { - Write-Host $_.Exception.Message -ForegroundColor Red - Write-Host "Setup script terminated due to the checks failure." -ForegroundColor Red - exit 1 -} - -$MissingParameterValues = $false - -if (-not $ProjectName) { - $ProjectName = $(gh repo view --json name -q '.name' 2> $null) - if (-not $ProjectName) { $MissingParameterValues = $true } -} - -$ScriptParameters = @{ - "ProjectName" = $ProjectName -} - -Write-Host -Write-Host "This script performs cleanup operations to delete resource groups, Azure AD applications, and purge deleted key vaults associated with a specific project hosted on GitHub. It searches for resources based on the project name and performs the necessary deletion and purging actions. The script leverages the Azure CLI to interact with Azure resources. It aims to facilitate the cleanup process and remove unnecessary resources from your Azure environment." -Write-Host -Write-Host "Parameters:" -ForegroundColor Green -$ScriptParameters | Format-Table -AutoSize -Write-Host - -if ($MissingParameterValues) { - Write-Host "Script execution cancelled. Missing parameter values." -ForegroundColor Red - return -} - -Write-Host "Warning: This script will perform cleanup operations, including deleting resource groups, Azure AD applications, and purging deleted key vaults starting with the project name '$ProjectName'. Make sure you understand the consequences and have verified the project name before proceeding." -ForegroundColor Red -Write-Host -Write-Host "Disclaimer: Use this script at your own risk. The author and contributors are not responsible for any loss of data or unintended consequences resulting from running this script." -ForegroundColor Yellow -Write-Host - -$confirmation = Read-Host "Do you want to continue? (y/N)" - -if ($confirmation -ne "y") { - Write-Host "Script execution cancelled." -ForegroundColor Red - return -} - -Write-Host "🔍 Searching for Resource Groups..." -$resourceGroups = az group list --query "[?starts_with(name, '$ProjectName')].name" --output tsv - -foreach ($rg in $resourceGroups) { - Write-Host "🔥 Deleting: $rg" - az group delete --name $rg --yes > $null 2>&1 -} - -Write-Host "🔍 Searching for Azure AD Applications..." -$appRegistrations = az ad app list --display-name $ProjectName --query "[].{Name:displayName, AppId:appId}" --output json | ConvertFrom-Json - -foreach ($appRegistration in $appRegistrations) { - $appName = $appRegistration.Name - $appId = $appRegistration.AppId - - Write-Host "🔥 Deleting: $appName" - az ad app delete --id $appId > $null 2>&1 -} - -Write-Host "🔍 Searching for Deleted Key Vaults..." -$deletedKeyVaults = az keyvault list-deleted --query "[?starts_with(name, 'kv-$ProjectName')].name" --output tsv - -foreach ($vaultName in $deletedKeyVaults) { - Write-Host "🔥 Purging: $vaultName" - az keyvault purge --name $vaultName > $null 2>&1 -} - -Write-Host "✅ Done" diff --git a/.scripts/environments.json b/.scripts/environments.json deleted file mode 100644 index 727fc40..0000000 --- a/.scripts/environments.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "Prd": "Production" -} diff --git a/.scripts/setup.ps1 b/.scripts/setup.ps1 deleted file mode 100644 index f178ac9..0000000 --- a/.scripts/setup.ps1 +++ /dev/null @@ -1,208 +0,0 @@ -Param( - [String]$GitHubOrganisationName, - [String]$GitHubRepositoryName, - [String]$AzureLocation, - [String]$AzureSubscriptionId, - [String]$AzureTenantId, - [ValidateLength(4, 17)] - [String]$ProjectName, - [String]$AzureSqlLogin = "SqlAdmin" -) - -$scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path -$checksScript = Join-Path $scriptRoot "checks.ps1" -$environmentsFile = Join-Path $scriptRoot "environments.json" - -try { - . $checksScript -} catch { - Write-Host $_.Exception.Message -ForegroundColor Red - Write-Host "Setup script terminated due to the checks failure." -ForegroundColor Red - exit 1 -} - -$MissingParameterValues = $false - -if (-not $GitHubOrganisationName) { - $ownerJson = gh repo view --json owner 2>$null | ConvertFrom-Json - if ($ownerJson -and $ownerJson.owner -and $ownerJson.owner.login) { - $GitHubOrganisationName = $ownerJson.owner.login - } - else { - $MissingParameterValues = $true - } -} - -if (-not $GitHubRepositoryName) { - $GitHubRepositoryName = $(gh repo view --json name -q '.name' 2> $null) - if (-not $GitHubRepositoryName) { $MissingParameterValues = $true } -} - -if (-not $AzureLocation) { - $AzureLocation = "northeurope" -} - -if (-not $AzureSubscriptionId) { - $AzureSubscriptionId = $(az account show --query id --output tsv 2> $null) - if (-not $AzureSubscriptionId) { $MissingParameterValues = $true } -} - -if (-not $AzureTenantId) { - $AzureTenantId = $(az account show --query tenantId --output tsv 2> $null) - if (-not $AzureTenantId) { $MissingParameterValues = $true } -} - -if (-not $ProjectName) { - if ($GitHubRepositoryName) { - $ProjectName = $GitHubRepositoryName - } - - if (-not $ProjectName) { $MissingParameterValues = $true } -} - -$repoUrl = "https://github.com/$GitHubOrganisationName/$GitHubRepositoryName" - -$environments = Get-Content -Raw -Path $environmentsFile | ConvertFrom-Json - -$ParametersTableData = @{ - "AzureLocation" = $AzureLocation - "AzureSubscriptionID" = $AzureSubscriptionId - "AzureTenantID" = $AzureTenantId - "GitHubOrganisationName" = $GitHubOrganisationName - "GitHubRepositoryName" = $GitHubRepositoryName - "ProjectName" = $ProjectName - "AzureSqlLogin" = $AzureSqlLogin -} - -Write-Host -Write-Host "This script automates the setup of environments, resources, and credentials for a project hosted on GitHub and deployed to Azure. It creates workload identities in Azure AD, sets up resource groups, and configures environment-specific variables and secrets in the GitHub repository. The script leverages the Azure CLI, GitHub CLI, and GitHub APIs to perform these tasks. It aims to streamline the process of setting up and configuring development, staging, and production environments for the project." -Write-Host -Write-Host "Parameters:" -ForegroundColor Green -$ParametersTableData | Format-Table -AutoSize - -if ($MissingParameterValues) { - Write-Host "Script execution cancelled. Missing parameter values." -ForegroundColor Red - exit 1 -} - -$EnvironmentTableData = foreach ($environment in $environments.PSObject.Properties) { - [PSCustomObject]@{ - Abbreviation = $environment.Name - Name = $environment.Value - } -} - -Write-Host "Environments:" -ForegroundColor Green -$EnvironmentTableData | Select-Object Name, Abbreviation | Format-Table -AutoSize -Write-Host - -Write-Host "Warning: Running this script will perform various operations in your GitHub repository and Azure subscription. Ensure that you have the necessary permissions and understand the consequences. " -ForegroundColor Red -Write-Host -Write-Host "Disclaimer: Use this script at your own risk. The author and contributors are not responsible for any loss of data or unintended consequences resulting from running this script." -ForegroundColor Yellow -Write-Host - -$confirmation = Read-Host "Do you want to continue? (y/N)" - -if ($confirmation -ne "y") { - Write-Host "Script execution cancelled." -ForegroundColor Red - return -} - -Write-Host - -function CreateWorkloadIdentity { - param ( - $environmentAbbr, - $environmentName - ) - - # Create Azure AD Application Registration - $applicationRegistrationDetails=$(az ad app create --display-name "$ProjectName$environmentAbbr") | ConvertFrom-Json - - # Create federated credentials - $credential = @{ - name="$ProjectName$environmentName"; - issuer="https://token.actions.githubusercontent.com"; - subject="repo:${GitHubOrganisationName}/${GitHubRepositoryName}:environment:$environmentName"; - audiences=@("api://AzureADTokenExchange") - } | ConvertTo-Json - - $credential | az ad app federated-credential create --id $applicationRegistrationDetails.id --parameters "@-" | Out-Null - - $credential = @{ - name="$ProjectName"; - issuer="https://token.actions.githubusercontent.com"; - subject="repo:${GitHubOrganisationName}/${GitHubRepositoryName}:ref:refs/heads/main"; - audiences=@("api://AzureADTokenExchange") - } | ConvertTo-Json - - $credential | az ad app federated-credential create --id $applicationRegistrationDetails.id --parameters "@-" | Out-Null - - return $applicationRegistrationDetails.appId -} - -function CreateResourceGroup { - param ( - $environmentAbbr, - $appId - ) - - $resourceGroupId = $(az group create --name "$ProjectName$environmentAbbr" --location $AzureLocation --query id --output tsv) - az ad sp create --id $appId - az role assignment create --assignee $appId --role Contributor --scope $resourceGroupId -} - -function CreateEnvironment { - param ( - $environmentName - ) - - $token = gh auth token - $header = @{"Authorization" = "token $token" } - $contentType = "application/json" - - $uri = "https://api.github.com/repos/$GitHubOrganisationName/$GitHubRepositoryName/environments/$environmentName" - Invoke-WebRequest -Method PUT -Header $header -ContentType $contentType -Uri $uri -} - -function GenerateRandomPassword { - param ( - [int]$Length = 16 - ) - - $ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#^_-+=?<>|~" - $Password = -join ((Get-Random -Count $Length -InputObject $ValidChars) | Get-Random -Count $Length) - - return $Password -} - -function SetVariables() { - gh variable set AZURE_TENANT_ID --body $AzureTenantId --repo $repoUrl - gh variable set AZURE_SUBSCRIPTION_ID --body $AzureSubscriptionId --repo $repoUrl - gh variable set PROJECT_NAME --body $ProjectName --repo $repoUrl -} - -function SetEnvironmentVariablesAndSecrets { - param( - $environmentAbbr, - $environmentName, - $appId - ) - - gh variable set AZURE_CLIENT_ID --body "$appId" --env $environmentName --repo $repoUrl - gh variable set AZURE_RESOURCE_GROUP_NAME --body "$ProjectName$environmentAbbr" --env $environmentName --repo $repoUrl - gh variable set AZURE_SQL_ADMINISTRATOR_USERNAME --body "$AzureSqlLogin" --env $environmentName --repo $repoUrl - gh secret set AZURE_SQL_ADMINISTRATOR_PASSWORD --body (GenerateRandomPassword) --env $environmentName --repo $repoUrl -} - -SetVariables - - $environmentAbbr = $environment.Name - $environmentName = $environment.Value - - CreateEnvironment $environmentName - $appId = CreateWorkloadIdentity $environmentAbbr $environmentName - CreateResourceGroup $environmentAbbr $appId - SetEnvironmentVariablesAndSecrets $environmentAbbr $environmentName $appId - -Write-Host "✅ Done" \ No newline at end of file