Hello,
We are not able to update sensitive variables (Common and Projects Template scope) for tenants via REST API. Here is some info:
Case:
We want to update variables (Common Tenant template and Project Template for specified tenant.
We get a some kind of template via Invoke-RestMethod:
function Get-TenantVariables
{
<#
.Synopsis
Get variables for specified tenant
.DESCRIPTION
Get variables for specified tenant
.EXAMPLE
Get-TenantVariables -TenantName 'New Tenant Template' -OctopusAPIKey 'API-JC92IPF2PMJUHYM30O1QWRSDOA'
.EXAMPLE
Get-TenantVariables -TenantName 'New Tenant Template' -OctopusAPIKey 'API-JC92IPF2PMJUHYM30O1QWRSDOA' | ConvertTo-Json -Depth 10 | Out-File tenantvars.json
.LINK
https://octopus.com/docs/octopus-rest-api/examples/tenants/update-tenant-variable
#>
param (
[string][Parameter(Mandatory=$false)]$OctopusURL='http://localhost',
[string][Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]$TenantName,
[string][Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]$OctopusAPIKey,
[switch]$ToJsonFile
)
# Define working variables
$header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey }
# Get Tenant
$tenantsSearch = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/tenants?name=$TenantName" -Headers $header)
$tenant = $tenantsSearch.Items | Select-Object -First 1
if(!$tenant) {Write-Output "Tenant '$TenantName' doesn't exist"; exit}
$variables = (Invoke-RestMethod -Method Get -Uri "$OctopusURL/api/tenants/$($tenant.Id)/variables" -Headers $header)
return $variables
}
Get-TenantVariables -TenantName 'New Tenant Template' -OctopusAPIKey 'API-XXX' | ConvertTo-Json -Depth 10 | Out-File tenantvars.json
Then we change values in the template (based on Update tenant variables - Octopus Deploy)
function Update-TenantVariables
{
<#
.Synopsis
Update variables for specified tenant
.DESCRIPTION
Update variables for specified tenant
.EXAMPLE
Update-TenantVariables -TenantName 'New Tenant Template' -OctopusAPIKey 'API-XXX' -TenantVariablesFile tenantvars.json -TenantConfig tenantconfig.json
.LINK
https://octopus.com/docs/octopus-rest-api/examples/tenants/update-tenant-variable
#>
param (
[string][Parameter(Mandatory=$false)]$OctopusURL='http://localhost',
[string][Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]$TenantName,
[string][Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]$OctopusAPIKey,
[string][Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]$TenantVariablesFile,
[string][Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]$TenantConfig
)
# Define working variables
$header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey }
# Get Tenant
$tenantsSearch = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/tenants?name=$TenantName" -Headers $header)
$tenant = $tenantsSearch.Items | Select-Object -First 1
if(!$tenant) {Write-Output "Tenant '$TenantName' doesn't exist"; exit}
$variables = Get-Content $TenantVariablesFile | ConvertFrom-Json -Depth 10
$config = Get-Content $TenantConfig | ConvertFrom-Json -Depth 10
# Update variables
$variables.TenantId = $tenant.Id
$variables.TenantName = $TenantName
# Some Project
$variables.ProjectVariables.'Projects-11'.Variables.'Environments-11'.'1aad00c3-e723-456e-8c73-809df6fb8aa5' = $config.SomeVar
$variables.ProjectVariables.'Projects-11'.Variables.'Environments-12'.'1aad00c3-e723-456e-8c73-809df6fb8aa5' = $config.SomeVar
$variables.ProjectVariables.'Projects-11.Variables.'Environments-13'.'1aad00c3-e723-456e-8c73-809df6fb8aa5' = $config.SomeVar
$variables.ProjectVariables.'Projects-11'.Variables.'Environments-11'.'2aad00c3-e723-456e-8c73-809df6fb8aa5' = $config.SensitiveVar
$variables.ProjectVariables.'Projects-11'.Variables.'Environments-12'.'2aad00c3-e723-456e-8c73-809df6fb8aa5' = $config.SentitiveVar
$variables.ProjectVariables.'Projects-11.Variables.'Environments-13'.'2aad00c3-e723-456e-8c73-809df6fb8aa5' = $config.SensitiveVar
# Some Variable set
$variables.LibraryVariables.'LibraryVariableSets-1'.Variables.'e9f6b010-0804-4b40-a32e-39ffa4429ea7' = $config.SensitiveVar
# Links
$variables.Links.Self = "/api/tenants/$($tenant.Id)/variables"
$variables.Links.Tenant = "/api/tenants/$($tenant.Id)"
# Update the variables with the new value
Invoke-RestMethod -Method Put -Uri "$octopusURL/api/tenants/$($tenant.Id)/variables" -Headers $header -Body ($variables | ConvertTo-Json -Depth 10)
}
Update-TenantVariables -TenantName 'New Tenant Template' -OctopusAPIKey 'API-XXX' -TenantVariablesFile tenantvars.json -TenantConfig tenantconfig.json
Problem:
All variables (Library and Project Variables) are updated w/o issues except sensitive variables. They are displayed in Octopus UI as a plaintext.
Few notes:
$tenantconfig.json file is:
{
"SomeVar": "simple var",
"SensitiveVar": "some secret"
}
When I get variables from any tenant, sensitive variables in json file looks like:
{
...
"Environments-11": {
"2aad00c3-e723-456e-8c73-809df6fb8aa5": {
"HasValue": true,
"NewValue": null
}
}
...
}
I tried to write something like above w/o any luck:
$variables.ProjectVariables.'Projects-11.Variables.'Environments-13'.'2aad00c3-e723-456e-8c73-809df6fb8aa5' = @{"HasValue"= $true; "NewValue" = $config.SensitiveVar}
Octopus version: 2018.10.1 LTS
Is it possible to update sensitive variables within POST API call to “/api/tenants/$($tenant.Id)/variables”? ANy other suggestions?
P.S.
I read Unable to update sensitive varibale in octopus using powershell script - #17 by capanusha193, but can’t map it to my case when I need to update variables for specific tenant (both Library and Project)