Skip to content

Commit

Permalink
Merge pull request #3119 from freddydk/master
Browse files Browse the repository at this point in the history
Issue #3116 + 2 enhancements
  • Loading branch information
freddydk committed Jul 15, 2023
2 parents 665a4c7 + 6c32711 commit 333cd1d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 36 deletions.
11 changes: 6 additions & 5 deletions AppHandling/Publish-NavContainerApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ try {
}
$sslVerificationDisabled = ($protocol -eq "https://")
if ($sslVerificationDisabled) {
Write-Host "Disabling SSL Verification"
$handler.ServerCertificateCustomValidationCallback = [SslVerification]::DisabledServerCertificateValidationCallback
Write-Host "Disabling SSL Verification on HttpClient"
[SslVerification]::DisableSsl($handler)
}
if ($customConfig.ClientServicesCredentialType -eq "Windows") {
$handler.UseDefaultCredentials = $true
Expand Down Expand Up @@ -286,13 +286,14 @@ try {
throw $message
}
}
catch {
GetExtendedErrorMessage -errorRecord $_ | Out-Host
throw
}
finally {
$FileStream.Close()
}

if ($sslverificationdisabled) {
Write-Host "Restoring SSL Verification" # no action required - only to enforce blocks consistency
}
if ($bcContainerHelperConfig.NoOfSecondsToSleepAfterPublishBcContainerApp -gt 0) {
# Avoid race condition
Start-Sleep -Seconds $bcContainerHelperConfig.NoOfSecondsToSleepAfterPublishBcContainerApp
Expand Down
29 changes: 29 additions & 0 deletions ContainerHandling/Flush-ContainerHelperCache.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
- sandboxartifacts are artifacts downloaded for spinning up containers
- images are images built on artifacts using New-BcImage or New-BcContainer
- compilerFolders are folders used for Dockerless builds
- exitedContainers are containers which have been stopped
- all is all of the above (except for exited Containers)
.Parameter keepDays
When specifying a value in keepDays, the function will try to keep cached information, which has been used during the last keepDays days. Default is 0 - to flush all cache.
.Example
Expand Down Expand Up @@ -46,6 +48,33 @@ try {
$artifactsCacheFolder = $bcContainerHelperConfig.bcartifactsCacheFolder
$caches = $cache.ToLowerInvariant().Split(',')

if ($caches.Contains('exitedcontainers')) {
docker container ls --format "{{.ID}}:{{.Names}}" --no-trunc -a --filter "status=exited" | ForEach-Object {
$containerID = $_.Split(':')[0]
$containerName = $_.Split(':')[1]
$inspect = docker inspect $containerID | ConvertFrom-Json
try {
$finishedAt = [DateTime]::Parse($inspect.state.FinishedAt)
$exitedDaysAgo = [DateTime]::Now.Subtract($finishedAt).Days
if ($exitedDaysAgo -ge $keepDays) {
if (($inspect.Config.Labels.psobject.Properties.Match('maintainer').Count -ne 0 -and $inspect.Config.Labels.maintainer -eq "Dynamics SMB")) {
Write-Host "Removing container $containerName"
docker rm $containerID -f
}
else {
Write-Host "Container $containerName (exited $exitedDaysAgo day$(if($exitedDaysAgo -ne 1){'s'}) ago) is not recognized as a Business Central Container - not removing"
}
}
else {
Write-Host "Keeping container $containerName (exited $exitedDaysAgo day$(if($exitedDaysAgo -ne 1){'s'}) ago) - removing after $keepDays day$(if($keepDays -ne 1){'s'})"
}
}
catch {
# ignore any errors
}
}
}

$folders = @()
if ($caches.Contains('all') -or $caches.Contains('calSourceCache')) {
$folders += @("extensions\original-*-??","extensions\original-*-??-newsyntax")
Expand Down
25 changes: 18 additions & 7 deletions ContainerHandling/Remove-NavContainerSession.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,29 @@ function Remove-BcContainerSession {
Process {
if ($sessions.ContainsKey($containerName)) {
$session = $sessions[$containerName]
if ($killPsSessionProcess -and !$isInsideContainer) {
$inspect = docker inspect $containerName | ConvertFrom-Json
if ($inspect.HostConfig.Isolation -eq "process") {
$processID = Invoke-Command -Session $session -ScriptBlock { $PID }
Stop-Process -Id $processID -Force
try {
if ($killPsSessionProcess -and !$isInsideContainer) {
$inspect = docker inspect $containerName | ConvertFrom-Json
if ($inspect.HostConfig.Isolation -eq "process") {
try {
$processID = Invoke-Command -Session $session -ScriptBlock { $PID }
Stop-Process -Id $processID -Force
}
catch {
Write-Host "Error killing process in container"
Remove-PSSession -Session $session
}
}
else {
Remove-PSSession -Session $session
}
}
else {
Remove-PSSession -Session $session
}
}
else {
Remove-PSSession -Session $session
catch {
Write-Host "Error removing session for container"
}

$sessions.Remove($containerName)
Expand Down
46 changes: 22 additions & 24 deletions HelperFunctions.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$useTimeOutWebClient = $false
if ($PSVersionTable.PSVersion -lt "6.0.0" -and !$useTimeOutWebClient) {
if ($PSVersionTable.PSVersion -lt "6.0.0" -or $useTimeOutWebClient) {
$timeoutWebClientCode = @"
using System.Net;
Expand All @@ -23,31 +23,32 @@ if ($PSVersionTable.PSVersion -lt "6.0.0" -and !$useTimeOutWebClient) {
}
}
"@;

try {
Add-Type -TypeDefinition $timeoutWebClientCode -Language CSharp -WarningAction SilentlyContinue | Out-Null
$useTimeOutWebClient = $true
}
catch {}
if (-not ([System.Management.Automation.PSTypeName]"TimeoutWebClient").Type) {
Add-Type -TypeDefinition $timeoutWebClientCode -Language CSharp -WarningAction SilentlyContinue | Out-Null
$useTimeOutWebClient = $true
}
}

$sslCallbackCode = @"
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public static class SslVerification
{
public static bool DisabledServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }
public static void Disable() { System.Net.ServicePointManager.ServerCertificateValidationCallback = DisabledServerCertificateValidationCallback; }
public static void Enable() { System.Net.ServicePointManager.ServerCertificateValidationCallback = null; }
}
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public static class SslVerification
{
public static bool DisabledServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }
public static void Disable() { System.Net.ServicePointManager.ServerCertificateValidationCallback = DisabledServerCertificateValidationCallback; }
public static void Enable() { System.Net.ServicePointManager.ServerCertificateValidationCallback = null; }
public static void DisableSsl(System.Net.Http.HttpClientHandler handler) { handler.ServerCertificateCustomValidationCallback = DisabledServerCertificateValidationCallback; }
}
"@
try {
if (-not ([System.Management.Automation.PSTypeName]"SslVerification").Type) {
if (-not ([System.Management.Automation.PSTypeName]"SslVerification").Type) {
if ($isPsCore) {
Add-Type -TypeDefinition $sslCallbackCode -Language CSharp -WarningAction SilentlyContinue | Out-Null
}
else {
Add-Type -TypeDefinition $sslCallbackCode -Language CSharp -ReferencedAssemblies @('System.Net.Http') -WarningAction SilentlyContinue | Out-Null
}
}
catch {}

function Get-DefaultCredential {
Param(
Expand Down Expand Up @@ -1039,8 +1040,8 @@ function DownloadFileLow {

$handler = New-Object System.Net.Http.HttpClientHandler
if ($skipCertificateCheck) {
Write-Host "Disabling SSL Verification"
$handler.ServerCertificateCustomValidationCallback = [SslVerification]::DisabledServerCertificateValidationCallback
Write-Host "Disabling SSL Verification on HttpClient"
[SslVerification]::DisableSsl($handler)
}
if ($useDefaultCredentials) {
$handler.UseDefaultCredentials = $true
Expand Down Expand Up @@ -1071,9 +1072,6 @@ function DownloadFileLow {
if ($stream) {
$stream.Dispose()
}
if ($skipCertificateCheck) {
Write-Host "Restoring SSL Verification" # no action required - only to enforce blocks consistency
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
5.0.5
Add new option (exitedContainers) on Flush-ContainerHelperCache. Note that exited containers are NOT remove by the all flag, you need all,exitedContainers
Add resilience to Remove-BcContainerSession
Issue #3116 Publish-NavContainerApp: Exception setting "ServerCertificateCustomValidationCallback": "Cannot convert the "static bool DisabledServerCertificateValidationCallback
Add new parameter -useEnvironmentUpdateWindow to Install-BcAppFromAppSource, to schedule installation of apps to run inside update window

5.0.4
Expand Down

0 comments on commit 333cd1d

Please sign in to comment.