Configuration - Encrypt Section

Octopus.Script exported 2016-12-27 by cjuroz belongs to ‘Encrypt’ category.

Encrypts several configuration sections for the specified file in a given directory.

Parameters

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

Website directory

WebsiteDirectory

The path to the website physical directory that contains a Web.Config file.

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

Section to encrypt

SectionsToEncrypt

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

Provider Name

Provider

The provider to use for encryption

Configuration File

ConfigFile = web.config

The configuration file to encrypt.

Other Files

OtherFiles

A list of other files in the #{WebsiteDirectory} folder that should be included when encrypting the specified #{SectionToEncrypt}. For example, connectionStrings.config. Values should be separated by a comma.

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
	}
}

$websiteDirectory = Get-Parameter "WebsiteDirectory" -Required
$sectionsToEncrypt = (Get-Parameter "SectionsToEncrypt" -Required) -split ',' | where {$_} | %{$_.Trim()}
$provider = Get-Parameter "Provider" ""
$configFile = Get-Parameter "ConfigFile" "web.config"
$otherFiles = (Get-Parameter "OtherFiles" "") -split ',' | where {$_} | %{$_.Trim()}

Write-Host "Configuration - Encrypt .config"
Write-Host "WebsiteDirectory: $websiteDirectory"
Write-Host "SectionsToEncrypt: $sectionsToEncrypt"
Write-Host "Provider: $provider"
Write-Host "ConfigFile: $configFile"


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

$configFilePath = Join-Path $websiteDirectory $configFile
Write-Host "configFilePath: $configFilePath"
if (!(Test-Path $configFilePath)) {
	HandleError "Specified file $configFile or a Web.Config file must exist in the directory $websiteDirectory"
}

$frameworkPath = [System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory();
$regiis = "$frameworkPath\aspnet_regiis.exe"

if (!(Test-Path $regiis)) {
	HandleError "The tool aspnet_regiis does not exist in the directory $frameworkPath"
}

# Create a temp directory to work out of and copy our config file to web.config
$tempPath = Join-Path $websiteDirectory $([guid]::NewGuid()).ToString()
if (!$whatIf) {
	New-Item $tempPath -ItemType "directory"
} else {
	Write-Host "WhatIf: New-Item $tempPath -ItemType ""directory""" -Foreground Yellow
}

$tempFile = Join-Path $tempPath "web.config"
if (!$whatIf) {
    New-Item -ItemType File -Path $tempFile -Force
	Copy-Item $configFilePath $tempFile -Force
} else {
	Write-Host "WhatIf: Copy-Item $configFilePath $tempFile" -Foreground Yellow
}

Foreach($fileName in $otherFiles){
  if (!$whatIf) {
     New-Item -ItemType File -Path (Join-Path $tempPath $fileName) -Force
	 Copy-Item (Join-Path $websiteDirectory $fileName) (Join-Path $tempPath $fileName) -Force
  } else {
	 Write-Host "WhatIf: Copy-Item $configFilePath $tempFile" -Foreground Yellow
  }
}

Foreach($sectionToEncrypt in $sectionsToEncrypt){
	# Determine arguments
	if ($provider) {
		$args = "-pef", $sectionToEncrypt, $tempPath, "-prov", $provider
	} else {
		$args = "-pef", $sectionToEncrypt, $tempPath
	}

	# Encrypt Web.Config file in directory
	if (!$whatIf) {
		& $regiis $args
		if ($LASTEXITCODE) {
		    HandleError "There was an error trying to encrypt section: $sectionToEncrypt"
		}
	} else {
		Write-Host "WhatIf: $regiis $args" -Foreground Yellow
	}
}

# Copy the web.config back to original file and delete the temp dir
if (!$whatIf) {
	Copy-Item $tempFile $configFilePath -Force

  Foreach($fileName in $otherFiles){
    if (!$whatIf) {
  	 Copy-Item (Join-Path $tempPath $fileName) (Join-Path $websiteDirectory $fileName) -Force
    } else {
  	 Write-Host "WhatIf: Copy-Item $configFilePath $tempFile" -Foreground Yellow
    }
  }

  Remove-Item $tempPath -Recurse
} else {
	Write-Host "WhatIf: Copy-Item $tempFile $configFilePath -Force" -Foreground Yellow
	Write-Host "WhatIf: Remove-Item $tempPath -Recurse" -Foreground Yellow
}

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": "193628c4-2251-41e9-a782-225e632ef871",
  "Name": "Configuration - Encrypt Section",
  "Description": "Encrypts several configuration sections for the specified file in a given directory.",
  "Version": 4,
  "ExportedAt": "2016-12-27T03:36:42.383Z",
  "ActionType": "Octopus.Script",
  "Author": "cjuroz",
  "Parameters": [
    {
      "Id": "0cae0018-f915-47c5-a8d6-cdef437346df",
      "Name": "WebsiteDirectory",
      "Label": "Website directory",
      "HelpText": "The path to the website physical directory that contains a `Web.Config` file. \n\nYou can get the InstallationDirectoryPath like so `#{Octopus.Action[StepName].Output.Package.InstallationDirectoryPath}`",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    },
    {
      "Id": "3d7e5485-106e-415e-83b7-cf12c59e776b",
      "Name": "SectionsToEncrypt",
      "Label": "Section to encrypt",
      "HelpText": "The name of the section in the web config to encrypt e.g. `appSettings`, `connectionStrings` etc.\nFor multiple sections, separate with a comma (,)",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    },
    {
      "Id": "12a46b72-3835-4086-b8d6-bd5ad3f99f10",
      "Name": "Provider",
      "Label": "Provider Name",
      "HelpText": "The provider to use for encryption",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    },
    {
      "Id": "998aa949-74b6-4da6-a838-5637e7a5a322",
      "Name": "ConfigFile",
      "Label": "Configuration File",
      "HelpText": "The configuration file to encrypt.",
      "DefaultValue": "web.config",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    },
    {
      "Id": "67415967-4722-449c-8d81-28eff7bf9876",
      "Name": "OtherFiles",
      "Label": "Other Files",
      "HelpText": "A list of other files in the `#{WebsiteDirectory}` folder that should be included when encrypting the specified `#{SectionToEncrypt}`. For example, `connectionStrings.config`. Values should be separated by a comma.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "MultiLineText"
      },
      "Links": {}
    }
  ],
  "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$websiteDirectory = Get-Parameter \"WebsiteDirectory\" -Required\n$sectionsToEncrypt = (Get-Parameter \"SectionsToEncrypt\" -Required) -split ',' | where {$_} | %{$_.Trim()}\n$provider = Get-Parameter \"Provider\" \"\"\n$configFile = Get-Parameter \"ConfigFile\" \"web.config\"\n$otherFiles = (Get-Parameter \"OtherFiles\" \"\") -split ',' | where {$_} | %{$_.Trim()}\n\nWrite-Host \"Configuration - Encrypt .config\"\nWrite-Host \"WebsiteDirectory: $websiteDirectory\"\nWrite-Host \"SectionsToEncrypt: $sectionsToEncrypt\"\nWrite-Host \"Provider: $provider\"\nWrite-Host \"ConfigFile: $configFile\"\n\n\nif (!(Test-Path $websiteDirectory)) {\n\tHandleError \"The directory $websiteDirectory must exist\"\n}\n\n$configFilePath = Join-Path $websiteDirectory $configFile\nWrite-Host \"configFilePath: $configFilePath\"\nif (!(Test-Path $configFilePath)) {\n\tHandleError \"Specified file $configFile or a Web.Config file must exist in the directory $websiteDirectory\"\n}\n\n$frameworkPath = [System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory();\n$regiis = \"$frameworkPath\\aspnet_regiis.exe\"\n\nif (!(Test-Path $regiis)) {\n\tHandleError \"The tool aspnet_regiis does not exist in the directory $frameworkPath\"\n}\n\n# Create a temp directory to work out of and copy our config file to web.config\n$tempPath = Join-Path $websiteDirectory $([guid]::NewGuid()).ToString()\nif (!$whatIf) {\n\tNew-Item $tempPath -ItemType \"directory\"\n} else {\n\tWrite-Host \"WhatIf: New-Item $tempPath -ItemType \"\"directory\"\"\" -Foreground Yellow\n}\n\n$tempFile = Join-Path $tempPath \"web.config\"\nif (!$whatIf) {\n    New-Item -ItemType File -Path $tempFile -Force\n\tCopy-Item $configFilePath $tempFile -Force\n} else {\n\tWrite-Host \"WhatIf: Copy-Item $configFilePath $tempFile\" -Foreground Yellow\n}\n\nForeach($fileName in $otherFiles){\n  if (!$whatIf) {\n     New-Item -ItemType File -Path (Join-Path $tempPath $fileName) -Force\n\t Copy-Item (Join-Path $websiteDirectory $fileName) (Join-Path $tempPath $fileName) -Force\n  } else {\n\t Write-Host \"WhatIf: Copy-Item $configFilePath $tempFile\" -Foreground Yellow\n  }\n}\n\nForeach($sectionToEncrypt in $sectionsToEncrypt){\n\t# Determine arguments\n\tif ($provider) {\n\t\t$args = \"-pef\", $sectionToEncrypt, $tempPath, \"-prov\", $provider\n\t} else {\n\t\t$args = \"-pef\", $sectionToEncrypt, $tempPath\n\t}\n\n\t# Encrypt Web.Config file in directory\n\tif (!$whatIf) {\n\t\t& $regiis $args\n\t\tif ($LASTEXITCODE) {\n\t\t    HandleError \"There was an error trying to encrypt section: $sectionToEncrypt\"\n\t\t}\n\t} else {\n\t\tWrite-Host \"WhatIf: $regiis $args\" -Foreground Yellow\n\t}\n}\n\n# Copy the web.config back to original file and delete the temp dir\nif (!$whatIf) {\n\tCopy-Item $tempFile $configFilePath -Force\n\n  Foreach($fileName in $otherFiles){\n    if (!$whatIf) {\n  \t Copy-Item (Join-Path $tempPath $fileName) (Join-Path $websiteDirectory $fileName) -Force\n    } else {\n  \t Write-Host \"WhatIf: Copy-Item $configFilePath $tempFile\" -Foreground Yellow\n    }\n  }\n\n  Remove-Item $tempPath -Recurse\n} else {\n\tWrite-Host \"WhatIf: Copy-Item $tempFile $configFilePath -Force\" -Foreground Yellow\n\tWrite-Host \"WhatIf: Remove-Item $tempPath -Recurse\" -Foreground Yellow\n}\n",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.RunOnServer": "false",
    "Octopus.Action.Script.ScriptFileName": null,
    "Octopus.Action.Package.FeedId": null,
    "Octopus.Action.Package.PackageId": null
  },
  "Category": "Encrypt",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/configuration-encrypt-web-config-section.json",
  "Website": "/step-templates/193628c4-2251-41e9-a782-225e632ef871",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAIAAAAiOjnJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAB6NJREFUeNrsnd9vU2UYx/ejXW1lxVlMG+hMtsUFtoTOZBjKBSaacIUJGuKNCSReeaEXeqnX4p03+g/gpZhwxQ2GJXDBUGbcSDZgZCWhm2nDCq7Fdl076uNOXAiwruftOe37nn4+IcvYj6Z79znP8z3veXrWXavVugCcpoclAMQCxALEAkAsQCxALADEAsQCxAJALEAsQCwAxALEAsQCQCxALEAsAMQCTfGxBPl8vlgsWm9LpZJ8ZPud3ZfP59u7d6+84/f7w+FwMBgMhUKRSIRV7e7MV+mIRplMJpfLra2tVatVxx9fJItsEYvFEKsjfEqn06JUgwXJEcStaDQ6ODiIWF6jUqksLy+nUqlW+vRi0xTDRkdHpVcilheUun//vijlRr9TQ/qj6OX5HOZZsTRU6jm9EomEh6uXN8WSIDU/P6+nUs8yNDQk1UvOKBF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