<commit_before>﻿if ($deployHelpLoaded -eq $null)
{
    $DeployToolsDir = Split-Path ((Get-Variable MyInvocation -Scope 0).Value.MyCommand.Path)
    . $DeployToolsDir\deployHelp.ps1
}

Write-Host "Ensconce - KubernetesHelper Loading"
if ([string]::IsNullOrWhiteSpace($KubeCtlExe))
{
    $KubeCtlExe = "$DeployToolsDir\Tools\KubeCtl\kubectl.exe"
}
if ([string]::IsNullOrWhiteSpace($DatreeExe))
{
    $DatreeExe = "$DeployToolsDir\Tools\Datree\datree.exe"
}
$rootConfigPath = "$Home\.kube"

if (Test-Path $KubeCtlExe)
{
    (& $KubeCtlExe version --client 2>&1) | ForEach-Object {
        if ($_ -match "^Client Version.*")
        {
            $data = ConvertFrom-Json ($_ -replace "Client Version: version.Info", "")
            $clientVersion = $data.GitVersion
            Write-Host "KubeCtl Version: $clientVersion"
        }
    }
}
else
{
    throw "'$KubeCtlExe' doesn't exist"
}

if (Test-Path $DatreeExe)
{
    (& $DatreeExe version 2>&1) | Select-Object -First 1 {
        Write-Host "Datree Version: $_"
    }
    $DatreeExeFound = $true
}
else
{
    Write-Warning "Datree exe not found at $DatreeExe"
    $DatreeExeFound = $false
}

function PreProcessYaml([string]$yamlDirectory)
{
    if ((Test-Path -Path "$yamlDirectory\kustomization.yaml") -eq $false)
    {
        Write-Host "Creating kustomization.yaml"
        Add-Content -Path "$yamlDirectory\kustomization.yaml" -Value "resources:"
        Get-ChildItem "$yamlDirectory" -Filter *.yaml | Foreach-Object {
            if ($_.Name -ne "kustomization.yaml")
            {
                Add-Content -Path "$yamlDirectory\kustomization.yaml" -Value $_.Name
            }
        }
    }

    Write-Host "Replace tags in yaml in $yamlDirectory"
    ensconce --deployFrom $yamlDirectory --treatAsTemplateFilter=*.yaml | Write-Host

    Write-Host "Running kustomize in $yamlDirectory"
    $output = & $KubeCtlExe kustomize $yamlDirectory

    if ($LASTEXITCODE -ne 0)
    {
        Write-Error "Kustomize error processing $kustomizationPath"
        exit $LASTEXITCODE
    }

    Out-File -FilePath "$yamlDirectory\kustomization-output.yaml" -InputObject $output

    "$yamlDirectory\kustomization-output.yaml"
}

function ValidateK8sYaml([string]$yamlFile, [string]$kubernetesConfigFile)
{
    $kubernetesConfigFilePath = "$rootConfigPath\$kubernetesConfigFile"

    if($DatreeExeFound)
    {
        $kubeServerVersion = "1.15.0"
        (& $KubeCtlExe version --kubeconfig=$kubernetesConfigFilePath 2>&1) | ForEach-Object {
            if ($_ -match "^Server Version.*")
            {
                $data = ConvertFrom-Json ($_ -replace "Server Version: version.Info", "")
                $kubeServerVersion = $data.GitVersion
            }
        }

        $mtx = New-Object System.Threading.Mutex($false, "datree-mutex")
        $mtxResult = $mtx.WaitOne(180000) #3 minutes timeout
        if($mtxResult -eq $false)
        {
            throw "Couldn't aquire 'datree-mutex' in 3 minutes"
        }

        try
        {
            if([string]::IsNullOrWhiteSpace($DatreeToken))
            {
                & $DatreeExe config set token $DatreeToken
            }

            if([string]::IsNullOrWhiteSpace($DatreePolicyYaml) -eq $false)
            {
                $TempFolder = [System.IO.Path]::GetTempPath()
                $Guid = ([System.Guid]::NewGuid()).ToString()
                $datreeFolder = [IO.Path]::Combine($TempFolder, "datree")
                EnsurePath $datreeFolder
                $PolicyYamlFile = [IO.Path]::Combine($datreeFolder, "policy-$Guid.yaml")
                $DatreePolicyYaml | Out-File -FilePath $PolicyYamlFile
                & $DatreeExe publish $PolicyYamlFile
                Remove-Item $PolicyYamlFile -force
            }
        }
        finally
        {<commit_msg>if the server value starts with "v" remove it.<commit_after>﻿if ($deployHelpLoaded -eq $null)
{
    $DeployToolsDir = Split-Path ((Get-Variable MyInvocation -Scope 0).Value.MyCommand.Path)
    . $DeployToolsDir\deployHelp.ps1
}

Write-Host "Ensconce - KubernetesHelper Loading"
if ([string]::IsNullOrWhiteSpace($KubeCtlExe))
{
    $KubeCtlExe = "$DeployToolsDir\Tools\KubeCtl\kubectl.exe"
}
if ([string]::IsNullOrWhiteSpace($DatreeExe))
{
    $DatreeExe = "$DeployToolsDir\Tools\Datree\datree.exe"
}
$rootConfigPath = "$Home\.kube"

if (Test-Path $KubeCtlExe)
{
    (& $KubeCtlExe version --client 2>&1) | ForEach-Object {
        if ($_ -match "^Client Version.*")
        {
            $data = ConvertFrom-Json ($_ -replace "Client Version: version.Info", "")
            $clientVersion = $data.GitVersion
            Write-Host "KubeCtl Version: $clientVersion"
        }
    }
}
else
{
    throw "'$KubeCtlExe' doesn't exist"
}

if (Test-Path $DatreeExe)
{
    (& $DatreeExe version 2>&1) | Select-Object -First 1 {
        Write-Host "Datree Version: $_"
    }
    $DatreeExeFound = $true
}
else
{
    Write-Warning "Datree exe not found at $DatreeExe"
    $DatreeExeFound = $false
}

function PreProcessYaml([string]$yamlDirectory)
{
    if ((Test-Path -Path "$yamlDirectory\kustomization.yaml") -eq $false)
    {
        Write-Host "Creating kustomization.yaml"
        Add-Content -Path "$yamlDirectory\kustomization.yaml" -Value "resources:"
        Get-ChildItem "$yamlDirectory" -Filter *.yaml | Foreach-Object {
            if ($_.Name -ne "kustomization.yaml")
            {
                Add-Content -Path "$yamlDirectory\kustomization.yaml" -Value $_.Name
            }
        }
    }

    Write-Host "Replace tags in yaml in $yamlDirectory"
    ensconce --deployFrom $yamlDirectory --treatAsTemplateFilter=*.yaml | Write-Host

    Write-Host "Running kustomize in $yamlDirectory"
    $output = & $KubeCtlExe kustomize $yamlDirectory

    if ($LASTEXITCODE -ne 0)
    {
        Write-Error "Kustomize error processing $kustomizationPath"
        exit $LASTEXITCODE
    }

    Out-File -FilePath "$yamlDirectory\kustomization-output.yaml" -InputObject $output

    "$yamlDirectory\kustomization-output.yaml"
}

function ValidateK8sYaml([string]$yamlFile, [string]$kubernetesConfigFile)
{
    $kubernetesConfigFilePath = "$rootConfigPath\$kubernetesConfigFile"

    if($DatreeExeFound)
    {
        $kubeServerVersion = "1.15.0"
        (& $KubeCtlExe version --kubeconfig=$kubernetesConfigFilePath 2>&1) | ForEach-Object {
            if ($_ -match "^Server Version.*")
            {
                $data = ConvertFrom-Json ($_ -replace "Server Version: version.Info", "")
                $kubeServerVersion = $data.GitVersion
                if($kubeServerVersion.StartsWith('v'))
                {
                    $kubeServerVersion = $kubeServerVersion.Substring(1)
                }
            }
        }

        $mtx = New-Object System.Threading.Mutex($false, "datree-mutex")
        $mtxResult = $mtx.WaitOne(180000) #3 minutes timeout
        if($mtxResult -eq $false)
        {
            throw "Couldn't aquire 'datree-mutex' in 3 minutes"
        }

        try
        {
            if([string]::IsNullOrWhiteSpace($DatreeToken))
            {
                & $DatreeExe config set token $DatreeToken
            }

            if([string]::IsNullOrWhiteSpace($DatreePolicyYaml) -eq $false)
            {
                $TempFolder = [System.IO.Path]::GetTempPath()
                $Guid = ([System.Guid]::NewGuid()).ToString()
                $datreeFolder = [IO.Path]::Combine($TempFolder, "datree")
                EnsurePath $datreeFolder
                $PolicyYamlFile = [IO.Path]::Combine($datreeFolder, "policy-$Guid.yaml")
                $DatreePolicyYaml | Out-File -FilePath $PolicyYamlFile
                & $DatreeExe publish $PolicyYamlFile
                Remove-Item $PolicyYamlFile -force
            }
        }
        finally
        {
{'fraction_non_alphanumeric': 0.12732401589947429, 'fraction_numerical': 0.006026413642774715, 'mean_word_length': 2.3795493934142113, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '104954', 'n_tokens_mistral': 2524, 'n_tokens_neox': 2426, 'n_words': 457}