|
| 1 | +--- |
| 2 | +title: Harbor CLI Config Management |
| 3 | +weight: 25 |
| 4 | +--- |
| 5 | + |
| 6 | +# Harbor CLI Configuration Management |
| 7 | + |
| 8 | +> **Note** |
| 9 | +> The Harbor CLI follows the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) for configuration and data storage by default. |
| 10 | +
|
| 11 | +## Introduction |
| 12 | +Harbor CLI is a flexible command-line tool that lets you manage various Harbor environments with different credentials. Whether you need a production-ready setup or quick testing configurations, the CLI's hierarchical structure and XDG support help keep things organized. |
| 13 | + |
| 14 | + |
| 15 | +## Understanding the Configuration Structure |
| 16 | +The Harbor CLI can manage multiple credentials and keep track of which credential is currently active. This setup allows you to maintain separate contexts for different Harbor instances or user accounts without having to rewrite configuration files manually. While the Harbor CLI configuration file manages your credentials, passwords themselves are never stored in plain text. Instead, they are secured using the AES-GCM encryption described in the [Harbor CLI Encryption documentation](../cli-config). |
| 17 | + |
| 18 | +### Example Configuration File |
| 19 | +Below is a simplified example of a typical Harbor CLI configuration file: |
| 20 | +```yaml |
| 21 | +current-credential-name: example@demo-harbor |
| 22 | +credentials: |
| 23 | + - name: example@demo-harbor |
| 24 | + username: example-user |
| 25 | + password: example-password |
| 26 | + serveraddress: https://demo.goharbor.io |
| 27 | +``` |
| 28 | +
|
| 29 | +In this configuration: |
| 30 | +- **current-credential-name** references the active credential by name. |
| 31 | +- **credentials** holds one or more sets of user credentials, each following the same structure. |
| 32 | +
|
| 33 | +## Managing Multiple Credentials |
| 34 | +If you need to work with multiple sets of credentials—such as development, staging, or production — Harbor CLI makes it easy to create and switch between them. |
| 35 | +
|
| 36 | +> **Note**: For more login command details please refer to the [login command reference](../cli-docs/harbor-login.md). |
| 37 | +### Creating a New Credentials Entry |
| 38 | +Use the `harbor login` command with the required arguments to store new credentials: |
| 39 | +```bash |
| 40 | +harbor login --name my-new-credential \ |
| 41 | + --username myuser \ |
| 42 | + --password mypass \ |
| 43 | + https://my-harbor-instance.com |
| 44 | +``` |
| 45 | +This adds a new entry to your credentials list, allowing you to manage different Harbor accounts from the same CLI. |
| 46 | + |
| 47 | +### Switching Between Credentials |
| 48 | +To switch to another credential set, run: |
| 49 | +```bash |
| 50 | +harbor login --name <name-of-credential> |
| 51 | +``` |
| 52 | +The CLI will then set the specified credential as the active one, eliminating the need to manually edit your configuration files. This will overwrite the `current-credential-name`. |
| 53 | + |
| 54 | + |
| 55 | +## Configuration Hierarchy (Highest to Lowest Priority) |
| 56 | + |
| 57 | +1. **Explicit Config Flag** |
| 58 | + Provide a custom config file at runtime using `--config`: |
| 59 | + ```bash |
| 60 | + harbor --config /path/to/custom/config.yaml artifact list |
| 61 | + ``` |
| 62 | + |
| 63 | +2. **Environment Variable** |
| 64 | + Set a persistent configuration through the `HARBOR_CLI_CONFIG` environment variable: |
| 65 | + ```bash |
| 66 | + export HARBOR_CLI_CONFIG="$HOME/.custom/harbor-config.yaml" |
| 67 | + harbor artifact list # Uses the environment-specified config |
| 68 | + ``` |
| 69 | + |
| 70 | +3. **XDG Default Paths** |
| 71 | + Automatically discover configuration in the following order: |
| 72 | + ```bash |
| 73 | + ${XDG_CONFIG_HOME}/harbor-cli/config.yaml # If XDG_CONFIG_HOME is set |
| 74 | + ~/.config/harbor-cli/config.yaml # Fallback default |
| 75 | + ``` |
| 76 | + |
| 77 | +## Data Storage Management |
| 78 | +### Data File Location |
| 79 | + |
| 80 | +- **Primary Path**: `$XDG_DATA_HOME/harbor-cli/data.yaml` |
| 81 | +- **Fallback Path**: `$HOME/.local/share/harbor-cli/data.yaml` |
| 82 | + |
| 83 | +> **Important** |
| 84 | +> The data file automatically tracks the last-used configuration file path |
| 85 | + |
| 86 | +## Configuration Precedence Summary |
| 87 | +| Priority | Method | Example | |
| 88 | +|----------|----------------------------|---------------------------------------| |
| 89 | +| 1 | --config flag | harbor --config ./test.yaml ... | |
| 90 | +| 2 | HARBOR_CLI_CONFIG env var | export HARBOR_CLI_CONFIG=... | |
| 91 | +| 3 | XDG Default Locations | ~/.config/harbor-cli/config.yaml | |
| 92 | + |
| 93 | +## Practical Usage Examples |
| 94 | +### Scenario 1: Temporary Config Override |
| 95 | +```bash |
| 96 | +harbor --config ./experimental.yaml project create "new-project" |
| 97 | +``` |
| 98 | + |
| 99 | +### Scenario 2: Persistent Environment-based Config |
| 100 | +```bash |
| 101 | +echo 'export HARBOR_CLI_CONFIG="$HOME/work/configs/prod-harbor.yaml"' >> ~/.zshrc |
| 102 | +source ~/.zshrc |
| 103 | +harbor config list # Uses production config |
| 104 | +``` |
| 105 | + |
| 106 | +### Scenario 3: Reset to Default Configuration |
| 107 | +```bash |
| 108 | +unset HARBOR_CLI_CONFIG |
| 109 | +harbor config delete --current # Deletes current context |
| 110 | +``` |
0 commit comments