infisical icon indicating copy to clipboard operation
infisical copied to clipboard

feat(cli): plain secret value output

Open Grraahaam opened this issue 1 year ago â€ĸ 2 comments

Description đŸ“Ŗ

User wasn't able to get its expanded/imported secrets as plain text when executing infisical secrets get FOO.

This PR includes the following to address the above issue :

đŸ’ģ CLI

  • Expand and import secrets when using infisical secret get
    • Get the processed reference (e.g. ${dev.VAR1})
    • Get the imported secrets (e.g. when imported variables from env A to env B, for now in the web app)
  • Output plain value(s) without the secret name using the --plain flag
    • For both infisical secret & infisical secret get
      • Is it useful to implement such feature when printing all secrets (infisical secret)? As the initial use case was to easily integrate with scripts by getting only a single Infisical secret value. I've implemented it for the sake of consistency, but it can be easily removed if needed.
    • e.g. infisical secret get VAR1 --plain (more examples in the tests below)

📚 Docs

  • Updated the /cli/commands/secrets page to add the new --plain flag

â„šī¸ I've just updated the main feature in the official docs, but the CLI documentation should be automatically generated IMO (docs are outdated for now, 2024-02-08). Mintlify does support OpenAPI docs, but I haven't seen any support/integration for CLI docs auto-generation... (through cobra for example, or just from the man page)

Related issue

  • https://github.com/Infisical/infisical/issues/1351
  • https://github.com/Infisical/infisical/issues/845
  • https://github.com/Infisical/infisical/pull/1532

Type ✨

  • [ ] Bug fix
  • [x] New feature
  • [ ] Breaking change
  • [x] Documentation

Tests đŸ› ī¸

đŸ’ģ CLI

Here's the Infisical project, development environment (imported in the staging environment too)

# --expand=true (default)
┌─────────────â”Ŧ──────────────â”Ŧ─────────────┐
│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │
├─────────────â”ŧ──────────────â”ŧ─────────────┤
│ VAR1        │ value1       │ shared      │
│ VAR2        │ value2       │ shared      │
│ VAR3        │ value3       │ shared      │
│ VAR4        │ value1       │ shared      │
└─────────────┴──────────────┴─────────────┘

# --expand=false
┌─────────────â”Ŧ──────────────â”Ŧ─────────────┐
│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │
├─────────────â”ŧ──────────────â”ŧ─────────────┤
│ VAR1        │ value1       │ shared      │
│ VAR2        │ value2       │ shared      │
│ VAR3        │ value3       │ shared      │
│ VAR4        │ ${VAR1}      │ shared      │
└─────────────┴──────────────┴─────────────┘

Formatted
# get all secrets
./infisical-merge secrets
┌─────────────â”Ŧ──────────────â”Ŧ─────────────┐
│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │
├─────────────â”ŧ──────────────â”ŧ─────────────┤
│ VAR1        │ value1       │ shared      │
│ VAR2        │ value2       │ shared      │
│ VAR3        │ value3       │ shared      │
│ VAR4        │ value1       │ shared      │
└─────────────┴──────────────┴─────────────┘

# get single secret (expanded)
./infisical-merge secrets get VAR1
┌─────────────â”Ŧ──────────────â”Ŧ─────────────┐
│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │
├─────────────â”ŧ──────────────â”ŧ─────────────┤
│ VAR1        │ value1       │ shared      │
└─────────────┴──────────────┴─────────────┘

# get multiple secrets (expanded)
./infisical-merge secrets get VAR1 VAR2
┌─────────────â”Ŧ──────────────â”Ŧ─────────────┐
│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │
├─────────────â”ŧ──────────────â”ŧ─────────────┤
│ VAR1        │ value1       │ shared      │
│ VAR2        │ value2       │ shared      │
└─────────────┴──────────────┴─────────────┘
Plain
# get all secrets
./infisical-merge secrets --plain
value1
value2
value3
value1

# get single secret (plain)
./infisical-merge secrets get VAR1 --plain
value1

# get multiple secrets (plain)
./infisical-merge secrets get VAR1 VAR2 --plain
value1
value2

# get single secret (raw)
./infisical-merge secrets get VAR4 --expand=false --plain
${VAR1}

# get multiple secrets (raw)
./infisical-merge secrets get VAR3 VAR4 --expand=false --plain
value3
${VAR1}
Imported
# get all secrets
./infisical-merge secrets --env stg
┌─────────────â”Ŧ──────────────â”Ŧ─────────────┐
│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │
├─────────────â”ŧ──────────────â”ŧ─────────────┤
│ VAR1        │ value1       │ shared      │
│ VAR2        │ value2       │ shared      │
│ VAR3        │ value3       │ shared      │
│ VAR4        │ value1       │ shared      │
└─────────────┴──────────────┴─────────────┘

# get single secret
./infisical-merge secrets get VAR4 --env stg
┌─────────────â”Ŧ──────────────â”Ŧ─────────────┐
│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │
├─────────────â”ŧ──────────────â”ŧ─────────────┤
│ VAR4        │ value1       │ shared      │
└─────────────┴──────────────┴─────────────┘
Use cases
# script (single value)
api_key="$(./infisical-merge secrets get VAR1 --plain)"
echo "$api_key"
value1

# script (multiple value)
api_key="$(./infisical-merge secrets get VAR1 VAR2 --plain)"
echo "$api_key"
value1
value2

📚 Docs

Here's how to test the fix/feat regarding the --plain, --expand, --include-imports flags in the docs

# documentation preview
mintlify dev

# or with npx
npx mintlify dev

Head over to the below page :

  • http://localhost:3000/cli/commands/secrets#sub-commands

CLI - Secrets

secrets


Grraahaam avatar Feb 08 '24 16:02 Grraahaam

Isn't this resolved by another PR. 🤔

akhilmhdh avatar Mar 12 '24 13:03 akhilmhdh

Isn't this resolved by another PR. 🤔

Only partially by https://github.com/Infisical/infisical/pull/1532, but the discussion is undergoing with @maidul98 through slack.

This PR address additional issues to the infisical secret get command, adding and defaulting --expand and include-imports to true for example.

And the possibility to support multiple secret plain output (one per line), which isn't the case in #1532

Grraahaam avatar Mar 12 '24 17:03 Grraahaam

Hey @Grraahaam, pr looks good to me. Thanks for adding the docs as well. Can you please fix the merge conflicts?

maidul98 avatar May 20 '24 20:05 maidul98

âš ī¸ GitGuardian has uncovered 5 secrets following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secrets in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
9387833 Triggered Generic Password 092665737fc337f4535c615316ba976837a515df .env.example View secret
8529478 Triggered Generic High Entropy Secret 092665737fc337f4535c615316ba976837a515df .env.example View secret
9605380 Triggered Generic Private Key 092665737fc337f4535c615316ba976837a515df backend/e2e-test/routes/v3/secrets.spec.ts View secret
9605380 Triggered Generic Private Key 092665737fc337f4535c615316ba976837a515df backend/e2e-test/routes/v3/secrets.spec.ts View secret
9605381 Triggered Generic High Entropy Secret 092665737fc337f4535c615316ba976837a515df .env.test.example View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secrets safely. Learn here the best practices.
  3. Revoke and rotate these secrets.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


đŸĻ‰ GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

gitguardian[bot] avatar May 24 '24 15:05 gitguardian[bot]

Merge conflicts resolved @maidul98 ✌đŸŊ

As discussed on Slack, I've left the --raw-value flag and deprecated it, I've added some backward compatibility tho to avoid breaking any CI/script currently running.

I've tested the latest commit and everything works as described previously, let me know if there's something more to fix before merging it!

Once merged this issue can be closed as well : https://github.com/Infisical/infisical/issues/1351

Grraahaam avatar May 24 '24 15:05 Grraahaam