goharbor / goharbor/harbor-cli

[bug]: Config directory path via --config or HARBOR_CLI_CONFIG corrupts data.yaml and bricks CLI

Open
#993 0 comments 0 reactions 0 assignees View on GitHub
bug status/in-progress status/needs-feedback
Dominant language
Go
Stars
163
Forks
211
Avg merge
1m
Merged PRs (30d)
1

Description

## Description
Passing a **directory** path via `--config` or `HARBOR_CLI_CONFIG` causes Harbor CLI to persist that directory into `data.yaml` before validating it is a file. Every subsequent invocation then fails with a fatal error until `data.yaml` is manually repaired.

This is especially problematic because the [README setup instructions](https://github.com/goharbor/harbor-cli/blob/main/README.md) tell users to export `HARBOR_CLI_CONFIG=$HOME/.config/harbor-cli` (a directory), which breaks the native binary when users follow the documented container-oriented setup.

## Steps to Reproduce

### Case 1: `--config` flag with a directory
1. Run command: `harbor -c ~/.config/harbor-cli version`
2. With arguments/config: pass the config **directory** instead of `config.yaml`
3. Observe error:
```
Data file at ~/.local/share/harbor-cli/data.yaml updated with new configPath: /home//.config/harbor-cli
level=fatal msg="expected a file but found a directory at path: /home//.config/harbor-cli"
```
4. Run `harbor version` again — it still fails until `~/.local/share/harbor-cli/data.yaml` is manually edited to point to a valid file path.

### Case 2: `HARBOR_CLI_CONFIG` env var (documented setup)
1. Run: `export HARBOR_CLI_CONFIG=$HOME/.config/harbor-cli`
2. Run: `harbor version`
3. Observe the same fatal error as above.

## Expected Behavior
- If `HARBOR_CLI_CONFIG` or `--config` points to a directory, the CLI should resolve it to `/config.yaml` (matching the default XDG path behavior), **or** reject the path with a clear error **before** persisting it to `data.yaml`.
- A single bad invocation should not permanently corrupt CLI state.

## Actual Behavior
- `DetermineConfigPath` in `pkg/utils/config.go` returns directory paths as-is without validation.
- `ApplyDataFile` persists the invalid directory path to `data.yaml` **before** `EnsureConfigFileExists` checks whether the path is a file.
- The CLI fatals on every subsequent run until the user manually fixes `data.yaml`.

## Environment
- OS: Linux (also affects macOS/Windows with same config logic)
- Tool version: `main` branch (tested on commit fbbc7fc)
- Other relevant details:
- Affected code: `pkg/utils/config.go` — `DetermineConfigPath` (lines ~130–165), `InitConfig` (lines ~67–92), `EnsureConfigFileExists` (lines ~169–191)
- README documents `export HARBOR_CLI_CONFIG=$HOME/.config/harbor-cli` which triggers this bug for native binary users

## Additional Context

**Suggested fix (for contributors):**
1. In `DetermineConfigPath`, if the resolved path is a directory, append `config.yaml`.
2. Validate the config path is a file (not a directory) **before** calling `ApplyDataFile`.
3. Add a unit test covering directory paths for both `--config` and `HARBOR_CLI_CONFIG`.

**Workaround:** Manually edit `~/.local/share/harbor-cli/data.yaml` and set `configpath` to the full file path (e.g. `~/.config/harbor-cli/config.yaml`), or unset `HARBOR_CLI_CONFIG`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.