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

nixcache: configure auth with devbox cache configure #1961

Merged
merged 8 commits into from
Apr 8, 2024

Conversation

gcurtis
Copy link
Collaborator

@gcurtis gcurtis commented Apr 4, 2024

Depends on #1959 and #1960. The diff contains commits from those PRs.


Add a hidden devbox cache configure command that sets up Nix so it can authenticate with a private Devbox cache. This is a first step. Ultimately, we'd like to do this configuration automatically before building. We just need to make sure we don't re-prompt the user for sudo or slow down commands like devbox run or devbox shell.

Configuring auth goes as follows:

  1. Check to see if the Nix daemon is running. If it isn't, we don't need to do anything.
  2. Otherwise, we check to see if ~root/.aws/config exists. If it does, we assume that we've already configured the root user's AWS credentials. This won't handle situations where the user already has a root AWS config and can be improved.
  3. If the AWS config doesn't exist, prompt the user to escalate to root. If they say yes, relaunch devbox cache configure.
  4. The new devbox process running as root writes an AWS config file that tells the AWS CLI/SDKs to authenticate by running devbox cache credentials.

Note that we need to use the default AWS profile. There's a bug in Nix where non-default profiles (specified in the S3 URI with ?profile=devbox) don't use the default credential chain. If this gets fixed, we should use a separate profile so that we don't interfere with any AWS options set by the user (although hopefully this is rare for root).

`nix.DaemonVersion` attempts to connect to the Nix daemon and returns
its version string. Devbox will use the error from this function when
configuring cache authentication:

- If the nix daemon is running, we need to update ~root/.aws/config so
  that it can authenticate with the private devbox cache. This requires
  a sudo prompt.
- If it's not running, then it's probably a single-user install without
  a daemon. We won't need sudo.
The `devbox cache credentials` subcommand prints out short-lived AWS STS
credentials that grant access to the user's Nix cache.

The output follows the format described in `aws help config-vars` under
`Sourcing Credentials From External Processes`. This allows the AWS
CLI/SDKs to obtain credentials from Devbox. For example:

	[default]
	credential_process = /usr/local/bin/devbox cache credentials

Because Nix uses the AWS SDK's default credential chain, this allows it
to automatically authenticate with private Devbox caches.

Note: this can be improved by using the newer credentials-only API
endpoint, which is faster.
Add a hidden `devbox cache configure` command that sets up Nix so it can
authenticate with a private Devbox cache. This is a first step.
Ultimately, we'd like to do this configuration automatically before
building. We just need to make sure we don't re-prompt the user for sudo
or slow down commands like `devbox run` or `devbox shell`.

Configuring auth goes as follows:

1. Check to see if the Nix daemon is running. If it isn't, we don't need
   to do anything.
2. Otherwise, we check to see if `~root/.aws/config` exists. If it does,
   we assume that we've already configured the root user's AWS
   credentials. This won't handle situations where the user already has a
   root AWS config and can be improved.
3. If the AWS config doesn't exist, prompt the user to escalate to root.
   If they say yes, relaunch `devbox cache configure`.
4. The new devbox process running as root writes an AWS config file that
   tells the AWS CLI/SDKs to authenticate by running `devbox cache
   credentials`.

Note that we need to use the default AWS profile. There's a bug in Nix
where non-default profiles (specified in the S3 URI with
?profile=devbox) don't use the default credential chain. If this gets
fixed, we should use a separate profile so that we don't interfere with
any AWS options set by the user (although hopefully this is rare for
root).
Copy link
Contributor

@mikeland73 mikeland73 left a comment

Choose a reason for hiding this comment

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

For followup:

  • We should save the configure state to a place that the user can access so devbox can remember if it's already been configured. Specifically, for a non-root user in a multi user install, the cache wont work at all if they haven't configured. Since we don't want to ask for sudo every time, saving some state to non a non root location (maybe XDG config) so that we can warn the user they need to configure if they have not previously done so.

internal/devbox/packages.go Show resolved Hide resolved
internal/devbox/providers/nixcache/nixcache.go Outdated Show resolved Hide resolved
internal/devbox/providers/nixcache/nixcache.go Outdated Show resolved Hide resolved
internal/devbox/providers/nixcache/nixcache.go Outdated Show resolved Hide resolved
internal/devbox/providers/nixcache/nixcache.go Outdated Show resolved Hide resolved
@gcurtis gcurtis merged commit 653e884 into main Apr 8, 2024
24 checks passed
@gcurtis gcurtis deleted the gcurtis/cache-auth branch April 8, 2024 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants