hashicorp / hashicorp/packer-plugin-sdk
Setting elevated_user to build user dynamically breaks Powershell provisioner
- Dominant language
- Go
- Stars
- 42
- Forks
- 62
- Avg merge
- 29m
- Merged PRs (30d)
- 2
Description
### Overview of the Issue
There seems to be some kind of templating/interpolation failure when using a `build` variable as the value for the `elevated_user` property of the `powershell` provisioner. The interpolation that is failing appears to be here:
https://github.com/hashicorp/packer-plugin-sdk/blob/7e45483efa78142ca694da2667ad69867502c262/guestexec/elevated.go#L97
### Reproduction Steps
Run a build with the following provisioner:
```
{
"type": "powershell",
"script": "common/test.ps1",
"environment_vars": [
"TEST_PACKER_1={{build `User`}}",
"TEST_PACKER_2={{build `Password`}}"
],
"elevated_user": "{{build `User`}}",
"elevated_password": "{{build `Password`}}"
}
```
Interestingly, with the example above, the environment variable `TEST_PACKER_1` will be successfully set to the build username (so I don't believe it's an issue with that function), however, the script will fail to launch with the error below. Also, if the username is set explicitly (e.g. `"elevated_user": "ExplicitUsername"`), everything works fine.
### Log Fragments
```
==> (AWS) Elevated_Test: Provisioning with powershell script: common/test.ps1
==> (AWS) Elevated_Test: (12,8):UserId:
==> (AWS) Elevated_Test: At C:\Windows\Temp\packer-elevated-shell-634090c0-1adf-5fb0-c9b2-82dad6d5bf56.ps1:60 char:1
==> (AWS) Elevated_Test: + $f.RegisterTaskDefinition($name, $t, 6, "{{.User}}", $password, $logo ...
==> (AWS) Elevated_Test: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
==> (AWS) Elevated_Test: + CategoryInfo : OperationStopped: (:) [], COMException
==> (AWS) Elevated_Test: + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
```
As shown above, the value `{{.User}}` is being passed into Powershell directly, which is not intended behavior.
### Operating system and Environment details
I'm able to reproduce this issue with Packer 1.6.6 and 1.8.3 on both Windows and Linux.
I'm able to reproduce in both `amazon-ebs` and `azure-arm` builders.
Contributor guide
Assessment
This issue has not been assessed yet.