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

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

Open
alexgmathews opened this issue Nov 18, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@alexgmathews
Copy link

alexgmathews commented Nov 18, 2024

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.
@alexgmathews alexgmathews added the bug Something isn't working label Nov 18, 2024
@optik-aper optik-aper self-assigned this Nov 18, 2024
@optik-aper
Copy link
Member

Possibly a restatement of #277

@alexgmathews alexgmathews changed the title [BUG] Incorrect configuration order evaluation causes CLI to fail when executed by systemd in system context [BUG] CLI fails in system context because of missing local environment variables, despite passing an explicit configuration path Nov 24, 2024
@alexgmathews alexgmathews changed the title [BUG] CLI fails in system context because of missing local environment variables, despite passing an explicit configuration path [BUG] CLI fails in system context because of missing local environment variables, despite passing explicit configuration Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants