Skip to content

feat: allow specifying access_token as an env var #179

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

Merged
merged 4 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Build package
run: poetry build

- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
path: |
./dist/*.tar.gz
Expand Down
14 changes: 13 additions & 1 deletion ctfcli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
from ctfcli.cli.pages import PagesCommand
from ctfcli.cli.plugins import PluginsCommand
from ctfcli.cli.templates import TemplatesCommand
from ctfcli.core.exceptions import ProjectNotInitialized
from ctfcli.core.exceptions import (
MissingAPIKey,
MissingInstanceURL,
ProjectNotInitialized,
)
from ctfcli.core.plugins import load_plugins
from ctfcli.utils.git import check_if_dir_is_inside_git_repo

Expand Down Expand Up @@ -148,6 +152,14 @@ def main():
if isinstance(ret, int):
sys.exit(ret)

except MissingInstanceURL as e:
click.secho(e, fg="red")
sys.exit(1)

except MissingAPIKey as e:
click.secho(e, fg="red")
sys.exit(1)

except ProjectNotInitialized:
if click.confirm(
"Outside of a ctfcli project, would you like to start a new project in this directory?",
Expand Down
12 changes: 10 additions & 2 deletions ctfcli/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
from requests import Session

from ctfcli.core.config import Config
from ctfcli.core.exceptions import MissingAPIKey, MissingInstanceURL


class API(Session):
def __init__(self):
config = Config()

# Load required configuration values
self.url = config["config"]["url"]
self.access_token = config["config"]["access_token"]
try:
self.url = config["config"]["url"]
except KeyError:
raise MissingInstanceURL()

try:
self.access_token = config["config"]["access_token"]
except KeyError:
raise MissingAPIKey()

# Handle SSL verification disabling
try:
Expand Down
23 changes: 23 additions & 0 deletions ctfcli/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@


class Config:
_env_vars = {
"CTFCLI_ACCESS_TOKEN": "access_token",
"CTFCLI_URL": "url",
}

def __init__(self):
self.base_path = self.get_base_path()
self.project_path = self.get_project_path()
Expand All @@ -26,6 +31,24 @@ def __init__(self):
self.config = parser
self.challenges = dict(self.config["challenges"])

# Load environment variables
self._env_overrides()

def _env_overrides(self):
"""
For each environment variable specified in _env_vars, check if it exists
and if so, add it to the config under the "config" section.
"""
for env_var, config_key in self._env_vars.items():
env_value = os.getenv(env_var)
if not env_value:
continue

if not self.config.has_section("config"):
self.config.add_section("config")

self.config["config"][config_key] = env_value

def __getitem__(self, key):
return self.config[key]

Expand Down
16 changes: 16 additions & 0 deletions ctfcli/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
import click


class MissingAPIKey(Exception):
def __str__(self):
return (
"Missing API key. "
"Please set the API key in your configuration file or set the CTFCLI_ACCESS_TOKEN environment variable."
)


class MissingInstanceURL(Exception):
def __str__(self):
return (
"Missing CTFd instance URL. "
"Please set the instance URL in your configuration file or set the CTFCLI_URL environment variable."
)


class ProjectNotInitialized(Exception):
pass

Expand Down