Skip to content

Fly adds quotes when inserting vars into part of a string #360

Closed

Description

@danger-ranger

For example:

  regexp: upgrades/{{env_name}}/ert/cf-(.*).pivotal

Becomes:

  regexp: upgrades/"test"/ert/cf-(.*).pivotal

Which doesn't work. :(

Activity

  1. concourse-bot commented on Apr 8, 2016

    @concourse-bot

    Hi there!

    We use Pivotal Tracker to provide visibility into what our team is working on. A story for this issue has been automatically created.

    The current status is as follows:

    • #117229091 Fly adds quotes when inserting vars into part of a string

    This comment, as well as the labels on the issue, will be automatically updated as the status in Tracker changes.

  2. danger-ranger commented on Apr 11, 2016

    @danger-ranger
    ContributorAuthor

    sadness

  3. keymon commented on Apr 27, 2016

    @keymon
    Contributor

    +1 for fixing this

  4. keymon commented on Apr 29, 2016

    @keymon
    Contributor

    Other place where this is annoying, if you are using the s3-resource:

              put: bosh-init-state
              params:
                file: updated-bosh-init-state/{{bosh_manifest_state}}
    

    It will fail because it tries to upload a file with quotes:

    error running command: no matches found for pattern: updated-bosh-init-state/"bosh-manifest-state-eu-west-1a.json"
    

    +1 it is quite annoying.

    In general we workaround this issue by:

    • Adding logic before uploading the pipeline in a shell script, doing the interpolation before and uploading different variables. In this case a variable called generated_bosh_manifest_state: --var generated_bosh_manifest_state=updated-bosh-init-state/${BOSH_MANIFEST_STATE}.
    • For cases where pure YAML is required, as @rmasand describes, we have to do string substitution in the pipeline before uploading. Other option is using spruce or spiff
    • If the variable is going to be consumed by tasks, we do interpolation using environment variables and shell script in the task.
    • In the case of S3 resource, use a wildcard which matches only that file: "updated-bosh-init-state/bosh-manifest-state-*.json", s3 recognises wildcards.

    All of these solutions are suboptimal workarounds.

  5. mtekel commented on Apr 29, 2016

    @mtekel

    +1 !

  6. combor commented on Jun 9, 2016

    @combor
    Contributor

    +1

  7. christophe-f commented on Jun 9, 2016

    @christophe-f

    +1

  8. vito commented on Jul 13, 2016

    @vito
    Contributor

    I'm hesitant to fix this as I'm not convinced text substitution is the correct layer to be doing anything more than passing raw values around. I also don't want to end up with another templating language on my hands. Especially since if you really need fancier parameterization, you could always just use any of those tools: there's no need to make Fly's any fancier.

    Fly's templating system is intentionally braindead simple. It's not just that it wraps quotes around the strings - it JSON encodes it so it can be safely passed into the YAML unsullied - and from there it'll be passed along through wherever you put the value, explicitly handed to whatever needs it.

    Raw text substitution at this layer is an easy way to quickly hack something together and introduce all kinds of implicit dependencies and subtle bugs. For example, if you were to interpolate it into an argument passed to an embedded task script, what happens when that argument starts to contain special characters that BASH interprets? ($, !, etc. - pretty likely if the param is e.g. a generated password). Passing along the value wholesale via params would then be the solution, which is the same thing you'd do today.

    In most other cases, there's normally a better answer that removes the need for the param in the first place. For example, having abstract filenames, or a consistent pattern for them. Related to @keymon's point, I actually think the S3 wildcard pattern approach is better than using a parameter as it seems more self-documenting, and is a common pattern today already for matching filenames with version numbers in them.

    Sometimes the right answer is to just move the whole string into the parameter and don't try to get too fancy with it. This tends to make the pipeline usable in more cases, e.g. when you don't want to put your files in the exact same folder structure as the other team using the template. Params are cheap: they're usually just kept in a file, and I don't think having to write a bit more around the param is a big deal.

    So: rather than just +1'ing the issue, can you each give a specific example of when you needed this?

  9. keymon commented on Jul 13, 2016

    @keymon
    Contributor

    +1

  10. danger-ranger commented on Jul 13, 2016

    @danger-ranger
    ContributorAuthor

    "Passing along the value wholesale via params would then be the solution, which is the same thing you'd do today."

    Could you explain what you mean by this bit? Do you mean params in a task? If so I don't think this would apply to my three examples: s3 regexp, s3 private config boolean, empty worker tags list.

  11. keymon commented on Jul 13, 2016

    @keymon
    Contributor

    😆 I'm joking about the +1.

    I see your point of view.

    I explained my motivations and I wanted to avoid having to use a template engine like spiff/spruce, but if the position of the concourse development is not handling this, it is fine for me and it justifies introduce 3rd party templating solutions.

  12. 1 remaining item

  13. nebhale commented on Jul 13, 2016

    @nebhale

    @vito My use cause is the externalizing the credentials for a URI. In one of our tests, we need to pass in the URI to use to access a private Cloud Foundry buildpack. That param is TEST_BUILDPACK: https://username:[email protected]/org/repo.git. At the moment, this now requires me to external the entirety of the URI, unlike everywhere else in our many pipelines that use URIs. Ideally I'd like to be able to write ``TEST_BUILDPACK: https://username:{{github-access-token}}@github.com/org/repo.git` instead.

  14. danger-ranger commented on Jul 13, 2016

    @danger-ranger
    ContributorAuthor

    I'm still not clear on what you meant by the bit I quoted above, but I get that anything more than quick and dirty templating should be done with a 3rd party tool. Which is fine because we like to put all of our fly set-pipeline calls in scripts anyway.

    Ohhh, you were probably referring to the Using {{parameters}} section of the fly set-pipeline documentation. Yeah, for substituting parts of strings that makes sense. Doesn't address the boolean field problem, which is too bad but easily fixed pre-fly. And the dynamic workers sound cool :) I'm excited to see what that looks like.

  15. vito commented on Jul 13, 2016

    @vito
    Contributor

    @xtreme-gavin-enns Ah sorry - I was just pointing out that raw text substitution can get pretty dangerous as it propagates through abstraction layers.

    For example, from YAML to Bash embedded in the same document:

    task: foo
    config:
      run:
        path: bash
        args:
        - -c
        - |
          # ...
          curl http://{{my-username}}:{{my-password}}@example.com
          # ...

    If my-password is set to foo!, this will blow up:

    (sid)alex@localhost:~$ curl http://foo:foo!@example.com
    bash: !@example.com: event not found

    However, if the value is propagated through params and into the task's env:

    task: foo
    params:
      MY_USERNAME: {{my-username}}
      MY_PASSWORD: {{my-password}}
    config:
      run:
        path: bash
        args:
        - -c
        - |
          # ...
          curl http://$MY_USERNAME:[email protected]
          # ...

    ...that'll work fine:

    (sid)alex@localhost:~$ export MY_PASSWORD="foo!"
    (sid)alex@localhost:~$ curl -I http://foo:$MY_PASSWORD@example.com
    HTTP/1.1 200 OK
    Accept-Ranges: bytes
    Cache-Control: max-age=604800
    Content-Type: text/html
    Date: Wed, 13 Jul 2016 16:27:33 GMT
    Etag: "359670651"
    Expires: Wed, 20 Jul 2016 16:27:33 GMT
    Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
    Server: ECS (rhv/818F)
    X-Cache: HIT
    x-ec-custom-error: 1
    Content-Length: 1270

    Having {{params}} behave as stupidly as they do now is a pretty OK way of preventing this kind of thing from happening. I'd sooner go farther though and have them only valid as the complete value of a YAML node.

  16. keymon commented on Jul 13, 2016

    @keymon
    Contributor

    That params usage is good for tasks, but in the case of resources does not work. Because that, you need to build a url like http://{{my-username}}:{{my-password}}@example.com using a shell script or a template engine before calling fly.

  17. vito commented on Jul 13, 2016

    @vito
    Contributor

    @keymon In that case you should use explicit username and password fields, which the git, semver, and pool resources for example support.

  18. vito commented on Jul 13, 2016

    @vito
    Contributor

    @nebhale Similar case there: it may make sense to just have separate username and password fields. Curl and most tools support explicit username and password handling. If your tooling doesn't support it you could always just add them to the URL in your task.

  19. freelock commented on Jul 13, 2016

    @freelock
    Contributor

    Hi,

    In my case, I'm templating a fairly extensive pipeline -- it's currently up to ~20 resources, similar number of jobs. And I see getting up to 200 copies of this pipeline in the relatively near term (we currently have 35).

    I use a credentials file to populate git keys, etc that are all shared. Otherwise the pipeline varies by a single "Alias" for the specific project that I'm passing into it.

    The alias is used to specify a bunch of different things: path inside S3 bucket, git repo name, pipeline name, various filenames.

    So far what I've done is create a shell script for creating each pipeline -- the main purpose of which is purely to expand the alias into a bunch of variables to plug into the pipeline (because otherwise it gets quoted and breaks all kinds of things). For example, here's what I currently have in the shell script:

    fly -t concourse set-pipeline -c run_tests.yml -l $creds -v project-alias=$site \
      -v [email protected]:$site \
      -v project-s3-shots=artifacts/shots/${site}_shots.tgz \
      -v project-version-file=artifacts/versions/${site}_version \
      -v aws-artifacts-regexp="artifacts/tests/${site}-(.*)-(?P<version>[0-9]+\.[0-9]+\.[0-9]+.*)\.(tgz|tar.gz)" \
      -p ${site} -n
    
    fly -t concourse unpause-pipeline -p $site
    

    ... And I'm nowhere near done yet. I'll probably need to add at least another 6 - 7 variables like this to the shell script, if the parameter expansion stays as is. This solution does work just fine for me, but it "feels" a bit ugly... I think in my case it would be far clearer to allow the parameters to be embedded in the Yaml strings without quotes -- as it is, it's quite unclear whether a parameter is comprised of something expanded from the site alias, or something coming from the shared credential file.

  20. vito commented on Jul 15, 2016

    @vito
    Contributor

    I'm beginning to think that once we get to implementing Vault support (#291) we should consider deprecating {{params}} usage entirely. It was added as a stopgap to get credentials safely split out of a pipeline. Vault support will mean switching to or adding a syntax that gets sent to the ATC to interpret, which is different from today's behavior (straight text substitution). Today's {{params}} syntax is also not valid YAML which would make that difficult, so we'd probably use a different syntax for Vault references.

    With our intentionally dumb templating system out of the way people will more quickly lean on any of the dozens of more appropriate tools for the hopefully fewer cases (now that Vault is used for creds) where they need parameterization.

  21. robdimsdale commented on Jul 15, 2016

    @robdimsdale
    Contributor

    I tend to agree that parameterization should be external to fly. Partly because of single-responsibilities, and partly because this would pave the way for some better tooling to emerge - perhaps some first-class tooling around building pipelines from snippets?

    Separately, as a developer of the concourse-pipeline-resource which consumes both the go-concourse library and needs to reimplement the templating logic from fly, not having to support templating would make my life much easier. I infer the same is true for developers of fly too.

  22. vito commented on Jul 18, 2016

    @vito
    Contributor

    Another set of ideas after chatting with @cppforlife who has basically the same goals but with BOSH:

    • Keep parameters, but make them follow YAML structural semantics.
    • Generalize the Vault parameter syntax in Using external credential managers for build credentials #291 so that it fits the same structure, and isn't coupled to Vault.
    • Resolve some parameters client-side, and leave anything left over to the server to try to resolve via its credential service (i.e. Vault).

    This would probably entail switching from the current raw substitution method, which means switching to (( these guys )) so that we can parse the YAML first and then interpret any embedded template values.

    We would then special-case foo((bar))baz like so: expand it into something like (( "foo" bar "baz" )), load the bar value, require that it be a String, and then concatenate.

    This would also fix the true/false case, as we'd be pulling in whatever the value was from the template files. However this would leave -v foo=true ambiguous, so maybe we'd start encouraging using a template file for those.

    You could also do (( foo.bar.baz )), which would work if your vars file had something like foo: {bar: baz: 42}} in it. If not, we would leave it un-interpolated, and the ATC would look it up in the credential store (i.e. Vault) instead. Which would support the same interpolation rules, though I'm not sure Vault supports typed values, and I don't want to require that people store JSON.

  23. nebhale commented on Jul 18, 2016

    @nebhale

    @vito This certainly meets all of my requirements and I don't find the change from curly-braces to parenthesis to be onerous.

  24. vito commented on Jul 19, 2016

    @vito
    Contributor

    Consolidating this and all related issues into #545 so I can break this out into multiple stories for the one issue. Thanks for the feedback everyone!

  25. added a commit that references this issue on Aug 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions