Skip to content

Commit 5df70f8

Browse files
Merge pull request #596 from danielballan/add-api-key-option
Add `--api-key` CLI option
2 parents ebfa643 + fbf8292 commit 5df70f8

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

docs/source/explanations/security.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ client, a cookie will be set in your client and you won’t need to use the toke
3535
again. It is valid indefinitely.
3636

3737
For horizontally-scaled deployments where you need multiple instances of the
38-
server to share the same secret, you can set it via an environment variable like
39-
so.
38+
server to share the same secret, you can set it with a CLI option
39+
40+
```
41+
tiled serve ... --api-key=YOUR_SECRET
42+
```
43+
44+
or via an environment variable
4045

4146
```
4247
TILED_SINGLE_USER_API_KEY=YOUR_SECRET tiled serve ...

tiled/commandline/_serve.py

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ def serve_directory(
3434
"this option selected."
3535
),
3636
),
37+
api_key: str = typer.Option(
38+
None,
39+
"--api-key",
40+
help=(
41+
"Set the single-user API key. "
42+
"By default, a random key is generated at startup and printed."
43+
),
44+
),
3745
keep_ext: bool = typer.Option(
3846
False,
3947
"--keep-ext",
@@ -182,7 +190,10 @@ def serve_directory(
182190
register_logger.setLevel("INFO")
183191
web_app = build_app(
184192
catalog_adapter,
185-
{"allow_anonymous_access": public},
193+
{
194+
"allow_anonymous_access": public,
195+
"single_user_api_key": api_key,
196+
},
186197
server_settings,
187198
)
188199
if watch:
@@ -270,6 +281,14 @@ def serve_catalog(
270281
"this option selected."
271282
),
272283
),
284+
api_key: str = typer.Option(
285+
None,
286+
"--api-key",
287+
help=(
288+
"Set the single-user API key. "
289+
"By default, a random key is generated at startup and printed."
290+
),
291+
),
273292
host: str = typer.Option(
274293
"127.0.0.1",
275294
help=(
@@ -388,7 +407,13 @@ def serve_catalog(
388407
init_if_not_exists=init,
389408
)
390409
web_app = build_app(
391-
tree, {"allow_anonymous_access": public}, server_settings, scalable=scalable
410+
tree,
411+
{
412+
"allow_anonymous_access": public,
413+
"single_user_api_key": api_key,
414+
},
415+
server_settings,
416+
scalable=scalable,
392417
)
393418
print_admin_api_key_if_generated(web_app, host=host, port=port)
394419

@@ -414,6 +439,14 @@ def serve_pyobject(
414439
"option selected."
415440
),
416441
),
442+
api_key: str = typer.Option(
443+
None,
444+
"--api-key",
445+
help=(
446+
"Set the single-user API key. "
447+
"By default, a random key is generated at startup and printed."
448+
),
449+
),
417450
host: str = typer.Option(
418451
"127.0.0.1",
419452
help=(
@@ -453,7 +486,13 @@ def serve_pyobject(
453486
"available_bytes"
454487
] = object_cache_available_bytes
455488
web_app = build_app(
456-
tree, {"allow_anonymous_access": public}, server_settings, scalable=scalable
489+
tree,
490+
{
491+
"allow_anonymous_access": public,
492+
"single_user_api_key": api_key,
493+
},
494+
server_settings,
495+
scalable=scalable,
457496
)
458497
print_admin_api_key_if_generated(web_app, host=host, port=port)
459498

@@ -507,6 +546,14 @@ def serve_config(
507546
"option selected."
508547
),
509548
),
549+
api_key: str = typer.Option(
550+
None,
551+
"--api-key",
552+
help=(
553+
"Set the single-user API key. "
554+
"By default, a random key is generated at startup and printed."
555+
),
556+
),
510557
host: str = typer.Option(
511558
None,
512559
help=(
@@ -543,6 +590,11 @@ def serve_config(
543590
if "authentication" not in parsed_config:
544591
parsed_config["authentication"] = {}
545592
parsed_config["authentication"]["allow_anonymous_access"] = True
593+
# Let --api-key flag override config.
594+
if api_key:
595+
if "authentication" not in parsed_config:
596+
parsed_config["authentication"] = {}
597+
parsed_config["authentication"]["single_user_api_key"] = api_key
546598

547599
# Delay this import so that we can fail faster if config-parsing fails above.
548600

tiled/server/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ def override_get_settings():
409409
]:
410410
if authentication.get(item) is not None:
411411
setattr(settings, item, authentication[item])
412+
if authentication.get("single_user_api_key") is not None:
413+
settings.single_user_api_key_generated = False
412414
for item in [
413415
"allow_origins",
414416
"response_bytesize_limit",

0 commit comments

Comments
 (0)