Calamari icon indicating copy to clipboard operation
Calamari copied to clipboard

Add variable enumeration for bash

Open IsaacCalligeros95 opened this issue 2 weeks ago • 0 comments

Support enumerating variable in bash scripts. This change is based off this 2018 change and will add support for accessing variables in bash scripts as associative arrays. In Powershell this is supported by the OctopusParameters property, in bash this will be backed by octopus_parameters. Details on iterating associative arrays are available here. As an example this can be used with

The implementation for this is based off of the PowerShell implementation of OctopusParameters Decrypt-Variables.

For PowerShell we do the following in C#

  • Base 64 encode the variable name and value with a $ delimiter between them. (This is to make sure all special characters are removed)
  • Encrypt the combined string of all variables (We can a base string and IV)
  • Base64 encode the base string and convert the IV to hex.

These are then string replaced in the PowerShell bootstrap.ps1 script.

The PowerShell bootstrap script does the following:

  • Decodes the IV and Hex
  • Decrypts the whole string
  • Decodes one by one the variable name and values

This works well in Powershell ~3000 variables takes about 1 second in this process. In base we use Openssl to decode base64 strings, this spins up a process and overall repeating this same process for ~3000 variables takes ~60 seconds.

To work around this I've settled on Hex encoding the variable names, unlike base64 hex encoded strings can be concatenated and decrypted all at once, this also doesn't depend on the openssl process being invoked. In this version I've settled on xxd, and if it's not available we do not populate the octopus_parameters variable. We can handle this ourselves but the implementation is messy and I did run into some issues with encoding/decoding emojis.

Accessing keys

echo ${!example_array[@]}

Accessing values

echo ${example_array[@]}

Accessing a value

echo ${example_array["key1"]}

Combined example.

for key in "${!octopus_parameters[@]}"; do
    value="${octopus_parameters[$key]}"
    echo "Key: $key, Value: $value"
done

After: image

image

IsaacCalligeros95 avatar Feb 13 '25 03:02 IsaacCalligeros95