Packer: powershell provisioner incorrectly parses a command

Created on 20 Apr 2018  ·  7Comments  ·  Source: hashicorp/packer

It seems that the powershell provisioner incorrectly parses an inline command i'm providing:
~JSON
{
"type": "powershell",
"inline": "(new-object net.webclient).DownloadFile('https://raw.githubusercontent.com/fireeye/flare-vm/master/install.ps1', 'C:\Temp\install.ps1')"
},
~

generating the following (incorrect) script:
~
$ cat /tmp/packer-powershell-provisioner193103905
powershell.exe -Command "(new-object net.webclient).DownloadFile('https://raw.githubusercontent.com/fireeye/flare-vm/master/install.ps1'
'C:\Temp\install.ps1')'"
~

The comma , has disappeared, and there is a newline inserted.

Therefore my script fails with the following error:
~
==> qemu: Provisioning with powershell script: /tmp/packer-powershell-provisioner309169358
qemu: Missing ')' in method call.
qemu: At C:\Windows\Temp\script-5ad9e063-149c-985c-1fe6-1bc5a1452876.ps1:2 char:2
qemu: + <<<< "C:\Temp\install.ps1")
qemu: + CategoryInfo : ParserError: (CloseParenToken:TokenId) [], Parse
qemu: Exception
qemu: + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
qemu:
~

  • Packer version: Packer v1.2.2
  • Host: Ubuntu 16.04
  • Debug log gist
  • reproduce the issue from this repo: packer-flare

Thanks a lot !

bug core

Most helpful comment

Wow. This is fascinating. This error is occurring when Packer tries to parse the Inline data into a slice -- it looks like the parser is pretty naive and will split the string into a slice based solely on commas in said string: https://github.com/hashicorp/packer/blob/master/helper/config/decode.go#L71. This boggles me a bit because I'm shocked more people don't use commas in their inline scripts and we haven't had more reports of this.

For now, the workaround for you is moving your code into a script file. I'll have to think about whether there's a way to make this parsing smarter without causing more headaches than it solves.

All 7 comments

Wow. This is fascinating. This error is occurring when Packer tries to parse the Inline data into a slice -- it looks like the parser is pretty naive and will split the string into a slice based solely on commas in said string: https://github.com/hashicorp/packer/blob/master/helper/config/decode.go#L71. This boggles me a bit because I'm shocked more people don't use commas in their inline scripts and we haven't had more reports of this.

For now, the workaround for you is moving your code into a script file. I'll have to think about whether there's a way to make this parsing smarter without causing more headaches than it solves.

Hi @SwampDragons , thanks for confirming this !

I spent quite some time trying to figure out what was wrong in my Powershell command...
Until I opened the generated script and started to incriminate the JSON parser :)

I revisited this and realized your actual issue is that you're providing a string instead of an array in the config. Try this instead:

        {
            "type": "powershell",
            "inline": ["(new-object net.webclient).DownloadFile('https://raw.githubusercontent.com/fireeye/flare-vm/master/install.ps1', 'C:\\Temp\\install.ps1')"]
        }

When you don't provide an array, we "helpfully" try to parse your string into an array. This isn't always successful.

@SwampDragons I confirm, it worked !
~
==> qemu: Provisioning with powershell script: /tmp/packer-powershell-provisioner405542002
==> qemu: Provisioning with Powershell...
==> qemu: Provisioning with powershell script: /tmp/packer-powershell-provisioner732599910
qemu:  ______ _ _____ ______ __ ____ __
qemu: | ____| | /\ | __ \| ____| \ \ / / \/ |
qemu: | |__ | | / \ | |__) | |__ _____\ \ / /| \ / |
qemu: | __| | | / /\ \ | _ /| __|______\ \/ / | |\/| |
qemu: | | | |____ / ____ \| | \ \| |____ \ / | | | |
qemu: |_| |______/_/ __| _______| \/ |_| |_|
qemu: I N S T A L L A T I O N
qemu: ________________________________________________________
qemu: Developed by
qemu: Peter Kacherginsky
qemu: FLARE (FireEye Labs Advanced Reverse Engineering)
qemu: _______________________________________________________
qemu:
qemu: [ * ] Installing Boxstarter
qemu: Welcome to the Boxstarter Module installer!
qemu: Chocolatey is going to be downloaded and installed on your machine. If you do not have the .NET Framework Version 4 or greater, that will also be downloaded and installed.
qemu: Downloading .net 4.5...
qemu: Installing .net 4.5...
~

So is this still a bug in the parser ?
because there is only one command here surrounded by two double quotes.

I'd argue that this is more of an error in our validation than our parser -- we should only accept arrays, rather than trying to force strings into arrays. When we move to HCL we'll be stricter about typing in the config, but in the meantime I don't really want to force the validation since it'll break current user's configs.

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

znerd picture znerd  ·  3Comments

Tensho picture Tensho  ·  3Comments

sourav82 picture sourav82  ·  3Comments

mvermaes picture mvermaes  ·  3Comments

jesse-c picture jesse-c  ·  3Comments