Configuration - Encrypt App.config Section

Octopus.Script exported 2016-12-13 by kp-tseng belongs to ‘Encrypt’ category.

Encrypts configuration sections for the specified executable.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Executable path

ExecutablePath

The path to the executable that has a corresponding [Executable].exe.config file.

You can get the InstallationDirectoryPath like so #{Octopus.Action[StepName].Output.Package.InstallationDirectoryPath}

Sections to encrypt

SectionsToEncrypt

The name of the section in the App config to encrypt e.g. appSettings, connectionStrings etc. For multiple sections, separate with a comma (,)

Provider Name

Provider

The provider to use for encryption

Script body

Steps based on this template will execute the following PowerShell script.

$ErrorActionPreference = "Stop" 
function Get-Parameter($Name, $Default, [switch]$Required) {
    $result = $null

    if ($OctopusParameters -ne $null) {
        $result = $OctopusParameters[$Name]
    }

    if ($result -eq $null) {
        if ($Required) {
            throw "Missing parameter value $Name"
        } else {
            $result = $Default
        }
    }

    return $result
}

function HandleError($message) {
	if (!$whatIf) {
		throw $message
	} else {
		Write-Host $message -Foreground Yellow
	}
}

$appPath = Get-Parameter "ExecutablePath" -Required
$sectionsToEncrypt = (Get-Parameter "SectionsToEncrypt" -Required) -split ',' | where {$_} | %{$_.Trim()}
$provider = Get-Parameter "Provider" "DataProtectionConfigurationProvider"

Write-Host "Configuration - Encrypt .config"
Write-Host "ExecutablePath: $appPath"
Write-Host "SectionToEncrypt: $sectionName"
Write-Host "Provider: $provider"

if (!(Test-Path $appPath)) {
    HandleError "The directory $appPath must exist"
}

$configurationAssembly = "System.Configuration, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a"
[void] [Reflection.Assembly]::Load($configurationAssembly)
 
$configuration = [System.Configuration.ConfigurationManager]::OpenExeConfiguration($appPath)

foreach ($sectionToEncrypt in $sectionsToEncrypt){
	$section = $configuration.GetSection($sectionToEncrypt)
 
    if (-not $section.SectionInformation.IsProtected)
    {
        $section.SectionInformation.ProtectSection($provider);
        $section.SectionInformation.ForceSave = [System.Boolean]::True;
    }
}

$configuration.Save([System.Configuration.ConfigurationSaveMode]::Modified);

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "d80a7d9a-8c7b-4aa6-934e-0929bce606fe",
  "Name": "Configuration - Encrypt App.config Section",
  "Description": "Encrypts configuration sections for the specified executable.",
  "Version": 2,
  "ExportedAt": "2016-12-13T07:19:21.741Z",
  "ActionType": "Octopus.Script",
  "Author": "kp-tseng",
  "Parameters": [
    {
      "Name": "ExecutablePath",
      "Label": "Executable path",
      "HelpText": "The path to the executable that has a corresponding `[Executable].exe.config` file. \n\nYou can get the InstallationDirectoryPath like so `#{Octopus.Action[StepName].Output.Package.InstallationDirectoryPath}`",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "SectionsToEncrypt",
      "Label": "Sections to encrypt",
      "HelpText": "The name of the section in the App config to encrypt e.g. `appSettings`, `connectionStrings` etc. For multiple sections, separate with a comma (,)",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "Provider",
      "Label": "Provider Name",
      "HelpText": "The provider to use for encryption",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "$ErrorActionPreference = \"Stop\" \nfunction Get-Parameter($Name, $Default, [switch]$Required) {\n    $result = $null\n\n    if ($OctopusParameters -ne $null) {\n        $result = $OctopusParameters[$Name]\n    }\n\n    if ($result -eq $null) {\n        if ($Required) {\n            throw \"Missing parameter value $Name\"\n        } else {\n            $result = $Default\n        }\n    }\n\n    return $result\n}\n\nfunction HandleError($message) {\n\tif (!$whatIf) {\n\t\tthrow $message\n\t} else {\n\t\tWrite-Host $message -Foreground Yellow\n\t}\n}\n\n$appPath = Get-Parameter \"ExecutablePath\" -Required\n$sectionsToEncrypt = (Get-Parameter \"SectionsToEncrypt\" -Required) -split ',' | where {$_} | %{$_.Trim()}\n$provider = Get-Parameter \"Provider\" \"DataProtectionConfigurationProvider\"\n\nWrite-Host \"Configuration - Encrypt .config\"\nWrite-Host \"ExecutablePath: $appPath\"\nWrite-Host \"SectionToEncrypt: $sectionName\"\nWrite-Host \"Provider: $provider\"\n\nif (!(Test-Path $appPath)) {\n    HandleError \"The directory $appPath must exist\"\n}\n\n$configurationAssembly = \"System.Configuration, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a\"\n[void] [Reflection.Assembly]::Load($configurationAssembly)\n \n$configuration = [System.Configuration.ConfigurationManager]::OpenExeConfiguration($appPath)\n\nforeach ($sectionToEncrypt in $sectionsToEncrypt){\n\t$section = $configuration.GetSection($sectionToEncrypt)\n \n    if (-not $section.SectionInformation.IsProtected)\n    {\n        $section.SectionInformation.ProtectSection($provider);\n        $section.SectionInformation.ForceSave = [System.Boolean]::True;\n    }\n}\n\n$configuration.Save([System.Configuration.ConfigurationSaveMode]::Modified);",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "Encrypt",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/configuration-encrypt-app-config-section.json",
  "Website": "/step-templates/d80a7d9a-8c7b-4aa6-934e-0929bce606fe",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAIAAAAiOjnJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAB6NJREFUeNrsnd9vU2UYx/ejXW1lxVlMG+hMtsUFtoTOZBjKBSaacIUJGuKNCSReeaEXeqnX4p03+g/gpZhwxQ2GJXDBUGbcSDZgZCWhm2nDCq7Fdl076uNOXAiwruftOe37nn4+IcvYj6Z79znP8z3veXrWXavVugCcpoclAMQCxALEAkAsQCxALADEAsQCxAJALEAsQCwAxALEAsQCQCxALEAsA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