Skip to content

Commit

Permalink
Sync templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ebronson68 committed Mar 15, 2024
1 parent 15447a1 commit f8bbd17
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 158 deletions.
10 changes: 5 additions & 5 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ version-resolver:
major:
labels:
- 'major'
- 'epic'
- 'feature'
minor:
labels:
- 'minor'
- 'epic'
- 'feature'
- 'story'
- 'maintenance'
- 'research'
patch:
labels:
- 'patch'
- 'story'
- 'bug'
- 'maintenance'
- 'research'
- 'sync'
default: patch
template: |
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ jobs:
prettierPlugins: ${{ vars.INSTALL_PRETTIER_PLUGINS }}
secrets:
PAT_ACTION_CI: ${{ secrets.PAT_ACTION_CI }}

do-not-merge:
name: Disable merging on label
if: ${{ contains(github.event.pull_request.labels.*.name, 'do not merge') }}
runs-on: ubuntu-latest
steps:
- name: Check for label
run: |
echo "Pull request is labeled as 'do not merge'"
echo "This workflow fails so that the pull request cannot be merged"
exit 1
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,20 @@ mono_crash.*
*Service*/[Ll]og/
*Service*/[Ll]ogs/
*Service*/bin/
SharedLibraries/*/[Dd]ebug/
SharedLibraries/*/[Dd]ebugPublic/
SharedLibraries/*/[Rr]elease/
SharedLibraries/*/[Rr]eleases/
SharedLibraries/*/x64/
SharedLibraries/*/x86/
SharedLibraries/*/[Ww][Ii][Nn]32/
SharedLibraries/*/[Aa][Rr][Mm]/
SharedLibraries/*/[Aa][Rr][Mm]64/
SharedLibraries/*/bld/
SharedLibraries/*/[Oo]bj/
SharedLibraries/*/[Ll]og/
SharedLibraries/*/[Ll]ogs/
SharedLibraries/*/bin/
*Function*/[Dd]ebug/
*Function*/[Dd]ebugPublic/
*Function*/[Rr]elease/
Expand Down
155 changes: 93 additions & 62 deletions Edit-Secrets.ps1
Original file line number Diff line number Diff line change
@@ -1,63 +1,113 @@
param (
[string]$TenantName = "Andrews McMeel Universal",
[string]$SubscriptionName = "AMU Pay-as-you-go",
[string]$File = 'Secrets.json',
[string]$RepositoryName = (git remote get-url origin).Split("/")[-1].Replace(".git", ""),
[string]$SetFile = 'Set-Secrets.ps1',
[string]$KeyVaultName,
[string]$SecretName,
[switch]$VersionHistory = $false,
[int]$VersionHistoryLength = 10,
[switch]$Force
[switch]$Force,
[switch]$Verbose = $false
)

# Check to see if Azure PowerShell Module is installed
if (!(Get-Module -ListAvailable Az.KeyVault)) {
Write-Host "Installing Azure Powershell Module..."
Install-Module -Name Az.KeyVault -Confirm:$false
$TenantName = "Andrews McMeel Universal"
$SubscriptionName = "AMU Pay-as-you-go"
$SetFile = 'Set-Secrets.ps1'

function Test-FileExists {
param(
[Parameter(Mandatory=$true)]
[string]$FilePath
)

if ((Test-Path $FilePath) -and (! $Force)) {
# Compare current and working files
if (((Get-FileHash "${SecretValue}.tmp").Hash) -ne ((Get-FileHash $FilePath).Hash)) {
# Ask user if they want to overwrite their existing file
$choice = $(Write-Host "File '$FilePath' exists. Overwrite? (y/N)" -ForegroundColor Yellow; Read-Host)
if ($choice.ToUpper() -eq "N") {
Write-Host "No changes made to $FilePath" -ForegroundColor DarkGray
Remove-Item -Path "${SecretValue}.tmp" -ErrorAction SilentlyContinue
return $false
}
}
else {
Write-Host "File $FilePath is already up-to-date." -ForegroundColor DarkGray
Remove-Item -Path "${SecretValue}.tmp" -ErrorAction SilentlyContinue
return $false
}
}
return $true
}

# Check if user needs to log in
if (!(Get-AzContext)) {
Write-Host "Cannot retrieve AzContext. Running 'Connect-AzAccount'" -ForegroundColor DarkGray
[void](Connect-AzAccount -Subscription $SubscriptionName -Force)
}
function Get-KeyVaultName {
param (
[string]$RepositoryName,
[switch]$Verbose
)

# Check if tenant is available
$Tenant = Get-AzTenant -ErrorAction SilentlyContinue | Where-Object Name -match "$TenantName"
if (!$Tenant) {
Write-Error "Cannot retrieve '$TenantName' tenant. Please try logging in with 'Connect-AzAccount'"
return
}
if ($Verbose) {
$VerbosePreference = 'Continue'
}

# Switch to the correct subscription and tenant
[void](Set-AzContext -SubscriptionName $SubscriptionName -Tenant $Tenant.Id)
Write-Host "AzContext set to 'TenantName=$TenantName' and 'SubscriptionName=$SubscriptionName'" -ForegroundColor DarkGray
# Search for key vault using tags
Write-Verbose "Searching for key vaults with tag: 'repository-name=$RepositoryName'"
$KeyVaultName = (Get-AzKeyVault -Tag @{"repository-name" = "$RepositoryName" }).VaultName

# Don't clear the ${File}.tmp file if using the VersionHistory option
if (!$VersionHistory) {
Clear-Content -Path "${File}.tmp" -ErrorAction SilentlyContinue
# Check if key vault name is empty
if (!$KeyVaultName) {
Write-Error "Key vault name cannot be found. Please confirm this repository's key vaults are tagged correctly."
return
}
else {
return $KeyVaultName
}
}

# Get key vault names
if (!$PSBoundParameters.ContainsKey('KeyVaultName')) {
Write-Host "Searching for key vaults with tag: 'repository-name=$RepositoryName'" -ForegroundColor DarkGray
$KeyVaultNames = (Get-AzKeyVault -Tag @{"repository-name" = "$RepositoryName" }).VaultName
function Set-AzureContext {
param (
[string]$SubscriptionName,
[string]$TenantName
)

if ($KeyVaultNames) {
Write-Host "Key vaults found: $KeyVaultNames" -ForegroundColor DarkGray
# Check to see if Azure PowerShell Module is installed
if (!(Get-Module -ListAvailable Az.KeyVault)) {
Write-Host "Installing Azure Powershell Module..."
Install-Module -Name Az.KeyVault -Confirm:$false
}
else {
Write-Error "No key vaults found. Please make sure the key vault is tagged correctly" -ForegroundColor Red

# Check if user needs to log in
if (!(Get-AzContext)) {
Write-Output "Cannot retrieve AzContext. Running 'Connect-AzAccount'"
[void](Connect-AzAccount -Subscription $SubscriptionName -Force)
}

# Check if tenant is available
$Tenant = Get-AzTenant -ErrorAction SilentlyContinue | Where-Object Name -match "$TenantName"
if (!$Tenant) {
Write-Error "Cannot retrieve '$TenantName' tenant. Please try logging in with 'Connect-AzAccount'"
return
}

# Switch to the correct subscription and tenant
[void](Set-AzContext -SubscriptionName $SubscriptionName -Tenant $Tenant.Id)
}

# Set Azure context
Set-AzureContext -SubscriptionName $SubscriptionName -TenantName $TenantName

# Get key vault name
if (!$PSBoundParameters.ContainsKey('KeyVaultName')) {
$KeyVaultName = Get-KeyVaultName -RepositoryName $RepositoryName -Verbose:$Verbose
}
else {
$KeyVaultNames = $KeyVaultName

# Clear file if not viewing version history
if (!$VersionHistory) {
Clear-Content -Path "${File}.tmp" -ErrorAction SilentlyContinue
}

# Create key vaults dictionary
$KeyVaults = New-Object PSCustomObject
$KeyVaultNames | ForEach-Object {
$KeyVaultName | ForEach-Object {
$KeyVaultName = $_
Write-Host "Generating secrets for $KeyVaultName..." -ForegroundColor DarkGray

Expand Down Expand Up @@ -140,29 +190,10 @@ if ($VersionHistory) {

Add-Content -Path "${File}.tmp" -Value ($KeyVaults | ConvertTo-Json)

# Check if ${File} already exists before proceeding
if ((Test-Path "${File}") -and (! $Force)) {
# Compare current and working files
if (((Get-FileHash "${File}.tmp").Hash) -ne ((Get-FileHash "${File}").Hash)) {
# Ask user if they want to overwrite their existing ${File}
$choice = $(Write-Host "File '$File' exists. Overwrite? (y/N)" -ForegroundColor Yellow; Read-Host)
if ($choice.ToUpper() -eq "N") {
Write-Host "No changes made to $File" -ForegroundColor DarkGray
Remove-Item -Path "${File}.tmp" -ErrorAction SilentlyContinue
# Exit out if user chooses not to overwrite the file
exit 0
}
}
else {
Write-Host "No changes made to file '$File'" -ForegroundColor DarkGray
Remove-Item -Path "${File}.tmp" -ErrorAction SilentlyContinue
# Exit out if the hashes are the same
exit 0
}
}

Copy-Item -Path "${File}.tmp" -Destination "${File}"
Remove-Item -Path "${File}.tmp" -ErrorAction SilentlyContinue

Write-Host "✨ File '$File' generated" -ForegroundColor Green
Write-Host "Once you've finished editing $File, please update this project's Azure Key Vaults by running '$SetFile'" -ForegroundColor Yellow
# Finish up
if (Test-FileExists -FilePath $SecretValue) {
Copy-Item -Path "${File}.tmp" -Destination "${File}"
Remove-Item -Path "${File}.tmp" -Force -ErrorAction SilentlyContinue
Write-Host "${File} file generated" -ForegroundColor Green
Write-Host "Once you've finished editing $File, please update this project's Azure Key Vaults by running '$SetFile'" -ForegroundColor Yellow
}
106 changes: 64 additions & 42 deletions Get-Secrets.ps1
Original file line number Diff line number Diff line change
@@ -1,58 +1,81 @@
param (
[string]$TenantName = "Andrews McMeel Universal",
[string]$SubscriptionName = "AMU Pay-as-you-go",
[string]$KeyVaultName,
[string]$File = '.env',
[string]$RepositoryName = ((git remote get-url origin).Split("/")[-1].Replace(".git", "")),
[string]$Environment = "development"
[string]$Environment = "development",
[switch]$Verbose = $false
)

# Check to see if Azure PowerShell Module is installed
if (!(Get-Module -ListAvailable Az.KeyVault)) {
Write-Host "Installing Azure Powershell Module..."
Install-Module -Name Az.KeyVault -Confirm:$false
}
$TenantName = "Andrews McMeel Universal"
$SubscriptionName = "AMU Pay-as-you-go"

# Check if user needs to log in
if (!(Get-AzContext)) {
Write-Host "Cannot retrieve AzContext. Running 'Connect-AzAccount'" -ForegroundColor DarkGray
[void](Connect-AzAccount -Subscription $SubscriptionName -Force)
}

# Check if tenant is available
$Tenant = Get-AzTenant -ErrorAction SilentlyContinue | Where-Object Name -Match "$TenantName"
if (!$Tenant) {
Write-Error "Cannot retrieve '$TenantName' tenant. Please try logging in with 'Connect-AzAccount'"
return
}

# Switch to the correct subscription and tenant
[void](Set-AzContext -SubscriptionName $SubscriptionName -Tenant $Tenant.Id)
Write-Host "AzContext set to 'TenantName=$TenantName' and 'SubscriptionName=$SubscriptionName'" -ForegroundColor DarkGray

# Clear temporary file
Clear-Content -Path "${File}.tmp" -ErrorAction SilentlyContinue
function Get-KeyVaultName {
param (
[string]$RepositoryName,
[string]$Environment,
[switch]$Verbose
)

# Check if searching for key vaults by repository name or otherwise, if key vault name argument is given
if (!$PSBoundParameters.ContainsKey('KeyVaultName')) {
# If $Environment argument isn't set, use default value
if (!$PSBoundParameters.ContainsKey('Environment')) {
Write-Host "Environment missing. Defaulting to $Environment." -ForegroundColor DarkGray
if ($Verbose) {
$VerbosePreference = 'Continue'
}

# Search for key vault using tags
Write-Host "Searching for key vault with tags: 'repository-name=$RepositoryName;environment=$Environment'" -ForegroundColor DarkGray
$KeyVaultName = (Get-AzKeyVault -Tag @{"environment" = "$Environment" } | Get-AzKeyVault -Tag @{"repository-name" = "$RepositoryName" }).VaultName
if ($Environment) {
Write-Verbose "Searching for key vault with tags: 'repository-name=$RepositoryName;environment=$Environment'"
$KeyVaultName = (Get-AzKeyVault -Tag @{"environment" = "$Environment" } | Get-AzKeyVault -Tag @{"repository-name" = "$RepositoryName" }).VaultName
}
else {
Write-Verbose "Searching for key vaults with tag: 'repository-name=$RepositoryName'"
$KeyVaultName = (Get-AzKeyVault -Tag @{"repository-name" = "$RepositoryName" }).VaultName
}

# Check if key vault name is empty
if (!$KeyVaultName) {
Write-Error "Key vault name cannot be found. Please confirm this repository's key vaults are tagged correctly."
return
}
else {
return $KeyVaultName
}
}

function Set-AzureContext {
param (
[string]$SubscriptionName,
[string]$TenantName
)

# Check to see if Azure PowerShell Module is installed
if (!(Get-Module -ListAvailable Az.KeyVault)) {
Write-Host "Installing Azure Powershell Module..."
Install-Module -Name Az.KeyVault -Confirm:$false
}

# Check if user needs to log in
if (!(Get-AzContext)) {
Write-Output "Cannot retrieve AzContext. Running 'Connect-AzAccount'"
[void](Connect-AzAccount -Subscription $SubscriptionName -Force)
}

# Check if tenant is available
$Tenant = Get-AzTenant -ErrorAction SilentlyContinue | Where-Object Name -match "$TenantName"
if (!$Tenant) {
Write-Error "Cannot retrieve '$TenantName' tenant. Please try logging in with 'Connect-AzAccount'"
return
}

# Switch to the correct subscription and tenant
[void](Set-AzContext -SubscriptionName $SubscriptionName -Tenant $Tenant.Id)
}
else {
# Just output KeyVaultName if passed as an argument
Write-Host "Searching for key vault named: $KeyVaultName" -ForegroundColor DarkGray

# Set Azure context
Set-AzureContext -SubscriptionName $SubscriptionName -TenantName $TenantName

# Get key vault name
if (!$PSBoundParameters.ContainsKey('KeyVaultName')) {
$KeyVaultName = Get-KeyVaultName -RepositoryName $RepositoryName -Environment $Environment -Verbose:$Verbose
}

# Get key vault object
Expand All @@ -71,7 +94,8 @@ $Secrets = (Get-AzKeyVaultSecret -VaultName "$KeyVaultName").Name
# Create secret hash
$SecretHash = @()

# Loop through secrets and add them to ${File}.tmp
# Loop through secrets and add them to the temporary file
Clear-Content -Path "${File}.tmp" -ErrorAction SilentlyContinue
Write-Host "Retrieving secrets..." -ForegroundColor DarkGray
$Secrets | ForEach-Object {
# Set secret object
Expand All @@ -96,9 +120,7 @@ $Secrets | ForEach-Object {
# Output secret variable
$SecretHash | Format-Table

# Copy the temporary file over the original file
# Finish up
Copy-Item -Path "${File}.tmp" -Destination "${File}"
Remove-Item -Path "${File}.tmp"-Force -ErrorAction SilentlyContinue

# Output success
Write-Host "✨ .env file generated from $KeyVaultName" -ForegroundColor Green
Remove-Item -Path "${File}.tmp" -Force -ErrorAction SilentlyContinue
Write-Host "${File} file generated" -ForegroundColor Green
Loading

0 comments on commit f8bbd17

Please sign in to comment.