| # Output Linux environment from Terraform | |
| provider "local" {} | |
| data "local_file" "env" { | |
| filename = "/proc/1/environ" | |
| } | |
| output "env" { | |
| value = data.local_file.env.content |
| #! /usr/bin/pwsh -nop | |
| $j = Get-Content "./Gsuite.json" | ConvertFrom-JSON | |
| $priv = $j.private_key | |
| $pub = (Invoke-RestMethod $j.client_x509_cert_url).($j.private_key_id) | |
| $rnd = Get-Random 1000001 | |
| $priv | Out-File ".\priv$rnd.key" | |
| $pub | Out-File ".\pub$rnd.cer" | |
| openssl pkcs12 -export -in "pub$rnd.cer" -inkey "priv$rnd.key" -out "pfx$rnd.p12" -password pass:notasecret |
| # Using curl to measure TLS negotiation time, as a proxy of PKCS #11 performance | |
| # https://blog.cloudflare.com/a-question-of-timing/ | |
| function Get-ConnectionTimes ([string] $Uri) { | |
| $curl = curl -w "%{time_namelookup},%{time_connect},%{time_appconnect}\n" -s -o /dev/null $Uri | |
| $namelookup,$connect,$appconnect = $curl -split "," | |
| $res = [PSCustomObject]@{ | |
| namelookup = [float]$namelookup | |
| connect = [float]$connect | |
| appconnect = [float]$appconnect |
| # JWT signature verification | |
| # $jwt should contain the JWT as a string | |
| $parts = $jwt.Split('.') | |
| $SHA256 = New-Object Security.Cryptography.SHA256Managed | |
| $computed = $SHA256.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($parts[0]+"."+$parts[1])) | |
| # Method A - just using X509Certificate2. TODO: initialise from file, no private key | |
| # Reference: https://blogs.msdn.microsoft.com/alejacma/2008/06/25/how-to-sign-and-verify-the-signature-with-net-and-a-certificate-c/ |
Quickly coding a function that allows flattening a structure. The goal is to use in comparing complex structures, such as created by the ConvertFrom-JSON or ConvertFrom-Yaml.
Outputs a hash table.
Example:
$res = Flatten-Object @{SH = @("bin", "bash"); A = 1; B = "ZZ"; C = @{CC = "CC"}; Logic = $true}
$res.Keys | Sort-Object | % { Write-Output "$_ = $($res[$_])"}| # Runs in PowerShell 5.1, PowerShell Core 6 on Windows and Linux, and PowerShell 7 preview | |
| # Calculating SHA1 hash and returning it as a hexadecimal string | |
| function Compute-SHA1Hash ([string] $string) { | |
| $sha1 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider | |
| $encoder = New-Object System.Text.UTF8Encoding | |
| $bytes = $encoder.GetBytes($string) | |
| $hash = ($sha1.ComputeHash($bytes) | % { $_.ToString("X2") }) -join '' | |
| return $hash |
| function quicksort($in) { | |
| $n = $in.count | |
| switch ($n) { | |
| 0 {} | |
| 1 { $in[0] } | |
| 2 { if ($in[0] -lt $in[1]) {$in[0], $in[1]} else {$in[1], $in[0]} } | |
| default { | |
| $anchor = $in | get-random | |
| $lt = $in | ? {$_ -lt $anchor} | |
| $eq = $in | ? {$_ -eq $anchor} |
| # This is a simple Powershell script that probes a system for management interface, negotiates protocol and queries management data | |
| # Some of it is pretty standard, such as make/model/RAM/OS | |
| # The interesting bit, quite useful in production, is the top processes by read and write I/O activity | |
| # Run in parallel against multiple systems, and you have dataset describing your environment! | |
| function Get-ComputerData { | |
| [CmdletBinding()] | |
| param ( |