File tree Expand file tree Collapse file tree 5 files changed +63
-4
lines changed Expand file tree Collapse file tree 5 files changed +63
-4
lines changed Original file line number Diff line number Diff line change 3333 - name : Build package
3434 run : poetry build
3535
36- - uses : actions/upload-artifact@v3
36+ - uses : actions/upload-artifact@v4
3737 with :
3838 path : |
3939 ./dist/*.tar.gz
Original file line number Diff line number Diff line change 1616from ctfcli .cli .pages import PagesCommand
1717from ctfcli .cli .plugins import PluginsCommand
1818from ctfcli .cli .templates import TemplatesCommand
19- from ctfcli .core .exceptions import ProjectNotInitialized
19+ from ctfcli .core .exceptions import (
20+ MissingAPIKey ,
21+ MissingInstanceURL ,
22+ ProjectNotInitialized ,
23+ )
2024from ctfcli .core .plugins import load_plugins
2125from ctfcli .utils .git import check_if_dir_is_inside_git_repo
2226
@@ -148,6 +152,14 @@ def main():
148152 if isinstance (ret , int ):
149153 sys .exit (ret )
150154
155+ except MissingInstanceURL as e :
156+ click .secho (e , fg = "red" )
157+ sys .exit (1 )
158+
159+ except MissingAPIKey as e :
160+ click .secho (e , fg = "red" )
161+ sys .exit (1 )
162+
151163 except ProjectNotInitialized :
152164 if click .confirm (
153165 "Outside of a ctfcli project, would you like to start a new project in this directory?" ,
Original file line number Diff line number Diff line change 33from requests import Session
44
55from ctfcli .core .config import Config
6+ from ctfcli .core .exceptions import MissingAPIKey , MissingInstanceURL
67
78
89class API (Session ):
910 def __init__ (self ):
1011 config = Config ()
1112
1213 # Load required configuration values
13- self .url = config ["config" ]["url" ]
14- self .access_token = config ["config" ]["access_token" ]
14+ try :
15+ self .url = config ["config" ]["url" ]
16+ except KeyError :
17+ raise MissingInstanceURL ()
18+
19+ try :
20+ self .access_token = config ["config" ]["access_token" ]
21+ except KeyError :
22+ raise MissingAPIKey ()
1523
1624 # Handle SSL verification disabling
1725 try :
Original file line number Diff line number Diff line change 1010
1111
1212class Config :
13+ _env_vars = {
14+ "CTFCLI_ACCESS_TOKEN" : "access_token" ,
15+ "CTFCLI_URL" : "url" ,
16+ }
17+
1318 def __init__ (self ):
1419 self .base_path = self .get_base_path ()
1520 self .project_path = self .get_project_path ()
@@ -26,6 +31,24 @@ def __init__(self):
2631 self .config = parser
2732 self .challenges = dict (self .config ["challenges" ])
2833
34+ # Load environment variables
35+ self ._env_overrides ()
36+
37+ def _env_overrides (self ):
38+ """
39+ For each environment variable specified in _env_vars, check if it exists
40+ and if so, add it to the config under the "config" section.
41+ """
42+ for env_var , config_key in self ._env_vars .items ():
43+ env_value = os .getenv (env_var )
44+ if not env_value :
45+ continue
46+
47+ if not self .config .has_section ("config" ):
48+ self .config .add_section ("config" )
49+
50+ self .config ["config" ][config_key ] = env_value
51+
2952 def __getitem__ (self , key ):
3053 return self .config [key ]
3154
Original file line number Diff line number Diff line change 33import click
44
55
6+ class MissingAPIKey (Exception ):
7+ def __str__ (self ):
8+ return (
9+ "Missing API key. "
10+ "Please set the API key in your configuration file or set the CTFCLI_ACCESS_TOKEN environment variable."
11+ )
12+
13+
14+ class MissingInstanceURL (Exception ):
15+ def __str__ (self ):
16+ return (
17+ "Missing CTFd instance URL. "
18+ "Please set the instance URL in your configuration file or set the CTFCLI_URL environment variable."
19+ )
20+
21+
622class ProjectNotInitialized (Exception ):
723 pass
824
You can’t perform that action at this time.
0 commit comments