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

Fix --config being ignored by loadConfig #1613

Merged
merged 1 commit into from
Sep 14, 2024
Merged

Conversation

lopter
Copy link
Contributor

@lopter lopter commented Sep 13, 2024

Use GlobalString in loadConfig since --config is not a sub-command flag.

This fixes --config being ignored and the CLI erroring out in a maddening way:

% sops --config config/sops.yaml encrypt --input-type json --output-type yaml /dev/stdin < data/test.json | head -n 3
config file not found, or has no creation rules, and no keys provided through command line options
% tree
./
├── config/
│   └── sops.yaml
└── data/
    └── test.json

3 directories, 2 files
% cat config/sops.yaml
creation_rules:
- path_regex: .*
  pgp: ADB6276965590A096004F6D1E114CBAE8FA29165
% cat data/test.json
{
    "key": 42
}
%

Maybe there is more to this fix, and I am happy to hear about it.


FWIW and future reference, here is how I got a debug build on Nix to debug this issue with delve:

$ cat ~/snippets/sops-debug-build.nix
sops.overrideAttrs(final: prev: {
  ldflags = [ "-X github.com/getsops/sops/v3/version.Version=${prev.version}" ];
  GOFLAGS = prev.GOFLAGS ++ [ "'-gcflags=all=-N -l'" ];
  dontStrip = true;
})
$ nix-store --realise $(nix-instantiate ~/snippets/sops-debug-build.nix)

@felixfontein
Copy link
Contributor

Can you elaborate on

CLI erroring out in a maddening way.

? Do you mean an error such as [CMD] FATA[0000] flag provided but not defined: -config when using a subcommand?

Did you try passing --config before the subcommand, like sops --config /path/to/config edit /path/to/file? That works fine for me.

@felixfontein
Copy link
Contributor

Or more precisely, that doesn't produce an error. It seems to be ignored, though. Is that what you mean, and what is fixed by using GlobalString?

@felixfontein
Copy link
Contributor

There are two more places that should change from context.String("config") to context.GlobalString("config") in lines 2044 and 2045.

Also you need to sign-off the commits (see the failing DCO check).

@felixfontein
Copy link
Contributor

While looking at #559 I noticed that this issue was already fixed once in #672. Either that fix was not correct, or got broken by a behavior change in the argument parsing library.

@lopter
Copy link
Contributor Author

lopter commented Sep 14, 2024

Can you elaborate on

CLI erroring out in a maddening way.

? Do you mean an error such as [CMD] FATA[0000] flag provided but not defined: -config when using a subcommand?

Did you try passing --config before the subcommand, like sops --config /path/to/config edit /path/to/file? That works fine for me.

I apologize for not including the error message:

% nix --experimental-features "nix-command flakes" run "nixpkgs#sops" -- --config config/sops.yaml encrypt --input-type json --output-type yaml /dev/stdin < data/test.json | head -n 3
config file not found, or has no creation rules, and no keys provided through command line options
% nix --experimental-features "nix-command flakes" run "github:lopter/nixpkgs/739ff1113708f7f5dea6a2dea423853d2800e42b#sops" -- --config config/sops.yaml encrypt --input-type json --output-type yaml /dev/stdin < data/test.json | head -n 3
key: ENC[AES256_GCM,data:hds=,iv:o4nE57sN3Hrqj4yTqiEeQR+/wqGcSuLJTuxIPrOxJVA=,tag:wmf30k4Kp8ZprWgxdAdX1g==,type:float]
sops:
    kms: []
% tree
./
├── config/
│   └── sops.yaml
└── data/
    └── test.json

3 directories, 2 files
% cat config/sops.yaml
creation_rules:
- path_regex: .*
  pgp: ADB6276965590A096004F6D1E114CBAE8FA29165
% cat data/test.json
{
    "key": 42
}
%

Looking at the blame for main.go, I am wondering if sub-commands were added down the road, and when they did the config loading code was not updated to look at --config as a global flag.

Let me update those two lines and commit with --signoff.

Thank you for the quick follow-up :)

Use `GlobalString` in `loadConfig` since `--config` is not a sub-command
flag.

This fixes `--config` being ignored and the CLI erroring out in a
maddening way:

```
% sops --config config/sops.yaml encrypt --input-type json --output-type yaml /dev/stdin < data/test.json | head -n 3
config file not found, or has no creation rules, and no keys provided through command line options
% tree
./
├── config/
│   └── sops.yaml
└── data/
    └── test.json

3 directories, 2 files
% cat config/sops.yaml
creation_rules:
- path_regex: .*
  pgp: ADB6276965590A096004F6D1E114CBAE8FA29165
% cat data/test.json
{
    "key": 42
}
%
```

---

FWIW and future reference, here is how I got a debug build on Nix to
debug this issue with delve:

```
$ cat ~/snippets/sops-debug-build.nix
sops.overrideAttrs(final: prev: {
  ldflags = [ "-X github.com/getsops/sops/v3/version.Version=${prev.version}" ];
  GOFLAGS = prev.GOFLAGS ++ [ "'-gcflags=all=-N -l'" ];
  dontStrip = true;
})
$ nix-store --realise $(nix-instantiate ~/snippets/sops-debug-build.nix)
```

Signed-off-by: Louis Opter <[email protected]>
@felixfontein
Copy link
Contributor

Looking at the blame for main.go, I am wondering if sub-commands were added down the road, and when they did the config loading code was not updated to look at --config as a global flag.

I guess it's a mixture... In any case, I think we should add some tests that make sure that all subcommands correctly support this option :)

@felixfontein felixfontein merged commit a3fc3c5 into getsops:main Sep 14, 2024
9 checks passed
@felixfontein
Copy link
Contributor

Thanks a lot for fixing this!

tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Oct 14, 2024
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [getsops/sops](https://github.com/getsops/sops) | patch | `v3.9.0` -> `v3.9.1` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>getsops/sops (getsops/sops)</summary>

### [`v3.9.1`](https://github.com/getsops/sops/releases/tag/v3.9.1)

[Compare Source](getsops/sops@v3.9.0...v3.9.1)

#### Installation

To install `sops`, download one of the pre-built binaries provided for your platform from the artifacts attached to this release.

For instance, if you are using Linux on an AMD64 architecture:

```shell

### Download the binary
curl -LO https://github.com/getsops/sops/releases/download/v3.9.1/sops-v3.9.1.linux.amd64

### Move the binary in to your PATH
mv sops-v3.9.1.linux.amd64 /usr/local/bin/sops

### Make the binary executable
chmod +x /usr/local/bin/sops
```

##### Verify checksums file signature

The checksums file provided within the artifacts attached to this release is signed using [Cosign](https://docs.sigstore.dev/cosign/overview/) with GitHub OIDC. To validate the signature of this file, run the following commands:

```shell

### Download the checksums file, certificate and signature
curl -LO https://github.com/getsops/sops/releases/download/v3.9.1/sops-v3.9.1.checksums.txt
curl -LO https://github.com/getsops/sops/releases/download/v3.9.1/sops-v3.9.1.checksums.pem
curl -LO https://github.com/getsops/sops/releases/download/v3.9.1/sops-v3.9.1.checksums.sig

### Verify the checksums file
cosign verify-blob sops-v3.9.1.checksums.txt \
  --certificate sops-v3.9.1.checksums.pem \
  --signature sops-v3.9.1.checksums.sig \
  --certificate-identity-regexp=https://github.com/getsops \
  --certificate-oidc-issuer=https://token.actions.githubusercontent.com
```

##### Verify binary integrity

To verify the integrity of the downloaded binary, you can utilize the checksums file after having validated its signature:

```shell

### Verify the binary using the checksums file
sha256sum -c sops-v3.9.1.checksums.txt --ignore-missing
```

##### Verify artifact provenance

The [SLSA provenance](https://slsa.dev/provenance/v0.2) of the binaries, packages, and SBOMs can be found within the artifacts associated with this release. It is presented through an [in-toto](https://in-toto.io/) link metadata file named `sops-v3.9.1.intoto.jsonl`. To verify the provenance of an artifact, you can utilize the [`slsa-verifier`](https://github.com/slsa-framework/slsa-verifier#artifacts) tool:

```shell

### Download the metadata file
curl -LO  https://github.com/getsops/sops/releases/download/v3.9.1/sops-v3.9.1.intoto.jsonl

### Verify the provenance of the artifact
slsa-verifier verify-artifact <artifact> \
  --provenance-path sops-v3.9.1.intoto.jsonl \
  --source-uri github.com/getsops/sops \
  --source-tag v3.9.1
```

#### Container Images

The `sops` binaries are also available as container images, based on Debian (slim) and Alpine Linux. The Debian-based container images include any dependencies which may be required to make use of certain key services, such as GnuPG, AWS KMS, Azure Key Vault, and Google Cloud KMS. The Alpine-based container images are smaller in size, but do not include these dependencies.

These container images are available for the following architectures: `linux/amd64` and `linux/arm64`.

##### GitHub Container Registry

-   `ghcr.io/getsops/sops:v3.9.1`
-   `ghcr.io/getsops/sops:v3.9.1-alpine`

##### Quay.io

-   `quay.io/getsops/sops:v3.9.1`
-   `quay.io/getsops/sops:v3.9.1-alpine`

##### Verify container image signature

The container images are signed using [Cosign](https://docs.sigstore.dev/cosign/overview/) with GitHub OIDC. To validate the signature of an image, run the following command:

```shell
cosign verify ghcr.io/getsops/sops:v3.9.1 \
  --certificate-identity-regexp=https://github.com/getsops \
  --certificate-oidc-issuer=https://token.actions.githubusercontent.com \
  -o text
```

##### Verify container image provenance

The container images include [SLSA provenance](https://slsa.dev/provenance/v0.2) attestations. For more information around the verification of this, please refer to the [`slsa-verifier` documentation](https://github.com/slsa-framework/slsa-verifier#containers).

#### Software Bill of Materials

The Software Bill of Materials (SBOM) for each binary is accessible within the artifacts enclosed with this release. It is presented as an [SPDX](https://spdx.dev/) JSON file, formatted as `<binary>.spdx.sbom.json`.

#### What's Changed

-   Explicitly mark goreleaser's version, to avoid confusion with the Go version by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1548
-   build(deps): Bump the go group with 8 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1550
-   build(deps): Bump github/codeql-action from 3.25.10 to 3.25.11 in the ci group by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1551
-   Fix the URL used for determining the latest SOPS version by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1553
-   build(deps): Bump the ci group with 4 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1555
-   build(deps): Bump the go group with 11 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1554
-   build(deps): Bump the go group with 10 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1558
-   build(deps): Bump the ci group with 3 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1559
-   build(deps): Bump the go group with 5 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1562
-   build(deps): Bump the ci group across 1 directory with 5 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1564
-   build(deps): Bump the go group with 4 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1565
-   build(deps): Bump github/codeql-action from 3.25.13 to 3.25.15 in the ci group by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1566
-   build(deps): Bump github.com/docker/docker from 27.0.1+incompatible to 27.1.0+incompatible by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1568
-   build(deps): Bump the go group with 5 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1575
-   build(deps): Bump the ci group with 2 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1574
-   build(deps): Bump github.com/docker/docker from 27.1.0+incompatible to 27.1.1+incompatible by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1581
-   build(deps): Bump the ci group with 3 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1584
-   Note on the name of the configuration file by [@&#8203;ErraticMaker](https://github.com/ErraticMaker) in getsops/sops#1569
-   build(deps): Bump the ci group with 2 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1586
-   build(deps): Bump the go group across 1 directory with 15 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1589
-   build(deps): Bump the ci group with 2 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1590
-   build(deps): Bump the go group with 7 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1593
-   build(deps): Bump the ci group with 2 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1592
-   tests: ignore env var SOPS_AGE_KEY_FILE by [@&#8203;duthils](https://github.com/duthils) in getsops/sops#1595
-   build(deps): Bump the go group with 12 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1602
-   build(deps): Bump github.com/opencontainers/runc from 1.1.13 to 1.1.14 by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1603
-   updatekeys subcommand: actually use option `--shamir-secret-sharing-threshold` by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1608
-   Added example of having age recipients in .sops.yaml by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1607
-   GitHub Actions linter for Rust formatting by [@&#8203;duthils](https://github.com/duthils) in getsops/sops#1604
-   github actions: set rust version globally by [@&#8203;duthils](https://github.com/duthils) in getsops/sops#1612
-   Fix `--config` being ignored by `loadConfig` by [@&#8203;lopter](https://github.com/lopter) in getsops/sops#1613
-   build(deps): Bump the go group with 4 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1618
-   build(deps): Bump github/codeql-action from 3.26.6 to 3.26.7 in the ci group by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1619
-   unit tests: add coverage for unset by [@&#8203;duthils](https://github.com/duthils) in getsops/sops#1617
-   sops_test: homogenize use of asserts by [@&#8203;duthils](https://github.com/duthils) in getsops/sops#1622
-   sops_test: add more tests on errors by [@&#8203;duthils](https://github.com/duthils) in getsops/sops#1624
-   Add 'check' task to CI that makes sure everything is green by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1621
-   build(deps): Bump the go group with 10 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1629
-   build(deps): Bump github/codeql-action from 3.26.7 to 3.26.8 in the ci group by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1628
-   refactor: specify missing file in error message by [@&#8203;t3hmrman](https://github.com/t3hmrman) in getsops/sops#1625
-   CI: Build with Go 1.22 and 1.23; update Vault to 1.14 by [@&#8203;reneleonhardt](https://github.com/reneleonhardt) in getsops/sops#1531
-   Allow edit subcommand to create files by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1596
-   Build release with Go 1.22.x by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1615
-   GHA: fix checkout action version by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1630
-   Do not encrypt if a key group is empty, or there are no key groups by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1600
-   updatekeys subcommand: show changes in shamir_threshold by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1609
-   Dependabot: fix config for Docker, add config for Rust/Cargo by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1632
-   build(deps): Bump the ci group with 2 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1634
-   build(deps): Bump the go group with 7 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1635
-   Add Cargo.lock for better reproducability of functional tests by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1637
-   build(deps): Bump the go group with 7 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1639
-   build(deps): Bump the rust group in /functional-tests with 5 updates by [@&#8203;dependabot](https://github.com/dependabot) in getsops/sops#1638
-   Update all go deps with 'go get -t -u ./...' by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1640
-   Do not ignore errors when trying to parse a config file by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1614
-   Release 3.9.1 by [@&#8203;felixfontein](https://github.com/felixfontein) in getsops/sops#1642

#### New Contributors

-   [@&#8203;ErraticMaker](https://github.com/ErraticMaker) made their first contribution in getsops/sops#1569
-   [@&#8203;lopter](https://github.com/lopter) made their first contribution in getsops/sops#1613
-   [@&#8203;t3hmrman](https://github.com/t3hmrman) made their first contribution in getsops/sops#1625
-   [@&#8203;reneleonhardt](https://github.com/reneleonhardt) made their first contribution in getsops/sops#1531

**Full Changelog**: getsops/sops@v3.9.0...v3.9.1

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40NDAuNyIsInVwZGF0ZWRJblZlciI6IjM3LjQ0MC43IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
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.

2 participants