Skip to content

Commit

Permalink
blueprints: add Env tag
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Langhammer <[email protected]>
  • Loading branch information
BeryJu committed Dec 24, 2022
1 parent fe1e2aa commit 94b9ebb
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
"!Find sequence",
"!KeyOf scalar",
"!Context scalar",
"!Context sequence",
"!Format sequence",
"!Condition sequence"
"!Condition sequence",
"!Env sequence",
"!Env scalar"
],
"typescript.preferences.importModuleSpecifier": "non-relative",
"typescript.preferences.importModuleSpecifierEnding": "index",
Expand Down
10 changes: 10 additions & 0 deletions authentik/blueprints/tests/fixtures/tags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ context:
policy_property: name
policy_property_value: foo-bar-baz-qux
entries:
- model: authentik_sources_oauth.oauthsource
identifiers:
slug: test
attrs:
name: test
provider_type: github
consumer_key: !Env foo
consumer_secret: !Env [bar, baz]
authentication_flow: !Find [authentik_flows.Flow, [slug, default-source-authentication]]
enrollment_flow: !Find [authentik_flows.Flow, [slug, default-source-enrollment]]
- attrs:
expression: return True
identifiers:
Expand Down
10 changes: 10 additions & 0 deletions authentik/blueprints/tests/test_v1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test blueprints v1"""
from os import environ

from django.test import TransactionTestCase

from authentik.blueprints.tests import load_yaml_fixture
Expand All @@ -9,6 +11,7 @@
from authentik.lib.generators import generate_id
from authentik.policies.expression.models import ExpressionPolicy
from authentik.policies.models import PolicyBinding
from authentik.sources.oauth.models import OAuthSource
from authentik.stages.prompt.models import FieldTypes, Prompt, PromptStage
from authentik.stages.user_login.models import UserLoginStage

Expand Down Expand Up @@ -132,6 +135,7 @@ def test_import_yaml_tags(self):
"""Test some yaml tags"""
ExpressionPolicy.objects.filter(name="foo-bar-baz-qux").delete()
Group.objects.filter(name="test").delete()
environ["foo"] = generate_id()
importer = Importer(load_yaml_fixture("fixtures/tags.yaml"), {"bar": "baz"})
self.assertTrue(importer.validate()[0])
self.assertTrue(importer.apply())
Expand All @@ -152,6 +156,12 @@ def test_import_yaml_tags(self):
}
)
)
self.assertTrue(
OAuthSource.objects.filter(
slug="test",
consumer_key=environ["foo"],
)
)

def test_export_validate_import_policies(self):
"""Test export and validate it"""
Expand Down
22 changes: 22 additions & 0 deletions authentik/blueprints/v1/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from enum import Enum
from functools import reduce
from operator import ixor
from os import getenv
from typing import Any, Literal, Optional
from uuid import UUID

Expand Down Expand Up @@ -160,6 +161,26 @@ def resolve(self, entry: BlueprintEntry, blueprint: Blueprint) -> Any:
)


class Env(YAMLTag):
"""Lookup environment variable with optional default"""

key: str
default: Optional[Any]

# pylint: disable=unused-argument
def __init__(self, loader: "BlueprintLoader", node: ScalarNode | SequenceNode) -> None:
super().__init__()
self.default = None
if isinstance(node, ScalarNode):
self.key = node.value
if isinstance(node, SequenceNode):
self.key = node.value[0].value
self.default = node.value[1].value

def resolve(self, entry: BlueprintEntry, blueprint: Blueprint) -> Any:
return getenv(self.key, self.default)


class Context(YAMLTag):
"""Lookup key from instance context"""

Expand Down Expand Up @@ -332,6 +353,7 @@ def __init__(self, *args, **kwargs):
self.add_constructor("!Context", Context)
self.add_constructor("!Format", Format)
self.add_constructor("!Condition", Condition)
self.add_constructor("!Env", Env)


class EntryInvalidError(SentryIgnoredException):
Expand Down
6 changes: 6 additions & 0 deletions website/developer-docs/blueprints/v1/tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ Resolves to the primary key of the model instance defined by id _my-policy-id_.

If no matching entry can be found, an error is raised and the blueprint is invalid.

#### `!Env`

Example: `password: !Env my_env_var`

Returns the value of the given environment variable. Can be used as a scalar with `!Env my_env_var, default` to return a default value.

#### `!Find`

Examples:
Expand Down

0 comments on commit 94b9ebb

Please sign in to comment.