Skip to content

[BUG] CLI fails in system context because of missing local environment variables, despite passing explicit configuration #492

@alexgmathews

Description

@alexgmathews

Bug Description
Vultr-CLI fails when executed by systemd, which runs by default at a system context. The system context does not define $XDG_CONFIG_HOME or $HOME. Vultr-CLI checks for these environment variables and fails if they are undefined before checking --config and $VULTR_API_KEY. Vultr-CLI's order of configuration evaluation causes Vultr-CLI to fail even when a configuration file is passed as an argument (test 1) and when the API key is exported to the executing environment (test 2). The issue does not occur when instructing systemd to execute at a user context (see the addition of User=root in Test 3's systemd service file).

Test Environment

  1. Operating System: Debian 12 x86_64

  2. Vultr-CLI binary vultr-cli_v3.4.0_linux_amd64 installed to /usr/local/sbin/vultr-cli with owner/group root:root and permissions 700

  3. Vultr-CLI configuration file placed at /etc/vultr-cli.yaml with contents api-key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX with owner/group root:root and permissions 600 (for test 1)

  4. systemd service file /etc/systemd/system/vultr-cli-test-1.service:

[Unit]
Description=Vultr CLI Test 1

[Service]
Type=simple
Restart=no
ExecStart=bash -c "vultr-cli account --config /etc/vultr-cli.yaml"
SyslogIdentifier=vultr-cli-test-1

[Install]
WantedBy=multi-user.target
  1. systemd service file /etc/systemd/system/vultr-cli-test-2.service:
[Unit]
Description=Vultr CLI Test 2

[Service]
Type=simple
Restart=no
ExecStart=bash -c "export VULTR_API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; vultr-cli account"
SyslogIdentifier=vultr-cli-test-2

[Install]
WantedBy=multi-user.target
  1. systemd service file /etc/systemd/system/vultr-cli-test-3.service:
[Unit]
Description=Vultr CLI Test 3

[Service]
Type=simple
Restart=no
User=root
ExecStart=bash -c "export VULTR_API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; vultr-cli account"
SyslogIdentifier=vultr-cli-test-3

[Install]
WantedBy=multi-user.target

Test 1 Execution
$ systemctl start vultr-cli-test-1

Test 1 Results

$ systemctl status vultr-cli-test-1
× vultr-cli-test-1.service - Vultr CLI Test 1
     Loaded: loaded (/etc/systemd/system/vultr-cli-test-1.service; disabled; preset: enabled)
     Active: failed (Result: exit-code) since Mon 2024-11-18 18:55:14 UTC; 11s ago
   Duration: 17ms
    Process: 2632 ExecStart=bash -c vultr-cli account --config /etc/vultr-cli.yaml (code=exited, status=1/FAILURE)
   Main PID: 2632 (code=exited, status=1/FAILURE)
        CPU: 9ms

Nov 18 18:55:14 vultr systemd[1]: Started vultr-cli-test-1.service - Vultr CLI Test 1.
Nov 18 18:55:14 vultr vultr-cli-test-2[2632]: Unable to determine default user config directory : neither $XDG_CONFIG_HOME nor $HOME are defined
Nov 18 18:55:14 vultr systemd[1]: vultr-cli-test-1.service: Main process exited, code=exited, status=1/FAILURE
Nov 18 18:55:14 vultr systemd[1]: vultr-cli-test-1.service: Failed with result 'exit-code'.

Test 2 Execution
$ systemctl start vultr-cli-test-2

Test 2 Results

$ systemctl status vultr-cli-test-2
× vultr-cli-test-2.service - Vultr CLI Test 2
     Loaded: loaded (/etc/systemd/system/vultr-cli-test-2.service; disabled; preset: enabled)
     Active: failed (Result: exit-code) since Mon 2024-11-18 18:52:52 UTC; 44s ago
   Duration: 8ms
    Process: 2620 ExecStart=bash -c export VULTR_API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; vultr-cli account (code=exited, status=1/FAILURE)
   Main PID: 2620 (code=exited, status=1/FAILURE)
        CPU: 6ms

Nov 18 18:52:52 vultr systemd[1]: Started vultr-cli-test-2.service - Vultr CLI Test 2.
Nov 18 18:52:52 vultr vultr-cli-test-1[2620]: Unable to determine default user config directory : neither $XDG_CONFIG_HOME nor $HOME are defined
Nov 18 18:52:52 vultr systemd[1]: vultr-cli-test-2.service: Main process exited, code=exited, status=1/FAILURE
Nov 18 18:52:52 vultr systemd[1]: vultr-cli-test-2.service: Failed with result 'exit-code'.

Test 3 Execution
$ systemctl start vultr-cli-test-3

Test 3 Results

$ systemctl status vultr-cli-test-3
○ vultr-cli-test-3.service - Vultr CLI Test 3
     Loaded: loaded (/etc/systemd/system/vultr-cli-test-3.service; disabled; preset: enabled)
     Active: inactive (dead)

Nov 18 19:21:52 vultr systemd[1]: Started vultr-cli-test-3.service - Vultr CLI Test 3.
Nov 18 19:21:53 vultr vultr-cli-test-3[3020]: [REDACTED]
Nov 18 19:21:53 vultr vultr-cli-test-3[3020]: [REDACTED]
Nov 18 19:21:53 vultr systemd[1]: vultr-cli-test-3.service: Deactivated successfully.

Suggested Revision
Changing Vultr-CLI's order of configuration evaluation to something like the following would resolve the issue:

  1. If the --config flag is set, use the defined path to set the API key.
  2. If $VULTR_API_KEY is defined in the environment, use its value to set the API key.
  3. Test if $XDG_CONFIG_HOME is defined in the environment. If it is, test if the file path per [Feature] - Use XDG_CONFIG_HOME to store config file #478 exists. If it does, use its value to set the API key.
  4. Test if $HOME is defined in the environment. If it is, test if the file $HOME/.vultr-cli.yaml exists. If it does, use its value to set the API key.
  5. Else, raise an error.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions