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

Check if the installed version of Locksmith is recent or out of date #100

Merged
merged 16 commits into from
Feb 10, 2024
Merged
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
63 changes: 63 additions & 0 deletions Private/Test-IsRecentVersion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function Test-IsRecentVersion {
[CmdletBinding()]
param (
# Check a specific version number from the script
[Parameter(Mandatory)]
[string]$Version,
# Define the number of days past a module release date at which to consider the release "out of date."
[Parameter()]
[int16]$Days = 60
)

# Strip the 'v' if it was used so the script can work with or without it in the input
$Version = $Version.Replace('v','')
try {
# Checking the most recent release in GitHub, but we could also use PowerShell Gallery.
$Uri = "https://api.github.com/repos/trimarcjake/locksmith/releases"
$Releases = Invoke-RestMethod -Uri $uri -Method Get -DisableKeepAlive -ErrorAction Stop
$LatestRelease = $Releases | Sort-Object -Property Published_At -Descending | Select-Object -First 1
# Get the release date of the currently running version via the version parameter
[datetime]$InstalledVersionReleaseDate = ($Releases | Where-Object {$_.tag_name -like "?$Version"}).published_at
[datetime]$LatestReleaseDate = $LatestRelease.published_at
# $ModuleDownloadLink = ( ($LatestRelease.Assets).Where({$_.Name -like "Locksmith-v*.zip"}) ).browser_download_url
$ScriptDownloadLink = ( ($LatestRelease.Assets).Where({$_.Name -eq 'Invoke-Locksmith.zip'}) ).browser_download_url

$LatestReleaseInfo = @"
Locksmith Module Details:

Latest Version:`t`t $($LatestRelease.name)
Published at: `t`t $LatestReleaseDate
Install Module:`t`t Install-Module -Name Locksmith
Standalone Script:`t $ScriptDownloadLink
"@
}
catch {
Write-Warning "Unable to find the latest available version of the Locksmith module on GitHub." -WarningAction Continue
# Find the approximate release date of the installed version. Handles version with or without 'v' prefix.
$InstalledVersionMonth = [datetime]::Parse(($Version.Replace('v','')).Replace('.','-')+"-01")
# Release date is typically the first Saturday of the month. Let's guess as close as possible!
$InstalledVersionReleaseDate = $InstalledVersionMonth.AddDays( 6 - ($InstallVersionMonth.DayOfWeek) )
}

# The date at which to consider this module "out of date" is based on the $Days parameter
$OutOfDateDate = (Get-Date).Date.AddDays(-$Days)
$OutOfDateMessage = "Your currently installed version of Locksmith ($Version) is more than $Days days old. We recommend that you update to ensure the latest findings are included."

# Compare the installed version release date to the latest release date
if ( ($LatestReleaseDate) -and ($InstalledVersionReleaseDate -le ($LatestReleaseDate.AddDays(-$Days))) ) {
# If we found the latest release date online and the installed version is more than [x] days older than it:
Write-Warning -Verbose -Message $OutOfDateMessage -WarningAction Continue
Write-Information -MessageData $LatestReleaseInfo -InformationAction Continue
$IsRecentVersion = $false
} elseif ( $InstalledVersionReleaseDate -le $OutOfDateDate ) {
# If we didn't get the latest release date online, use the estimated release date to check age.
Write-Warning -Verbose -Message $OutOfDateMessage -WarningAction Continue
$IsRecentVersion = $false
} else {
# The installed version has not been found to be out of date.
$IsRecentVersion = $True
}

# Return true/false
$IsRecentVersion
}
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ A ~~tiny~~ small tool built to detect and fix common misconfigurations in Active
[![GitHub contributors](https://img.shields.io/github/contributors/trimarcjake/locksmith.svg)](https://github.com/trimarcjake/locksmith/graphs/contributors/)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com)
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/trimarcjake/Locksmith/powershell.yml?logo=github&label=PSScriptAnalyzer)
[![MegaLinter](https://github.com/trimarcjake/locksmith/workflows/MegaLinter/badge.svg?branch=testing)](https://github.com/trimarcjake/locksmith/actions?query=workflow%3AMegaLinter+branch%3Atesting)
![PowerShell Gallery Downloads](https://img.shields.io/powershellgallery/dt/locksmith?logo=powershell&label=PowerShell%20Gallery%20Downloads&color=blue)
[![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Checkout+Locksmith+and+fix+common+misconfigurations+in+Active+Directory+Certificate+Services.&url=https://github.com/trimarcjake/locksmith&hashtags=ADCS,PKI,infosec,powershell)
<!-- locksmith-badges-end -->
Expand Down
Loading