Skip to content

Instantly share code, notes, and snippets.

@itsho
Last active July 12, 2021 11:14
Show Gist options
  • Save itsho/6c81accfe9cc9bcaa2534b3a8f5c00a4 to your computer and use it in GitHub Desktop.
Save itsho/6c81accfe9cc9bcaa2534b3a8f5c00a4 to your computer and use it in GitHub Desktop.
Add given certificate to ca-bundle.crt
# self elevate this script to run as admin
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit
}
$newCertContent = "# Certificate name for easy search
-----BEGIN CERTIFICATE-----
MIITHISISMYCERT_hereBeDragonshereBeDragons
-----END CERTIFICATE-----
"
$certPartToSeek = "copy paste a single line from the cert to here (preferably - the last line or one before that)"
$possibleRootLocations = New-Object Collections.Generic.List[String]
$possibleRootLocations.Add("${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\mingw32\ssl\certs")
$possibleRootLocations.Add("${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\mingw32\ssl\certs")
$possibleRootLocations.Add("${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\mingw32\ssl\certs")
$possibleRootLocations.Add("${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\mingw32\ssl\certs")
$possibleRootLocations.Add("${env:ProgramFiles(x86)}\Git\usr\ssl\certs")
$possibleRootLocations.Add("${env:ProgramFiles(x86)}\Git\mingw32\ssl\certs")
$possibleRootLocations.Add("$Env:ProgramW6432\Git\usr\ssl\certs")
$possibleRootLocations.Add("$Env:ProgramW6432\Git\mingw64\ssl\certs")
$possibleRootLocations.Add("$Env:LOCALAPPDATA\Fork\gitInstance")
$possibleRootLocations.Add("$Env:AGENT_HOMEDIRECTORY\externals\git\mingw64\ssl\certs")
Write-Host "Validating list of folder (removing invalid items)..."
foreach ($folder in $possibleRootLocations.ToArray()) {
if(![System.IO.Directory]::Exists($folder)){
$possibleRootLocations.Remove($folder) | Out-Null
}
}
if ($possibleRootLocations.Count -eq 0){
Write-Error "All of the folders in possibleRoot are invalid - please add more options or find the find manually"
exit 0
}
# find all ca-bundle.crt
Write-Host "=============================="
Write-Host "Searching for ca-bundle.crt..."
$files = Get-Childitem –Path $possibleRootLocations -Recurse -Include "ca-bundle.crt"
Write-Host "=============================="
Write-Host "Checking ca-bundle.crt - which file does NOT contain the cert..."
foreach ($certFile in $files) {
#Write-Host "Found $certFile"
$certContent = Get-Content $certFile
# check if file already contains tfs.cellebrite.com cert
if (-not $certContent.Contains($certPartToSeek)) {
# add it at the bottom
Write-Host "File $certFile does NOT contain the cert :-( trying to add..."
$newContent = $certContent + "`n`n`n" + $newCertContent
Set-Content -Path $certFile -Value $newContent
Write-Host "Added cert to file $certFile :-)"
}
else {
Write-Host "File $certFile ALREADY containing the cert :-)"
}
}
Write-Host "Press any key to close"
[Console]::ReadKey()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment