woodpecker-ci / woodpecker

Woodpecker is a simple yet powerful CI/CD engine with great extensibility.
https://woodpecker-ci.org
Apache License 2.0
3.95k stars 351 forks source link

Parsing issue when using YAML anchors/aliases for secrets #2331

Closed lonix1 closed 8 months ago

lonix1 commented 11 months ago

Component

server

Describe the bug

As discussed in discord.

There is an issue with how the parser deals with yaml anchors/aliases when used for secrets.

A minimal repro below:

.woodpecker.yml

variables:
  - &USERNAME some_global_secret_1
  - &TOKEN some_global_secret_2

steps:

  use-as-list:
    image: busybox
    commands:
      - echo $USERNAME
      - echo $TOKEN
    secrets:
      - *USERNAME
      - *TOKEN

  use-as-scalar:
    image: woodpeckerci/plugin-docker-buildx
    settings:
      dockerfile: Dockerfile
      registry: ${CI_FORGE_URL##https://}
      username: ${CI_REPO_OWNER}
      password:
        from_secret: *TOKEN
      repo: ${CI_FORGE_URL##https://}/${CI_REPO_OWNER}/myrepo
      tag: test

Dockerfile

FROM busybox
ENTRYPOINT ["echo", "hello"]

Result:

failed to parse pipeline: yaml: unknown anchor 'USERNAME' referenced

@6543 Spent some time investigating. Some preliminary results:

System Info

1.0.2
docker

Additional context

No response

Validations

lonix1 commented 11 months ago

I suspect I found a related issue.

It's slightly different, as above example allows use of an alias as a scalar (from_secret: *TOKEN), whereas below it does not (secrets: *TOKEN). Maybe the schemas as different, and so it's the same bug, I'm unsure.

variables:
  - &TOKEN watchtower_token

steps:

  # other steps...

  deploy:
    image: busybox
    commands:
      - curl -sSf -H "Authorization: Bearer $WATCHTOWER_TOKEN" https://watchtower.example.com/v1/update
   #secrets: ...see various cases below...

Passing case 1: hardcoded literal in list (the only syntax that works)

secrets:
  - watchtower_token

Failing case 1: hardcoded literal

secrets: watchtower_token

failed to parse pipeline: yaml: unmarshal errors: line 1: cannot unmarshal !!str global_... into []*types.Secret

Failing case 2: alias as scalar

secrets: *TOKEN

failed to parse pipeline: yaml: unmarshal errors: line 1: cannot unmarshal !!str global_... into []*types.Secret

Failing case 3: alias in list

secrets:
  - *TOKEN

failed to parse pipeline: yaml: unknown anchor 'TOKEN' referenced

All those cases should be valid yaml.

qwerty287 commented 9 months ago

I don't think this is a bug.

All those cases should be valid yaml.

No. We require secrets to be a list, so it can't be a scalar - yes, we added this option to other fields and we could add it for secrets too, but currently, that's not the case and you must use a list.

To use anchors with secrets, the anchor itself must be a list too:

variables:
  - &SECRET [ test_secret ]

...

    secrets: *SECRET

If you have multiple secret anchors, you can combine them like this:

variables:
  - &SECRET [ test_secret ]
  - &SECRET_2 [ test_2 ]

...

    secrets:
      - << *SECRET
      - << *SECRET_2

Both are parsed by woodpecker without errors.

qwerty287 commented 8 months ago

Closing for now. If there's still issue, just comment.