Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce powershell linter rules #325

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@ on:
jobs:
lint:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Lint Shell Scripts
continue-on-error: true
run: |
sudo apt-get update
sudo apt-get install shellcheck
shellcheck **/*.sh

- name: Lint PowerShell Scripts
continue-on-error: true
if: failure()
run: |
pwsh -Command "Invoke-ScriptAnalyzer -EnableExit -Recurse -Path ."

- name: Lint Lua
continue-on-error: true
if: failure()
run: |
sudo apt-get install -y luarocks
sudo luarocks install luacheck
luacheck **/*.lua

- name: Lint TeX Files
continue-on-error: true
if: failure()
run: |
sudo apt-get install chktex
chktex **/*.tex

- name: Lint YAML Files
continue-on-error: true
if: failure()
run: |
sudo apt-get update
sudo apt-get install yamllint
Expand All @@ -58,7 +58,6 @@ jobs:
uses: actions/checkout@v4

- name: Semgrep Scan
continue-on-error: true
run: |
semgrep --config "p/r2c" .

Expand Down
16 changes: 8 additions & 8 deletions testing/InstallTestbed.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ else {
}

if ($Version -ne $false -and -not ($Version -match '^[0-9]+\.[0-9]+\.[0-9]+$')) {
Write-Host "Invalid version format: $Version. Expected format: X.Y.Z (e.g., 1.3.0)"
Write-Error "Invalid version format: $Version. Expected format: X.Y.Z (e.g., 1.3.0)"
exit 1
}

Expand Down Expand Up @@ -136,7 +136,7 @@ if (-Not $LinuxOnly) {
Start-Sleep 10

# See if we can see the forwarding computers in the DC
write-host "`nChecking if we can see the forwarding computers in the DC..."
Write-Output "`nChecking if we can see the forwarding computers in the DC..."
$listForwardingComputersResponse = .\run_script_in_container.ps1 `
-ResourceGroup $ResourceGroup `
-VMName $DomainController `
Expand Down Expand Up @@ -255,7 +255,7 @@ $getElasticsearchPasswordsResponse = az vm run-command invoke `
--command-id RunShellScript `
--name $LinuxVM `
--resource-group $ResourceGroup `
--scripts 'sed -n "/^## elastic/,/^####################/p" "/opt/lme/Chapter 3 Files/output.log"'
--scripts 'sed -n "/^## elastic/,/^####################/p" "/opt/lme/Chapter 3 Files/output.log"'

Write-Output $ProcessSeparator

Expand Down Expand Up @@ -364,15 +364,15 @@ $runTestResponse = az vm run-command invoke `
--scripts '/home/admin.ackbar/lme/configure/linux_test_install.sh' | ConvertFrom-Json

$message = $runTestResponse.value[0].message
Write-Host "$message`n"
Write-Host "--------------------------------------------"
Write-Output "$message`n"
Write-Output "--------------------------------------------"

# Check if there is stderr content in the message field
if ($message -match '\[stderr\]\n(.+)$') {
Write-Host "Tests failed"
Write-Output "Tests failed"
exit 1
} else {
Write-Host "Tests succeeded"
Write-Output "Tests succeeded"
}

Write-Output "`nInstall completed."
Expand All @@ -399,4 +399,4 @@ Branch: $Branch
# Output the parameters to the end of the password file
$paramsToWrite | Out-File -Append -FilePath $PasswordPath

Get-Content -Path $PasswordPath
Get-Content -Path $PasswordPath
19 changes: 11 additions & 8 deletions testing/SetupTestbed.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function Get-RandomPassword {
}

function Set-AutoShutdown {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "", Scope="Function")]
param (
[Parameter(Mandatory)]
[string]$VMName
Expand All @@ -146,7 +147,8 @@ function Set-AutoShutdown {
}
}

function Set-NetworkRules {
function Set-NetworkRule {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "", Scope="Function")]
param (
[Parameter(Mandatory)]
$AllowedSourcesList
Expand Down Expand Up @@ -176,10 +178,11 @@ function Set-NetworkRules {
"--source-address-prefixes $AllowedSourcesList " +
"--destination-address-prefixes '*' " +
"--destination-port-ranges $port " +
"--description 'Allow inbound from $sources on $port via $protocol connections.' "
"--description 'Allow inbound from $sources on $port via $protocol connections.'"

Write-Output "Running command: $command"

# TODO: Avoid using Invoke-Expression; https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/avoid-using-invoke-expression?view=powershell-7.4
$networkRuleResponse = Invoke-Expression $command
Write-Output $networkRuleResponse

Expand Down Expand Up @@ -265,7 +268,7 @@ $createNsgResponse = az network nsg create --name NSG1 `
--tags project=$Project created=$Today createdBy=$CurrentUser
Write-Output $createNsgResponse

Set-NetworkRules -AllowedSourcesList $AllowedSourcesList
Set-NetworkRule -AllowedSourcesList $AllowedSourcesList

##################
# Create the VMs #
Expand Down Expand Up @@ -432,14 +435,14 @@ if (-Not $LinuxOnly){
Add-DnsServerResourceRecordA -Name LS1 -ZoneName $DomainName. -AllowUpdateAny -IPv4Address $LsIP -TimeToLive 01:00:00 -AsJob
}
`$job = Start-Job -ScriptBlock `$scriptBlock
`$timeout = 120
`$timeout = 120
if (Wait-Job -Job `$job -Timeout `$timeout) {
Receive-Job -Job `$job
Write-Host 'The script completed within the timeout period.'
Write-Output 'The script completed within the timeout period.'
} else {
Stop-Job -Job `$job
Remove-Job -Job `$job
Write-Host 'The script timed out after `$timeout seconds.'
Write-Output 'The script timed out after `$timeout seconds.'
}
"@

Expand Down Expand Up @@ -473,15 +476,15 @@ if (Wait-Job -Job `$job -Timeout `$timeout) {
--scripts "Add-Content -Path 'C:\windows\system32\drivers\etc\hosts' -Value '$LsIP ls1.$DomainName ls1'"
Show-FormattedOutput -FormattedOutput (Format-AzVmRunCommandOutput -JsonResponse "$writeToHostsFileResponse")

Write-Host "Checking if ls1 resolves. This should resolve to ls1.lme.local->${LsIP}, not another domain..."
Write-Output "Checking if ls1 resolves. This should resolve to ls1.lme.local->${LsIP}, not another domain..."
$resolveLs1Response = az vm run-command invoke `
--command-id RunPowerShellScript `
--resource-group $ResourceGroup `
--name DC1 `
--scripts "Resolve-DnsName ls1"
Show-FormattedOutput -FormattedOutput (Format-AzVmRunCommandOutput -JsonResponse "$resolveLs1Response")

Write-Host "Removing the Dns script. No output expected..."
Write-Output "Removing the Dns script. No output expected..."
$removeDnsRecordScriptResponse = az vm run-command invoke `
--command-id RunPowerShellScript `
--name DC1 `
Expand Down
4 changes: 2 additions & 2 deletions testing/configure/azure_scripts/copy_file_to_container.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ $UploadResponse = az storage blob upload `
--overwrite

# Write the upload response to the standard output stream
Write-Host $UploadResponse
Write-Output $UploadResponse

$BlobName = (Split-Path $LocalFilePath -Leaf)
$ExpiryTime = (Get-Date).AddDays(1).ToString('yyyy-MM-ddTHH:mm:ssZ')
Expand All @@ -72,7 +72,7 @@ $SasUrl = az storage blob generate-sas `
--output tsv

# Write the SAS URL generation response to the standard output stream
Write-Host "SAS URL generated successfully."
Write-Output "SAS URL generated successfully."

# Set the full url var for returning to the user for use in the next script
$FullUrl = "https://${StorageAccountName}.blob.core.windows.net/${ContainerName}/${BlobName}?${SasUrl}"
Expand Down
3 changes: 2 additions & 1 deletion testing/configure/azure_scripts/create_blob_container.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ Replace "YourResourceGroupName" with the name of your Azure Resource Group.

#>


[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "")]
param(
[Parameter(Mandatory=$true)]
[string]$ResourceGroup
)

function New-AzureName {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "", Scope="Function")]
param (
[Parameter(Mandatory=$true)]
[string]$Prefix
Expand Down
3 changes: 3 additions & 0 deletions testing/configure/azure_scripts/download_in_container.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ if ($Os -eq "linux") {
$DirectoryCreationScript = "mkdir -p '/home/$UserName/lme'"
# TODO: We don't want to output this until we fix it so we can put all of the output from thw whole script into one json object
# We are just ignoring the output for now
#
# Suppress error message temporarily because of the above TODO: remove when fixed
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
$CreateDirectoryResponse = az vm run-command invoke `
--command-id RunShellScript `
--resource-group $ResourceGroup `
Expand Down
1 change: 1 addition & 0 deletions testing/configure/azure_scripts/lib/utilityFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function Get-PrivateKeyFromJson {
}

function Invoke-GPUpdateOnVMs {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "")]
param(
[Parameter(Mandatory = $true)]
[string]$ResourceGroup,
Expand Down
2 changes: 2 additions & 0 deletions testing/configure/trust_ls1_ssh_key.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if (-not (Test-Path -Path $SshDirectory)) {

# Function to set ACL for the directory, granting FullControl to SYSTEM and applying inheritance
function Set-SystemOnlyAclForDirectory {
[CmdletBinding(SupportsShouldProcess)]
param (
[string]$path
)
Expand All @@ -31,6 +32,7 @@ function Set-SystemOnlyAclForDirectory {

# Function to set ACL for a file, granting FullControl only to SYSTEM
function Set-SystemOnlyAclForFile {
[CmdletBinding(SupportsShouldProcess)]
param (
[string]$path
)
Expand Down
4 changes: 2 additions & 2 deletions testing/development/destroy_cluster.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $resourceGroupExists = az group exists --name "$env:RESOURCE_GROUP"
if ($resourceGroupExists -eq 'true') {
# Delete the resource group if it exists
az group delete --name "$env:RESOURCE_GROUP" --yes --no-wait
Write-Host "Deletion of resource group $($env:RESOURCE_GROUP) initiated."
Write-Output "Deletion of resource group $($env:RESOURCE_GROUP) initiated."
} else {
Write-Host "Resource group $($env:RESOURCE_GROUP) does not exist. No action taken."
Write-Output "Resource group $($env:RESOURCE_GROUP) does not exist. No action taken."
}
3 changes: 2 additions & 1 deletion testing/development/install_lme.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $targetDirectory = Join-Path -Path $PSScriptRoot -ChildPath "..\\"
Set-Location -Path $targetDirectory

# Prepare the parameters for InstallTestbed.ps1
$installTestbedParams = ""
$installTestbedParams = ""
if ($v) {
$installTestbedParams += " -v $v "
}
Expand All @@ -37,4 +37,5 @@ if ($m) {
$command = ".\InstallTestbed.ps1 -ResourceGroup $env:RESOURCE_GROUP $installTestbedParams | Tee-Object -FilePath ./$env:RESOURCE_GROUP.output.log"

# Execute the command
# TODO: Remove use of Invoke-Expression
Invoke-Expression $command
5 changes: 2 additions & 3 deletions testing/internet_toggle.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "")]
param (
[Parameter(Mandatory)]
[Alias("RG")]
Expand Down Expand Up @@ -28,7 +27,7 @@ function disable {
--access Deny `
--destination-address-prefixes Internet `
--destination-port-ranges '*'

az network nsg rule create --name DENYLOAD `
--resource-group $ResourceGroup `
--nsg-name $NSG `
Expand Down
Loading