Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split mixin command args by whitespace #931

Merged
merged 3 commits into from
Mar 9, 2020

Conversation

carolynvs
Copy link
Member

What does this change

Some mixin commands have whitespace and right now we are passing them in as packed into a single array element when executing the command

exec.Cmd("aws", []string{"ec2", "run-instances", "--security-group-ids group1 group2"})

When the debug command is printed, we can't tell that this has happened, and why the command failed. What needs to happen is that when there is a space, and it's not enclosed in double quotes, it should be split into its own array element

exec.Cmd("aws", []string{"ec2", "run-instances", "--security-group-ids", "group1", "group2"})

What issue does it fix

I think this will help with getporter/aws-mixin#15

Here is how it would work

install:
  - aws:
      description: "Provision VM"
      service: ec2
      operation: run-instances
      flags:
        image-id: ami-xxxxxxxxxxxxxxxxx
        instance-type: t2.micro
        region: eu-west-1
        subnet-id: subnet-xxxxxx
        security-group-ids: sg-1 sg2

If you really needed the flag to get both arguments passed together then you would use this:

install:
  - aws:
      description: "Provision VM"
      service: ec2
      operation: run-instances
      flags:
        image-id: ami-xxxxxxxxxxxxxxxxx
        instance-type: t2.micro
        region: eu-west-1
        subnet-id: subnet-xxxxxx
        security-group-ids: '"sg-1 sg2"'

Notes for the reviewer

Since the command looks the same when printed, I wasn't sure how to test this out other than just testing the function I added.

Checklist

  • Unit Tests
  • Documentation
    • Documentation Not Impacted

@carolynvs
Copy link
Member Author

/azp run porter

@carolynvs
Copy link
Member Author

/azp run porter-integration

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

1 similar comment
@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

// Split up any arguments or flags that have spaces so that we pass them as separate array elements
// It doesn't show up any differently in the printed command, but it matters to how the command
// it executed against the system.
args = expandOnWhitespace(args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think passing the command off to the shell (which parses this for us) be something that would work here instead of parsing the command ourselves? Looks similar to the problem I ran into here: https://github.com/deislabs/porter/pull/921/files#diff-9cde54dd12e8d18ebea48c996aa54c92R45

Copy link
Member Author

@carolynvs carolynvs Feb 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@halkyon Do you have a cross platform way of doing that? I was looking for ideas yesterday on how to make the shell deal with this instead of that kludge function (that doesn't work anyway). I would love some help fixing this problem! 😂

In some invocation images, there isn't a shell, they may have built the image on something without bash. That's why I ask.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is so tempting to be like bash -c "dump all their stuff here" but actually their command may also be a bash -c "..." command too. Or it could be calling to a binary that is in a scratch based invocation image.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, I didn’t realise the environments commands could be running in, still getting my head around porter. Sorry for butting in a bit on this PR by the way!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh hey no worries, I liked the suggestion and would have used it in a heartbeat if I could!

@carolynvs carolynvs force-pushed the mixin-arg-parsing branch from 3e66098 to 4019357 Compare March 2, 2020 16:36
@carolynvs carolynvs marked this pull request as ready for review March 2, 2020 19:40
Some mixin commands have whitespace and right now we are passing them in
as packed into a single array element when executing the command

exec.Cmd("aws", []string{"ec2", "run-instances", "--security-group-ids
group1 group2"})

When the debug command is printed, we can't tell that this has happened,
and why the command failed. What needs to happen is that when there is a
space, and it's not enclosed in double quotes, it should be split into
its own array element

exec.Cmd("aws", []string{"ec2", "run-instances", "--security-group-ids",
"group1", "group2"})
@carolynvs carolynvs force-pushed the mixin-arg-parsing branch from 4019357 to 0b4a4bb Compare March 3, 2020 15:05
@carolynvs
Copy link
Member Author

I don't think I'm going to get away with my cheap trick with the csv parser. 😉 I just added a failing test case for enclosing escaped double quotes and will work on writing an artisanal small batch parser for our own needs.

* Split by space
* Keep single and double quoted text together
* Ignore escaped quotes
* When quotes don't match, preserve original string
@carolynvs carolynvs force-pushed the mixin-arg-parsing branch from fe25930 to d8bea60 Compare March 5, 2020 22:58
@carolynvs
Copy link
Member Author

Okay, artisanal small batch parser works. I want to hold off on merging this until I get some more pieces in place however since this is a disruptive change. It removes a bug that people relied upon that allowed them to call bash -c foo bar baz without the quotes around "foo bar baz".

Here are some things that need to be done before we do a release:

  • Add a best practice around embedding bash in porter.yaml
  • Scan for us using anything that relied upon the bug in this repo
  • Add a build hard validation for anyone calling bash -c without wrapping quotes because the quotes are necessary now.
  • Add a build soft validation about using bash -c with the exec mixin, and point to the best practice.

We don't need all of these done before this can be merged, I just want to get a plan in place to do this in a timely fashion so that we don't have canary borked for a while.

Copy link
Member

@vdice vdice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strong parsing skills here! Test cases seem to cover all bases... still looking good to me.

@carolynvs carolynvs merged commit bda88fe into getporter:master Mar 9, 2020
@carolynvs carolynvs deleted the mixin-arg-parsing branch March 9, 2020 20:47
carolynvs pushed a commit to carolynvs/aws-mixin that referenced this pull request Mar 27, 2020
Do not assume that mixin arguments and flags should be passed as a quoted string
to the cli. Require that they are wrapped in quotes instead and
otherwise split on space to pass them separately.

See getporter/porter#931 for how this works.
carolynvs pushed a commit to carolynvs/aws-mixin that referenced this pull request Mar 27, 2020
Do not assume that mixin arguments and flags should be passed as a quoted string
to the cli. Require that they are wrapped in quotes instead and
otherwise split on space to pass them separately.

See getporter/porter#931 for how this works.
carolynvs pushed a commit to carolynvs/az-mixin that referenced this pull request Mar 27, 2020
Do not assume that mixin arguments and flags should be passed as a
quoted string to the cli. Require that they are wrapped in quotes
instead and otherwise split on space to pass them separately.

See getporter/porter#931 for how this works.
carolynvs pushed a commit to carolynvs/gcloud-mixin that referenced this pull request Mar 27, 2020
Do not assume that mixin arguments and flags should be passed as a quoted string
to the cli. Require that they are wrapped in quotes instead and
otherwise split on space to pass them separately.

See getporter/porter#931 for how this works.
carolynvs pushed a commit to carolynvs/porter-helm that referenced this pull request Mar 30, 2020
Do not assume that mixin arguments and flags should be passed as a quoted string
to the cli. Require that they are wrapped in quotes instead and
otherwise split on space to pass them separately.

See getporter/porter#931 for how this works.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants