Search code examples
azureyamlspecial-charactersazure-keyvault

YAML strings including special characters % and & were not printed correctly for Windows environment


I have a YAML file. The YAML file is orchestrating the test execution in a remote agent in Azure. One part of YAML file is responsible to Get Key secrets from Azure (key vaults) and passes them to the tests. The YAML file is perfectly working as long as the target agent is a Linux machine, but not for a Windows machine.

The core of the problem is that the key secrets in Azure have many special characters. Two of my credentials have & and % in the middle. Since I'm reading the credentials from Azure, I can't modify them and insert escape characters before special characters in my strings.

I tried to encapsulate the strings with " and ' and some different combinations and was unable to print the credentials correctly. For those credentials with % and & in the middle, I see a partial print. I like to print them out before continuing with the next task to make sure that I got the credentials correctly.

Does someone have encounter such a problem for Windows machine? If yes, what is the solution?

So that is part of the YAML code #Reading 4 secrets

steps:
- task: AzureKeyVault@2
  inputs:
     azureSubscription: 'DEV'
     KeyVaultName: 'my key vault'
     SecretsFilter: 'secret1, secret2, secret3, secret4'
  displayName: 'Get DEV secrets'

#Printing the secrets

- script: echo "$(secret1)"
- script: echo "$(secret2)"   # have problem here
- script: echo "$(secret3)"
- script: echo "$(secret4)" 

In my case, the strings are something like "%bm^nOYfTk4^G%GDEfAb" or "ktUG!nr9&Tv5M5H5Tt$U". In the first case for Windows, I get "GDEfAb" which is not the whole string. In the second case for Windows, I get "ktUG!nr9" which is also a partial result.

As I mentioned earlier, I tried to replace " with ' and some other combinations in printing tasks and none of them worked for Windows so far. Any idea how I can resolve and fix the issue for Windows?

Somehow, I need to dictate to the system that it should read/print the whole string as literal and it should not try interpreting the special characters % and &.

To reiterate the point, I'm able to read and print the whole strings when my target machine is Linux, but the same file has trouble when I change my target machine to Windows OS.

I tried to replace " with ' and some other combinations in printing tasks and none of them worked for Windows so far.


Solution

  • When running on Windows, the default script handler in Azure DevOps uses cmd.exe, unless you explicitly change it.

    Which is causing your partial outputs for strings that contains cmd-reserved characters, cmd is trying to parse %...% as an environment variable or uses & to chain commands.

    Either switch to PowerShell on Windows or escape it properly.

    Me personally, I'd recommend you to use the PowerShell approach.