Skip to content

Allow setting of token in aikido_zen.protect() #401

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 2 commits into from
Jun 16, 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
7 changes: 6 additions & 1 deletion aikido_zen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Aggregates from the different modules
"""

import os

# Re-export functions :
from aikido_zen.context.users import set_user
from aikido_zen.middleware import should_block_request
Expand All @@ -17,7 +19,7 @@
from aikido_zen.helpers.aikido_disabled_flag_active import aikido_disabled_flag_active


def protect(mode="daemon"):
def protect(mode="daemon", token=""):
"""
Mode can be set to :
- daemon : Default, imports sinks/sources and starts background_process
Expand All @@ -28,6 +30,9 @@ def protect(mode="daemon"):
if aikido_disabled_flag_active():
# Do not run any aikido code when the disabled flag is on
return
if token:
os.environ["AIKIDO_TOKEN"] = token

if mode in ("daemon", "daemon_only"):
start_background_process()
if mode == "daemon_only":
Expand Down
8 changes: 8 additions & 0 deletions aikido_zen/init_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest

import aikido_zen
from aikido_zen import protect
from aikido_zen.background_process import get_comms, reset_comms
from aikido_zen.helpers.token import get_token_from_env


def test_protect_with_django(monkeypatch, caplog):
Expand All @@ -9,3 +12,8 @@ def test_protect_with_django(monkeypatch, caplog):
assert "starting" in caplog.text
reset_comms()
assert get_comms() == None


def test_protect_sets_token():
aikido_zen.protect(token="MY_TOKEN_1")
assert get_token_from_env().token == "MY_TOKEN_1"