Skip to content

Add credentials:fetch command#53119

Merged
byroot merged 1 commit intorails:mainfrom
n-studio:fetch_credentials
Aug 4, 2025
Merged

Add credentials:fetch command#53119
byroot merged 1 commit intorails:mainfrom
n-studio:fetch_credentials

Conversation

@n-studio
Copy link
Contributor

Motivation / Background

With Kamal 2, secrets are now stored in .kamal/secrets. We are expected to use a 3rd party password manager such as 1password to fetch secrets such as KAMAL_REGISTRY_PASSWORD.

eg. KAMAL_REGISTRY_PASSWORD=$(kamal secrets extract KAMAL_REGISTRY_PASSWORD ${SECRETS})

I think it would be interesting to be able to fetch the password directly from Rails credentials.

eg. KAMAL_REGISTRY_PASSWORD=$(bin/rails credentials:fetch kamal_registry/password)

In order to allow it, I introduce the new command credentials:fetch.

Detail

Usage:

# credentials.yml.enc
secret_key_base: ****
kamal_registry:
  password: abcd1234
bin/rails credentials:fetch kamal_registry/password
# abcd1234

Additional information

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@MatheusRich
Copy link
Contributor

This looks very useful. I've been using KAMAL_REGISTRY_PASSWORD=$(rails runner "print Rails.application.credentials.kamal_registry_password") currently, which is very cumbersome.

@MatheusRich MatheusRich requested a review from dhh September 30, 2024 19:00
@MatheusRich MatheusRich added the ready PRs ready to merge label Sep 30, 2024
@zzak
Copy link
Member

zzak commented Oct 3, 2024

I'm not super familiar but it seems like kamal secrets is preferred for fetching the password:

# KAMAL_REGISTRY_PASSWORD=$(kamal secrets extract KAMAL_REGISTRY_PASSWORD ${SECRETS})

@n-studio
Copy link
Contributor Author

n-studio commented Oct 3, 2024

@zzak I can make a PR to add an adapter in kamal, but I would still need the credentials:fetch command line in Rails. Unless I use rails runner instead.

Maybe I can remove the comments in kamal-secrets.tt for now and see what the maintainers of Kamal think?

@zzak
Copy link
Member

zzak commented Oct 4, 2024

I'm not sure but if new apps are using that why can't old ones? Maybe I'm missing something.

@MatheusRich
Copy link
Contributor

@zzak I believe Kamal secrets fetches a secrets from passeord managers like 1password/Bitwarden. There's no specific Rails support.

@dhh
Copy link
Member

dhh commented Oct 4, 2024

This is really clever. I like the concept that for some apps, they can just use rails credentials. We should add that as an example in the default .kamal/secrets file that we generate.

cc @djmb

Copy link
Member

@byroot byroot left a comment

Choose a reason for hiding this comment

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

There's some work needed, but I'll take care of it.

load_environment_config!

if (yaml = credentials.read)
YAML.load(yaml).dig(*path.split("/")).tap do |value|
Copy link
Member

Choose a reason for hiding this comment

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

We should return an error if the key is missing. Also I think . is much more logical to nagigate than /.

say value.to_s
end
else
say missing_credentials_message
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
say missing_credentials_message
say_error missing_credentials_message

Error message should go on stderr, not stdout, it also should exit with non zero status code, otherwise this is very hard to use in a script.

```bash
$ bin/rails credentials:fetch kamal_registry/password
```

Co-Authored-By: Jean Boussier <jean.boussier@gmail.com>
@byroot byroot force-pushed the fetch_credentials branch from 2645f1b to ccf22d6 Compare August 4, 2025 08:49
@byroot byroot merged commit 9a64857 into rails:main Aug 4, 2025
3 checks passed
@keithschacht
Copy link
Contributor

I noticed the default kamal/secrets file never got this added as an example. I skimmed the open PRs and did not see one there. This is the current status of the secrets template: https://github.com/basecamp/kamal/blob/4b88852aea58bf94009a6d854afdc630a5c111b5/lib/kamal/cli/templates/secrets#L4

MatheusRich added a commit to MatheusRich/kamal that referenced this pull request Aug 11, 2025
This was [recently introduced in Rails][1] and it's worth teaching this new concept.

[1]: rails/rails#53119
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

railties ready PRs ready to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants