skip to content

In DVC, an S3 secret key set with `dvc remote modify` ended up in a Git commit — why, and where should remote credentials live?

level: middleimportance: must knowfreq 16%

answer

  1. which file the command writes
  2. tracked by default
  3. a Git-ignored sibling file
  4. the --local flag
  5. env vars and credential files

basics

~20 s

Without a level flag, dvc remote modify writes .dvc/config, the project file that Git tracks. Secrets belong in the Git-ignored .dvc/config.local (written with --local) or outside DVC config entirely: environment variables, a credentials file or profile, SSH config.

solid answer

~40 s

`dvc remote modify` defaults to `--project`, so it edits `.dvc/config`, which is meant to be committed; a key set that way rides along with the next `git commit`. Write secrets with `--local` instead: they land in `.dvc/config.local`, which DVC keeps Git-ignored and which overrides `.dvc/config` key by key, so the shared `url` stays in the tracked file. Better still, keep the secret out of DVC config: with nothing set, an S3 remote uses the AWS CLI's configuration or environment variables such as `AWS_ACCESS_KEY_ID`, and `credentialpath` or `profile` can point at a credentials file. After a leak, revoke and rotate the key, then remove it with `dvc remote modify -u`; deleting it in a new commit leaves it in history.

code

bash · 13 lines
bash
# shared settings stay in the tracked .dvc/config
dvc remote add -d ci-s3 s3://example-lidar-scans/dvc
dvc remote modify ci-s3 region us-east-2

# remove the leaked values from the tracked file
dvc remote modify -u ci-s3 access_key_id
dvc remote modify -u ci-s3 secret_access_key

# private settings go to the Git-ignored .dvc/config.local
dvc remote modify --local ci-s3 credentialpath ~/.aws/lidar-team

# see which file every value comes from
dvc config --list --show-origin

go deeper

for a junior

Remember that .dvc/config is committed and .dvc/config.local is not, and that --local is the flag that writes secrets to the private file.

for a middle

Explain the four configuration levels and their merge, which keys are safe to share, and how S3, GCS, Azure and SSH remotes pick up credentials from the environment.

for a senior

Lead the response to a leaked key: rotate first, unset it from the tracked file, re-supply it privately, and verify origins with dvc config --list --show-origin.

for a principal

Decide how credentials reach people and CI across many projects: short-lived role credentials and profiles over static keys, and which settings a shared config may carry.

## Four configuration files, merged DVC reads configuration from up to four files and merges them; a higher-priority file overrides individual keys from a lower one: | Priority | Level | File | Shared through Git? | |---|---|---|---| | 1 | local | `.dvc/config.local` | no, Git-ignored | | 2 | project (default) | `.dvc/config` | yes, meant to be committed | | 3 | global | per-user file, e.g. `~/.config/dvc/config` on Linux | no, outside the repo | | 4 | system | machine-wide file, e.g. `/etc/xdg/dvc/config` | no, outside the repo | `dvc remote add`, `dvc remote modify`, `dvc remote default` and `dvc config` all take `--local`, `--project`, `--global` or `--system`; without one they write the **project** file. DVC adds `.dvc/config.local` to Git's ignore list, and its documentation warns that `.dvc/config` should not hold passwords, keys or other secrets. ## Why the key landed in Git A data scientist ran `dvc remote modify ci-s3 secret_access_key ...` with no level flag. The value went into `.dvc/config` under `['remote "ci-s3"']`, the file showed up as modified, and it was committed with the next change. The protection is choosing the right file before the value is written. ## Where credentials belong The usual split: the location is shared, the secret is private. - **Shared in `.dvc/config`:** `url`, `region`, `endpointurl` for an S3-compatible server, timeouts, `jobs`, and a `profile` name, which identifies a profile without containing a secret. - **Private in `.dvc/config.local` (`--local`):** `access_key_id`, `secret_access_key`, `session_token`, `credentialpath`, an Azure `connection_string` or `sas_token`, an SSH `password`, `passphrase` or `keyfile` path, a WebDAV `token`. - **Outside DVC config entirely:** for S3, DVC uses the AWS CLI's own configuration and the boto3 environment variables (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`) when no credential parameters are set; Google Cloud Storage reads `GOOGLE_APPLICATION_CREDENTIALS`; Azure reads variables such as `AZURE_STORAGE_CONNECTION_STRING` or its default credential; SSH takes users and keys from the SSH configuration when the URL and DVC config do not set them. Because the local file only overrides the keys it sets, a remote can be split across both files: `url` in `.dvc/config`, `credentialpath` in `.dvc/config.local`. The cost is that every clone must set its own local values, and CI gets them from its secret store, usually as environment variables. ## Credential sources by storage type | Remote type | Secrets to set with `--local` | Picked up without DVC config | |---|---|---| | S3 and S3-compatible | `access_key_id`, `secret_access_key`, `session_token`, `credentialpath` | the AWS CLI's config and credentials files, `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` | | Google Cloud Storage | `credentialpath` to a service-account JSON file | `GOOGLE_APPLICATION_CREDENTIALS` | | Azure Blob | `connection_string`, `account_key`, `sas_token` | Azure's default credential, `AZURE_STORAGE_CONNECTION_STRING` and related variables | | SSH | `password`, `passphrase`, `keyfile` | user, port and keys from the SSH configuration, plus the SSH agent (`allow_agent` is `true` by default) | Two details are easy to miss. A `profile` name is not a secret, so DVC's own docs set it in the shared file while `credentialpath` goes to the local one. And SSH and WebDAV remotes can ask instead of store: `ask_password` set to `true` (and, for SSH keys, `ask_passphrase`) prompts when the secret is needed, so it sits in no file at all. WebDAV's `token`, `user` and `password` otherwise belong in the local file like any other secret. ## Cleaning up after a leak 1. **Revoke and rotate the key** with the cloud provider first. Once the commit is pushed, the key is in every clone and fork made since; a later commit that deletes it does not take it back. 2. **Remove it from the tracked file:** `dvc remote modify -u ci-s3 secret_access_key` (and `access_key_id`), then commit. 3. **Re-supply the new credential privately:** `dvc remote modify --local ...`, a `credentialpath` or `profile`, or environment variables. 4. **Check where values come from:** `dvc config --list --show-origin` prints each value with the file that holds it. 5. Rewriting Git history can stop further spread, but it never replaces rotation. ## Commands that write secrets elsewhere The same discipline applies beyond `dvc remote modify`. `dvc import --remote-config key=value` records those values inside the import `.dvc` file, which Git tracks, so it suits a profile name and not a key. Any credential written to a tracked file is a leak waiting for its next commit.

  • How does CI authenticate to the S3 remote with no credentials in any DVC config file?
    With no credential parameters set, DVC uses the AWS CLI's configuration and the boto3 environment variables, so CI exports `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` from its secret store. Google Cloud Storage reads `GOOGLE_APPLICATION_CREDENTIALS` and Azure reads variables such as `AZURE_STORAGE_CONNECTION_STRING`.
  • If `.dvc/config` sets a remote's `url` and `.dvc/config.local` sets its `credentialpath`, what does DVC use?
    Both. The files are merged, the local file has the highest priority, and it overrides only the keys it sets, so the remote gets the shared `url` and the private `credentialpath`. Each clone has to configure its own local file.
  • Does deleting the key from `.dvc/config` in a new commit fix the leak?
    No. The old commit still holds it, and any clone or fork made since already has it. Revoke and rotate the key with the provider, then remove it with `dvc remote modify -u` and supply the new one through `--local`, a credentials file or environment variables.

  • dvc remote modify writes to a private, untracked file by default
  • Deleting the key in a follow-up commit makes the leak harmless
  • config.local replaces .dvc/config, so the remote url must be repeated there
  • Remote credentials can only be supplied through dvc remote modify
  • The --global flag writes to .dvc/config.local inside the project