Compare commits
434 Commits
version-20
...
version/20
Author | SHA1 | Date | |
---|---|---|---|
15c34c6f1f | |||
0ab8f4eed7 | |||
070714abe4 | |||
810c04bacf | |||
b624bf1cb7 | |||
f56a4b00ce | |||
6ec0411930 | |||
fb59969bce | |||
eec9c46533 | |||
a3afd47850 | |||
8ffae4505f | |||
0cc83c23c4 | |||
514c48a986 | |||
fdb8fb4b4c | |||
d8a68407f9 | |||
417156d659 | |||
9d58407e25 | |||
f4441c9fcf | |||
0e9762072a | |||
0cfffa28ad | |||
1ad4c8fc29 | |||
dd2facdc57 | |||
549dfa4c3a | |||
7f8ae24e8d | |||
d05aeb91f2 | |||
ab25216c1f | |||
fb5eb7b868 | |||
bda218f7fc | |||
198c940a80 | |||
c900411d5a | |||
1adc6948b4 | |||
e87236b285 | |||
846b63a17b | |||
48fb3c98ff | |||
b488a6fec9 | |||
f014bd5f30 | |||
03dd079e8c | |||
1281e842d1 | |||
f7601d9571 | |||
4d9c9160e7 | |||
ad1f913e54 | |||
3da0233c40 | |||
ff788edd9b | |||
aea0958f3f | |||
1f9e9f9ca0 | |||
98ffec87c0 | |||
b52d5dccac | |||
e96a4fa181 | |||
c53b0830c4 | |||
435d126a1c | |||
ef5407be33 | |||
e8b30b75d2 | |||
2c09ac4fd6 | |||
e9c1276634 | |||
71d6304407 | |||
6000a33a8e | |||
54eeb7add6 | |||
d7e8ca1c8f | |||
740f4c7d56 | |||
088eb716fc | |||
fb08e1db2b | |||
f1b167c134 | |||
36d0817579 | |||
d4c1ef36f9 | |||
102b191e94 | |||
c735267373 | |||
55b5e9834d | |||
4386ffb8d7 | |||
ca2b1a9c3e | |||
22d33bb796 | |||
ab15846480 | |||
61f4049e51 | |||
5ac4542f37 | |||
0bba536187 | |||
e48d5a5fda | |||
4c9878313c | |||
6356ddd9f3 | |||
7ea0b4b9e4 | |||
a520a60c82 | |||
54c16129ea | |||
2858682866 | |||
b1f7dfaeb4 | |||
eb27be222f | |||
bcbeaa8382 | |||
d5cb95fedd | |||
fda0da4659 | |||
1c569c79f3 | |||
872c18dddc | |||
201bea6d30 | |||
7e3c21d77b | |||
d1a02cbc0c | |||
da666871dd | |||
2fa6cf855d | |||
3b86144ae5 | |||
f01f10c5e5 | |||
e1249d3760 | |||
dcbf106daa | |||
0227ba90bb | |||
b095d63b4d | |||
20648808bc | |||
89fef0ae72 | |||
85640d402f | |||
d5703dce39 | |||
ec42d378ab | |||
2a5a975d9a | |||
a398e2470b | |||
2ce8e18bab | |||
9a9c826c0b | |||
612587dd34 | |||
506722587d | |||
c158604424 | |||
4af6f29025 | |||
d1004e3798 | |||
20aeed139d | |||
b2dbc87a4f | |||
11bd7c72e0 | |||
896cf7594e | |||
457eeb10ab | |||
0d94ffb91b | |||
bd4fccb97b | |||
0cfae088f5 | |||
b4e7663209 | |||
bdca49b265 | |||
c22954b6a4 | |||
dfe97625f0 | |||
74bc0a6301 | |||
2bd29e2fdd | |||
3cd0a782af | |||
d88ce7a43f | |||
a023eee9bf | |||
7a05c6faef | |||
6ab85f7f18 | |||
d12907b81f | |||
553989d17f | |||
475019202b | |||
a9bca55b95 | |||
89c84f10d0 | |||
882250a85e | |||
db1dd196e0 | |||
d4b8dd7fcc | |||
fcf4657833 | |||
393d7ec486 | |||
b7b5168910 | |||
1dcec17a58 | |||
b41acebf5b | |||
e0478e1775 | |||
d6b1a22563 | |||
cada292e00 | |||
d4af47f576 | |||
83eba36f8d | |||
b82a142745 | |||
2a42c203b2 | |||
ade2d4879c | |||
e14798dcdc | |||
e49050af19 | |||
0248755cda | |||
1f90359310 | |||
008fc19f0d | |||
5cd5b71b18 | |||
253d98ba73 | |||
3e2c04d029 | |||
32d3e33c83 | |||
282ebeb38b | |||
81f4d86e6d | |||
bcb90997bb | |||
277df4f04f | |||
de26c65fa0 | |||
55739ee982 | |||
10b48b27b0 | |||
d08856c1fe | |||
0d108b043d | |||
a37245b7c8 | |||
19c4ebdda9 | |||
34d4b71f59 | |||
bd8794f646 | |||
e0adcd3277 | |||
e2fe615a31 | |||
0d2536fdfd | |||
c5813204d5 | |||
5064c39a94 | |||
1880f98fa1 | |||
d3c360d9e5 | |||
388c4b9653 | |||
87f1cf17c4 | |||
dae6493a3e | |||
fc0275314d | |||
c6fa19155b | |||
2fa0191d65 | |||
ad07984158 | |||
f909b86338 | |||
327df6529b | |||
658dc63c4c | |||
4edec5f666 | |||
d150a0c135 | |||
d4242781a0 | |||
7369ca0b25 | |||
561f427cc5 | |||
8049ab703a | |||
9c2a97263a | |||
345504c1a4 | |||
549f6f2077 | |||
35c6decc75 | |||
b3abeb78ff | |||
0562a1ad42 | |||
febb0920fd | |||
549662beb0 | |||
1ea4440c5d | |||
787abdff5b | |||
2237807241 | |||
e9d9d658c4 | |||
e704092d19 | |||
305f72c197 | |||
fb6b6b4476 | |||
791cc74dbb | |||
41f139589c | |||
df24e3020b | |||
e44c716cbe | |||
d35302923d | |||
4d928368bc | |||
c055d7a470 | |||
9e1b49e181 | |||
db6a9ede1b | |||
86df0a448e | |||
5ec052bd92 | |||
6f7984d05a | |||
f6d64d1d4b | |||
ef0c7a5a57 | |||
34dfbf8e9e | |||
71d38e6fd0 | |||
9a9ba2560b | |||
2432e51970 | |||
47434cd62d | |||
ff500b44a6 | |||
4c14c7f3a4 | |||
019c4bf182 | |||
2cbc291f04 | |||
5197a3a461 | |||
52be87785f | |||
8e19fb3a8c | |||
0448dcf655 | |||
b8f74ab9e7 | |||
501ce5cebb | |||
b896ca7ef6 | |||
d497db3010 | |||
24f95fdeaa | |||
d1c4818724 | |||
9f736a9d99 | |||
49cce6a968 | |||
713337130b | |||
0a73e7ac9f | |||
3344af72c2 | |||
41eb44137e | |||
94a9667d86 | |||
8b56a7defb | |||
5a4b9b4239 | |||
f37308461c | |||
9721098178 | |||
0ca5e67dad | |||
da94564d5e | |||
1f33237659 | |||
62e5979c13 | |||
8a1e18e087 | |||
a951daddce | |||
690f6d444a | |||
b733930745 | |||
f316a3000b | |||
ddae9dc6e1 | |||
0348d6558a | |||
6a497b32f6 | |||
47acc0ea90 | |||
967c952a4a | |||
b648d159dd | |||
aecd9387d9 | |||
6e8a5e1426 | |||
607899be56 | |||
5a92a8639a | |||
4cd629b5fc | |||
6020736430 | |||
14a4047bdd | |||
23c1e22a04 | |||
2a2ae4bc4f | |||
5f4812e1d0 | |||
3ab475d916 | |||
453d64eea5 | |||
17d33f4b19 | |||
c39a5933e1 | |||
a9636b5727 | |||
5e3f44dd87 | |||
1c64616ebd | |||
23273f53cc | |||
d11ce0a86e | |||
766ceda57a | |||
eb633c607e | |||
c72d56d02d | |||
e758c434ea | |||
90e3ae9457 | |||
0e825ffcfd | |||
8a19c71f62 | |||
5a7eff041a | |||
552459834a | |||
cc6325bf6a | |||
9597ea9e1f | |||
69b5670659 | |||
56fd436e5d | |||
b7558ae28c | |||
ea60c389be | |||
f6042f29f6 | |||
983882f5a0 | |||
a6d3fd92df | |||
96f39904b8 | |||
ee347aa7ef | |||
6437334e67 | |||
2f57d7f427 | |||
db07f564aa | |||
d1479a1b16 | |||
4d80e207da | |||
e7be7ac9b4 | |||
e0954c0f89 | |||
7ae061909c | |||
45a806f46b | |||
feb6b07657 | |||
1d98582d29 | |||
06663edba2 | |||
de0d1dc94d | |||
1652ea25e4 | |||
d794e3055c | |||
a92c68ac85 | |||
dd41789230 | |||
022401b60e | |||
ef218ff1ff | |||
f933bf2f40 | |||
4fc761adea | |||
d11c214d32 | |||
ffbbe5ca5f | |||
8582091219 | |||
28c8eb3ee6 | |||
3a00a5ac3d | |||
20035e0f1b | |||
67021b0e7c | |||
c5a2831665 | |||
768f073e49 | |||
504338ea66 | |||
a8c04f96d2 | |||
340faf5341 | |||
a76c39aff9 | |||
bb728a53cc | |||
5c28a7dd44 | |||
e1efb47543 | |||
e50a296a18 | |||
e211265c30 | |||
1f143a24db | |||
48f490b810 | |||
aed382de0c | |||
8ecf40a58b | |||
aca3c75e17 | |||
f28509608b | |||
ff6c508de7 | |||
7319ea2dcf | |||
6a4efaecb0 | |||
29b0eae43f | |||
9f3e742fb1 | |||
c8e09fea33 | |||
437e932471 | |||
ce07d71d23 | |||
9815c591e0 | |||
db7a3ab630 | |||
3fa772c32e | |||
6c9dc7a15b | |||
ece0429ea8 | |||
d56ddb16b1 | |||
b6267fdf28 | |||
1f190a9255 | |||
1f0fc0a6a2 | |||
3ba678851e | |||
0869ef3d0d | |||
91100ce1e2 | |||
a65ce47736 | |||
def17bbc1e | |||
eb7da8f414 | |||
c6f29d9eb4 | |||
0d96e68c1e | |||
29d3db5112 | |||
cdf88e4477 | |||
7caac1d0c7 | |||
45364d6553 | |||
2298eb124f | |||
6dff1f8e5e | |||
a944701f3a | |||
23866fe459 | |||
0a83b04419 | |||
e6ecdf8b1d | |||
2d48fe42f4 | |||
5894ccdaf2 | |||
79bec6f6b2 | |||
9610f96c11 | |||
36a326cd81 | |||
c0c222a0b8 | |||
e17f7020e6 | |||
6d9579d3e6 | |||
9f15ee8cb8 | |||
e892ed14da | |||
093a67525a | |||
1c62a3db6e | |||
c4b4c7134d | |||
82cb6d41b8 | |||
423380d987 | |||
175d97fdcf | |||
5dbbf970b0 | |||
1541d477af | |||
d745331654 | |||
defbdc5068 | |||
350f0d8365 | |||
b5c93fb3e3 | |||
5be45ebf8e | |||
ad8fe9fe81 | |||
c2f7edaa42 | |||
6821402fef | |||
8dbb0bd2c6 | |||
24a21c1761 | |||
0cad56ec73 | |||
4d8021c403 | |||
6573cbb16c | |||
bdf76bb4b7 | |||
74ce9cc6fd | |||
070a6d866e | |||
5e2d647a6c | |||
7beebe030d | |||
66f4a31b4c | |||
beddd6a460 | |||
faec866581 | |||
effed50cc1 | |||
38ad6096ad | |||
bd53042553 | |||
039d896dee |
@ -1,30 +1,18 @@
|
|||||||
[bumpversion]
|
[bumpversion]
|
||||||
current_version = 2022.6.3
|
current_version = 2022.8.2
|
||||||
tag = True
|
tag = True
|
||||||
commit = True
|
commit = True
|
||||||
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\-?(?P<release>.*)
|
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)
|
||||||
serialize =
|
serialize = {major}.{minor}.{patch}
|
||||||
{major}.{minor}.{patch}-{release}
|
|
||||||
{major}.{minor}.{patch}
|
|
||||||
message = release: {new_version}
|
message = release: {new_version}
|
||||||
tag_name = version/{new_version}
|
tag_name = version/{new_version}
|
||||||
|
|
||||||
[bumpversion:part:release]
|
|
||||||
optional_value = stable
|
|
||||||
first_value = beta
|
|
||||||
values =
|
|
||||||
alpha
|
|
||||||
beta
|
|
||||||
stable
|
|
||||||
|
|
||||||
[bumpversion:file:pyproject.toml]
|
[bumpversion:file:pyproject.toml]
|
||||||
|
|
||||||
[bumpversion:file:docker-compose.yml]
|
[bumpversion:file:docker-compose.yml]
|
||||||
|
|
||||||
[bumpversion:file:schema.yml]
|
[bumpversion:file:schema.yml]
|
||||||
|
|
||||||
[bumpversion:file:.github/workflows/release-publish.yml]
|
|
||||||
|
|
||||||
[bumpversion:file:authentik/__init__.py]
|
[bumpversion:file:authentik/__init__.py]
|
||||||
|
|
||||||
[bumpversion:file:internal/constants/constants.go]
|
[bumpversion:file:internal/constants/constants.go]
|
||||||
|
92
.github/actions/comment-pr-instructions/action.yml
vendored
Normal file
92
.github/actions/comment-pr-instructions/action.yml
vendored
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
name: 'Comment usage instructions on PRs'
|
||||||
|
description: 'Comment usage instructions on PRs'
|
||||||
|
|
||||||
|
inputs:
|
||||||
|
tag:
|
||||||
|
description: "Image tag to pull"
|
||||||
|
required: true
|
||||||
|
|
||||||
|
runs:
|
||||||
|
using: "composite"
|
||||||
|
steps:
|
||||||
|
- name: Generate config
|
||||||
|
id: ev
|
||||||
|
shell: python
|
||||||
|
run: |
|
||||||
|
"""Helper script to get the actual branch name, docker safe"""
|
||||||
|
import os
|
||||||
|
from time import time
|
||||||
|
|
||||||
|
env_pr_branch = "GITHUB_HEAD_REF"
|
||||||
|
default_branch = "GITHUB_REF"
|
||||||
|
sha = "GITHUB_SHA"
|
||||||
|
|
||||||
|
branch_name = os.environ[default_branch]
|
||||||
|
if os.environ.get(env_pr_branch, "") != "":
|
||||||
|
branch_name = os.environ[env_pr_branch]
|
||||||
|
|
||||||
|
should_build = str(os.environ.get("DOCKER_USERNAME", "") != "").lower()
|
||||||
|
|
||||||
|
print("##[set-output name=branchName]%s" % branch_name)
|
||||||
|
print(
|
||||||
|
"##[set-output name=branchNameContainer]%s"
|
||||||
|
% branch_name.replace("refs/heads/", "").replace("/", "-")
|
||||||
|
)
|
||||||
|
print("##[set-output name=timestamp]%s" % int(time()))
|
||||||
|
print("##[set-output name=sha]%s" % os.environ[sha])
|
||||||
|
print("##[set-output name=shouldBuild]%s" % should_build)
|
||||||
|
|
||||||
|
import configparser
|
||||||
|
parser = configparser.ConfigParser()
|
||||||
|
parser.read(".bumpversion.cfg")
|
||||||
|
version = parser.get("bumpversion", "current_version")
|
||||||
|
version_family = ".".join(version.split(".")[:-1])
|
||||||
|
print("##[set-output name=version]%s" % version)
|
||||||
|
print("##[set-output name=versionFamily]%s" % version_family)
|
||||||
|
- name: Find Comment
|
||||||
|
uses: peter-evans/find-comment@v2
|
||||||
|
id: fc
|
||||||
|
with:
|
||||||
|
issue-number: ${{ github.event.pull_request.number }}
|
||||||
|
comment-author: 'github-actions[bot]'
|
||||||
|
body-includes: authentik PR Installation instructions
|
||||||
|
- name: Create or update comment
|
||||||
|
uses: peter-evans/create-or-update-comment@v2
|
||||||
|
with:
|
||||||
|
comment-id: ${{ steps.fc.outputs.comment-id }}
|
||||||
|
issue-number: ${{ github.event.pull_request.number }}
|
||||||
|
body: |
|
||||||
|
authentik PR Installation instructions
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Instructions for docker-compose</summary>
|
||||||
|
|
||||||
|
Add the following block to your `.env` file:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
AUTHENTIK_IMAGE=ghcr.io/goauthentik/dev-server
|
||||||
|
AUTHENTIK_TAG=${{ inputs.tag }}
|
||||||
|
AUTHENTIK_OUTPOSTS__CONTAINER_IMAGE_BASE=ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s
|
||||||
|
```
|
||||||
|
|
||||||
|
Afterwards, run the upgrade commands from the latest release notes.
|
||||||
|
</details>
|
||||||
|
<details>
|
||||||
|
<summary>Instructions for Kubernetes</summary>
|
||||||
|
|
||||||
|
Add the following block to your `values.yml` file:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
authentik:
|
||||||
|
outposts:
|
||||||
|
container_image_base: ghcr.io/goauthentik/dev-%(type)s:gh-%(build_hash)s
|
||||||
|
image:
|
||||||
|
repository: ghcr.io/goauthentik/dev-server
|
||||||
|
tag: ${{ inputs.tag }}
|
||||||
|
# pullPolicy: Always to ensure you always get the latest version
|
||||||
|
pullPolicy: Always
|
||||||
|
```
|
||||||
|
|
||||||
|
Afterwards, run the upgrade commands from the latest release notes.
|
||||||
|
</details>
|
||||||
|
edit-mode: replace
|
@ -17,6 +17,12 @@ outputs:
|
|||||||
sha:
|
sha:
|
||||||
description: "sha"
|
description: "sha"
|
||||||
value: ${{ steps.ev.outputs.sha }}
|
value: ${{ steps.ev.outputs.sha }}
|
||||||
|
version:
|
||||||
|
description: "version"
|
||||||
|
value: ${{ steps.ev.outputs.version }}
|
||||||
|
versionFamily:
|
||||||
|
description: "versionFamily"
|
||||||
|
value: ${{ steps.ev.outputs.versionFamily }}
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
@ -47,3 +53,11 @@ runs:
|
|||||||
print("##[set-output name=timestamp]%s" % int(time()))
|
print("##[set-output name=timestamp]%s" % int(time()))
|
||||||
print("##[set-output name=sha]%s" % os.environ[sha])
|
print("##[set-output name=sha]%s" % os.environ[sha])
|
||||||
print("##[set-output name=shouldBuild]%s" % should_build)
|
print("##[set-output name=shouldBuild]%s" % should_build)
|
||||||
|
|
||||||
|
import configparser
|
||||||
|
parser = configparser.ConfigParser()
|
||||||
|
parser.read(".bumpversion.cfg")
|
||||||
|
version = parser.get("bumpversion", "current_version")
|
||||||
|
version_family = ".".join(version.split(".")[:-1])
|
||||||
|
print("##[set-output name=version]%s" % version)
|
||||||
|
print("##[set-output name=versionFamily]%s" % version_family)
|
108
.github/dependabot.yml
vendored
108
.github/dependabot.yml
vendored
@ -1,50 +1,62 @@
|
|||||||
version: 2
|
version: 2
|
||||||
updates:
|
updates:
|
||||||
- package-ecosystem: "github-actions"
|
- package-ecosystem: "github-actions"
|
||||||
directory: "/"
|
directory: "/"
|
||||||
schedule:
|
schedule:
|
||||||
interval: daily
|
interval: daily
|
||||||
time: "04:00"
|
time: "04:00"
|
||||||
open-pull-requests-limit: 10
|
open-pull-requests-limit: 10
|
||||||
assignees:
|
reviewers:
|
||||||
- BeryJu
|
- "@goauthentik/core"
|
||||||
- package-ecosystem: gomod
|
commit-message:
|
||||||
directory: "/"
|
prefix: "ci:"
|
||||||
schedule:
|
- package-ecosystem: gomod
|
||||||
interval: daily
|
directory: "/"
|
||||||
time: "04:00"
|
schedule:
|
||||||
open-pull-requests-limit: 10
|
interval: daily
|
||||||
assignees:
|
time: "04:00"
|
||||||
- BeryJu
|
open-pull-requests-limit: 10
|
||||||
- package-ecosystem: npm
|
reviewers:
|
||||||
directory: "/web"
|
- "@goauthentik/core"
|
||||||
schedule:
|
commit-message:
|
||||||
interval: daily
|
prefix: "core:"
|
||||||
time: "04:00"
|
- package-ecosystem: npm
|
||||||
open-pull-requests-limit: 10
|
directory: "/web"
|
||||||
assignees:
|
schedule:
|
||||||
- BeryJu
|
interval: daily
|
||||||
- package-ecosystem: npm
|
time: "04:00"
|
||||||
directory: "/website"
|
open-pull-requests-limit: 10
|
||||||
schedule:
|
reviewers:
|
||||||
interval: daily
|
- "@goauthentik/core"
|
||||||
time: "04:00"
|
commit-message:
|
||||||
open-pull-requests-limit: 10
|
prefix: "web:"
|
||||||
assignees:
|
- package-ecosystem: npm
|
||||||
- BeryJu
|
directory: "/website"
|
||||||
- package-ecosystem: pip
|
schedule:
|
||||||
directory: "/"
|
interval: daily
|
||||||
schedule:
|
time: "04:00"
|
||||||
interval: daily
|
open-pull-requests-limit: 10
|
||||||
time: "04:00"
|
reviewers:
|
||||||
open-pull-requests-limit: 10
|
- "@goauthentik/core"
|
||||||
assignees:
|
commit-message:
|
||||||
- BeryJu
|
prefix: "website:"
|
||||||
- package-ecosystem: docker
|
- package-ecosystem: pip
|
||||||
directory: "/"
|
directory: "/"
|
||||||
schedule:
|
schedule:
|
||||||
interval: daily
|
interval: daily
|
||||||
time: "04:00"
|
time: "04:00"
|
||||||
open-pull-requests-limit: 10
|
open-pull-requests-limit: 10
|
||||||
assignees:
|
reviewers:
|
||||||
- BeryJu
|
- "@goauthentik/core"
|
||||||
|
commit-message:
|
||||||
|
prefix: "core:"
|
||||||
|
- package-ecosystem: docker
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: daily
|
||||||
|
time: "04:00"
|
||||||
|
open-pull-requests-limit: 10
|
||||||
|
reviewers:
|
||||||
|
- "@goauthentik/core"
|
||||||
|
commit-message:
|
||||||
|
prefix: "core:"
|
||||||
|
17
.github/workflows/ci-main.yml
vendored
17
.github/workflows/ci-main.yml
vendored
@ -106,7 +106,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
domain: ${{github.repository_owner}}
|
domain: ${{github.repository_owner}}
|
||||||
- name: Create k8s Kind Cluster
|
- name: Create k8s Kind Cluster
|
||||||
uses: helm/kind-action@v1.2.0
|
uses: helm/kind-action@v1.3.0
|
||||||
- name: run integration
|
- name: run integration
|
||||||
run: |
|
run: |
|
||||||
poetry run make test-integration
|
poetry run make test-integration
|
||||||
@ -133,12 +133,13 @@ jobs:
|
|||||||
uses: actions/cache@v3
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: web/dist
|
path: web/dist
|
||||||
key: ${{ runner.os }}-web-${{ hashFiles('web/package-lock.json', 'web/**') }}
|
key: ${{ runner.os }}-web-${{ hashFiles('web/package-lock.json', 'web/src/**') }}
|
||||||
- name: prepare web ui
|
- name: prepare web ui
|
||||||
if: steps.cache-web.outputs.cache-hit != 'true'
|
if: steps.cache-web.outputs.cache-hit != 'true'
|
||||||
working-directory: web
|
working-directory: web
|
||||||
run: |
|
run: |
|
||||||
npm ci
|
npm ci
|
||||||
|
make -C .. gen-client-web
|
||||||
npm run build
|
npm run build
|
||||||
- name: run e2e
|
- name: run e2e
|
||||||
run: |
|
run: |
|
||||||
@ -166,12 +167,13 @@ jobs:
|
|||||||
uses: actions/cache@v3
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: web/dist
|
path: web/dist
|
||||||
key: ${{ runner.os }}-web-${{ hashFiles('web/package-lock.json', 'web/**') }}
|
key: ${{ runner.os }}-web-${{ hashFiles('web/package-lock.json', 'web/src/**') }}
|
||||||
- name: prepare web ui
|
- name: prepare web ui
|
||||||
if: steps.cache-web.outputs.cache-hit != 'true'
|
if: steps.cache-web.outputs.cache-hit != 'true'
|
||||||
working-directory: web/
|
working-directory: web/
|
||||||
run: |
|
run: |
|
||||||
npm ci
|
npm ci
|
||||||
|
make -C .. gen-client-web
|
||||||
npm run build
|
npm run build
|
||||||
- name: run e2e
|
- name: run e2e
|
||||||
run: |
|
run: |
|
||||||
@ -211,10 +213,10 @@ jobs:
|
|||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v2
|
uses: docker/setup-buildx-action@v2
|
||||||
- name: prepare variables
|
- name: prepare variables
|
||||||
|
uses: ./.github/actions/docker-push-variables
|
||||||
id: ev
|
id: ev
|
||||||
env:
|
env:
|
||||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
||||||
uses: ./.github/actions/docker-setup
|
|
||||||
- name: Login to Container Registry
|
- name: Login to Container Registry
|
||||||
uses: docker/login-action@v2
|
uses: docker/login-action@v2
|
||||||
if: ${{ steps.ev.outputs.shouldBuild == 'true' }}
|
if: ${{ steps.ev.outputs.shouldBuild == 'true' }}
|
||||||
@ -231,4 +233,11 @@ jobs:
|
|||||||
ghcr.io/goauthentik/dev-server:gh-${{ steps.ev.outputs.branchNameContainer }}-${{ steps.ev.outputs.timestamp }}-${{ steps.ev.outputs.sha }}
|
ghcr.io/goauthentik/dev-server:gh-${{ steps.ev.outputs.branchNameContainer }}-${{ steps.ev.outputs.timestamp }}-${{ steps.ev.outputs.sha }}
|
||||||
build-args: |
|
build-args: |
|
||||||
GIT_BUILD_HASH=${{ steps.ev.outputs.sha }}
|
GIT_BUILD_HASH=${{ steps.ev.outputs.sha }}
|
||||||
|
VERSION_FAMILY=${{ steps.ev.outputs.versionFamily }}
|
||||||
platforms: ${{ matrix.arch }}
|
platforms: ${{ matrix.arch }}
|
||||||
|
- name: Comment on PR
|
||||||
|
if: github.event_name == 'pull_request'
|
||||||
|
continue-on-error: true
|
||||||
|
uses: ./.github/actions/comment-pr-instructions
|
||||||
|
with:
|
||||||
|
tag: gh-${{ steps.ev.outputs.branchNameContainer }}
|
||||||
|
5
.github/workflows/ci-outpost.yml
vendored
5
.github/workflows/ci-outpost.yml
vendored
@ -67,8 +67,8 @@ jobs:
|
|||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v2
|
uses: docker/setup-buildx-action@v2
|
||||||
- name: prepare variables
|
- name: prepare variables
|
||||||
|
uses: ./.github/actions/docker-push-variables
|
||||||
id: ev
|
id: ev
|
||||||
uses: ./.github/actions/docker-setup
|
|
||||||
env:
|
env:
|
||||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
||||||
- name: Login to Container Registry
|
- name: Login to Container Registry
|
||||||
@ -91,6 +91,7 @@ jobs:
|
|||||||
file: ${{ matrix.type }}.Dockerfile
|
file: ${{ matrix.type }}.Dockerfile
|
||||||
build-args: |
|
build-args: |
|
||||||
GIT_BUILD_HASH=${{ steps.ev.outputs.sha }}
|
GIT_BUILD_HASH=${{ steps.ev.outputs.sha }}
|
||||||
|
VERSION_FAMILY=${{ steps.ev.outputs.versionFamily }}
|
||||||
platforms: ${{ matrix.arch }}
|
platforms: ${{ matrix.arch }}
|
||||||
build-outpost-binary:
|
build-outpost-binary:
|
||||||
timeout-minutes: 120
|
timeout-minutes: 120
|
||||||
@ -110,7 +111,7 @@ jobs:
|
|||||||
- uses: actions/setup-go@v3
|
- uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: "^1.17"
|
go-version: "^1.17"
|
||||||
- uses: actions/setup-node@v3.3.0
|
- uses: actions/setup-node@v3.4.1
|
||||||
with:
|
with:
|
||||||
node-version: '16'
|
node-version: '16'
|
||||||
cache: 'npm'
|
cache: 'npm'
|
||||||
|
15
.github/workflows/ci-web.yml
vendored
15
.github/workflows/ci-web.yml
vendored
@ -15,7 +15,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-node@v3.3.0
|
- uses: actions/setup-node@v3.4.1
|
||||||
with:
|
with:
|
||||||
node-version: '16'
|
node-version: '16'
|
||||||
cache: 'npm'
|
cache: 'npm'
|
||||||
@ -31,7 +31,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-node@v3.3.0
|
- uses: actions/setup-node@v3.4.1
|
||||||
with:
|
with:
|
||||||
node-version: '16'
|
node-version: '16'
|
||||||
cache: 'npm'
|
cache: 'npm'
|
||||||
@ -47,13 +47,18 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-node@v3.3.0
|
- uses: actions/setup-node@v3.4.1
|
||||||
with:
|
with:
|
||||||
node-version: '16'
|
node-version: '16'
|
||||||
cache: 'npm'
|
cache: 'npm'
|
||||||
cache-dependency-path: web/package-lock.json
|
cache-dependency-path: web/package-lock.json
|
||||||
- working-directory: web/
|
- working-directory: web/
|
||||||
run: npm ci
|
run: |
|
||||||
|
npm ci
|
||||||
|
# lit-analyse doesn't understand path rewrites, so make it
|
||||||
|
# belive it's an actual module
|
||||||
|
cd node_modules/@goauthentik
|
||||||
|
ln -s ../../src/ web
|
||||||
- name: Generate API
|
- name: Generate API
|
||||||
run: make gen-client-web
|
run: make gen-client-web
|
||||||
- name: lit-analyse
|
- name: lit-analyse
|
||||||
@ -73,7 +78,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-node@v3.3.0
|
- uses: actions/setup-node@v3.4.1
|
||||||
with:
|
with:
|
||||||
node-version: '16'
|
node-version: '16'
|
||||||
cache: 'npm'
|
cache: 'npm'
|
||||||
|
2
.github/workflows/ci-website.yml
vendored
2
.github/workflows/ci-website.yml
vendored
@ -15,7 +15,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-node@v3.3.0
|
- uses: actions/setup-node@v3.4.1
|
||||||
with:
|
with:
|
||||||
node-version: '16'
|
node-version: '16'
|
||||||
cache: 'npm'
|
cache: 'npm'
|
||||||
|
29
.github/workflows/release-publish.yml
vendored
29
.github/workflows/release-publish.yml
vendored
@ -5,7 +5,6 @@ on:
|
|||||||
types: [published, created]
|
types: [published, created]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
# Build
|
|
||||||
build-server:
|
build-server:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
@ -14,6 +13,9 @@ jobs:
|
|||||||
uses: docker/setup-qemu-action@v2.0.0
|
uses: docker/setup-qemu-action@v2.0.0
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v2
|
uses: docker/setup-buildx-action@v2
|
||||||
|
- name: prepare variables
|
||||||
|
uses: ./.github/actions/docker-push-variables
|
||||||
|
id: ev
|
||||||
- name: Docker Login Registry
|
- name: Docker Login Registry
|
||||||
uses: docker/login-action@v2
|
uses: docker/login-action@v2
|
||||||
with:
|
with:
|
||||||
@ -30,9 +32,11 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
push: ${{ github.event_name == 'release' }}
|
push: ${{ github.event_name == 'release' }}
|
||||||
tags: |
|
tags: |
|
||||||
beryju/authentik:2022.6.3,
|
beryju/authentik:${{ steps.ev.outputs.version }},
|
||||||
|
beryju/authentik:${{ steps.ev.outputs.versionFamily }},
|
||||||
beryju/authentik:latest,
|
beryju/authentik:latest,
|
||||||
ghcr.io/goauthentik/server:2022.6.3,
|
ghcr.io/goauthentik/server:${{ steps.ev.outputs.version }},
|
||||||
|
ghcr.io/goauthentik/server:${{ steps.ev.outputs.versionFamily }},
|
||||||
ghcr.io/goauthentik/server:latest
|
ghcr.io/goauthentik/server:latest
|
||||||
platforms: linux/amd64,linux/arm64
|
platforms: linux/amd64,linux/arm64
|
||||||
context: .
|
context: .
|
||||||
@ -53,6 +57,9 @@ jobs:
|
|||||||
uses: docker/setup-qemu-action@v2.0.0
|
uses: docker/setup-qemu-action@v2.0.0
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v2
|
uses: docker/setup-buildx-action@v2
|
||||||
|
- name: prepare variables
|
||||||
|
uses: ./.github/actions/docker-push-variables
|
||||||
|
id: ev
|
||||||
- name: Docker Login Registry
|
- name: Docker Login Registry
|
||||||
uses: docker/login-action@v2
|
uses: docker/login-action@v2
|
||||||
with:
|
with:
|
||||||
@ -69,9 +76,11 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
push: ${{ github.event_name == 'release' }}
|
push: ${{ github.event_name == 'release' }}
|
||||||
tags: |
|
tags: |
|
||||||
beryju/authentik-${{ matrix.type }}:2022.6.3,
|
beryju/authentik-${{ matrix.type }}:${{ steps.ev.outputs.version }},
|
||||||
|
beryju/authentik-${{ matrix.type }}:${{ steps.ev.outputs.versionFamily }},
|
||||||
beryju/authentik-${{ matrix.type }}:latest,
|
beryju/authentik-${{ matrix.type }}:latest,
|
||||||
ghcr.io/goauthentik/${{ matrix.type }}:2022.6.3,
|
ghcr.io/goauthentik/${{ matrix.type }}:${{ steps.ev.outputs.version }},
|
||||||
|
ghcr.io/goauthentik/${{ matrix.type }}:${{ steps.ev.outputs.versionFamily }},
|
||||||
ghcr.io/goauthentik/${{ matrix.type }}:latest
|
ghcr.io/goauthentik/${{ matrix.type }}:latest
|
||||||
file: ${{ matrix.type }}.Dockerfile
|
file: ${{ matrix.type }}.Dockerfile
|
||||||
platforms: linux/amd64,linux/arm64
|
platforms: linux/amd64,linux/arm64
|
||||||
@ -91,7 +100,7 @@ jobs:
|
|||||||
- uses: actions/setup-go@v3
|
- uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: "^1.17"
|
go-version: "^1.17"
|
||||||
- uses: actions/setup-node@v3.3.0
|
- uses: actions/setup-node@v3.4.1
|
||||||
with:
|
with:
|
||||||
node-version: '16'
|
node-version: '16'
|
||||||
cache: 'npm'
|
cache: 'npm'
|
||||||
@ -129,7 +138,7 @@ jobs:
|
|||||||
docker-compose pull -q
|
docker-compose pull -q
|
||||||
docker-compose up --no-start
|
docker-compose up --no-start
|
||||||
docker-compose start postgresql redis
|
docker-compose start postgresql redis
|
||||||
docker-compose run -u root server test
|
docker-compose run -u root server test-all
|
||||||
sentry-release:
|
sentry-release:
|
||||||
needs:
|
needs:
|
||||||
- build-server
|
- build-server
|
||||||
@ -138,6 +147,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
- name: prepare variables
|
||||||
|
uses: ./.github/actions/docker-push-variables
|
||||||
|
id: ev
|
||||||
- name: Get static files from docker image
|
- name: Get static files from docker image
|
||||||
run: |
|
run: |
|
||||||
docker pull ghcr.io/goauthentik/server:latest
|
docker pull ghcr.io/goauthentik/server:latest
|
||||||
@ -145,6 +157,7 @@ jobs:
|
|||||||
docker cp ${container}:web/ .
|
docker cp ${container}:web/ .
|
||||||
- name: Create a Sentry.io release
|
- name: Create a Sentry.io release
|
||||||
uses: getsentry/action-release@v1
|
uses: getsentry/action-release@v1
|
||||||
|
continue-on-error: true
|
||||||
if: ${{ github.event_name == 'release' }}
|
if: ${{ github.event_name == 'release' }}
|
||||||
env:
|
env:
|
||||||
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
||||||
@ -152,7 +165,7 @@ jobs:
|
|||||||
SENTRY_PROJECT: authentik
|
SENTRY_PROJECT: authentik
|
||||||
SENTRY_URL: https://sentry.beryju.org
|
SENTRY_URL: https://sentry.beryju.org
|
||||||
with:
|
with:
|
||||||
version: authentik@2022.6.3
|
version: authentik@${{ steps.ev.outputs.version }}
|
||||||
environment: beryjuorg-prod
|
environment: beryjuorg-prod
|
||||||
sourcemaps: './web/dist'
|
sourcemaps: './web/dist'
|
||||||
url_prefix: '~/static/dist'
|
url_prefix: '~/static/dist'
|
||||||
|
7
.github/workflows/release-tag.yml
vendored
7
.github/workflows/release-tag.yml
vendored
@ -16,15 +16,12 @@ jobs:
|
|||||||
echo "PG_PASS=$(openssl rand -base64 32)" >> .env
|
echo "PG_PASS=$(openssl rand -base64 32)" >> .env
|
||||||
echo "AUTHENTIK_SECRET_KEY=$(openssl rand -base64 32)" >> .env
|
echo "AUTHENTIK_SECRET_KEY=$(openssl rand -base64 32)" >> .env
|
||||||
docker buildx install
|
docker buildx install
|
||||||
docker build \
|
docker build -t testing:latest .
|
||||||
--no-cache \
|
|
||||||
-t testing:latest \
|
|
||||||
-f Dockerfile .
|
|
||||||
echo "AUTHENTIK_IMAGE=testing" >> .env
|
echo "AUTHENTIK_IMAGE=testing" >> .env
|
||||||
echo "AUTHENTIK_TAG=latest" >> .env
|
echo "AUTHENTIK_TAG=latest" >> .env
|
||||||
docker-compose up --no-start
|
docker-compose up --no-start
|
||||||
docker-compose start postgresql redis
|
docker-compose start postgresql redis
|
||||||
docker-compose run -u root server test
|
docker-compose run -u root server test-all
|
||||||
- name: Extract version number
|
- name: Extract version number
|
||||||
id: get_version
|
id: get_version
|
||||||
uses: actions/github-script@v6
|
uses: actions/github-script@v6
|
||||||
|
8
.github/workflows/web-api-publish.yml
vendored
8
.github/workflows/web-api-publish.yml
vendored
@ -11,7 +11,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
# Setup .npmrc file to publish to npm
|
# Setup .npmrc file to publish to npm
|
||||||
- uses: actions/setup-node@v3.3.0
|
- uses: actions/setup-node@v3.4.1
|
||||||
with:
|
with:
|
||||||
node-version: '16'
|
node-version: '16'
|
||||||
registry-url: 'https://registry.npmjs.org'
|
registry-url: 'https://registry.npmjs.org'
|
||||||
@ -35,8 +35,8 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
token: ${{ secrets.GITHUB_TOKEN }}
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
branch: update-web-api-client
|
branch: update-web-api-client
|
||||||
commit-message: "web: Update Web API Client version"
|
commit-message: "web: bump API Client version"
|
||||||
title: "web: Update Web API Client version"
|
title: "web: bump API Client version"
|
||||||
body: "web: Update Web API Client version"
|
body: "web: bump API Client version"
|
||||||
delete-branch: true
|
delete-branch: true
|
||||||
signoff: true
|
signoff: true
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -193,7 +193,6 @@ pip-selfcheck.json
|
|||||||
# End of https://www.gitignore.io/api/python,django
|
# End of https://www.gitignore.io/api/python,django
|
||||||
/static/
|
/static/
|
||||||
local.env.yml
|
local.env.yml
|
||||||
.vscode/
|
|
||||||
|
|
||||||
# Selenium Screenshots
|
# Selenium Screenshots
|
||||||
selenium_screenshots/
|
selenium_screenshots/
|
||||||
|
12
.vscode/settings.json
vendored
12
.vscode/settings.json
vendored
@ -20,7 +20,15 @@
|
|||||||
"todo-tree.tree.showCountsInTree": true,
|
"todo-tree.tree.showCountsInTree": true,
|
||||||
"todo-tree.tree.showBadges": true,
|
"todo-tree.tree.showBadges": true,
|
||||||
"python.formatting.provider": "black",
|
"python.formatting.provider": "black",
|
||||||
"files.associations": {
|
"yaml.customTags": [
|
||||||
"*.akflow": "json"
|
"!Find sequence",
|
||||||
|
"!KeyOf scalar"
|
||||||
|
],
|
||||||
|
"typescript.preferences.importModuleSpecifier": "non-relative",
|
||||||
|
"typescript.preferences.importModuleSpecifierEnding": "index",
|
||||||
|
"typescript.tsdk": "./web/node_modules/typescript/lib",
|
||||||
|
"typescript.enablePromptUseWorkspaceTsdk": true,
|
||||||
|
"yaml.schemas": {
|
||||||
|
"./blueprints/schema.json": "blueprints/**/*.yaml"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
86
.vscode/tasks.json
vendored
Normal file
86
.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "authentik[core]: format & test",
|
||||||
|
"command": "poetry",
|
||||||
|
"args": [
|
||||||
|
"run",
|
||||||
|
"make"
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "authentik[core]: run",
|
||||||
|
"command": "poetry",
|
||||||
|
"args": [
|
||||||
|
"run",
|
||||||
|
"make",
|
||||||
|
"run",
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
"presentation": {
|
||||||
|
"panel": "dedicated",
|
||||||
|
"group": "running"
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "authentik[web]: format",
|
||||||
|
"command": "make",
|
||||||
|
"args": ["web"],
|
||||||
|
"group": "build",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "authentik[web]: watch",
|
||||||
|
"command": "make",
|
||||||
|
"args": ["web-watch"],
|
||||||
|
"group": "build",
|
||||||
|
"presentation": {
|
||||||
|
"panel": "dedicated",
|
||||||
|
"group": "running"
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "authentik: install",
|
||||||
|
"command": "make",
|
||||||
|
"args": ["install"],
|
||||||
|
"group": "build",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "authentik: i18n-extract",
|
||||||
|
"command": "poetry",
|
||||||
|
"args": [
|
||||||
|
"run",
|
||||||
|
"make",
|
||||||
|
"i18n-extract"
|
||||||
|
],
|
||||||
|
"group": "build",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "authentik[website]: format",
|
||||||
|
"command": "make",
|
||||||
|
"args": ["website"],
|
||||||
|
"group": "build",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "authentik[website]: watch",
|
||||||
|
"command": "make",
|
||||||
|
"args": ["website-watch"],
|
||||||
|
"group": "build",
|
||||||
|
"presentation": {
|
||||||
|
"panel": "dedicated",
|
||||||
|
"group": "running"
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "authentik[api]: generate",
|
||||||
|
"command": "poetry",
|
||||||
|
"args": [
|
||||||
|
"run",
|
||||||
|
"make",
|
||||||
|
"gen"
|
||||||
|
],
|
||||||
|
"group": "build"
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
16
Dockerfile
16
Dockerfile
@ -2,6 +2,7 @@
|
|||||||
FROM --platform=${BUILDPLATFORM} docker.io/node:18 as website-builder
|
FROM --platform=${BUILDPLATFORM} docker.io/node:18 as website-builder
|
||||||
|
|
||||||
COPY ./website /work/website/
|
COPY ./website /work/website/
|
||||||
|
COPY ./blueprints /work/blueprints/
|
||||||
|
|
||||||
ENV NODE_ENV=production
|
ENV NODE_ENV=production
|
||||||
WORKDIR /work/website
|
WORKDIR /work/website
|
||||||
@ -18,7 +19,7 @@ WORKDIR /work/web
|
|||||||
RUN npm ci && npm run build
|
RUN npm ci && npm run build
|
||||||
|
|
||||||
# Stage 3: Poetry to requirements.txt export
|
# Stage 3: Poetry to requirements.txt export
|
||||||
FROM docker.io/python:3.10.4-slim-bullseye AS poetry-locker
|
FROM docker.io/python:3.10.6-slim-bullseye AS poetry-locker
|
||||||
|
|
||||||
WORKDIR /work
|
WORKDIR /work
|
||||||
COPY ./pyproject.toml /work
|
COPY ./pyproject.toml /work
|
||||||
@ -29,7 +30,7 @@ RUN pip install --no-cache-dir poetry && \
|
|||||||
poetry export -f requirements.txt --dev --output requirements-dev.txt
|
poetry export -f requirements.txt --dev --output requirements-dev.txt
|
||||||
|
|
||||||
# Stage 4: Build go proxy
|
# Stage 4: Build go proxy
|
||||||
FROM docker.io/golang:1.18.3-bullseye AS builder
|
FROM docker.io/golang:1.19.0-bullseye AS go-builder
|
||||||
|
|
||||||
WORKDIR /work
|
WORKDIR /work
|
||||||
|
|
||||||
@ -45,7 +46,7 @@ COPY ./go.sum /work/go.sum
|
|||||||
RUN go build -o /work/authentik ./cmd/server/main.go
|
RUN go build -o /work/authentik ./cmd/server/main.go
|
||||||
|
|
||||||
# Stage 5: Run
|
# Stage 5: Run
|
||||||
FROM docker.io/python:3.10.4-slim-bullseye
|
FROM docker.io/python:3.10.6-slim-bullseye AS final-image
|
||||||
|
|
||||||
LABEL org.opencontainers.image.url https://goauthentik.io
|
LABEL org.opencontainers.image.url https://goauthentik.io
|
||||||
LABEL org.opencontainers.image.description goauthentik.io Main server image, see https://goauthentik.io for more info.
|
LABEL org.opencontainers.image.description goauthentik.io Main server image, see https://goauthentik.io for more info.
|
||||||
@ -72,7 +73,7 @@ RUN apt-get update && \
|
|||||||
apt-get clean && \
|
apt-get clean && \
|
||||||
rm -rf /tmp/* /var/lib/apt/lists/* /var/tmp/ && \
|
rm -rf /tmp/* /var/lib/apt/lists/* /var/tmp/ && \
|
||||||
adduser --system --no-create-home --uid 1000 --group --home /authentik authentik && \
|
adduser --system --no-create-home --uid 1000 --group --home /authentik authentik && \
|
||||||
mkdir -p /certs /media && \
|
mkdir -p /certs /media /blueprints && \
|
||||||
mkdir -p /authentik/.ssh && \
|
mkdir -p /authentik/.ssh && \
|
||||||
chown authentik:authentik /certs /media /authentik/.ssh
|
chown authentik:authentik /certs /media /authentik/.ssh
|
||||||
|
|
||||||
@ -81,13 +82,14 @@ COPY ./pyproject.toml /
|
|||||||
COPY ./xml /xml
|
COPY ./xml /xml
|
||||||
COPY ./tests /tests
|
COPY ./tests /tests
|
||||||
COPY ./manage.py /
|
COPY ./manage.py /
|
||||||
|
COPY ./blueprints /blueprints
|
||||||
COPY ./lifecycle/ /lifecycle
|
COPY ./lifecycle/ /lifecycle
|
||||||
COPY --from=builder /work/authentik /authentik-proxy
|
COPY --from=go-builder /work/authentik /authentik-proxy
|
||||||
COPY --from=web-builder /work/web/dist/ /web/dist/
|
COPY --from=web-builder /work/web/dist/ /web/dist/
|
||||||
COPY --from=web-builder /work/web/authentik/ /web/authentik/
|
COPY --from=web-builder /work/web/authentik/ /web/authentik/
|
||||||
COPY --from=website-builder /work/website/help/ /website/help/
|
COPY --from=website-builder /work/website/help/ /website/help/
|
||||||
|
|
||||||
USER authentik
|
USER 1000
|
||||||
|
|
||||||
ENV TMPDIR /dev/shm/
|
ENV TMPDIR /dev/shm/
|
||||||
ENV PYTHONUNBUFFERED 1
|
ENV PYTHONUNBUFFERED 1
|
||||||
@ -95,4 +97,4 @@ ENV PATH "/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin
|
|||||||
|
|
||||||
HEALTHCHECK --interval=30s --timeout=30s --start-period=60s --retries=3 CMD [ "/lifecycle/ak", "healthcheck" ]
|
HEALTHCHECK --interval=30s --timeout=30s --start-period=60s --retries=3 CMD [ "/lifecycle/ak", "healthcheck" ]
|
||||||
|
|
||||||
ENTRYPOINT [ "/lifecycle/ak" ]
|
ENTRYPOINT [ "/usr/local/bin/dumb-init", "--", "/lifecycle/ak" ]
|
||||||
|
33
Makefile
33
Makefile
@ -33,8 +33,8 @@ test:
|
|||||||
coverage report
|
coverage report
|
||||||
|
|
||||||
lint-fix:
|
lint-fix:
|
||||||
isort authentik tests lifecycle
|
isort authentik tests scripts lifecycle
|
||||||
black authentik tests lifecycle
|
black authentik tests scripts lifecycle
|
||||||
codespell -I .github/codespell-words.txt -S 'web/src/locales/**' -w \
|
codespell -I .github/codespell-words.txt -S 'web/src/locales/**' -w \
|
||||||
authentik \
|
authentik \
|
||||||
internal \
|
internal \
|
||||||
@ -45,17 +45,18 @@ lint-fix:
|
|||||||
website/developer-docs
|
website/developer-docs
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
bandit -r authentik tests lifecycle -x node_modules
|
|
||||||
pylint authentik tests lifecycle
|
pylint authentik tests lifecycle
|
||||||
|
bandit -r authentik tests lifecycle -x node_modules
|
||||||
golangci-lint run -v
|
golangci-lint run -v
|
||||||
|
|
||||||
i18n-extract: i18n-extract-core web-extract
|
i18n-extract: i18n-extract-core web-extract
|
||||||
|
|
||||||
i18n-extract-core:
|
i18n-extract-core:
|
||||||
./manage.py makemessages --ignore web --ignore internal --ignore web --ignore web-api --ignore website -l en
|
ak makemessages --ignore web --ignore internal --ignore web --ignore web-api --ignore website -l en
|
||||||
|
|
||||||
gen-build:
|
gen-build:
|
||||||
AUTHENTIK_DEBUG=true ./manage.py spectacular --file schema.yml
|
AUTHENTIK_DEBUG=true ak make_blueprint_schema > blueprints/schema.json
|
||||||
|
AUTHENTIK_DEBUG=true ak spectacular --file schema.yml
|
||||||
|
|
||||||
gen-clean:
|
gen-clean:
|
||||||
rm -rf web/api/src/
|
rm -rf web/api/src/
|
||||||
@ -91,6 +92,9 @@ gen-client-go:
|
|||||||
go mod edit -replace goauthentik.io/api/v3=./gen-go-api
|
go mod edit -replace goauthentik.io/api/v3=./gen-go-api
|
||||||
rm -rf config.yaml ./templates/
|
rm -rf config.yaml ./templates/
|
||||||
|
|
||||||
|
gen-dev-config:
|
||||||
|
python -m scripts.generate_config
|
||||||
|
|
||||||
gen: gen-build gen-clean gen-client-web
|
gen: gen-build gen-clean gen-client-web
|
||||||
|
|
||||||
migrate:
|
migrate:
|
||||||
@ -106,12 +110,15 @@ run:
|
|||||||
web-build: web-install
|
web-build: web-install
|
||||||
cd web && npm run build
|
cd web && npm run build
|
||||||
|
|
||||||
web: web-lint-fix web-lint web-extract
|
web: web-lint-fix web-lint
|
||||||
|
|
||||||
web-install:
|
web-install:
|
||||||
cd web && npm ci
|
cd web && npm ci
|
||||||
|
|
||||||
web-watch:
|
web-watch:
|
||||||
|
rm -rf web/dist/
|
||||||
|
mkdir web/dist/
|
||||||
|
touch web/dist/.gitkeep
|
||||||
cd web && npm run watch
|
cd web && npm run watch
|
||||||
|
|
||||||
web-lint-fix:
|
web-lint-fix:
|
||||||
@ -162,12 +169,16 @@ ci-pyright: ci--meta-debug
|
|||||||
pyright e2e lifecycle
|
pyright e2e lifecycle
|
||||||
|
|
||||||
ci-pending-migrations: ci--meta-debug
|
ci-pending-migrations: ci--meta-debug
|
||||||
./manage.py makemigrations --check
|
ak makemigrations --check
|
||||||
|
|
||||||
install: web-install website-install
|
install: web-install website-install
|
||||||
poetry install
|
poetry install
|
||||||
|
|
||||||
a: install
|
dev-reset:
|
||||||
tmux \
|
dropdb -U postgres -h localhost authentik
|
||||||
new-session 'make run' \; \
|
createdb -U postgres -h localhost authentik
|
||||||
split-window 'make web-watch'
|
redis-cli -n 0 flushall
|
||||||
|
redis-cli -n 1 flushall
|
||||||
|
redis-cli -n 2 flushall
|
||||||
|
redis-cli -n 3 flushall
|
||||||
|
make migrate
|
||||||
|
@ -6,8 +6,9 @@
|
|||||||
|
|
||||||
| Version | Supported |
|
| Version | Supported |
|
||||||
| ---------- | ------------------ |
|
| ---------- | ------------------ |
|
||||||
| 2022.4.x | :white_check_mark: |
|
| 2022.6.x | :white_check_mark: |
|
||||||
| 2022.5.x | :white_check_mark: |
|
| 2022.7.x | :white_check_mark: |
|
||||||
|
| 2022.8.x | :white_check_mark: |
|
||||||
|
|
||||||
## Reporting a Vulnerability
|
## Reporting a Vulnerability
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
from os import environ
|
from os import environ
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
__version__ = "2022.6.3"
|
__version__ = "2022.8.2"
|
||||||
ENV_GIT_HASH_KEY = "GIT_BUILD_HASH"
|
ENV_GIT_HASH_KEY = "GIT_BUILD_HASH"
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ from rest_framework.response import Response
|
|||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
from authentik.core.api.utils import PassiveSerializer
|
from authentik.core.api.utils import PassiveSerializer
|
||||||
from authentik.outposts.managed import MANAGED_OUTPOST
|
from authentik.outposts.apps import MANAGED_OUTPOST
|
||||||
from authentik.outposts.models import Outpost
|
from authentik.outposts.models import Outpost
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
"""authentik administration overview"""
|
"""authentik administration overview"""
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from drf_spectacular.utils import extend_schema, inline_serializer
|
from drf_spectacular.utils import extend_schema, inline_serializer
|
||||||
from prometheus_client import Gauge
|
|
||||||
from rest_framework.fields import IntegerField
|
from rest_framework.fields import IntegerField
|
||||||
from rest_framework.permissions import IsAdminUser
|
from rest_framework.permissions import IsAdminUser
|
||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
@ -10,8 +9,6 @@ from rest_framework.views import APIView
|
|||||||
|
|
||||||
from authentik.root.celery import CELERY_APP
|
from authentik.root.celery import CELERY_APP
|
||||||
|
|
||||||
GAUGE_WORKERS = Gauge("authentik_admin_workers", "Currently connected workers")
|
|
||||||
|
|
||||||
|
|
||||||
class WorkerView(APIView):
|
class WorkerView(APIView):
|
||||||
"""Get currently connected worker count."""
|
"""Get currently connected worker count."""
|
||||||
|
@ -1,15 +1,20 @@
|
|||||||
"""authentik admin app config"""
|
"""authentik admin app config"""
|
||||||
from importlib import import_module
|
from prometheus_client import Gauge, Info
|
||||||
|
|
||||||
from django.apps import AppConfig
|
from authentik.blueprints.manager import ManagedAppConfig
|
||||||
|
|
||||||
|
PROM_INFO = Info("authentik_version", "Currently running authentik version")
|
||||||
|
GAUGE_WORKERS = Gauge("authentik_admin_workers", "Currently connected workers")
|
||||||
|
|
||||||
|
|
||||||
class AuthentikAdminConfig(AppConfig):
|
class AuthentikAdminConfig(ManagedAppConfig):
|
||||||
"""authentik admin app config"""
|
"""authentik admin app config"""
|
||||||
|
|
||||||
name = "authentik.admin"
|
name = "authentik.admin"
|
||||||
label = "authentik_admin"
|
label = "authentik_admin"
|
||||||
verbose_name = "authentik Admin"
|
verbose_name = "authentik Admin"
|
||||||
|
default = True
|
||||||
|
|
||||||
def ready(self):
|
def reconcile_load_admin_signals(self):
|
||||||
import_module("authentik.admin.signals")
|
"""Load admin signals"""
|
||||||
|
self.import_module("authentik.admin.signals")
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
|
|
||||||
from authentik.admin.api.tasks import TaskInfo
|
from authentik.admin.api.tasks import TaskInfo
|
||||||
from authentik.admin.api.workers import GAUGE_WORKERS
|
from authentik.admin.apps import GAUGE_WORKERS
|
||||||
from authentik.root.celery import CELERY_APP
|
from authentik.root.celery import CELERY_APP
|
||||||
from authentik.root.monitoring import monitoring_set
|
from authentik.root.monitoring import monitoring_set
|
||||||
|
|
||||||
|
@ -3,12 +3,13 @@ import re
|
|||||||
|
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.core.validators import URLValidator
|
from django.core.validators import URLValidator
|
||||||
|
from django.db import DatabaseError, InternalError, ProgrammingError
|
||||||
from packaging.version import parse
|
from packaging.version import parse
|
||||||
from prometheus_client import Info
|
|
||||||
from requests import RequestException
|
from requests import RequestException
|
||||||
from structlog.stdlib import get_logger
|
from structlog.stdlib import get_logger
|
||||||
|
|
||||||
from authentik import __version__, get_build_hash
|
from authentik import __version__, get_build_hash
|
||||||
|
from authentik.admin.apps import PROM_INFO
|
||||||
from authentik.events.models import Event, EventAction, Notification
|
from authentik.events.models import Event, EventAction, Notification
|
||||||
from authentik.events.monitored_tasks import (
|
from authentik.events.monitored_tasks import (
|
||||||
MonitoredTask,
|
MonitoredTask,
|
||||||
@ -25,7 +26,6 @@ VERSION_CACHE_KEY = "authentik_latest_version"
|
|||||||
VERSION_CACHE_TIMEOUT = 8 * 60 * 60 # 8 hours
|
VERSION_CACHE_TIMEOUT = 8 * 60 * 60 # 8 hours
|
||||||
# Chop of the first ^ because we want to search the entire string
|
# Chop of the first ^ because we want to search the entire string
|
||||||
URL_FINDER = URLValidator.regex.pattern[1:]
|
URL_FINDER = URLValidator.regex.pattern[1:]
|
||||||
PROM_INFO = Info("authentik_version", "Currently running authentik version")
|
|
||||||
LOCAL_VERSION = parse(__version__)
|
LOCAL_VERSION = parse(__version__)
|
||||||
|
|
||||||
|
|
||||||
@ -40,7 +40,9 @@ def _set_prom_info():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@CELERY_APP.task()
|
@CELERY_APP.task(
|
||||||
|
throws=(DatabaseError, ProgrammingError, InternalError),
|
||||||
|
)
|
||||||
def clear_update_notifications():
|
def clear_update_notifications():
|
||||||
"""Clear update notifications on startup if the notification was for the version
|
"""Clear update notifications on startup if the notification was for the version
|
||||||
we're running now."""
|
we're running now."""
|
||||||
|
@ -5,10 +5,10 @@ from django.test import TestCase
|
|||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
from authentik import __version__
|
from authentik import __version__
|
||||||
|
from authentik.blueprints.tests import reconcile_app
|
||||||
from authentik.core.models import Group, User
|
from authentik.core.models import Group, User
|
||||||
from authentik.core.tasks import clean_expired_models
|
from authentik.core.tasks import clean_expired_models
|
||||||
from authentik.events.monitored_tasks import TaskResultStatus
|
from authentik.events.monitored_tasks import TaskResultStatus
|
||||||
from authentik.managed.tasks import managed_reconcile
|
|
||||||
|
|
||||||
|
|
||||||
class TestAdminAPI(TestCase):
|
class TestAdminAPI(TestCase):
|
||||||
@ -93,9 +93,8 @@ class TestAdminAPI(TestCase):
|
|||||||
response = self.client.get(reverse("authentik_api:apps-list"))
|
response = self.client.get(reverse("authentik_api:apps-list"))
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
@reconcile_app("authentik_outposts")
|
||||||
def test_system(self):
|
def test_system(self):
|
||||||
"""Test system API"""
|
"""Test system API"""
|
||||||
# pyright: reportGeneralTypeIssues=false
|
|
||||||
managed_reconcile() # pylint: disable=no-value-for-parameter
|
|
||||||
response = self.client.get(reverse("authentik_api:admin_system"))
|
response = self.client.get(reverse("authentik_api:admin_system"))
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
@ -7,9 +7,11 @@ from rest_framework.exceptions import AuthenticationFailed
|
|||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
from structlog.stdlib import get_logger
|
from structlog.stdlib import get_logger
|
||||||
|
|
||||||
from authentik.core.middleware import KEY_AUTH_VIA, LOCAL
|
from authentik.core.middleware import CTX_AUTH_VIA
|
||||||
from authentik.core.models import Token, TokenIntents, User
|
from authentik.core.models import Token, TokenIntents, User
|
||||||
from authentik.outposts.models import Outpost
|
from authentik.outposts.models import Outpost
|
||||||
|
from authentik.providers.oauth2.constants import SCOPE_AUTHENTIK_API
|
||||||
|
from authentik.providers.oauth2.models import RefreshToken
|
||||||
|
|
||||||
LOGGER = get_logger()
|
LOGGER = get_logger()
|
||||||
|
|
||||||
@ -24,7 +26,7 @@ def validate_auth(header: bytes) -> str:
|
|||||||
if auth_type.lower() != "bearer":
|
if auth_type.lower() != "bearer":
|
||||||
LOGGER.debug("Unsupported authentication type, denying", type=auth_type.lower())
|
LOGGER.debug("Unsupported authentication type, denying", type=auth_type.lower())
|
||||||
raise AuthenticationFailed("Unsupported authentication type")
|
raise AuthenticationFailed("Unsupported authentication type")
|
||||||
if auth_credentials == "": # nosec
|
if auth_credentials == "": # nosec # noqa
|
||||||
raise AuthenticationFailed("Malformed header")
|
raise AuthenticationFailed("Malformed header")
|
||||||
return auth_credentials
|
return auth_credentials
|
||||||
|
|
||||||
@ -35,13 +37,27 @@ def bearer_auth(raw_header: bytes) -> Optional[User]:
|
|||||||
if not auth_credentials:
|
if not auth_credentials:
|
||||||
return None
|
return None
|
||||||
# first, check traditional tokens
|
# first, check traditional tokens
|
||||||
token = Token.filter_not_expired(key=auth_credentials, intent=TokenIntents.INTENT_API).first()
|
key_token = Token.filter_not_expired(
|
||||||
if hasattr(LOCAL, "authentik"):
|
key=auth_credentials, intent=TokenIntents.INTENT_API
|
||||||
LOCAL.authentik[KEY_AUTH_VIA] = "api_token"
|
).first()
|
||||||
if token:
|
if key_token:
|
||||||
return token.user
|
CTX_AUTH_VIA.set("api_token")
|
||||||
|
return key_token.user
|
||||||
|
# then try to auth via JWT
|
||||||
|
jwt_token = RefreshToken.filter_not_expired(
|
||||||
|
refresh_token=auth_credentials, _scope__icontains=SCOPE_AUTHENTIK_API
|
||||||
|
).first()
|
||||||
|
if jwt_token:
|
||||||
|
# Double-check scopes, since they are saved in a single string
|
||||||
|
# we want to check the parsed version too
|
||||||
|
if SCOPE_AUTHENTIK_API not in jwt_token.scope:
|
||||||
|
raise AuthenticationFailed("Token invalid/expired")
|
||||||
|
CTX_AUTH_VIA.set("jwt")
|
||||||
|
return jwt_token.user
|
||||||
|
# then try to auth via secret key (for embedded outpost/etc)
|
||||||
user = token_secret_key(auth_credentials)
|
user = token_secret_key(auth_credentials)
|
||||||
if user:
|
if user:
|
||||||
|
CTX_AUTH_VIA.set("secret_key")
|
||||||
return user
|
return user
|
||||||
raise AuthenticationFailed("Token invalid/expired")
|
raise AuthenticationFailed("Token invalid/expired")
|
||||||
|
|
||||||
@ -49,15 +65,13 @@ def bearer_auth(raw_header: bytes) -> Optional[User]:
|
|||||||
def token_secret_key(value: str) -> Optional[User]:
|
def token_secret_key(value: str) -> Optional[User]:
|
||||||
"""Check if the token is the secret key
|
"""Check if the token is the secret key
|
||||||
and return the service account for the managed outpost"""
|
and return the service account for the managed outpost"""
|
||||||
from authentik.outposts.managed import MANAGED_OUTPOST
|
from authentik.outposts.apps import MANAGED_OUTPOST
|
||||||
|
|
||||||
if value != settings.SECRET_KEY:
|
if value != settings.SECRET_KEY:
|
||||||
return None
|
return None
|
||||||
outposts = Outpost.objects.filter(managed=MANAGED_OUTPOST)
|
outposts = Outpost.objects.filter(managed=MANAGED_OUTPOST)
|
||||||
if not outposts:
|
if not outposts:
|
||||||
return None
|
return None
|
||||||
if hasattr(LOCAL, "authentik"):
|
|
||||||
LOCAL.authentik[KEY_AUTH_VIA] = "secret_key"
|
|
||||||
outpost = outposts.first()
|
outpost = outposts.first()
|
||||||
return outpost.user
|
return outpost.user
|
||||||
|
|
||||||
|
@ -7,34 +7,72 @@ from guardian.shortcuts import get_anonymous_user
|
|||||||
from rest_framework.exceptions import AuthenticationFailed
|
from rest_framework.exceptions import AuthenticationFailed
|
||||||
|
|
||||||
from authentik.api.authentication import bearer_auth
|
from authentik.api.authentication import bearer_auth
|
||||||
|
from authentik.blueprints.tests import reconcile_app
|
||||||
from authentik.core.models import USER_ATTRIBUTE_SA, Token, TokenIntents
|
from authentik.core.models import USER_ATTRIBUTE_SA, Token, TokenIntents
|
||||||
from authentik.outposts.managed import OutpostManager
|
from authentik.core.tests.utils import create_test_flow
|
||||||
|
from authentik.lib.generators import generate_id
|
||||||
|
from authentik.providers.oauth2.constants import SCOPE_AUTHENTIK_API
|
||||||
|
from authentik.providers.oauth2.models import OAuth2Provider, RefreshToken
|
||||||
|
|
||||||
|
|
||||||
class TestAPIAuth(TestCase):
|
class TestAPIAuth(TestCase):
|
||||||
"""Test API Authentication"""
|
"""Test API Authentication"""
|
||||||
|
|
||||||
def test_valid_bearer(self):
|
|
||||||
"""Test valid token"""
|
|
||||||
token = Token.objects.create(intent=TokenIntents.INTENT_API, user=get_anonymous_user())
|
|
||||||
self.assertEqual(bearer_auth(f"Bearer {token.key}".encode()), token.user)
|
|
||||||
|
|
||||||
def test_invalid_type(self):
|
def test_invalid_type(self):
|
||||||
"""Test invalid type"""
|
"""Test invalid type"""
|
||||||
with self.assertRaises(AuthenticationFailed):
|
with self.assertRaises(AuthenticationFailed):
|
||||||
bearer_auth("foo bar".encode())
|
bearer_auth("foo bar".encode())
|
||||||
|
|
||||||
|
def test_invalid_empty(self):
|
||||||
|
"""Test invalid type"""
|
||||||
|
self.assertIsNone(bearer_auth("Bearer ".encode()))
|
||||||
|
self.assertIsNone(bearer_auth("".encode()))
|
||||||
|
|
||||||
def test_invalid_no_token(self):
|
def test_invalid_no_token(self):
|
||||||
"""Test invalid with no token"""
|
"""Test invalid with no token"""
|
||||||
with self.assertRaises(AuthenticationFailed):
|
with self.assertRaises(AuthenticationFailed):
|
||||||
auth = b64encode(":abc".encode()).decode()
|
auth = b64encode(":abc".encode()).decode()
|
||||||
self.assertIsNone(bearer_auth(f"Basic :{auth}".encode()))
|
self.assertIsNone(bearer_auth(f"Basic :{auth}".encode()))
|
||||||
|
|
||||||
|
def test_bearer_valid(self):
|
||||||
|
"""Test valid token"""
|
||||||
|
token = Token.objects.create(intent=TokenIntents.INTENT_API, user=get_anonymous_user())
|
||||||
|
self.assertEqual(bearer_auth(f"Bearer {token.key}".encode()), token.user)
|
||||||
|
|
||||||
def test_managed_outpost(self):
|
def test_managed_outpost(self):
|
||||||
"""Test managed outpost"""
|
"""Test managed outpost"""
|
||||||
with self.assertRaises(AuthenticationFailed):
|
with self.assertRaises(AuthenticationFailed):
|
||||||
user = bearer_auth(f"Bearer {settings.SECRET_KEY}".encode())
|
bearer_auth(f"Bearer {settings.SECRET_KEY}".encode())
|
||||||
|
|
||||||
OutpostManager().run()
|
@reconcile_app("authentik_outposts")
|
||||||
|
def test_managed_outpost_success(self):
|
||||||
|
"""Test managed outpost"""
|
||||||
user = bearer_auth(f"Bearer {settings.SECRET_KEY}".encode())
|
user = bearer_auth(f"Bearer {settings.SECRET_KEY}".encode())
|
||||||
self.assertEqual(user.attributes[USER_ATTRIBUTE_SA], True)
|
self.assertEqual(user.attributes[USER_ATTRIBUTE_SA], True)
|
||||||
|
|
||||||
|
def test_jwt_valid(self):
|
||||||
|
"""Test valid JWT"""
|
||||||
|
provider = OAuth2Provider.objects.create(
|
||||||
|
name=generate_id(), client_id=generate_id(), authorization_flow=create_test_flow()
|
||||||
|
)
|
||||||
|
refresh = RefreshToken.objects.create(
|
||||||
|
user=get_anonymous_user(),
|
||||||
|
provider=provider,
|
||||||
|
refresh_token=generate_id(),
|
||||||
|
_scope=SCOPE_AUTHENTIK_API,
|
||||||
|
)
|
||||||
|
self.assertEqual(bearer_auth(f"Bearer {refresh.refresh_token}".encode()), refresh.user)
|
||||||
|
|
||||||
|
def test_jwt_missing_scope(self):
|
||||||
|
"""Test valid JWT"""
|
||||||
|
provider = OAuth2Provider.objects.create(
|
||||||
|
name=generate_id(), client_id=generate_id(), authorization_flow=create_test_flow()
|
||||||
|
)
|
||||||
|
refresh = RefreshToken.objects.create(
|
||||||
|
user=get_anonymous_user(),
|
||||||
|
provider=provider,
|
||||||
|
refresh_token=generate_id(),
|
||||||
|
_scope="",
|
||||||
|
)
|
||||||
|
with self.assertRaises(AuthenticationFailed):
|
||||||
|
self.assertEqual(bearer_auth(f"Bearer {refresh.refresh_token}".encode()), refresh.user)
|
||||||
|
@ -68,13 +68,12 @@ class ConfigView(APIView):
|
|||||||
caps.append(Capabilities.CAN_IMPERSONATE)
|
caps.append(Capabilities.CAN_IMPERSONATE)
|
||||||
return caps
|
return caps
|
||||||
|
|
||||||
@extend_schema(responses={200: ConfigSerializer(many=False)})
|
def get_config(self) -> ConfigSerializer:
|
||||||
def get(self, request: Request) -> Response:
|
"""Get Config"""
|
||||||
"""Retrieve public configuration options"""
|
return ConfigSerializer(
|
||||||
config = ConfigSerializer(
|
|
||||||
{
|
{
|
||||||
"error_reporting": {
|
"error_reporting": {
|
||||||
"enabled": CONFIG.y("error_reporting.enabled") and not settings.DEBUG,
|
"enabled": CONFIG.y("error_reporting.enabled"),
|
||||||
"environment": CONFIG.y("error_reporting.environment"),
|
"environment": CONFIG.y("error_reporting.environment"),
|
||||||
"send_pii": CONFIG.y("error_reporting.send_pii"),
|
"send_pii": CONFIG.y("error_reporting.send_pii"),
|
||||||
"traces_sample_rate": float(CONFIG.y("error_reporting.sample_rate", 0.4)),
|
"traces_sample_rate": float(CONFIG.y("error_reporting.sample_rate", 0.4)),
|
||||||
@ -86,4 +85,8 @@ class ConfigView(APIView):
|
|||||||
"cache_timeout_reputation": int(CONFIG.y("redis.cache_timeout_reputation")),
|
"cache_timeout_reputation": int(CONFIG.y("redis.cache_timeout_reputation")),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
return Response(config.data)
|
|
||||||
|
@extend_schema(responses={200: ConfigSerializer(many=False)})
|
||||||
|
def get(self, request: Request) -> Response:
|
||||||
|
"""Retrieve public configuration options"""
|
||||||
|
return Response(self.get_config().data)
|
||||||
|
@ -12,9 +12,10 @@ from authentik.admin.api.version import VersionView
|
|||||||
from authentik.admin.api.workers import WorkerView
|
from authentik.admin.api.workers import WorkerView
|
||||||
from authentik.api.v3.config import ConfigView
|
from authentik.api.v3.config import ConfigView
|
||||||
from authentik.api.views import APIBrowserView
|
from authentik.api.views import APIBrowserView
|
||||||
|
from authentik.blueprints.api import BlueprintInstanceViewSet
|
||||||
from authentik.core.api.applications import ApplicationViewSet
|
from authentik.core.api.applications import ApplicationViewSet
|
||||||
from authentik.core.api.authenticated_sessions import AuthenticatedSessionViewSet
|
from authentik.core.api.authenticated_sessions import AuthenticatedSessionViewSet
|
||||||
from authentik.core.api.devices import DeviceViewSet
|
from authentik.core.api.devices import AdminDeviceViewSet, DeviceViewSet
|
||||||
from authentik.core.api.groups import GroupViewSet
|
from authentik.core.api.groups import GroupViewSet
|
||||||
from authentik.core.api.propertymappings import PropertyMappingViewSet
|
from authentik.core.api.propertymappings import PropertyMappingViewSet
|
||||||
from authentik.core.api.providers import ProviderViewSet
|
from authentik.core.api.providers import ProviderViewSet
|
||||||
@ -131,6 +132,8 @@ router.register("events/notifications", NotificationViewSet)
|
|||||||
router.register("events/transports", NotificationTransportViewSet)
|
router.register("events/transports", NotificationTransportViewSet)
|
||||||
router.register("events/rules", NotificationRuleViewSet)
|
router.register("events/rules", NotificationRuleViewSet)
|
||||||
|
|
||||||
|
router.register("managed/blueprints", BlueprintInstanceViewSet)
|
||||||
|
|
||||||
router.register("sources/all", SourceViewSet)
|
router.register("sources/all", SourceViewSet)
|
||||||
router.register("sources/user_connections/all", UserSourceConnectionViewSet)
|
router.register("sources/user_connections/all", UserSourceConnectionViewSet)
|
||||||
router.register("sources/user_connections/oauth", UserOAuthSourceConnectionViewSet)
|
router.register("sources/user_connections/oauth", UserOAuthSourceConnectionViewSet)
|
||||||
@ -171,6 +174,11 @@ router.register("authenticators/sms", SMSDeviceViewSet)
|
|||||||
router.register("authenticators/static", StaticDeviceViewSet)
|
router.register("authenticators/static", StaticDeviceViewSet)
|
||||||
router.register("authenticators/totp", TOTPDeviceViewSet)
|
router.register("authenticators/totp", TOTPDeviceViewSet)
|
||||||
router.register("authenticators/webauthn", WebAuthnDeviceViewSet)
|
router.register("authenticators/webauthn", WebAuthnDeviceViewSet)
|
||||||
|
router.register(
|
||||||
|
"authenticators/admin/all",
|
||||||
|
AdminDeviceViewSet,
|
||||||
|
basename="admin-device",
|
||||||
|
)
|
||||||
router.register(
|
router.register(
|
||||||
"authenticators/admin/duo",
|
"authenticators/admin/duo",
|
||||||
DuoAdminDeviceViewSet,
|
DuoAdminDeviceViewSet,
|
||||||
|
109
authentik/blueprints/api.py
Normal file
109
authentik/blueprints/api.py
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
"""Serializer mixin for managed models"""
|
||||||
|
from drf_spectacular.utils import extend_schema, inline_serializer
|
||||||
|
from rest_framework.decorators import action
|
||||||
|
from rest_framework.exceptions import ValidationError
|
||||||
|
from rest_framework.fields import CharField, DateTimeField, JSONField
|
||||||
|
from rest_framework.permissions import IsAdminUser
|
||||||
|
from rest_framework.request import Request
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from rest_framework.serializers import ListSerializer, ModelSerializer
|
||||||
|
from rest_framework.viewsets import ModelViewSet
|
||||||
|
|
||||||
|
from authentik.api.decorators import permission_required
|
||||||
|
from authentik.blueprints.models import BlueprintInstance, BlueprintRetrievalFailed
|
||||||
|
from authentik.blueprints.v1.tasks import apply_blueprint, blueprints_find_dict
|
||||||
|
from authentik.core.api.used_by import UsedByMixin
|
||||||
|
from authentik.core.api.utils import PassiveSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class ManagedSerializer:
|
||||||
|
"""Managed Serializer"""
|
||||||
|
|
||||||
|
managed = CharField(read_only=True, allow_null=True)
|
||||||
|
|
||||||
|
|
||||||
|
class MetadataSerializer(PassiveSerializer):
|
||||||
|
"""Serializer for blueprint metadata"""
|
||||||
|
|
||||||
|
name = CharField()
|
||||||
|
labels = JSONField()
|
||||||
|
|
||||||
|
|
||||||
|
class BlueprintInstanceSerializer(ModelSerializer):
|
||||||
|
"""Info about a single blueprint instance file"""
|
||||||
|
|
||||||
|
def validate_path(self, path: str) -> str:
|
||||||
|
"""Ensure the path specified is retrievable"""
|
||||||
|
try:
|
||||||
|
BlueprintInstance(path=path).retrieve()
|
||||||
|
except BlueprintRetrievalFailed as exc:
|
||||||
|
raise ValidationError(exc) from exc
|
||||||
|
return path
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
|
||||||
|
model = BlueprintInstance
|
||||||
|
fields = [
|
||||||
|
"pk",
|
||||||
|
"name",
|
||||||
|
"path",
|
||||||
|
"context",
|
||||||
|
"last_applied",
|
||||||
|
"last_applied_hash",
|
||||||
|
"status",
|
||||||
|
"enabled",
|
||||||
|
"managed_models",
|
||||||
|
"metadata",
|
||||||
|
]
|
||||||
|
extra_kwargs = {
|
||||||
|
"status": {"read_only": True},
|
||||||
|
"last_applied": {"read_only": True},
|
||||||
|
"last_applied_hash": {"read_only": True},
|
||||||
|
"managed_models": {"read_only": True},
|
||||||
|
"metadata": {"read_only": True},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class BlueprintInstanceViewSet(UsedByMixin, ModelViewSet):
|
||||||
|
"""Blueprint instances"""
|
||||||
|
|
||||||
|
permission_classes = [IsAdminUser]
|
||||||
|
serializer_class = BlueprintInstanceSerializer
|
||||||
|
queryset = BlueprintInstance.objects.all()
|
||||||
|
search_fields = ["name", "path"]
|
||||||
|
filterset_fields = ["name", "path"]
|
||||||
|
|
||||||
|
@extend_schema(
|
||||||
|
responses={
|
||||||
|
200: ListSerializer(
|
||||||
|
child=inline_serializer(
|
||||||
|
"BlueprintFile",
|
||||||
|
fields={
|
||||||
|
"path": CharField(),
|
||||||
|
"last_m": DateTimeField(),
|
||||||
|
"hash": CharField(),
|
||||||
|
"meta": MetadataSerializer(required=False, read_only=True),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@action(detail=False, pagination_class=None, filter_backends=[])
|
||||||
|
def available(self, request: Request) -> Response:
|
||||||
|
"""Get blueprints"""
|
||||||
|
files: list[dict] = blueprints_find_dict.delay().get()
|
||||||
|
return Response(files)
|
||||||
|
|
||||||
|
@permission_required("authentik_blueprints.view_blueprintinstance")
|
||||||
|
@extend_schema(
|
||||||
|
request=None,
|
||||||
|
responses={
|
||||||
|
200: BlueprintInstanceSerializer(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
@action(detail=True, pagination_class=None, filter_backends=[], methods=["POST"])
|
||||||
|
def apply(self, request: Request, *args, **kwargs) -> Response:
|
||||||
|
"""Apply a blueprint"""
|
||||||
|
blueprint = self.get_object()
|
||||||
|
apply_blueprint.delay(str(blueprint.pk)).get()
|
||||||
|
return self.retrieve(request, *args, **kwargs)
|
22
authentik/blueprints/apps.py
Normal file
22
authentik/blueprints/apps.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
"""authentik Blueprints app"""
|
||||||
|
|
||||||
|
from authentik.blueprints.manager import ManagedAppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class AuthentikBlueprintsConfig(ManagedAppConfig):
|
||||||
|
"""authentik Blueprints app"""
|
||||||
|
|
||||||
|
name = "authentik.blueprints"
|
||||||
|
label = "authentik_blueprints"
|
||||||
|
verbose_name = "authentik Blueprints"
|
||||||
|
default = True
|
||||||
|
|
||||||
|
def reconcile_load_blueprints_v1_tasks(self):
|
||||||
|
"""Load v1 tasks"""
|
||||||
|
self.import_module("authentik.blueprints.v1.tasks")
|
||||||
|
|
||||||
|
def reconcile_blueprints_discover(self):
|
||||||
|
"""Run blueprint discovery"""
|
||||||
|
from authentik.blueprints.v1.tasks import blueprints_discover
|
||||||
|
|
||||||
|
blueprints_discover.delay()
|
28
authentik/blueprints/management/commands/apply_blueprint.py
Normal file
28
authentik/blueprints/management/commands/apply_blueprint.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
"""Apply blueprint from commandline"""
|
||||||
|
from django.core.management.base import BaseCommand, no_translations
|
||||||
|
from structlog.stdlib import get_logger
|
||||||
|
|
||||||
|
from authentik.blueprints.models import BlueprintInstance
|
||||||
|
from authentik.blueprints.v1.importer import Importer
|
||||||
|
|
||||||
|
LOGGER = get_logger()
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
"""Apply blueprint from commandline"""
|
||||||
|
|
||||||
|
@no_translations
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
"""Apply all blueprints in order, abort when one fails to import"""
|
||||||
|
for blueprint_path in options.get("blueprints", []):
|
||||||
|
content = BlueprintInstance(path=blueprint_path).retrieve()
|
||||||
|
importer = Importer(content)
|
||||||
|
valid, logs = importer.validate()
|
||||||
|
if not valid:
|
||||||
|
for log in logs:
|
||||||
|
LOGGER.debug(**log)
|
||||||
|
raise ValueError("blueprint invalid")
|
||||||
|
importer.apply()
|
||||||
|
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
parser.add_argument("blueprints", nargs="+", type=str)
|
17
authentik/blueprints/management/commands/export_blueprint.py
Normal file
17
authentik/blueprints/management/commands/export_blueprint.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
"""Export blueprint of current authentik install"""
|
||||||
|
from django.core.management.base import BaseCommand, no_translations
|
||||||
|
from structlog.stdlib import get_logger
|
||||||
|
|
||||||
|
from authentik.blueprints.v1.exporter import Exporter
|
||||||
|
|
||||||
|
LOGGER = get_logger()
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
"""Export blueprint of current authentik install"""
|
||||||
|
|
||||||
|
@no_translations
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
"""Export blueprint of current authentik install"""
|
||||||
|
exporter = Exporter()
|
||||||
|
self.stdout.write(exporter.export_to_string())
|
@ -0,0 +1,38 @@
|
|||||||
|
"""Generate JSON Schema for blueprints"""
|
||||||
|
from json import dumps, loads
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from django.apps import apps
|
||||||
|
from django.core.management.base import BaseCommand, no_translations
|
||||||
|
from structlog.stdlib import get_logger
|
||||||
|
|
||||||
|
from authentik.blueprints.v1.importer import is_model_allowed
|
||||||
|
from authentik.lib.models import SerializerModel
|
||||||
|
|
||||||
|
LOGGER = get_logger()
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
"""Generate JSON Schema for blueprints"""
|
||||||
|
|
||||||
|
schema: dict
|
||||||
|
|
||||||
|
@no_translations
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
"""Generate JSON Schema for blueprints"""
|
||||||
|
path = Path(__file__).parent.joinpath("./schema_template.json")
|
||||||
|
with open(path, "r", encoding="utf-8") as _template_file:
|
||||||
|
self.schema = loads(_template_file.read())
|
||||||
|
self.set_model_allowed()
|
||||||
|
self.stdout.write(dumps(self.schema, indent=4))
|
||||||
|
|
||||||
|
def set_model_allowed(self):
|
||||||
|
"""Set model enum"""
|
||||||
|
model_names = []
|
||||||
|
for model in apps.get_models():
|
||||||
|
if not is_model_allowed(model):
|
||||||
|
continue
|
||||||
|
if SerializerModel not in model.__mro__:
|
||||||
|
continue
|
||||||
|
model_names.append(f"{model._meta.app_label}.{model._meta.model_name}")
|
||||||
|
self.schema["properties"]["entries"]["items"]["properties"]["model"]["enum"] = model_names
|
@ -0,0 +1,90 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema",
|
||||||
|
"$id": "http://example.com/example.json",
|
||||||
|
"type": "object",
|
||||||
|
"title": "authentik Blueprint schema",
|
||||||
|
"default": {},
|
||||||
|
"required": [
|
||||||
|
"version",
|
||||||
|
"entries"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"version": {
|
||||||
|
"$id": "#/properties/version",
|
||||||
|
"type": "integer",
|
||||||
|
"title": "Blueprint version",
|
||||||
|
"default": 1
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"$id": "#/properties/metadata",
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"name"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"labels": {
|
||||||
|
"type": "object"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"context": {
|
||||||
|
"$id": "#/properties/context",
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": true
|
||||||
|
},
|
||||||
|
"entries": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$id": "#entry",
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"model",
|
||||||
|
"identifiers"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"model": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": [
|
||||||
|
"placeholder"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"attrs": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Commonly available field, may not exist on all models"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"default": {},
|
||||||
|
"additionalProperties": true
|
||||||
|
},
|
||||||
|
"identifiers": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"pk": {
|
||||||
|
"description": "Commonly available field, may not exist on all models",
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"type": "number"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"format": "uuid"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
44
authentik/blueprints/manager.py
Normal file
44
authentik/blueprints/manager.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
"""Managed objects manager"""
|
||||||
|
from importlib import import_module
|
||||||
|
from inspect import ismethod
|
||||||
|
|
||||||
|
from django.apps import AppConfig
|
||||||
|
from django.db import DatabaseError, InternalError, ProgrammingError
|
||||||
|
from structlog.stdlib import BoundLogger, get_logger
|
||||||
|
|
||||||
|
LOGGER = get_logger()
|
||||||
|
|
||||||
|
|
||||||
|
class ManagedAppConfig(AppConfig):
|
||||||
|
"""Basic reconciliation logic for apps"""
|
||||||
|
|
||||||
|
_logger: BoundLogger
|
||||||
|
|
||||||
|
def __init__(self, app_name: str, *args, **kwargs) -> None:
|
||||||
|
super().__init__(app_name, *args, **kwargs)
|
||||||
|
self._logger = get_logger().bind(app_name=app_name)
|
||||||
|
|
||||||
|
def ready(self) -> None:
|
||||||
|
self.reconcile()
|
||||||
|
return super().ready()
|
||||||
|
|
||||||
|
def import_module(self, path: str):
|
||||||
|
"""Load module"""
|
||||||
|
import_module(path)
|
||||||
|
|
||||||
|
def reconcile(self) -> None:
|
||||||
|
"""reconcile ourselves"""
|
||||||
|
prefix = "reconcile_"
|
||||||
|
for meth_name in dir(self):
|
||||||
|
meth = getattr(self, meth_name)
|
||||||
|
if not ismethod(meth):
|
||||||
|
continue
|
||||||
|
if not meth_name.startswith(prefix):
|
||||||
|
continue
|
||||||
|
name = meth_name.replace(prefix, "")
|
||||||
|
try:
|
||||||
|
self._logger.debug("Starting reconciler", name=name)
|
||||||
|
meth()
|
||||||
|
self._logger.debug("Successfully reconciled", name=name)
|
||||||
|
except (DatabaseError, ProgrammingError, InternalError) as exc:
|
||||||
|
self._logger.debug("Failed to run reconcile", name=name, exc=exc)
|
135
authentik/blueprints/migrations/0001_initial.py
Normal file
135
authentik/blueprints/migrations/0001_initial.py
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
# Generated by Django 4.0.6 on 2022-07-31 17:35
|
||||||
|
import uuid
|
||||||
|
from glob import glob
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import django.contrib.postgres.fields
|
||||||
|
from dacite import from_dict
|
||||||
|
from django.apps.registry import Apps
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
||||||
|
from yaml import load
|
||||||
|
|
||||||
|
from authentik.blueprints.v1.labels import LABEL_AUTHENTIK_SYSTEM
|
||||||
|
from authentik.lib.config import CONFIG
|
||||||
|
|
||||||
|
|
||||||
|
def check_blueprint_v1_file(BlueprintInstance: type["BlueprintInstance"], path: Path):
|
||||||
|
"""Check if blueprint should be imported"""
|
||||||
|
from authentik.blueprints.models import BlueprintInstanceStatus
|
||||||
|
from authentik.blueprints.v1.common import BlueprintLoader, BlueprintMetadata
|
||||||
|
from authentik.blueprints.v1.labels import LABEL_AUTHENTIK_INSTANTIATE
|
||||||
|
|
||||||
|
with open(path, "r", encoding="utf-8") as blueprint_file:
|
||||||
|
raw_blueprint = load(blueprint_file.read(), BlueprintLoader)
|
||||||
|
if not raw_blueprint:
|
||||||
|
return
|
||||||
|
metadata = raw_blueprint.get("metadata", None)
|
||||||
|
version = raw_blueprint.get("version", 1)
|
||||||
|
if version != 1:
|
||||||
|
return
|
||||||
|
blueprint_file.seek(0)
|
||||||
|
instance: BlueprintInstance = BlueprintInstance.objects.filter(path=path).first()
|
||||||
|
rel_path = path.relative_to(Path(CONFIG.y("blueprints_dir")))
|
||||||
|
meta = None
|
||||||
|
if metadata:
|
||||||
|
meta = from_dict(BlueprintMetadata, metadata)
|
||||||
|
if meta.labels.get(LABEL_AUTHENTIK_INSTANTIATE, "").lower() == "false":
|
||||||
|
return
|
||||||
|
if not instance:
|
||||||
|
instance = BlueprintInstance(
|
||||||
|
name=meta.name if meta else str(rel_path),
|
||||||
|
path=str(rel_path),
|
||||||
|
context={},
|
||||||
|
status=BlueprintInstanceStatus.UNKNOWN,
|
||||||
|
enabled=True,
|
||||||
|
managed_models=[],
|
||||||
|
last_applied_hash="",
|
||||||
|
metadata=metadata,
|
||||||
|
)
|
||||||
|
instance.save()
|
||||||
|
|
||||||
|
|
||||||
|
def migration_blueprint_import(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
||||||
|
BlueprintInstance = apps.get_model("authentik_blueprints", "BlueprintInstance")
|
||||||
|
Flow = apps.get_model("authentik_flows", "Flow")
|
||||||
|
|
||||||
|
db_alias = schema_editor.connection.alias
|
||||||
|
for file in glob(f"{CONFIG.y('blueprints_dir')}/**/*.yaml", recursive=True):
|
||||||
|
check_blueprint_v1_file(BlueprintInstance, Path(file))
|
||||||
|
|
||||||
|
for blueprint in BlueprintInstance.objects.using(db_alias).all():
|
||||||
|
# If we already have flows (and we should always run before flow migrations)
|
||||||
|
# then this is an existing install and we want to disable all blueprints
|
||||||
|
if Flow.objects.using(db_alias).all().exists():
|
||||||
|
blueprint.enabled = False
|
||||||
|
# System blueprints are always enabled
|
||||||
|
if blueprint.metadata.get("labels", {}).get(LABEL_AUTHENTIK_SYSTEM, "").lower() == "true":
|
||||||
|
blueprint.enabled = True
|
||||||
|
blueprint.save()
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [("authentik_flows", "0001_initial")]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="BlueprintInstance",
|
||||||
|
fields=[
|
||||||
|
("created", models.DateTimeField(auto_now_add=True)),
|
||||||
|
("last_updated", models.DateTimeField(auto_now=True)),
|
||||||
|
(
|
||||||
|
"managed",
|
||||||
|
models.TextField(
|
||||||
|
default=None,
|
||||||
|
help_text="Objects which are managed by authentik. These objects are created and updated automatically. This is flag only indicates that an object can be overwritten by migrations. You can still modify the objects via the API, but expect changes to be overwritten in a later update.",
|
||||||
|
null=True,
|
||||||
|
unique=True,
|
||||||
|
verbose_name="Managed by authentik",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"instance_uuid",
|
||||||
|
models.UUIDField(
|
||||||
|
default=uuid.uuid4, editable=False, primary_key=True, serialize=False
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("name", models.TextField()),
|
||||||
|
("metadata", models.JSONField(default=dict)),
|
||||||
|
("path", models.TextField()),
|
||||||
|
("context", models.JSONField(default=dict)),
|
||||||
|
("last_applied", models.DateTimeField(auto_now=True)),
|
||||||
|
("last_applied_hash", models.TextField()),
|
||||||
|
(
|
||||||
|
"status",
|
||||||
|
models.TextField(
|
||||||
|
choices=[
|
||||||
|
("successful", "Successful"),
|
||||||
|
("warning", "Warning"),
|
||||||
|
("error", "Error"),
|
||||||
|
("orphaned", "Orphaned"),
|
||||||
|
("unknown", "Unknown"),
|
||||||
|
],
|
||||||
|
default="unknown",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("enabled", models.BooleanField(default=True)),
|
||||||
|
(
|
||||||
|
"managed_models",
|
||||||
|
django.contrib.postgres.fields.ArrayField(
|
||||||
|
base_field=models.TextField(), default=list, size=None
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
"verbose_name": "Blueprint Instance",
|
||||||
|
"verbose_name_plural": "Blueprint Instances",
|
||||||
|
"unique_together": {("name", "path")},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
migrations.RunPython(migration_blueprint_import),
|
||||||
|
]
|
105
authentik/blueprints/models.py
Normal file
105
authentik/blueprints/models.py
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
"""Managed Object models"""
|
||||||
|
from pathlib import Path
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from django.contrib.postgres.fields import ArrayField
|
||||||
|
from django.db import models
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
from requests import RequestException
|
||||||
|
from rest_framework.serializers import Serializer
|
||||||
|
|
||||||
|
from authentik.lib.config import CONFIG
|
||||||
|
from authentik.lib.models import CreatedUpdatedModel, SerializerModel
|
||||||
|
from authentik.lib.sentry import SentryIgnoredException
|
||||||
|
from authentik.lib.utils.http import get_http_session
|
||||||
|
|
||||||
|
|
||||||
|
class BlueprintRetrievalFailed(SentryIgnoredException):
|
||||||
|
"""Error raised when we're unable to fetch the blueprint contents, whether it be HTTP files
|
||||||
|
not being accessible or local files not being readable"""
|
||||||
|
|
||||||
|
|
||||||
|
class ManagedModel(models.Model):
|
||||||
|
"""Model which can be managed by authentik exclusively"""
|
||||||
|
|
||||||
|
managed = models.TextField(
|
||||||
|
default=None,
|
||||||
|
null=True,
|
||||||
|
verbose_name=_("Managed by authentik"),
|
||||||
|
help_text=_(
|
||||||
|
(
|
||||||
|
"Objects which are managed by authentik. These objects are created and updated "
|
||||||
|
"automatically. This is flag only indicates that an object can be overwritten by "
|
||||||
|
"migrations. You can still modify the objects via the API, but expect changes "
|
||||||
|
"to be overwritten in a later update."
|
||||||
|
)
|
||||||
|
),
|
||||||
|
unique=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
|
||||||
|
abstract = True
|
||||||
|
|
||||||
|
|
||||||
|
class BlueprintInstanceStatus(models.TextChoices):
|
||||||
|
"""Instance status"""
|
||||||
|
|
||||||
|
SUCCESSFUL = "successful"
|
||||||
|
WARNING = "warning"
|
||||||
|
ERROR = "error"
|
||||||
|
ORPHANED = "orphaned"
|
||||||
|
UNKNOWN = "unknown"
|
||||||
|
|
||||||
|
|
||||||
|
class BlueprintInstance(SerializerModel, ManagedModel, CreatedUpdatedModel):
|
||||||
|
"""Instance of a single blueprint. Can be parameterized via context attribute when
|
||||||
|
blueprint in `path` has inputs."""
|
||||||
|
|
||||||
|
instance_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
||||||
|
|
||||||
|
name = models.TextField()
|
||||||
|
metadata = models.JSONField(default=dict)
|
||||||
|
path = models.TextField()
|
||||||
|
context = models.JSONField(default=dict)
|
||||||
|
last_applied = models.DateTimeField(auto_now=True)
|
||||||
|
last_applied_hash = models.TextField()
|
||||||
|
status = models.TextField(
|
||||||
|
choices=BlueprintInstanceStatus.choices, default=BlueprintInstanceStatus.UNKNOWN
|
||||||
|
)
|
||||||
|
enabled = models.BooleanField(default=True)
|
||||||
|
managed_models = ArrayField(models.TextField(), default=list)
|
||||||
|
|
||||||
|
def retrieve(self) -> str:
|
||||||
|
"""Retrieve blueprint contents"""
|
||||||
|
if urlparse(self.path).scheme != "":
|
||||||
|
try:
|
||||||
|
res = get_http_session().get(self.path, timeout=3, allow_redirects=True)
|
||||||
|
res.raise_for_status()
|
||||||
|
return res.text
|
||||||
|
except RequestException as exc:
|
||||||
|
raise BlueprintRetrievalFailed(exc) from exc
|
||||||
|
path = Path(CONFIG.y("blueprints_dir")).joinpath(Path(self.path))
|
||||||
|
with path.open("r", encoding="utf-8") as _file:
|
||||||
|
return _file.read()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serializer(self) -> Serializer:
|
||||||
|
from authentik.blueprints.api import BlueprintInstanceSerializer
|
||||||
|
|
||||||
|
return BlueprintInstanceSerializer
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return f"Blueprint Instance {self.name}"
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
|
||||||
|
verbose_name = _("Blueprint Instance")
|
||||||
|
verbose_name_plural = _("Blueprint Instances")
|
||||||
|
unique_together = (
|
||||||
|
(
|
||||||
|
"name",
|
||||||
|
"path",
|
||||||
|
),
|
||||||
|
)
|
12
authentik/blueprints/settings.py
Normal file
12
authentik/blueprints/settings.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
"""blueprint Settings"""
|
||||||
|
from celery.schedules import crontab
|
||||||
|
|
||||||
|
from authentik.lib.utils.time import fqdn_rand
|
||||||
|
|
||||||
|
CELERY_BEAT_SCHEDULE = {
|
||||||
|
"blueprints_v1_discover": {
|
||||||
|
"task": "authentik.blueprints.v1.tasks.blueprints_discover",
|
||||||
|
"schedule": crontab(minute=fqdn_rand("blueprints_v1_discover"), hour="*"),
|
||||||
|
"options": {"queue": "authentik_scheduled"},
|
||||||
|
},
|
||||||
|
}
|
48
authentik/blueprints/tests/__init__.py
Normal file
48
authentik/blueprints/tests/__init__.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
"""Blueprint helpers"""
|
||||||
|
from functools import wraps
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
|
from django.apps import apps
|
||||||
|
|
||||||
|
from authentik.blueprints.manager import ManagedAppConfig
|
||||||
|
from authentik.blueprints.models import BlueprintInstance
|
||||||
|
from authentik.lib.config import CONFIG
|
||||||
|
|
||||||
|
|
||||||
|
def apply_blueprint(*files: str):
|
||||||
|
"""Apply blueprint before test"""
|
||||||
|
|
||||||
|
from authentik.blueprints.v1.importer import Importer
|
||||||
|
|
||||||
|
def wrapper_outer(func: Callable):
|
||||||
|
"""Apply blueprint before test"""
|
||||||
|
|
||||||
|
@wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
for file in files:
|
||||||
|
content = BlueprintInstance(path=file).retrieve()
|
||||||
|
Importer(content).apply()
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
return wrapper_outer
|
||||||
|
|
||||||
|
|
||||||
|
def reconcile_app(app_name: str):
|
||||||
|
"""Re-reconcile AppConfig methods"""
|
||||||
|
|
||||||
|
def wrapper_outer(func: Callable):
|
||||||
|
"""Re-reconcile AppConfig methods"""
|
||||||
|
|
||||||
|
@wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
config = apps.get_app_config(app_name)
|
||||||
|
if isinstance(config, ManagedAppConfig):
|
||||||
|
config.reconcile()
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
return wrapper_outer
|
34
authentik/blueprints/tests/test_models.py
Normal file
34
authentik/blueprints/tests/test_models.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
"""authentik managed models tests"""
|
||||||
|
from typing import Callable, Type
|
||||||
|
|
||||||
|
from django.apps import apps
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from authentik.blueprints.v1.importer import is_model_allowed
|
||||||
|
from authentik.lib.models import SerializerModel
|
||||||
|
|
||||||
|
|
||||||
|
class TestModels(TestCase):
|
||||||
|
"""Test Models"""
|
||||||
|
|
||||||
|
|
||||||
|
def serializer_tester_factory(test_model: Type[SerializerModel]) -> Callable:
|
||||||
|
"""Test serializer"""
|
||||||
|
|
||||||
|
def tester(self: TestModels):
|
||||||
|
if test_model._meta.abstract:
|
||||||
|
return
|
||||||
|
model_class = test_model()
|
||||||
|
self.assertTrue(isinstance(model_class, SerializerModel))
|
||||||
|
self.assertIsNotNone(model_class.serializer)
|
||||||
|
|
||||||
|
return tester
|
||||||
|
|
||||||
|
|
||||||
|
for app in apps.get_app_configs():
|
||||||
|
if not app.label.startswith("authentik"):
|
||||||
|
continue
|
||||||
|
for model in app.get_models():
|
||||||
|
if not is_model_allowed(model):
|
||||||
|
continue
|
||||||
|
setattr(TestModels, f"test_{app.label}_{model.__name__}", serializer_tester_factory(model))
|
36
authentik/blueprints/tests/test_packaged.py
Normal file
36
authentik/blueprints/tests/test_packaged.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
"""test packaged blueprints"""
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
|
from django.test import TransactionTestCase
|
||||||
|
|
||||||
|
from authentik.blueprints.models import BlueprintInstance
|
||||||
|
from authentik.blueprints.tests import apply_blueprint
|
||||||
|
from authentik.blueprints.v1.importer import Importer
|
||||||
|
from authentik.tenants.models import Tenant
|
||||||
|
|
||||||
|
|
||||||
|
class TestPackaged(TransactionTestCase):
|
||||||
|
"""Empty class, test methods are added dynamically"""
|
||||||
|
|
||||||
|
@apply_blueprint("default/90-default-tenant.yaml")
|
||||||
|
def test_decorator_static(self):
|
||||||
|
"""Test @apply_blueprint decorator"""
|
||||||
|
self.assertTrue(Tenant.objects.filter(domain="authentik-default").exists())
|
||||||
|
|
||||||
|
|
||||||
|
def blueprint_tester(file_name: Path) -> Callable:
|
||||||
|
"""This is used instead of subTest for better visibility"""
|
||||||
|
|
||||||
|
def tester(self: TestPackaged):
|
||||||
|
base = Path("blueprints/")
|
||||||
|
rel_path = Path(file_name).relative_to(base)
|
||||||
|
importer = Importer(BlueprintInstance(path=str(rel_path)).retrieve())
|
||||||
|
self.assertTrue(importer.validate()[0])
|
||||||
|
self.assertTrue(importer.apply())
|
||||||
|
|
||||||
|
return tester
|
||||||
|
|
||||||
|
|
||||||
|
for blueprint_file in Path("blueprints/").glob("**/*.yaml"):
|
||||||
|
setattr(TestPackaged, f"test_blueprint_{blueprint_file}", blueprint_tester(blueprint_file))
|
@ -1,53 +1,56 @@
|
|||||||
"""Test flow transfer"""
|
"""Test blueprints v1"""
|
||||||
from json import dumps
|
|
||||||
|
|
||||||
from django.test import TransactionTestCase
|
from django.test import TransactionTestCase
|
||||||
|
|
||||||
|
from authentik.blueprints.v1.exporter import FlowExporter
|
||||||
|
from authentik.blueprints.v1.importer import Importer, transaction_rollback
|
||||||
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding
|
from authentik.flows.models import Flow, FlowDesignation, FlowStageBinding
|
||||||
from authentik.flows.transfer.common import DataclassEncoder
|
|
||||||
from authentik.flows.transfer.exporter import FlowExporter
|
|
||||||
from authentik.flows.transfer.importer import FlowImporter, transaction_rollback
|
|
||||||
from authentik.lib.generators import generate_id
|
from authentik.lib.generators import generate_id
|
||||||
from authentik.policies.expression.models import ExpressionPolicy
|
from authentik.policies.expression.models import ExpressionPolicy
|
||||||
from authentik.policies.models import PolicyBinding
|
from authentik.policies.models import PolicyBinding
|
||||||
from authentik.stages.prompt.models import FieldTypes, Prompt, PromptStage
|
from authentik.stages.prompt.models import FieldTypes, Prompt, PromptStage
|
||||||
from authentik.stages.user_login.models import UserLoginStage
|
from authentik.stages.user_login.models import UserLoginStage
|
||||||
|
|
||||||
STATIC_PROMPT_EXPORT = """{
|
STATIC_PROMPT_EXPORT = """version: 1
|
||||||
"version": 1,
|
entries:
|
||||||
"entries": [
|
- identifiers:
|
||||||
{
|
pk: cb954fd4-65a5-4ad9-b1ee-180ee9559cf4
|
||||||
"identifiers": {
|
model: authentik_stages_prompt.prompt
|
||||||
"pk": "cb954fd4-65a5-4ad9-b1ee-180ee9559cf4"
|
attrs:
|
||||||
},
|
field_key: username
|
||||||
"model": "authentik_stages_prompt.prompt",
|
label: Username
|
||||||
"attrs": {
|
type: username
|
||||||
"field_key": "username",
|
required: true
|
||||||
"label": "Username",
|
placeholder: Username
|
||||||
"type": "username",
|
order: 0
|
||||||
"required": true,
|
"""
|
||||||
"placeholder": "Username",
|
|
||||||
"order": 0
|
YAML_TAG_TESTS = """version: 1
|
||||||
}
|
context:
|
||||||
}
|
foo: bar
|
||||||
]
|
entries:
|
||||||
}"""
|
- attrs:
|
||||||
|
expression: return True
|
||||||
|
identifiers:
|
||||||
|
name: !Format [foo-%s-%s, !Context foo, !Context bar]
|
||||||
|
id: default-source-enrollment-if-username
|
||||||
|
model: authentik_policies_expression.expressionpolicy
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
class TestFlowTransfer(TransactionTestCase):
|
class TestBlueprintsV1(TransactionTestCase):
|
||||||
"""Test flow transfer"""
|
"""Test Blueprints"""
|
||||||
|
|
||||||
def test_bundle_invalid_format(self):
|
def test_blueprint_invalid_format(self):
|
||||||
"""Test bundle with invalid format"""
|
"""Test blueprint with invalid format"""
|
||||||
importer = FlowImporter('{"version": 3}')
|
importer = Importer('{"version": 3}')
|
||||||
self.assertFalse(importer.validate())
|
self.assertFalse(importer.validate()[0])
|
||||||
importer = FlowImporter(
|
importer = Importer(
|
||||||
(
|
(
|
||||||
'{"version": 1,"entries":[{"identifiers":{},"attrs":{},'
|
'{"version": 1,"entries":[{"identifiers":{},"attrs":{},'
|
||||||
'"model": "authentik_core.User"}]}'
|
'"model": "authentik_core.User"}]}'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.assertFalse(importer.validate())
|
self.assertFalse(importer.validate()[0])
|
||||||
|
|
||||||
def test_export_validate_import(self):
|
def test_export_validate_import(self):
|
||||||
"""Test export and validate it"""
|
"""Test export and validate it"""
|
||||||
@ -70,10 +73,10 @@ class TestFlowTransfer(TransactionTestCase):
|
|||||||
exporter = FlowExporter(flow)
|
exporter = FlowExporter(flow)
|
||||||
export = exporter.export()
|
export = exporter.export()
|
||||||
self.assertEqual(len(export.entries), 3)
|
self.assertEqual(len(export.entries), 3)
|
||||||
export_json = exporter.export_to_string()
|
export_yaml = exporter.export_to_string()
|
||||||
|
|
||||||
importer = FlowImporter(export_json)
|
importer = Importer(export_yaml)
|
||||||
self.assertTrue(importer.validate())
|
self.assertTrue(importer.validate()[0])
|
||||||
self.assertTrue(importer.apply())
|
self.assertTrue(importer.apply())
|
||||||
|
|
||||||
self.assertTrue(Flow.objects.filter(slug=flow_slug).exists())
|
self.assertTrue(Flow.objects.filter(slug=flow_slug).exists())
|
||||||
@ -82,18 +85,26 @@ class TestFlowTransfer(TransactionTestCase):
|
|||||||
"""Test export and import it twice"""
|
"""Test export and import it twice"""
|
||||||
count_initial = Prompt.objects.filter(field_key="username").count()
|
count_initial = Prompt.objects.filter(field_key="username").count()
|
||||||
|
|
||||||
importer = FlowImporter(STATIC_PROMPT_EXPORT)
|
importer = Importer(STATIC_PROMPT_EXPORT)
|
||||||
self.assertTrue(importer.validate())
|
self.assertTrue(importer.validate()[0])
|
||||||
self.assertTrue(importer.apply())
|
self.assertTrue(importer.apply())
|
||||||
|
|
||||||
count_before = Prompt.objects.filter(field_key="username").count()
|
count_before = Prompt.objects.filter(field_key="username").count()
|
||||||
self.assertEqual(count_initial + 1, count_before)
|
self.assertEqual(count_initial + 1, count_before)
|
||||||
|
|
||||||
importer = FlowImporter(STATIC_PROMPT_EXPORT)
|
importer = Importer(STATIC_PROMPT_EXPORT)
|
||||||
self.assertTrue(importer.apply())
|
self.assertTrue(importer.apply())
|
||||||
|
|
||||||
self.assertEqual(Prompt.objects.filter(field_key="username").count(), count_before)
|
self.assertEqual(Prompt.objects.filter(field_key="username").count(), count_before)
|
||||||
|
|
||||||
|
def test_import_yaml_tags(self):
|
||||||
|
"""Test some yaml tags"""
|
||||||
|
ExpressionPolicy.objects.filter(name="foo-foo-bar").delete()
|
||||||
|
importer = Importer(YAML_TAG_TESTS, {"bar": "baz"})
|
||||||
|
self.assertTrue(importer.validate()[0])
|
||||||
|
self.assertTrue(importer.apply())
|
||||||
|
self.assertTrue(ExpressionPolicy.objects.filter(name="foo-foo-bar"))
|
||||||
|
|
||||||
def test_export_validate_import_policies(self):
|
def test_export_validate_import_policies(self):
|
||||||
"""Test export and validate it"""
|
"""Test export and validate it"""
|
||||||
flow_slug = generate_id()
|
flow_slug = generate_id()
|
||||||
@ -116,12 +127,10 @@ class TestFlowTransfer(TransactionTestCase):
|
|||||||
PolicyBinding.objects.create(policy=flow_policy, target=fsb, order=0)
|
PolicyBinding.objects.create(policy=flow_policy, target=fsb, order=0)
|
||||||
|
|
||||||
exporter = FlowExporter(flow)
|
exporter = FlowExporter(flow)
|
||||||
export = exporter.export()
|
export_yaml = exporter.export_to_string()
|
||||||
|
|
||||||
export_json = dumps(export, cls=DataclassEncoder)
|
importer = Importer(export_yaml)
|
||||||
|
self.assertTrue(importer.validate()[0])
|
||||||
importer = FlowImporter(export_json)
|
|
||||||
self.assertTrue(importer.validate())
|
|
||||||
self.assertTrue(importer.apply())
|
self.assertTrue(importer.apply())
|
||||||
self.assertTrue(UserLoginStage.objects.filter(name=stage_name).exists())
|
self.assertTrue(UserLoginStage.objects.filter(name=stage_name).exists())
|
||||||
self.assertTrue(Flow.objects.filter(slug=flow_slug).exists())
|
self.assertTrue(Flow.objects.filter(slug=flow_slug).exists())
|
||||||
@ -161,10 +170,9 @@ class TestFlowTransfer(TransactionTestCase):
|
|||||||
FlowStageBinding.objects.create(target=flow, stage=first_stage, order=0)
|
FlowStageBinding.objects.create(target=flow, stage=first_stage, order=0)
|
||||||
|
|
||||||
exporter = FlowExporter(flow)
|
exporter = FlowExporter(flow)
|
||||||
export = exporter.export()
|
export_yaml = exporter.export_to_string()
|
||||||
export_json = dumps(export, cls=DataclassEncoder)
|
|
||||||
|
|
||||||
importer = FlowImporter(export_json)
|
importer = Importer(export_yaml)
|
||||||
|
|
||||||
self.assertTrue(importer.validate())
|
self.assertTrue(importer.validate()[0])
|
||||||
self.assertTrue(importer.apply())
|
self.assertTrue(importer.apply())
|
45
authentik/blueprints/tests/test_v1_api.py
Normal file
45
authentik/blueprints/tests/test_v1_api.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
"""Test blueprints v1 api"""
|
||||||
|
from json import loads
|
||||||
|
from tempfile import NamedTemporaryFile, mkdtemp
|
||||||
|
|
||||||
|
from django.urls import reverse
|
||||||
|
from rest_framework.test import APITestCase
|
||||||
|
from yaml import dump
|
||||||
|
|
||||||
|
from authentik.core.tests.utils import create_test_admin_user
|
||||||
|
from authentik.lib.config import CONFIG
|
||||||
|
|
||||||
|
TMP = mkdtemp("authentik-blueprints")
|
||||||
|
|
||||||
|
|
||||||
|
class TestBlueprintsV1API(APITestCase):
|
||||||
|
"""Test Blueprints API"""
|
||||||
|
|
||||||
|
def setUp(self) -> None:
|
||||||
|
self.user = create_test_admin_user()
|
||||||
|
self.client.force_login(self.user)
|
||||||
|
|
||||||
|
@CONFIG.patch("blueprints_dir", TMP)
|
||||||
|
def test_api_available(self):
|
||||||
|
"""Test valid file"""
|
||||||
|
with NamedTemporaryFile(mode="w+", suffix=".yaml", dir=TMP) as file:
|
||||||
|
file.write(
|
||||||
|
dump(
|
||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"entries": [],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
file.flush()
|
||||||
|
res = self.client.get(reverse("authentik_api:blueprintinstance-available"))
|
||||||
|
self.assertEqual(res.status_code, 200)
|
||||||
|
response = loads(res.content.decode())
|
||||||
|
self.assertEqual(len(response), 1)
|
||||||
|
self.assertEqual(
|
||||||
|
response[0]["hash"],
|
||||||
|
(
|
||||||
|
"e52bb445b03cd36057258dc9f0ce0fbed8278498ee1470e45315293e5f026d1bd1f9b352"
|
||||||
|
"6871c0003f5c07be5c3316d9d4a08444bd8fed1b3f03294e51e44522"
|
||||||
|
),
|
||||||
|
)
|
148
authentik/blueprints/tests/test_v1_tasks.py
Normal file
148
authentik/blueprints/tests/test_v1_tasks.py
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
"""Test blueprints v1 tasks"""
|
||||||
|
from hashlib import sha512
|
||||||
|
from tempfile import NamedTemporaryFile, mkdtemp
|
||||||
|
|
||||||
|
from django.test import TransactionTestCase
|
||||||
|
from yaml import dump
|
||||||
|
|
||||||
|
from authentik.blueprints.models import BlueprintInstance, BlueprintInstanceStatus
|
||||||
|
from authentik.blueprints.v1.tasks import apply_blueprint, blueprints_discover, blueprints_find
|
||||||
|
from authentik.lib.config import CONFIG
|
||||||
|
from authentik.lib.generators import generate_id
|
||||||
|
|
||||||
|
TMP = mkdtemp("authentik-blueprints")
|
||||||
|
|
||||||
|
|
||||||
|
class TestBlueprintsV1Tasks(TransactionTestCase):
|
||||||
|
"""Test Blueprints v1 Tasks"""
|
||||||
|
|
||||||
|
@CONFIG.patch("blueprints_dir", TMP)
|
||||||
|
def test_invalid_file_syntax(self):
|
||||||
|
"""Test syntactically invalid file"""
|
||||||
|
with NamedTemporaryFile(suffix=".yaml", dir=TMP) as file:
|
||||||
|
file.write(b"{")
|
||||||
|
file.flush()
|
||||||
|
blueprints = blueprints_find()
|
||||||
|
self.assertEqual(blueprints, [])
|
||||||
|
|
||||||
|
@CONFIG.patch("blueprints_dir", TMP)
|
||||||
|
def test_invalid_file_version(self):
|
||||||
|
"""Test invalid file"""
|
||||||
|
with NamedTemporaryFile(suffix=".yaml", dir=TMP) as file:
|
||||||
|
file.write(b"version: 2")
|
||||||
|
file.flush()
|
||||||
|
blueprints = blueprints_find()
|
||||||
|
self.assertEqual(blueprints, [])
|
||||||
|
|
||||||
|
@CONFIG.patch("blueprints_dir", TMP)
|
||||||
|
def test_valid(self):
|
||||||
|
"""Test valid file"""
|
||||||
|
blueprint_id = generate_id()
|
||||||
|
with NamedTemporaryFile(mode="w+", suffix=".yaml", dir=TMP) as file:
|
||||||
|
file.write(
|
||||||
|
dump(
|
||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"entries": [],
|
||||||
|
"metadata": {
|
||||||
|
"name": blueprint_id,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
file.seek(0)
|
||||||
|
file_hash = sha512(file.read().encode()).hexdigest()
|
||||||
|
file.flush()
|
||||||
|
blueprints_discover() # pylint: disable=no-value-for-parameter
|
||||||
|
instance = BlueprintInstance.objects.filter(name=blueprint_id).first()
|
||||||
|
self.assertEqual(instance.last_applied_hash, file_hash)
|
||||||
|
self.assertEqual(
|
||||||
|
instance.metadata,
|
||||||
|
{
|
||||||
|
"name": blueprint_id,
|
||||||
|
"labels": {},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
@CONFIG.patch("blueprints_dir", TMP)
|
||||||
|
def test_valid_updated(self):
|
||||||
|
"""Test valid file"""
|
||||||
|
with NamedTemporaryFile(mode="w+", suffix=".yaml", dir=TMP) as file:
|
||||||
|
file.write(
|
||||||
|
dump(
|
||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"entries": [],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
file.flush()
|
||||||
|
blueprints_discover() # pylint: disable=no-value-for-parameter
|
||||||
|
self.assertEqual(
|
||||||
|
BlueprintInstance.objects.first().last_applied_hash,
|
||||||
|
(
|
||||||
|
"e52bb445b03cd36057258dc9f0ce0fbed8278498ee1470e45315293e5f026d1b"
|
||||||
|
"d1f9b3526871c0003f5c07be5c3316d9d4a08444bd8fed1b3f03294e51e44522"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
self.assertEqual(BlueprintInstance.objects.first().metadata, {})
|
||||||
|
file.write(
|
||||||
|
dump(
|
||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"entries": [],
|
||||||
|
"metadata": {
|
||||||
|
"name": "foo",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
file.flush()
|
||||||
|
blueprints_discover() # pylint: disable=no-value-for-parameter
|
||||||
|
self.assertEqual(
|
||||||
|
BlueprintInstance.objects.first().last_applied_hash,
|
||||||
|
(
|
||||||
|
"fc62fea96067da8592bdf90927246d0ca150b045447df93b0652a0e20a8bc327"
|
||||||
|
"681510b5db37ea98759c61f9a98dd2381f46a3b5a2da69dfb45158897f14e824"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
BlueprintInstance.objects.first().metadata,
|
||||||
|
{
|
||||||
|
"name": "foo",
|
||||||
|
"labels": {},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
@CONFIG.patch("blueprints_dir", TMP)
|
||||||
|
def test_valid_disabled(self):
|
||||||
|
"""Test valid file"""
|
||||||
|
with NamedTemporaryFile(mode="w+", suffix=".yaml", dir=TMP) as file:
|
||||||
|
file.write(
|
||||||
|
dump(
|
||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"entries": [],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
file.flush()
|
||||||
|
instance: BlueprintInstance = BlueprintInstance.objects.create(
|
||||||
|
name=generate_id(),
|
||||||
|
path=file.name,
|
||||||
|
enabled=False,
|
||||||
|
status=BlueprintInstanceStatus.UNKNOWN,
|
||||||
|
)
|
||||||
|
instance.refresh_from_db()
|
||||||
|
self.assertEqual(instance.last_applied_hash, "")
|
||||||
|
self.assertEqual(
|
||||||
|
instance.status,
|
||||||
|
BlueprintInstanceStatus.UNKNOWN,
|
||||||
|
)
|
||||||
|
apply_blueprint(instance.pk) # pylint: disable=no-value-for-parameter
|
||||||
|
instance.refresh_from_db()
|
||||||
|
self.assertEqual(instance.last_applied_hash, "")
|
||||||
|
self.assertEqual(
|
||||||
|
instance.status,
|
||||||
|
BlueprintInstanceStatus.UNKNOWN,
|
||||||
|
)
|
0
authentik/blueprints/v1/__init__.py
Normal file
0
authentik/blueprints/v1/__init__.py
Normal file
255
authentik/blueprints/v1/common.py
Normal file
255
authentik/blueprints/v1/common.py
Normal file
@ -0,0 +1,255 @@
|
|||||||
|
"""transfer common classes"""
|
||||||
|
from collections import OrderedDict
|
||||||
|
from dataclasses import asdict, dataclass, field, is_dataclass
|
||||||
|
from enum import Enum
|
||||||
|
from typing import Any, Optional
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from django.apps import apps
|
||||||
|
from django.db.models import Model, Q
|
||||||
|
from rest_framework.fields import Field
|
||||||
|
from rest_framework.serializers import Serializer
|
||||||
|
from yaml import SafeDumper, SafeLoader, ScalarNode, SequenceNode
|
||||||
|
|
||||||
|
from authentik.lib.models import SerializerModel
|
||||||
|
from authentik.lib.sentry import SentryIgnoredException
|
||||||
|
from authentik.policies.models import PolicyBindingModel
|
||||||
|
|
||||||
|
|
||||||
|
def get_attrs(obj: SerializerModel) -> dict[str, Any]:
|
||||||
|
"""Get object's attributes via their serializer, and convert it to a normal dict"""
|
||||||
|
serializer: Serializer = obj.serializer(obj)
|
||||||
|
data = dict(serializer.data)
|
||||||
|
|
||||||
|
for field_name, _field in serializer.fields.items():
|
||||||
|
_field: Field
|
||||||
|
if field_name not in data:
|
||||||
|
continue
|
||||||
|
if _field.read_only:
|
||||||
|
data.pop(field_name, None)
|
||||||
|
if _field.get_initial() == data.get(field_name, None):
|
||||||
|
data.pop(field_name, None)
|
||||||
|
if field_name.endswith("_set"):
|
||||||
|
data.pop(field_name, None)
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BlueprintEntryState:
|
||||||
|
"""State of a single instance"""
|
||||||
|
|
||||||
|
instance: Optional[Model] = None
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BlueprintEntry:
|
||||||
|
"""Single entry of a blueprint"""
|
||||||
|
|
||||||
|
identifiers: dict[str, Any]
|
||||||
|
model: str
|
||||||
|
attrs: Optional[dict[str, Any]] = field(default_factory=dict)
|
||||||
|
|
||||||
|
# pylint: disable=invalid-name
|
||||||
|
id: Optional[str] = None
|
||||||
|
|
||||||
|
_state: BlueprintEntryState = field(default_factory=BlueprintEntryState)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_model(model: SerializerModel, *extra_identifier_names: str) -> "BlueprintEntry":
|
||||||
|
"""Convert a SerializerModel instance to a blueprint Entry"""
|
||||||
|
identifiers = {
|
||||||
|
"pk": model.pk,
|
||||||
|
}
|
||||||
|
all_attrs = get_attrs(model)
|
||||||
|
|
||||||
|
for extra_identifier_name in extra_identifier_names:
|
||||||
|
identifiers[extra_identifier_name] = all_attrs.pop(extra_identifier_name)
|
||||||
|
return BlueprintEntry(
|
||||||
|
identifiers=identifiers,
|
||||||
|
model=f"{model._meta.app_label}.{model._meta.model_name}",
|
||||||
|
attrs=all_attrs,
|
||||||
|
)
|
||||||
|
|
||||||
|
def tag_resolver(self, value: Any, blueprint: "Blueprint") -> Any:
|
||||||
|
"""Check if we have any special tags that need handling"""
|
||||||
|
if isinstance(value, YAMLTag):
|
||||||
|
return value.resolve(self, blueprint)
|
||||||
|
if isinstance(value, dict):
|
||||||
|
for key, inner_value in value.items():
|
||||||
|
value[key] = self.tag_resolver(inner_value, blueprint)
|
||||||
|
if isinstance(value, list):
|
||||||
|
for idx, inner_value in enumerate(value):
|
||||||
|
value[idx] = self.tag_resolver(inner_value, blueprint)
|
||||||
|
return value
|
||||||
|
|
||||||
|
def get_attrs(self, blueprint: "Blueprint") -> dict[str, Any]:
|
||||||
|
"""Get attributes of this entry, with all yaml tags resolved"""
|
||||||
|
return self.tag_resolver(self.attrs, blueprint)
|
||||||
|
|
||||||
|
def get_identifiers(self, blueprint: "Blueprint") -> dict[str, Any]:
|
||||||
|
"""Get attributes of this entry, with all yaml tags resolved"""
|
||||||
|
return self.tag_resolver(self.identifiers, blueprint)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BlueprintMetadata:
|
||||||
|
"""Optional blueprint metadata"""
|
||||||
|
|
||||||
|
name: str
|
||||||
|
labels: dict[str, str] = field(default_factory=dict)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Blueprint:
|
||||||
|
"""Dataclass used for a full export"""
|
||||||
|
|
||||||
|
version: int = field(default=1)
|
||||||
|
entries: list[BlueprintEntry] = field(default_factory=list)
|
||||||
|
|
||||||
|
metadata: Optional[BlueprintMetadata] = field(default=None)
|
||||||
|
context: Optional[dict] = field(default_factory=dict)
|
||||||
|
|
||||||
|
|
||||||
|
class YAMLTag:
|
||||||
|
"""Base class for all YAML Tags"""
|
||||||
|
|
||||||
|
def resolve(self, entry: BlueprintEntry, blueprint: Blueprint) -> Any:
|
||||||
|
"""Implement yaml tag logic"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
class KeyOf(YAMLTag):
|
||||||
|
"""Reference another object by their ID"""
|
||||||
|
|
||||||
|
id_from: str
|
||||||
|
|
||||||
|
# pylint: disable=unused-argument
|
||||||
|
def __init__(self, loader: "BlueprintLoader", node: ScalarNode) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.id_from = node.value
|
||||||
|
|
||||||
|
def resolve(self, entry: BlueprintEntry, blueprint: Blueprint) -> Any:
|
||||||
|
for _entry in blueprint.entries:
|
||||||
|
if _entry.id == self.id_from and _entry._state.instance:
|
||||||
|
# Special handling for PolicyBindingModels, as they'll have a different PK
|
||||||
|
# which is used when creating policy bindings
|
||||||
|
if (
|
||||||
|
isinstance(_entry._state.instance, PolicyBindingModel)
|
||||||
|
and entry.model.lower() == "authentik_policies.policybinding"
|
||||||
|
):
|
||||||
|
return _entry._state.instance.pbm_uuid
|
||||||
|
return _entry._state.instance.pk
|
||||||
|
raise ValueError(
|
||||||
|
f"KeyOf: failed to find entry with `id` of `{self.id_from}` and a model instance"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Context(YAMLTag):
|
||||||
|
"""Lookup key from instance context"""
|
||||||
|
|
||||||
|
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:
|
||||||
|
value = self.default
|
||||||
|
if self.key in blueprint.context:
|
||||||
|
value = blueprint.context[self.key]
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
class Format(YAMLTag):
|
||||||
|
"""Format a string"""
|
||||||
|
|
||||||
|
format_string: str
|
||||||
|
args: list[Any]
|
||||||
|
|
||||||
|
# pylint: disable=unused-argument
|
||||||
|
def __init__(self, loader: "BlueprintLoader", node: SequenceNode) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.format_string = node.value[0].value
|
||||||
|
self.args = []
|
||||||
|
for raw_node in node.value[1:]:
|
||||||
|
self.args.append(raw_node.value)
|
||||||
|
|
||||||
|
def resolve(self, entry: BlueprintEntry, blueprint: Blueprint) -> Any:
|
||||||
|
try:
|
||||||
|
return self.format_string % tuple(self.args)
|
||||||
|
except TypeError as exc:
|
||||||
|
raise EntryInvalidError(exc)
|
||||||
|
|
||||||
|
|
||||||
|
class Find(YAMLTag):
|
||||||
|
"""Find any object"""
|
||||||
|
|
||||||
|
model_name: str
|
||||||
|
conditions: list[list]
|
||||||
|
|
||||||
|
model_class: type[Model]
|
||||||
|
|
||||||
|
def __init__(self, loader: "BlueprintLoader", node: SequenceNode) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.model_name = node.value[0].value
|
||||||
|
self.model_class = apps.get_model(*self.model_name.split("."))
|
||||||
|
self.conditions = []
|
||||||
|
for raw_node in node.value[1:]:
|
||||||
|
values = []
|
||||||
|
for node_values in raw_node.value:
|
||||||
|
values.append(loader.construct_object(node_values))
|
||||||
|
self.conditions.append(values)
|
||||||
|
|
||||||
|
def resolve(self, entry: BlueprintEntry, blueprint: Blueprint) -> Any:
|
||||||
|
query = Q()
|
||||||
|
for cond in self.conditions:
|
||||||
|
query &= Q(**{cond[0]: cond[1]})
|
||||||
|
instance = self.model_class.objects.filter(query).first()
|
||||||
|
if instance:
|
||||||
|
return instance.pk
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class BlueprintDumper(SafeDumper):
|
||||||
|
"""Dump dataclasses to yaml"""
|
||||||
|
|
||||||
|
default_flow_style = False
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.add_representer(UUID, lambda self, data: self.represent_str(str(data)))
|
||||||
|
self.add_representer(OrderedDict, lambda self, data: self.represent_dict(dict(data)))
|
||||||
|
self.add_representer(Enum, lambda self, data: self.represent_str(data.value))
|
||||||
|
|
||||||
|
def represent(self, data) -> None:
|
||||||
|
if is_dataclass(data):
|
||||||
|
|
||||||
|
def factory(items):
|
||||||
|
final_dict = dict(items)
|
||||||
|
final_dict.pop("_state", None)
|
||||||
|
return final_dict
|
||||||
|
|
||||||
|
data = asdict(data, dict_factory=factory)
|
||||||
|
return super().represent(data)
|
||||||
|
|
||||||
|
|
||||||
|
class BlueprintLoader(SafeLoader):
|
||||||
|
"""Loader for blueprints with custom tag support"""
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.add_constructor("!KeyOf", KeyOf)
|
||||||
|
self.add_constructor("!Find", Find)
|
||||||
|
self.add_constructor("!Context", Context)
|
||||||
|
self.add_constructor("!Format", Format)
|
||||||
|
|
||||||
|
|
||||||
|
class EntryInvalidError(SentryIgnoredException):
|
||||||
|
"""Error raised when an entry is invalid"""
|
140
authentik/blueprints/v1/exporter.py
Normal file
140
authentik/blueprints/v1/exporter.py
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
"""Blueprint exporter"""
|
||||||
|
from typing import Iterator
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from django.apps import apps
|
||||||
|
from django.db.models import Q
|
||||||
|
from django.utils.timezone import now
|
||||||
|
from django.utils.translation import gettext as _
|
||||||
|
from yaml import dump
|
||||||
|
|
||||||
|
from authentik.blueprints.v1.common import (
|
||||||
|
Blueprint,
|
||||||
|
BlueprintDumper,
|
||||||
|
BlueprintEntry,
|
||||||
|
BlueprintMetadata,
|
||||||
|
)
|
||||||
|
from authentik.blueprints.v1.importer import is_model_allowed
|
||||||
|
from authentik.blueprints.v1.labels import LABEL_AUTHENTIK_GENERATED
|
||||||
|
from authentik.flows.models import Flow, FlowStageBinding, Stage
|
||||||
|
from authentik.lib.models import SerializerModel
|
||||||
|
from authentik.policies.models import Policy, PolicyBinding
|
||||||
|
from authentik.stages.prompt.models import PromptStage
|
||||||
|
|
||||||
|
|
||||||
|
class Exporter:
|
||||||
|
"""Export flow with attached stages into yaml"""
|
||||||
|
|
||||||
|
excluded_models = []
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.excluded_models = []
|
||||||
|
|
||||||
|
def get_entries(self) -> Iterator[BlueprintEntry]:
|
||||||
|
"""Get blueprint entries"""
|
||||||
|
for model in apps.get_models():
|
||||||
|
if not is_model_allowed(model):
|
||||||
|
continue
|
||||||
|
if model in self.excluded_models:
|
||||||
|
continue
|
||||||
|
if SerializerModel not in model.__mro__:
|
||||||
|
continue
|
||||||
|
for obj in model.objects.all():
|
||||||
|
yield BlueprintEntry.from_model(obj)
|
||||||
|
|
||||||
|
def _pre_export(self, blueprint: Blueprint):
|
||||||
|
"""Hook to run anything pre-export"""
|
||||||
|
|
||||||
|
def export(self) -> Blueprint:
|
||||||
|
"""Create a list of all objects and create a blueprint"""
|
||||||
|
blueprint = Blueprint()
|
||||||
|
self._pre_export(blueprint)
|
||||||
|
blueprint.metadata = BlueprintMetadata(
|
||||||
|
name=_("authentik Export - %(date)s" % {"date": str(now())}),
|
||||||
|
labels={
|
||||||
|
LABEL_AUTHENTIK_GENERATED: "true",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
blueprint.entries = list(self.get_entries())
|
||||||
|
return blueprint
|
||||||
|
|
||||||
|
def export_to_string(self) -> str:
|
||||||
|
"""Call export and convert it to yaml"""
|
||||||
|
blueprint = self.export()
|
||||||
|
return dump(blueprint, Dumper=BlueprintDumper)
|
||||||
|
|
||||||
|
|
||||||
|
class FlowExporter(Exporter):
|
||||||
|
"""Exporter customised to only return objects related to `flow`"""
|
||||||
|
|
||||||
|
flow: Flow
|
||||||
|
with_policies: bool
|
||||||
|
with_stage_prompts: bool
|
||||||
|
|
||||||
|
pbm_uuids: list[UUID]
|
||||||
|
|
||||||
|
def __init__(self, flow: Flow):
|
||||||
|
super().__init__()
|
||||||
|
self.flow = flow
|
||||||
|
self.with_policies = True
|
||||||
|
self.with_stage_prompts = True
|
||||||
|
|
||||||
|
def _pre_export(self, blueprint: Blueprint):
|
||||||
|
if not self.with_policies:
|
||||||
|
return
|
||||||
|
self.pbm_uuids = [self.flow.pbm_uuid]
|
||||||
|
self.pbm_uuids += FlowStageBinding.objects.filter(target=self.flow).values_list(
|
||||||
|
"pbm_uuid", flat=True
|
||||||
|
)
|
||||||
|
|
||||||
|
def walk_stages(self) -> Iterator[BlueprintEntry]:
|
||||||
|
"""Convert all stages attached to self.flow into BlueprintEntry objects"""
|
||||||
|
stages = Stage.objects.filter(flow=self.flow).select_related().select_subclasses()
|
||||||
|
for stage in stages:
|
||||||
|
if isinstance(stage, PromptStage):
|
||||||
|
pass
|
||||||
|
yield BlueprintEntry.from_model(stage, "name")
|
||||||
|
|
||||||
|
def walk_stage_bindings(self) -> Iterator[BlueprintEntry]:
|
||||||
|
"""Convert all bindings attached to self.flow into BlueprintEntry objects"""
|
||||||
|
bindings = FlowStageBinding.objects.filter(target=self.flow).select_related()
|
||||||
|
for binding in bindings:
|
||||||
|
yield BlueprintEntry.from_model(binding, "target", "stage", "order")
|
||||||
|
|
||||||
|
def walk_policies(self) -> Iterator[BlueprintEntry]:
|
||||||
|
"""Walk over all policies. This is done at the beginning of the export for stages that have
|
||||||
|
a direct foreign key to a policy."""
|
||||||
|
# Special case for PromptStage as that has a direct M2M to policy, we have to ensure
|
||||||
|
# all policies referenced in there we also include here
|
||||||
|
prompt_stages = PromptStage.objects.filter(flow=self.flow).values_list("pk", flat=True)
|
||||||
|
query = Q(bindings__in=self.pbm_uuids) | Q(promptstage__in=prompt_stages)
|
||||||
|
policies = Policy.objects.filter(query).select_related()
|
||||||
|
for policy in policies:
|
||||||
|
yield BlueprintEntry.from_model(policy)
|
||||||
|
|
||||||
|
def walk_policy_bindings(self) -> Iterator[BlueprintEntry]:
|
||||||
|
"""Walk over all policybindings relative to us. This is run at the end of the export, as
|
||||||
|
we are sure all objects exist now."""
|
||||||
|
bindings = PolicyBinding.objects.filter(target__in=self.pbm_uuids).select_related()
|
||||||
|
for binding in bindings:
|
||||||
|
yield BlueprintEntry.from_model(binding, "policy", "target", "order")
|
||||||
|
|
||||||
|
def walk_stage_prompts(self) -> Iterator[BlueprintEntry]:
|
||||||
|
"""Walk over all prompts associated with any PromptStages"""
|
||||||
|
prompt_stages = PromptStage.objects.filter(flow=self.flow)
|
||||||
|
for stage in prompt_stages:
|
||||||
|
for prompt in stage.fields.all():
|
||||||
|
yield BlueprintEntry.from_model(prompt)
|
||||||
|
|
||||||
|
def get_entries(self) -> Iterator[BlueprintEntry]:
|
||||||
|
entries = []
|
||||||
|
entries.append(BlueprintEntry.from_model(self.flow, "slug"))
|
||||||
|
if self.with_stage_prompts:
|
||||||
|
entries.extend(self.walk_stage_prompts())
|
||||||
|
if self.with_policies:
|
||||||
|
entries.extend(self.walk_policies())
|
||||||
|
entries.extend(self.walk_stages())
|
||||||
|
entries.extend(self.walk_stage_bindings())
|
||||||
|
if self.with_policies:
|
||||||
|
entries.extend(self.walk_policy_bindings())
|
||||||
|
return entries
|
@ -1,11 +1,11 @@
|
|||||||
"""Flow importer"""
|
"""Blueprint importer"""
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from json import loads
|
from typing import Any, Optional
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from dacite import from_dict
|
from dacite import from_dict
|
||||||
from dacite.exceptions import DaciteError
|
from dacite.exceptions import DaciteError
|
||||||
|
from deepmerge import always_merger
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db.models import Model
|
from django.db.models import Model
|
||||||
@ -14,14 +14,52 @@ from django.db.utils import IntegrityError
|
|||||||
from rest_framework.exceptions import ValidationError
|
from rest_framework.exceptions import ValidationError
|
||||||
from rest_framework.serializers import BaseSerializer, Serializer
|
from rest_framework.serializers import BaseSerializer, Serializer
|
||||||
from structlog.stdlib import BoundLogger, get_logger
|
from structlog.stdlib import BoundLogger, get_logger
|
||||||
|
from structlog.testing import capture_logs
|
||||||
|
from structlog.types import EventDict
|
||||||
|
from yaml import load
|
||||||
|
|
||||||
from authentik.flows.models import Flow, FlowStageBinding, Stage
|
from authentik.blueprints.v1.common import (
|
||||||
from authentik.flows.transfer.common import EntryInvalidError, FlowBundle, FlowBundleEntry
|
Blueprint,
|
||||||
|
BlueprintEntry,
|
||||||
|
BlueprintEntryState,
|
||||||
|
BlueprintLoader,
|
||||||
|
EntryInvalidError,
|
||||||
|
)
|
||||||
|
from authentik.core.models import (
|
||||||
|
AuthenticatedSession,
|
||||||
|
PropertyMapping,
|
||||||
|
Provider,
|
||||||
|
Source,
|
||||||
|
UserSourceConnection,
|
||||||
|
)
|
||||||
|
from authentik.flows.models import Stage
|
||||||
from authentik.lib.models import SerializerModel
|
from authentik.lib.models import SerializerModel
|
||||||
from authentik.policies.models import Policy, PolicyBinding
|
from authentik.outposts.models import OutpostServiceConnection
|
||||||
from authentik.stages.prompt.models import Prompt
|
from authentik.policies.models import Policy, PolicyBindingModel
|
||||||
|
|
||||||
ALLOWED_MODELS = (Flow, FlowStageBinding, Stage, Policy, PolicyBinding, Prompt)
|
|
||||||
|
def is_model_allowed(model: type[Model]) -> bool:
|
||||||
|
"""Check if model is allowed"""
|
||||||
|
# pylint: disable=imported-auth-user
|
||||||
|
from django.contrib.auth.models import Group as DjangoGroup
|
||||||
|
from django.contrib.auth.models import User as DjangoUser
|
||||||
|
|
||||||
|
excluded_models = (
|
||||||
|
DjangoUser,
|
||||||
|
DjangoGroup,
|
||||||
|
# Base classes
|
||||||
|
Provider,
|
||||||
|
Source,
|
||||||
|
PropertyMapping,
|
||||||
|
UserSourceConnection,
|
||||||
|
Stage,
|
||||||
|
OutpostServiceConnection,
|
||||||
|
Policy,
|
||||||
|
PolicyBindingModel,
|
||||||
|
# Classes that have other dependencies
|
||||||
|
AuthenticatedSession,
|
||||||
|
)
|
||||||
|
return model not in excluded_models
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
@ -34,19 +72,29 @@ def transaction_rollback():
|
|||||||
atomic.__exit__(IntegrityError, None, None)
|
atomic.__exit__(IntegrityError, None, None)
|
||||||
|
|
||||||
|
|
||||||
class FlowImporter:
|
class Importer:
|
||||||
"""Import Flow from json"""
|
"""Import Blueprint from YAML"""
|
||||||
|
|
||||||
logger: BoundLogger
|
logger: BoundLogger
|
||||||
|
|
||||||
def __init__(self, json_input: str):
|
def __init__(self, yaml_input: str, context: Optional[dict] = None):
|
||||||
self.__pk_map: dict[Any, Model] = {}
|
self.__pk_map: dict[Any, Model] = {}
|
||||||
self.logger = get_logger()
|
self.logger = get_logger()
|
||||||
import_dict = loads(json_input)
|
import_dict = load(yaml_input, BlueprintLoader)
|
||||||
try:
|
try:
|
||||||
self.__import = from_dict(FlowBundle, import_dict)
|
self.__import = from_dict(Blueprint, import_dict)
|
||||||
except DaciteError as exc:
|
except DaciteError as exc:
|
||||||
raise EntryInvalidError from exc
|
raise EntryInvalidError from exc
|
||||||
|
context = {}
|
||||||
|
always_merger.merge(context, self.__import.context)
|
||||||
|
if context:
|
||||||
|
always_merger.merge(context, context)
|
||||||
|
self.__import.context = context
|
||||||
|
|
||||||
|
@property
|
||||||
|
def blueprint(self) -> Blueprint:
|
||||||
|
"""Get imported blueprint"""
|
||||||
|
return self.__import
|
||||||
|
|
||||||
def __update_pks_for_attrs(self, attrs: dict[str, Any]) -> dict[str, Any]:
|
def __update_pks_for_attrs(self, attrs: dict[str, Any]) -> dict[str, Any]:
|
||||||
"""Replace any value if it is a known primary key of an other object"""
|
"""Replace any value if it is a known primary key of an other object"""
|
||||||
@ -75,7 +123,9 @@ class FlowImporter:
|
|||||||
"""Generate an or'd query from all identifiers in an entry"""
|
"""Generate an or'd query from all identifiers in an entry"""
|
||||||
# Since identifiers can also be pk-references to other objects (see FlowStageBinding)
|
# Since identifiers can also be pk-references to other objects (see FlowStageBinding)
|
||||||
# we have to ensure those references are also replaced
|
# we have to ensure those references are also replaced
|
||||||
main_query = Q(pk=attrs["pk"])
|
main_query = Q()
|
||||||
|
if "pk" in attrs:
|
||||||
|
main_query = Q(pk=attrs["pk"])
|
||||||
sub_query = Q()
|
sub_query = Q()
|
||||||
for identifier, value in attrs.items():
|
for identifier, value in attrs.items():
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
@ -85,19 +135,22 @@ class FlowImporter:
|
|||||||
sub_query &= Q(**{identifier: value})
|
sub_query &= Q(**{identifier: value})
|
||||||
return main_query | sub_query
|
return main_query | sub_query
|
||||||
|
|
||||||
def _validate_single(self, entry: FlowBundleEntry) -> BaseSerializer:
|
def _validate_single(self, entry: BlueprintEntry) -> BaseSerializer:
|
||||||
"""Validate a single entry"""
|
"""Validate a single entry"""
|
||||||
model_app_label, model_name = entry.model.split(".")
|
model_app_label, model_name = entry.model.split(".")
|
||||||
model: type[SerializerModel] = apps.get_model(model_app_label, model_name)
|
model: type[SerializerModel] = apps.get_model(model_app_label, model_name)
|
||||||
if not isinstance(model(), ALLOWED_MODELS):
|
# Don't use isinstance since we don't want to check for inheritance
|
||||||
|
if not is_model_allowed(model):
|
||||||
raise EntryInvalidError(f"Model {model} not allowed")
|
raise EntryInvalidError(f"Model {model} not allowed")
|
||||||
|
if entry.identifiers == {}:
|
||||||
|
raise EntryInvalidError("No identifiers")
|
||||||
|
|
||||||
# If we try to validate without referencing a possible instance
|
# If we try to validate without referencing a possible instance
|
||||||
# we'll get a duplicate error, hence we load the model here and return
|
# we'll get a duplicate error, hence we load the model here and return
|
||||||
# the full serializer for later usage
|
# the full serializer for later usage
|
||||||
# Because a model might have multiple unique columns, we chain all identifiers together
|
# Because a model might have multiple unique columns, we chain all identifiers together
|
||||||
# to create an OR query.
|
# to create an OR query.
|
||||||
updated_identifiers = self.__update_pks_for_attrs(entry.identifiers)
|
updated_identifiers = self.__update_pks_for_attrs(entry.get_identifiers(self.__import))
|
||||||
for key, value in list(updated_identifiers.items()):
|
for key, value in list(updated_identifiers.items()):
|
||||||
if isinstance(value, dict) and "pk" in value:
|
if isinstance(value, dict) and "pk" in value:
|
||||||
del updated_identifiers[key]
|
del updated_identifiers[key]
|
||||||
@ -114,6 +167,7 @@ class FlowImporter:
|
|||||||
pk=model_instance.pk,
|
pk=model_instance.pk,
|
||||||
)
|
)
|
||||||
serializer_kwargs["instance"] = model_instance
|
serializer_kwargs["instance"] = model_instance
|
||||||
|
serializer_kwargs["partial"] = True
|
||||||
else:
|
else:
|
||||||
self.logger.debug("initialise new instance", model=model, **updated_identifiers)
|
self.logger.debug("initialise new instance", model=model, **updated_identifiers)
|
||||||
model_instance = model()
|
model_instance = model()
|
||||||
@ -121,7 +175,7 @@ class FlowImporter:
|
|||||||
if "pk" in updated_identifiers:
|
if "pk" in updated_identifiers:
|
||||||
model_instance.pk = updated_identifiers["pk"]
|
model_instance.pk = updated_identifiers["pk"]
|
||||||
serializer_kwargs["instance"] = model_instance
|
serializer_kwargs["instance"] = model_instance
|
||||||
full_data = self.__update_pks_for_attrs(entry.attrs)
|
full_data = self.__update_pks_for_attrs(entry.get_attrs(self.__import))
|
||||||
full_data.update(updated_identifiers)
|
full_data.update(updated_identifiers)
|
||||||
serializer_kwargs["data"] = full_data
|
serializer_kwargs["data"] = full_data
|
||||||
|
|
||||||
@ -133,7 +187,7 @@ class FlowImporter:
|
|||||||
return serializer
|
return serializer
|
||||||
|
|
||||||
def apply(self) -> bool:
|
def apply(self) -> bool:
|
||||||
"""Apply (create/update) flow json, in database transaction"""
|
"""Apply (create/update) models yaml, in database transaction"""
|
||||||
try:
|
try:
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
if not self._apply_models():
|
if not self._apply_models():
|
||||||
@ -146,10 +200,9 @@ class FlowImporter:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def _apply_models(self) -> bool:
|
def _apply_models(self) -> bool:
|
||||||
"""Apply (create/update) flow json"""
|
"""Apply (create/update) models yaml"""
|
||||||
self.__pk_map = {}
|
self.__pk_map = {}
|
||||||
entries = deepcopy(self.__import.entries)
|
for entry in self.__import.entries:
|
||||||
for entry in entries:
|
|
||||||
model_app_label, model_name = entry.model.split(".")
|
model_app_label, model_name = entry.model.split(".")
|
||||||
try:
|
try:
|
||||||
model: SerializerModel = apps.get_model(model_app_label, model_name)
|
model: SerializerModel = apps.get_model(model_app_label, model_name)
|
||||||
@ -162,23 +215,32 @@ class FlowImporter:
|
|||||||
try:
|
try:
|
||||||
serializer = self._validate_single(entry)
|
serializer = self._validate_single(entry)
|
||||||
except EntryInvalidError as exc:
|
except EntryInvalidError as exc:
|
||||||
self.logger.warning("entry not valid", entry=entry, error=exc)
|
self.logger.warning("entry invalid", entry=entry, error=exc)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
model = serializer.save()
|
model = serializer.save()
|
||||||
self.__pk_map[entry.identifiers["pk"]] = model.pk
|
if "pk" in entry.identifiers:
|
||||||
|
self.__pk_map[entry.identifiers["pk"]] = model.pk
|
||||||
|
entry._state = BlueprintEntryState(model)
|
||||||
self.logger.debug("updated model", model=model, pk=model.pk)
|
self.logger.debug("updated model", model=model, pk=model.pk)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def validate(self) -> bool:
|
def validate(self) -> tuple[bool, list[EventDict]]:
|
||||||
"""Validate loaded flow export, ensure all models are allowed
|
"""Validate loaded blueprint export, ensure all models are allowed
|
||||||
and serializers have no errors"""
|
and serializers have no errors"""
|
||||||
self.logger.debug("Starting flow import validation")
|
self.logger.debug("Starting blueprint import validation")
|
||||||
|
orig_import = deepcopy(self.__import)
|
||||||
if self.__import.version != 1:
|
if self.__import.version != 1:
|
||||||
self.logger.warning("Invalid bundle version")
|
self.logger.warning("Invalid blueprint version")
|
||||||
return False
|
return False, []
|
||||||
with transaction_rollback():
|
with (
|
||||||
|
transaction_rollback(),
|
||||||
|
capture_logs() as logs,
|
||||||
|
):
|
||||||
successful = self._apply_models()
|
successful = self._apply_models()
|
||||||
if not successful:
|
if not successful:
|
||||||
self.logger.debug("Flow validation failed")
|
self.logger.debug("blueprint validation failed")
|
||||||
return successful
|
for log in logs:
|
||||||
|
self.logger.debug(**log)
|
||||||
|
self.__import = orig_import
|
||||||
|
return successful, logs
|
5
authentik/blueprints/v1/labels.py
Normal file
5
authentik/blueprints/v1/labels.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
"""Blueprint labels"""
|
||||||
|
|
||||||
|
LABEL_AUTHENTIK_SYSTEM = "blueprints.goauthentik.io/system"
|
||||||
|
LABEL_AUTHENTIK_INSTANTIATE = "blueprints.goauthentik.io/instantiate"
|
||||||
|
LABEL_AUTHENTIK_GENERATED = "blueprints.goauthentik.io/generated"
|
174
authentik/blueprints/v1/tasks.py
Normal file
174
authentik/blueprints/v1/tasks.py
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
"""v1 blueprints tasks"""
|
||||||
|
from dataclasses import asdict, dataclass, field
|
||||||
|
from hashlib import sha512
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from dacite import from_dict
|
||||||
|
from django.db import DatabaseError, InternalError, ProgrammingError
|
||||||
|
from django.utils.timezone import now
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
from structlog.stdlib import get_logger
|
||||||
|
from yaml import load
|
||||||
|
from yaml.error import YAMLError
|
||||||
|
|
||||||
|
from authentik.blueprints.models import (
|
||||||
|
BlueprintInstance,
|
||||||
|
BlueprintInstanceStatus,
|
||||||
|
BlueprintRetrievalFailed,
|
||||||
|
)
|
||||||
|
from authentik.blueprints.v1.common import BlueprintLoader, BlueprintMetadata
|
||||||
|
from authentik.blueprints.v1.importer import Importer
|
||||||
|
from authentik.blueprints.v1.labels import LABEL_AUTHENTIK_INSTANTIATE
|
||||||
|
from authentik.events.monitored_tasks import (
|
||||||
|
MonitoredTask,
|
||||||
|
TaskResult,
|
||||||
|
TaskResultStatus,
|
||||||
|
prefill_task,
|
||||||
|
)
|
||||||
|
from authentik.events.utils import sanitize_dict
|
||||||
|
from authentik.lib.config import CONFIG
|
||||||
|
from authentik.root.celery import CELERY_APP
|
||||||
|
|
||||||
|
LOGGER = get_logger()
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class BlueprintFile:
|
||||||
|
"""Basic info about a blueprint file"""
|
||||||
|
|
||||||
|
path: str
|
||||||
|
version: int
|
||||||
|
hash: str
|
||||||
|
last_m: int
|
||||||
|
meta: Optional[BlueprintMetadata] = field(default=None)
|
||||||
|
|
||||||
|
|
||||||
|
@CELERY_APP.task(
|
||||||
|
throws=(DatabaseError, ProgrammingError, InternalError),
|
||||||
|
)
|
||||||
|
def blueprints_find_dict():
|
||||||
|
"""Find blueprints as `blueprints_find` does, but return a safe dict"""
|
||||||
|
blueprints = []
|
||||||
|
for blueprint in blueprints_find():
|
||||||
|
blueprints.append(sanitize_dict(asdict(blueprint)))
|
||||||
|
return blueprints
|
||||||
|
|
||||||
|
|
||||||
|
def blueprints_find():
|
||||||
|
"""Find blueprints and return valid ones"""
|
||||||
|
blueprints = []
|
||||||
|
root = Path(CONFIG.y("blueprints_dir"))
|
||||||
|
for file in root.glob("**/*.yaml"):
|
||||||
|
path = Path(file)
|
||||||
|
LOGGER.debug("found blueprint", path=str(path))
|
||||||
|
with open(path, "r", encoding="utf-8") as blueprint_file:
|
||||||
|
try:
|
||||||
|
raw_blueprint = load(blueprint_file.read(), BlueprintLoader)
|
||||||
|
except YAMLError as exc:
|
||||||
|
raw_blueprint = None
|
||||||
|
LOGGER.warning("failed to parse blueprint", exc=exc, path=str(path))
|
||||||
|
if not raw_blueprint:
|
||||||
|
continue
|
||||||
|
metadata = raw_blueprint.get("metadata", None)
|
||||||
|
version = raw_blueprint.get("version", 1)
|
||||||
|
if version != 1:
|
||||||
|
LOGGER.warning("invalid blueprint version", version=version, path=str(path))
|
||||||
|
continue
|
||||||
|
file_hash = sha512(path.read_bytes()).hexdigest()
|
||||||
|
blueprint = BlueprintFile(path.relative_to(root), version, file_hash, path.stat().st_mtime)
|
||||||
|
blueprint.meta = from_dict(BlueprintMetadata, metadata) if metadata else None
|
||||||
|
blueprints.append(blueprint)
|
||||||
|
LOGGER.info(
|
||||||
|
"parsed & loaded blueprint",
|
||||||
|
hash=file_hash,
|
||||||
|
path=str(path),
|
||||||
|
)
|
||||||
|
return blueprints
|
||||||
|
|
||||||
|
|
||||||
|
@CELERY_APP.task(
|
||||||
|
throws=(DatabaseError, ProgrammingError, InternalError), base=MonitoredTask, bind=True
|
||||||
|
)
|
||||||
|
@prefill_task
|
||||||
|
def blueprints_discover(self: MonitoredTask):
|
||||||
|
"""Find blueprints and check if they need to be created in the database"""
|
||||||
|
count = 0
|
||||||
|
for blueprint in blueprints_find():
|
||||||
|
check_blueprint_v1_file(blueprint)
|
||||||
|
count += 1
|
||||||
|
self.set_status(
|
||||||
|
TaskResult(
|
||||||
|
TaskResultStatus.SUCCESSFUL,
|
||||||
|
messages=[_("Successfully imported %(count)d files." % {"count": count})],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def check_blueprint_v1_file(blueprint: BlueprintFile):
|
||||||
|
"""Check if blueprint should be imported"""
|
||||||
|
instance: BlueprintInstance = BlueprintInstance.objects.filter(path=blueprint.path).first()
|
||||||
|
if (
|
||||||
|
blueprint.meta
|
||||||
|
and blueprint.meta.labels.get(LABEL_AUTHENTIK_INSTANTIATE, "").lower() == "false"
|
||||||
|
):
|
||||||
|
return
|
||||||
|
if not instance:
|
||||||
|
instance = BlueprintInstance(
|
||||||
|
name=blueprint.meta.name if blueprint.meta else str(blueprint.path),
|
||||||
|
path=blueprint.path,
|
||||||
|
context={},
|
||||||
|
status=BlueprintInstanceStatus.UNKNOWN,
|
||||||
|
enabled=True,
|
||||||
|
managed_models=[],
|
||||||
|
metadata={},
|
||||||
|
)
|
||||||
|
instance.save()
|
||||||
|
if instance.last_applied_hash != blueprint.hash:
|
||||||
|
instance.metadata = asdict(blueprint.meta) if blueprint.meta else {}
|
||||||
|
instance.save()
|
||||||
|
apply_blueprint.delay(instance.pk.hex)
|
||||||
|
|
||||||
|
|
||||||
|
@CELERY_APP.task(
|
||||||
|
bind=True,
|
||||||
|
base=MonitoredTask,
|
||||||
|
)
|
||||||
|
def apply_blueprint(self: MonitoredTask, instance_pk: str):
|
||||||
|
"""Apply single blueprint"""
|
||||||
|
self.set_uid(instance_pk)
|
||||||
|
self.save_on_success = False
|
||||||
|
try:
|
||||||
|
instance: BlueprintInstance = BlueprintInstance.objects.filter(pk=instance_pk).first()
|
||||||
|
if not instance or not instance.enabled:
|
||||||
|
return
|
||||||
|
blueprint_content = instance.retrieve()
|
||||||
|
file_hash = sha512(blueprint_content.encode()).hexdigest()
|
||||||
|
importer = Importer(blueprint_content, instance.context)
|
||||||
|
valid, logs = importer.validate()
|
||||||
|
if not valid:
|
||||||
|
instance.status = BlueprintInstanceStatus.ERROR
|
||||||
|
instance.save()
|
||||||
|
self.set_status(TaskResult(TaskResultStatus.ERROR, [x["event"] for x in logs]))
|
||||||
|
return
|
||||||
|
applied = importer.apply()
|
||||||
|
if not applied:
|
||||||
|
instance.status = BlueprintInstanceStatus.ERROR
|
||||||
|
instance.save()
|
||||||
|
self.set_status(TaskResult(TaskResultStatus.ERROR, "Failed to apply"))
|
||||||
|
return
|
||||||
|
instance.status = BlueprintInstanceStatus.SUCCESSFUL
|
||||||
|
instance.last_applied_hash = file_hash
|
||||||
|
instance.last_applied = now()
|
||||||
|
instance.save()
|
||||||
|
self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL))
|
||||||
|
except (
|
||||||
|
DatabaseError,
|
||||||
|
ProgrammingError,
|
||||||
|
InternalError,
|
||||||
|
IOError,
|
||||||
|
BlueprintRetrievalFailed,
|
||||||
|
) as exc:
|
||||||
|
instance.status = BlueprintInstanceStatus.ERROR
|
||||||
|
instance.save()
|
||||||
|
self.set_status(TaskResult(TaskResultStatus.ERROR).with_error(exc))
|
@ -89,8 +89,15 @@ class ApplicationViewSet(UsedByMixin, ModelViewSet):
|
|||||||
"meta_publisher",
|
"meta_publisher",
|
||||||
"group",
|
"group",
|
||||||
]
|
]
|
||||||
|
filterset_fields = [
|
||||||
|
"name",
|
||||||
|
"slug",
|
||||||
|
"meta_launch_url",
|
||||||
|
"meta_description",
|
||||||
|
"meta_publisher",
|
||||||
|
"group",
|
||||||
|
]
|
||||||
lookup_field = "slug"
|
lookup_field = "slug"
|
||||||
filterset_fields = ["name", "slug"]
|
|
||||||
ordering = ["name"]
|
ordering = ["name"]
|
||||||
|
|
||||||
def _filter_queryset_for_list(self, queryset: QuerySet) -> QuerySet:
|
def _filter_queryset_for_list(self, queryset: QuerySet) -> QuerySet:
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
"""Authenticator Devices API Views"""
|
"""Authenticator Devices API Views"""
|
||||||
from django_otp import devices_for_user
|
from django_otp import device_classes, devices_for_user
|
||||||
from django_otp.models import Device
|
from django_otp.models import Device
|
||||||
from drf_spectacular.utils import extend_schema
|
from drf_spectacular.types import OpenApiTypes
|
||||||
from rest_framework.fields import CharField, IntegerField, SerializerMethodField
|
from drf_spectacular.utils import OpenApiParameter, extend_schema
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.fields import BooleanField, CharField, IntegerField, SerializerMethodField
|
||||||
|
from rest_framework.permissions import IsAdminUser, IsAuthenticated
|
||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.viewsets import ViewSet
|
from rest_framework.viewsets import ViewSet
|
||||||
@ -17,6 +18,7 @@ class DeviceSerializer(MetaNameSerializer):
|
|||||||
pk = IntegerField()
|
pk = IntegerField()
|
||||||
name = CharField()
|
name = CharField()
|
||||||
type = SerializerMethodField()
|
type = SerializerMethodField()
|
||||||
|
confirmed = BooleanField()
|
||||||
|
|
||||||
def get_type(self, instance: Device) -> str:
|
def get_type(self, instance: Device) -> str:
|
||||||
"""Get type of device"""
|
"""Get type of device"""
|
||||||
@ -34,3 +36,33 @@ class DeviceViewSet(ViewSet):
|
|||||||
"""Get all devices for current user"""
|
"""Get all devices for current user"""
|
||||||
devices = devices_for_user(request.user)
|
devices = devices_for_user(request.user)
|
||||||
return Response(DeviceSerializer(devices, many=True).data)
|
return Response(DeviceSerializer(devices, many=True).data)
|
||||||
|
|
||||||
|
|
||||||
|
class AdminDeviceViewSet(ViewSet):
|
||||||
|
"""Viewset for authenticator devices"""
|
||||||
|
|
||||||
|
serializer_class = DeviceSerializer
|
||||||
|
permission_classes = [IsAdminUser]
|
||||||
|
|
||||||
|
def get_devices(self, **kwargs):
|
||||||
|
"""Get all devices in all child classes"""
|
||||||
|
for model in device_classes():
|
||||||
|
device_set = model.objects.filter(**kwargs)
|
||||||
|
yield from device_set
|
||||||
|
|
||||||
|
@extend_schema(
|
||||||
|
parameters=[
|
||||||
|
OpenApiParameter(
|
||||||
|
name="user",
|
||||||
|
location=OpenApiParameter.QUERY,
|
||||||
|
type=OpenApiTypes.INT,
|
||||||
|
)
|
||||||
|
],
|
||||||
|
responses={200: DeviceSerializer(many=True)},
|
||||||
|
)
|
||||||
|
def list(self, request: Request) -> Response:
|
||||||
|
"""Get all devices for current user"""
|
||||||
|
kwargs = {}
|
||||||
|
if "user" in request.query_params:
|
||||||
|
kwargs = {"user": request.query_params["user"]}
|
||||||
|
return Response(DeviceSerializer(self.get_devices(**kwargs), many=True).data)
|
||||||
|
@ -62,6 +62,11 @@ class GroupSerializer(ModelSerializer):
|
|||||||
"attributes",
|
"attributes",
|
||||||
"users_obj",
|
"users_obj",
|
||||||
]
|
]
|
||||||
|
extra_kwargs = {
|
||||||
|
"users": {
|
||||||
|
"default": list,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class GroupFilter(FilterSet):
|
class GroupFilter(FilterSet):
|
||||||
|
@ -14,12 +14,12 @@ from rest_framework.serializers import ModelSerializer, SerializerMethodField
|
|||||||
from rest_framework.viewsets import GenericViewSet
|
from rest_framework.viewsets import GenericViewSet
|
||||||
|
|
||||||
from authentik.api.decorators import permission_required
|
from authentik.api.decorators import permission_required
|
||||||
|
from authentik.blueprints.api import ManagedSerializer
|
||||||
from authentik.core.api.used_by import UsedByMixin
|
from authentik.core.api.used_by import UsedByMixin
|
||||||
from authentik.core.api.utils import MetaNameSerializer, PassiveSerializer, TypeCreateSerializer
|
from authentik.core.api.utils import MetaNameSerializer, PassiveSerializer, TypeCreateSerializer
|
||||||
from authentik.core.expression import PropertyMappingEvaluator
|
from authentik.core.expression import PropertyMappingEvaluator
|
||||||
from authentik.core.models import PropertyMapping
|
from authentik.core.models import PropertyMapping
|
||||||
from authentik.lib.utils.reflection import all_subclasses
|
from authentik.lib.utils.reflection import all_subclasses
|
||||||
from authentik.managed.api import ManagedSerializer
|
|
||||||
from authentik.policies.api.exec import PolicyTestSerializer
|
from authentik.policies.api.exec import PolicyTestSerializer
|
||||||
|
|
||||||
|
|
||||||
|
@ -53,6 +53,7 @@ class SourceSerializer(ModelSerializer, MetaNameSerializer):
|
|||||||
"policy_engine_mode",
|
"policy_engine_mode",
|
||||||
"user_matching_mode",
|
"user_matching_mode",
|
||||||
"managed",
|
"managed",
|
||||||
|
"user_path_template",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,13 +15,13 @@ from rest_framework.viewsets import ModelViewSet
|
|||||||
|
|
||||||
from authentik.api.authorization import OwnerSuperuserPermissions
|
from authentik.api.authorization import OwnerSuperuserPermissions
|
||||||
from authentik.api.decorators import permission_required
|
from authentik.api.decorators import permission_required
|
||||||
|
from authentik.blueprints.api import ManagedSerializer
|
||||||
from authentik.core.api.used_by import UsedByMixin
|
from authentik.core.api.used_by import UsedByMixin
|
||||||
from authentik.core.api.users import UserSerializer
|
from authentik.core.api.users import UserSerializer
|
||||||
from authentik.core.api.utils import PassiveSerializer
|
from authentik.core.api.utils import PassiveSerializer
|
||||||
from authentik.core.models import USER_ATTRIBUTE_TOKEN_EXPIRING, Token, TokenIntents
|
from authentik.core.models import USER_ATTRIBUTE_TOKEN_EXPIRING, Token, TokenIntents
|
||||||
from authentik.events.models import Event, EventAction
|
from authentik.events.models import Event, EventAction
|
||||||
from authentik.events.utils import model_to_dict
|
from authentik.events.utils import model_to_dict
|
||||||
from authentik.managed.api import ManagedSerializer
|
|
||||||
|
|
||||||
|
|
||||||
class TokenSerializer(ManagedSerializer, ModelSerializer):
|
class TokenSerializer(ManagedSerializer, ModelSerializer):
|
||||||
|
@ -24,7 +24,13 @@ from drf_spectacular.utils import (
|
|||||||
)
|
)
|
||||||
from guardian.shortcuts import get_anonymous_user, get_objects_for_user
|
from guardian.shortcuts import get_anonymous_user, get_objects_for_user
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.fields import CharField, JSONField, SerializerMethodField
|
from rest_framework.fields import (
|
||||||
|
CharField,
|
||||||
|
IntegerField,
|
||||||
|
JSONField,
|
||||||
|
ListField,
|
||||||
|
SerializerMethodField,
|
||||||
|
)
|
||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.serializers import (
|
from rest_framework.serializers import (
|
||||||
@ -50,12 +56,16 @@ from authentik.core.middleware import (
|
|||||||
from authentik.core.models import (
|
from authentik.core.models import (
|
||||||
USER_ATTRIBUTE_SA,
|
USER_ATTRIBUTE_SA,
|
||||||
USER_ATTRIBUTE_TOKEN_EXPIRING,
|
USER_ATTRIBUTE_TOKEN_EXPIRING,
|
||||||
|
USER_PATH_SERVICE_ACCOUNT,
|
||||||
Group,
|
Group,
|
||||||
Token,
|
Token,
|
||||||
TokenIntents,
|
TokenIntents,
|
||||||
User,
|
User,
|
||||||
)
|
)
|
||||||
from authentik.events.models import EventAction
|
from authentik.events.models import EventAction
|
||||||
|
from authentik.flows.models import FlowToken
|
||||||
|
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlanner
|
||||||
|
from authentik.flows.views.executor import QS_KEY_TOKEN
|
||||||
from authentik.stages.email.models import EmailStage
|
from authentik.stages.email.models import EmailStage
|
||||||
from authentik.stages.email.tasks import send_mails
|
from authentik.stages.email.tasks import send_mails
|
||||||
from authentik.stages.email.utils import TemplateEmailMessage
|
from authentik.stages.email.utils import TemplateEmailMessage
|
||||||
@ -77,6 +87,15 @@ class UserSerializer(ModelSerializer):
|
|||||||
uid = CharField(read_only=True)
|
uid = CharField(read_only=True)
|
||||||
username = CharField(max_length=150)
|
username = CharField(max_length=150)
|
||||||
|
|
||||||
|
def validate_path(self, path: str) -> str:
|
||||||
|
"""Validate path"""
|
||||||
|
if path[:1] == "/" or path[-1] == "/":
|
||||||
|
raise ValidationError(_("No leading or trailing slashes allowed."))
|
||||||
|
for segment in path.split("/"):
|
||||||
|
if segment == "":
|
||||||
|
raise ValidationError(_("No empty segments in user path allowed."))
|
||||||
|
return path
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
||||||
model = User
|
model = User
|
||||||
@ -93,6 +112,7 @@ class UserSerializer(ModelSerializer):
|
|||||||
"avatar",
|
"avatar",
|
||||||
"attributes",
|
"attributes",
|
||||||
"uid",
|
"uid",
|
||||||
|
"path",
|
||||||
]
|
]
|
||||||
extra_kwargs = {
|
extra_kwargs = {
|
||||||
"name": {"allow_blank": True},
|
"name": {"allow_blank": True},
|
||||||
@ -208,6 +228,11 @@ class UsersFilter(FilterSet):
|
|||||||
is_superuser = BooleanFilter(field_name="ak_groups", lookup_expr="is_superuser")
|
is_superuser = BooleanFilter(field_name="ak_groups", lookup_expr="is_superuser")
|
||||||
uuid = CharFilter(field_name="uuid")
|
uuid = CharFilter(field_name="uuid")
|
||||||
|
|
||||||
|
path = CharFilter(
|
||||||
|
field_name="path",
|
||||||
|
)
|
||||||
|
path_startswith = CharFilter(field_name="path", lookup_expr="startswith")
|
||||||
|
|
||||||
groups_by_name = ModelMultipleChoiceFilter(
|
groups_by_name = ModelMultipleChoiceFilter(
|
||||||
field_name="ak_groups__name",
|
field_name="ak_groups__name",
|
||||||
to_field_name="name",
|
to_field_name="name",
|
||||||
@ -272,12 +297,23 @@ class UserViewSet(UsedByMixin, ModelViewSet):
|
|||||||
LOGGER.debug("No recovery flow set")
|
LOGGER.debug("No recovery flow set")
|
||||||
return None, None
|
return None, None
|
||||||
user: User = self.get_object()
|
user: User = self.get_object()
|
||||||
token, __ = Token.objects.get_or_create(
|
planner = FlowPlanner(flow)
|
||||||
identifier=f"{user.uid}-password-reset",
|
planner.allow_empty_flows = True
|
||||||
user=user,
|
plan = planner.plan(
|
||||||
intent=TokenIntents.INTENT_RECOVERY,
|
self.request._request,
|
||||||
|
{
|
||||||
|
PLAN_CONTEXT_PENDING_USER: user,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
querystring = urlencode({"token": token.key})
|
token, __ = FlowToken.objects.update_or_create(
|
||||||
|
identifier=f"{user.uid}-password-reset",
|
||||||
|
defaults={
|
||||||
|
"user": user,
|
||||||
|
"flow": flow,
|
||||||
|
"_plan": FlowToken.pickle(plan),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
querystring = urlencode({QS_KEY_TOKEN: token.key})
|
||||||
link = self.request.build_absolute_uri(
|
link = self.request.build_absolute_uri(
|
||||||
reverse_lazy("authentik_core:if-flow", kwargs={"flow_slug": flow.slug})
|
reverse_lazy("authentik_core:if-flow", kwargs={"flow_slug": flow.slug})
|
||||||
+ f"?{querystring}"
|
+ f"?{querystring}"
|
||||||
@ -299,6 +335,9 @@ class UserViewSet(UsedByMixin, ModelViewSet):
|
|||||||
{
|
{
|
||||||
"username": CharField(required=True),
|
"username": CharField(required=True),
|
||||||
"token": CharField(required=True),
|
"token": CharField(required=True),
|
||||||
|
"user_uid": CharField(required=True),
|
||||||
|
"user_pk": IntegerField(required=True),
|
||||||
|
"group_pk": CharField(required=False),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
@ -314,19 +353,27 @@ class UserViewSet(UsedByMixin, ModelViewSet):
|
|||||||
username=username,
|
username=username,
|
||||||
name=username,
|
name=username,
|
||||||
attributes={USER_ATTRIBUTE_SA: True, USER_ATTRIBUTE_TOKEN_EXPIRING: False},
|
attributes={USER_ATTRIBUTE_SA: True, USER_ATTRIBUTE_TOKEN_EXPIRING: False},
|
||||||
|
path=USER_PATH_SERVICE_ACCOUNT,
|
||||||
)
|
)
|
||||||
|
response = {
|
||||||
|
"username": user.username,
|
||||||
|
"user_uid": user.uid,
|
||||||
|
"user_pk": user.pk,
|
||||||
|
}
|
||||||
if create_group and self.request.user.has_perm("authentik_core.add_group"):
|
if create_group and self.request.user.has_perm("authentik_core.add_group"):
|
||||||
group = Group.objects.create(
|
group = Group.objects.create(
|
||||||
name=username,
|
name=username,
|
||||||
)
|
)
|
||||||
group.users.add(user)
|
group.users.add(user)
|
||||||
|
response["group_pk"] = str(group.pk)
|
||||||
token = Token.objects.create(
|
token = Token.objects.create(
|
||||||
identifier=slugify(f"service-account-{username}-password"),
|
identifier=slugify(f"service-account-{username}-password"),
|
||||||
intent=TokenIntents.INTENT_APP_PASSWORD,
|
intent=TokenIntents.INTENT_APP_PASSWORD,
|
||||||
user=user,
|
user=user,
|
||||||
expires=now() + timedelta(days=360),
|
expires=now() + timedelta(days=360),
|
||||||
)
|
)
|
||||||
return Response({"username": user.username, "token": token.key})
|
response["token"] = token.key
|
||||||
|
return Response(response)
|
||||||
except (IntegrityError) as exc:
|
except (IntegrityError) as exc:
|
||||||
return Response(data={"non_field_errors": [str(exc)]}, status=400)
|
return Response(data={"non_field_errors": [str(exc)]}, status=400)
|
||||||
|
|
||||||
@ -344,7 +391,7 @@ class UserViewSet(UsedByMixin, ModelViewSet):
|
|||||||
instance=request._request.session[SESSION_KEY_IMPERSONATE_ORIGINAL_USER],
|
instance=request._request.session[SESSION_KEY_IMPERSONATE_ORIGINAL_USER],
|
||||||
context=context,
|
context=context,
|
||||||
).data
|
).data
|
||||||
self.request.session.save()
|
self.request.session.modified = True
|
||||||
return Response(serializer.initial_data)
|
return Response(serializer.initial_data)
|
||||||
|
|
||||||
@permission_required("authentik_core.reset_user_password")
|
@permission_required("authentik_core.reset_user_password")
|
||||||
@ -464,3 +511,32 @@ class UserViewSet(UsedByMixin, ModelViewSet):
|
|||||||
if self.request.user.has_perm("authentik_core.view_user"):
|
if self.request.user.has_perm("authentik_core.view_user"):
|
||||||
return self._filter_queryset_for_list(queryset)
|
return self._filter_queryset_for_list(queryset)
|
||||||
return super().filter_queryset(queryset)
|
return super().filter_queryset(queryset)
|
||||||
|
|
||||||
|
@extend_schema(
|
||||||
|
responses={
|
||||||
|
200: inline_serializer(
|
||||||
|
"UserPathSerializer", {"paths": ListField(child=CharField(), read_only=True)}
|
||||||
|
)
|
||||||
|
},
|
||||||
|
parameters=[
|
||||||
|
OpenApiParameter(
|
||||||
|
name="search",
|
||||||
|
location=OpenApiParameter.QUERY,
|
||||||
|
type=OpenApiTypes.STR,
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@action(detail=False, pagination_class=None)
|
||||||
|
def paths(self, request: Request) -> Response:
|
||||||
|
"""Get all user paths"""
|
||||||
|
return Response(
|
||||||
|
data={
|
||||||
|
"paths": list(
|
||||||
|
self.filter_queryset(self.get_queryset())
|
||||||
|
.values("path")
|
||||||
|
.distinct()
|
||||||
|
.order_by("path")
|
||||||
|
.values_list("path", flat=True)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@ -1,17 +1,37 @@
|
|||||||
"""authentik core app config"""
|
"""authentik core app config"""
|
||||||
from importlib import import_module
|
from django.conf import settings
|
||||||
|
|
||||||
from django.apps import AppConfig
|
from authentik.blueprints.manager import ManagedAppConfig
|
||||||
|
|
||||||
|
|
||||||
class AuthentikCoreConfig(AppConfig):
|
class AuthentikCoreConfig(ManagedAppConfig):
|
||||||
"""authentik core app config"""
|
"""authentik core app config"""
|
||||||
|
|
||||||
name = "authentik.core"
|
name = "authentik.core"
|
||||||
label = "authentik_core"
|
label = "authentik_core"
|
||||||
verbose_name = "authentik Core"
|
verbose_name = "authentik Core"
|
||||||
mountpoint = ""
|
mountpoint = ""
|
||||||
|
default = True
|
||||||
|
|
||||||
def ready(self):
|
def reconcile_load_core_signals(self):
|
||||||
import_module("authentik.core.signals")
|
"""Load core signals"""
|
||||||
import_module("authentik.core.managed")
|
self.import_module("authentik.core.signals")
|
||||||
|
|
||||||
|
def reconcile_debug_worker_hook(self):
|
||||||
|
"""Dispatch startup tasks inline when debugging"""
|
||||||
|
if settings.DEBUG:
|
||||||
|
from authentik.root.celery import worker_ready_hook
|
||||||
|
|
||||||
|
worker_ready_hook()
|
||||||
|
|
||||||
|
def reconcile_source_inbuilt(self):
|
||||||
|
"""Reconcile inbuilt source"""
|
||||||
|
from authentik.core.models import Source
|
||||||
|
|
||||||
|
Source.objects.update_or_create(
|
||||||
|
defaults={
|
||||||
|
"name": "authentik Built-in",
|
||||||
|
"slug": "authentik-built-in",
|
||||||
|
},
|
||||||
|
managed="goauthentik.io/sources/inbuilt",
|
||||||
|
)
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
"""Core managed objects"""
|
|
||||||
from authentik.core.models import Source
|
|
||||||
from authentik.managed.manager import EnsureExists, ObjectManager
|
|
||||||
|
|
||||||
|
|
||||||
class CoreManager(ObjectManager):
|
|
||||||
"""Core managed objects"""
|
|
||||||
|
|
||||||
def reconcile(self):
|
|
||||||
return [
|
|
||||||
EnsureExists(
|
|
||||||
Source,
|
|
||||||
"goauthentik.io/sources/inbuilt",
|
|
||||||
name="authentik Built-in",
|
|
||||||
slug="authentik-built-in",
|
|
||||||
),
|
|
||||||
]
|
|
@ -4,7 +4,7 @@ from django.core.management.base import BaseCommand
|
|||||||
from authentik.root.celery import _get_startup_tasks
|
from authentik.root.celery import _get_startup_tasks
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand): # pragma: no cover
|
class Command(BaseCommand):
|
||||||
"""Run bootstrap tasks to ensure certain objects are created"""
|
"""Run bootstrap tasks to ensure certain objects are created"""
|
||||||
|
|
||||||
def handle(self, **options):
|
def handle(self, **options):
|
||||||
|
@ -2,9 +2,10 @@
|
|||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
from django.contrib.auth.management import create_permissions
|
from django.contrib.auth.management import create_permissions
|
||||||
from django.core.management.base import BaseCommand, no_translations
|
from django.core.management.base import BaseCommand, no_translations
|
||||||
|
from guardian.management import create_anonymous_user
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand): # pragma: no cover
|
class Command(BaseCommand):
|
||||||
"""Repair missing permissions"""
|
"""Repair missing permissions"""
|
||||||
|
|
||||||
@no_translations
|
@no_translations
|
||||||
@ -13,3 +14,4 @@ class Command(BaseCommand): # pragma: no cover
|
|||||||
for app in apps.get_app_configs():
|
for app in apps.get_app_configs():
|
||||||
self.stdout.write(f"Checking app {app.name} ({app.label})\n")
|
self.stdout.write(f"Checking app {app.name} ({app.label})\n")
|
||||||
create_permissions(app, verbosity=0)
|
create_permissions(app, verbosity=0)
|
||||||
|
create_anonymous_user(None, using="default")
|
@ -22,7 +22,7 @@ BANNER_TEXT = """### authentik shell ({authentik})
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand): # pragma: no cover
|
class Command(BaseCommand):
|
||||||
"""Start the Django shell with all authentik models already imported"""
|
"""Start the Django shell with all authentik models already imported"""
|
||||||
|
|
||||||
django_models = {}
|
django_models = {}
|
||||||
@ -40,9 +40,6 @@ class Command(BaseCommand): # pragma: no cover
|
|||||||
|
|
||||||
# Gather Django models and constants from each app
|
# Gather Django models and constants from each app
|
||||||
for app in apps.get_app_configs():
|
for app in apps.get_app_configs():
|
||||||
if not app.name.startswith("authentik"):
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Load models from each app
|
# Load models from each app
|
||||||
for model in app.get_models():
|
for model in app.get_models():
|
||||||
namespace[model.__name__] = model
|
namespace[model.__name__] = model
|
||||||
|
@ -1,19 +1,22 @@
|
|||||||
"""authentik admin Middleware to impersonate users"""
|
"""authentik admin Middleware to impersonate users"""
|
||||||
from logging import Logger
|
from contextvars import ContextVar
|
||||||
from threading import local
|
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from django.http import HttpRequest, HttpResponse
|
from django.http import HttpRequest, HttpResponse
|
||||||
from sentry_sdk.api import set_tag
|
from sentry_sdk.api import set_tag
|
||||||
|
from structlog.contextvars import STRUCTLOG_KEY_PREFIX
|
||||||
|
|
||||||
SESSION_KEY_IMPERSONATE_USER = "authentik/impersonate/user"
|
SESSION_KEY_IMPERSONATE_USER = "authentik/impersonate/user"
|
||||||
SESSION_KEY_IMPERSONATE_ORIGINAL_USER = "authentik/impersonate/original_user"
|
SESSION_KEY_IMPERSONATE_ORIGINAL_USER = "authentik/impersonate/original_user"
|
||||||
LOCAL = local()
|
|
||||||
RESPONSE_HEADER_ID = "X-authentik-id"
|
RESPONSE_HEADER_ID = "X-authentik-id"
|
||||||
KEY_AUTH_VIA = "auth_via"
|
KEY_AUTH_VIA = "auth_via"
|
||||||
KEY_USER = "user"
|
KEY_USER = "user"
|
||||||
|
|
||||||
|
CTX_REQUEST_ID = ContextVar(STRUCTLOG_KEY_PREFIX + "request_id", default=None)
|
||||||
|
CTX_HOST = ContextVar(STRUCTLOG_KEY_PREFIX + "host", default=None)
|
||||||
|
CTX_AUTH_VIA = ContextVar(STRUCTLOG_KEY_PREFIX + KEY_AUTH_VIA, default=None)
|
||||||
|
|
||||||
|
|
||||||
class ImpersonateMiddleware:
|
class ImpersonateMiddleware:
|
||||||
"""Middleware to impersonate users"""
|
"""Middleware to impersonate users"""
|
||||||
@ -47,26 +50,20 @@ class RequestIDMiddleware:
|
|||||||
if not hasattr(request, "request_id"):
|
if not hasattr(request, "request_id"):
|
||||||
request_id = uuid4().hex
|
request_id = uuid4().hex
|
||||||
setattr(request, "request_id", request_id)
|
setattr(request, "request_id", request_id)
|
||||||
LOCAL.authentik = {
|
CTX_REQUEST_ID.set(request_id)
|
||||||
"request_id": request_id,
|
CTX_HOST.set(request.get_host())
|
||||||
"host": request.get_host(),
|
|
||||||
}
|
|
||||||
set_tag("authentik.request_id", request_id)
|
set_tag("authentik.request_id", request_id)
|
||||||
|
if hasattr(request, "user") and getattr(request.user, "is_authenticated", False):
|
||||||
|
CTX_AUTH_VIA.set("session")
|
||||||
|
else:
|
||||||
|
CTX_AUTH_VIA.set("unauthenticated")
|
||||||
|
|
||||||
response = self.get_response(request)
|
response = self.get_response(request)
|
||||||
|
|
||||||
response[RESPONSE_HEADER_ID] = request.request_id
|
response[RESPONSE_HEADER_ID] = request.request_id
|
||||||
setattr(response, "ak_context", {})
|
setattr(response, "ak_context", {})
|
||||||
response.ak_context.update(LOCAL.authentik)
|
response.ak_context["request_id"] = CTX_REQUEST_ID.get()
|
||||||
|
response.ak_context["host"] = CTX_HOST.get()
|
||||||
|
response.ak_context[KEY_AUTH_VIA] = CTX_AUTH_VIA.get()
|
||||||
response.ak_context[KEY_USER] = request.user.username
|
response.ak_context[KEY_USER] = request.user.username
|
||||||
for key in list(LOCAL.authentik.keys()):
|
|
||||||
del LOCAL.authentik[key]
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
|
||||||
def structlog_add_request_id(logger: Logger, method_name: str, event_dict: dict):
|
|
||||||
"""If threadlocal has authentik defined, add request_id to log"""
|
|
||||||
if hasattr(LOCAL, "authentik"):
|
|
||||||
event_dict.update(LOCAL.authentik)
|
|
||||||
if hasattr(LOCAL, "authentik_task"):
|
|
||||||
event_dict.update(LOCAL.authentik_task)
|
|
||||||
return event_dict
|
|
||||||
|
@ -12,9 +12,9 @@ import authentik.core.models
|
|||||||
|
|
||||||
|
|
||||||
def create_default_user(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
def create_default_user(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
||||||
# We have to use a direct import here, otherwise we get an object manager error
|
from django.contrib.auth.hashers import make_password
|
||||||
from authentik.core.models import User
|
|
||||||
|
|
||||||
|
User = apps.get_model("authentik_core", "User")
|
||||||
db_alias = schema_editor.connection.alias
|
db_alias = schema_editor.connection.alias
|
||||||
|
|
||||||
akadmin, _ = User.objects.using(db_alias).get_or_create(
|
akadmin, _ = User.objects.using(db_alias).get_or_create(
|
||||||
@ -28,9 +28,9 @@ def create_default_user(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
|||||||
if "AUTHENTIK_BOOTSTRAP_PASSWORD" in environ:
|
if "AUTHENTIK_BOOTSTRAP_PASSWORD" in environ:
|
||||||
password = environ["AUTHENTIK_BOOTSTRAP_PASSWORD"]
|
password = environ["AUTHENTIK_BOOTSTRAP_PASSWORD"]
|
||||||
if password:
|
if password:
|
||||||
akadmin.set_password(password, signal=False)
|
akadmin.password = make_password(password)
|
||||||
else:
|
else:
|
||||||
akadmin.set_unusable_password()
|
akadmin.password = make_password(None)
|
||||||
akadmin.save()
|
akadmin.save()
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,9 +8,9 @@ from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
|||||||
|
|
||||||
|
|
||||||
def create_default_user(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
def create_default_user(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
||||||
# We have to use a direct import here, otherwise we get an object manager error
|
from django.contrib.auth.hashers import make_password
|
||||||
from authentik.core.models import User
|
|
||||||
|
|
||||||
|
User = apps.get_model("authentik_core", "User")
|
||||||
db_alias = schema_editor.connection.alias
|
db_alias = schema_editor.connection.alias
|
||||||
|
|
||||||
akadmin, _ = User.objects.using(db_alias).get_or_create(
|
akadmin, _ = User.objects.using(db_alias).get_or_create(
|
||||||
@ -24,9 +24,9 @@ def create_default_user(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
|||||||
if "AUTHENTIK_BOOTSTRAP_PASSWORD" in environ:
|
if "AUTHENTIK_BOOTSTRAP_PASSWORD" in environ:
|
||||||
password = environ["AUTHENTIK_BOOTSTRAP_PASSWORD"]
|
password = environ["AUTHENTIK_BOOTSTRAP_PASSWORD"]
|
||||||
if password:
|
if password:
|
||||||
akadmin.set_password(password, signal=False)
|
akadmin.password = make_password(password)
|
||||||
else:
|
else:
|
||||||
akadmin.set_unusable_password()
|
akadmin.password = make_password(None)
|
||||||
akadmin.save()
|
akadmin.save()
|
||||||
|
|
||||||
|
|
||||||
|
23
authentik/core/migrations/0021_source_user_path_user_path.py
Normal file
23
authentik/core/migrations/0021_source_user_path_user_path.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# Generated by Django 4.0.5 on 2022-06-13 18:51
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("authentik_core", "0020_application_open_in_new_tab"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="source",
|
||||||
|
name="user_path_template",
|
||||||
|
field=models.TextField(default="goauthentik.io/sources/%(slug)s"),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="user",
|
||||||
|
name="path",
|
||||||
|
field=models.TextField(default="users"),
|
||||||
|
),
|
||||||
|
]
|
26
authentik/core/migrations/0022_alter_group_parent.py
Normal file
26
authentik/core/migrations/0022_alter_group_parent.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
# Generated by Django 4.0.6 on 2022-08-05 22:01
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("authentik_core", "0021_source_user_path_user_path"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="group",
|
||||||
|
name="parent",
|
||||||
|
field=models.ForeignKey(
|
||||||
|
blank=True,
|
||||||
|
default=None,
|
||||||
|
null=True,
|
||||||
|
on_delete=django.db.models.deletion.SET_NULL,
|
||||||
|
related_name="children",
|
||||||
|
to="authentik_core.group",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
@ -23,14 +23,14 @@ from model_utils.managers import InheritanceManager
|
|||||||
from rest_framework.serializers import Serializer
|
from rest_framework.serializers import Serializer
|
||||||
from structlog.stdlib import get_logger
|
from structlog.stdlib import get_logger
|
||||||
|
|
||||||
|
from authentik.blueprints.models import ManagedModel
|
||||||
from authentik.core.exceptions import PropertyMappingExpressionException
|
from authentik.core.exceptions import PropertyMappingExpressionException
|
||||||
from authentik.core.signals import password_changed
|
from authentik.core.signals import password_changed
|
||||||
from authentik.core.types import UILoginButton, UserSettingSerializer
|
from authentik.core.types import UILoginButton, UserSettingSerializer
|
||||||
from authentik.lib.config import CONFIG
|
from authentik.lib.config import CONFIG, get_path_from_dict
|
||||||
from authentik.lib.generators import generate_id
|
from authentik.lib.generators import generate_id
|
||||||
from authentik.lib.models import CreatedUpdatedModel, DomainlessURLValidator, SerializerModel
|
from authentik.lib.models import CreatedUpdatedModel, DomainlessURLValidator, SerializerModel
|
||||||
from authentik.lib.utils.http import get_client_ip
|
from authentik.lib.utils.http import get_client_ip
|
||||||
from authentik.managed.models import ManagedModel
|
|
||||||
from authentik.policies.models import PolicyBindingModel
|
from authentik.policies.models import PolicyBindingModel
|
||||||
|
|
||||||
LOGGER = get_logger()
|
LOGGER = get_logger()
|
||||||
@ -46,6 +46,9 @@ USER_ATTRIBUTE_CHANGE_NAME = "goauthentik.io/user/can-change-name"
|
|||||||
USER_ATTRIBUTE_CHANGE_EMAIL = "goauthentik.io/user/can-change-email"
|
USER_ATTRIBUTE_CHANGE_EMAIL = "goauthentik.io/user/can-change-email"
|
||||||
USER_ATTRIBUTE_CAN_OVERRIDE_IP = "goauthentik.io/user/override-ips"
|
USER_ATTRIBUTE_CAN_OVERRIDE_IP = "goauthentik.io/user/override-ips"
|
||||||
|
|
||||||
|
USER_PATH_SYSTEM_PREFIX = "goauthentik.io"
|
||||||
|
USER_PATH_SERVICE_ACCOUNT = USER_PATH_SYSTEM_PREFIX + "/service-accounts"
|
||||||
|
|
||||||
GRAVATAR_URL = "https://secure.gravatar.com"
|
GRAVATAR_URL = "https://secure.gravatar.com"
|
||||||
DEFAULT_AVATAR = static("dist/assets/images/user_default.png")
|
DEFAULT_AVATAR = static("dist/assets/images/user_default.png")
|
||||||
|
|
||||||
@ -65,7 +68,7 @@ def default_token_key():
|
|||||||
return generate_id(int(CONFIG.y("default_token_length")))
|
return generate_id(int(CONFIG.y("default_token_length")))
|
||||||
|
|
||||||
|
|
||||||
class Group(models.Model):
|
class Group(SerializerModel):
|
||||||
"""Custom Group model which supports a basic hierarchy"""
|
"""Custom Group model which supports a basic hierarchy"""
|
||||||
|
|
||||||
group_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
group_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
||||||
@ -79,11 +82,18 @@ class Group(models.Model):
|
|||||||
"Group",
|
"Group",
|
||||||
blank=True,
|
blank=True,
|
||||||
null=True,
|
null=True,
|
||||||
|
default=None,
|
||||||
on_delete=models.SET_NULL,
|
on_delete=models.SET_NULL,
|
||||||
related_name="children",
|
related_name="children",
|
||||||
)
|
)
|
||||||
attributes = models.JSONField(default=dict, blank=True)
|
attributes = models.JSONField(default=dict, blank=True)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serializer(self) -> Serializer:
|
||||||
|
from authentik.core.api.groups import GroupSerializer
|
||||||
|
|
||||||
|
return GroupSerializer
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def num_pk(self) -> int:
|
def num_pk(self) -> int:
|
||||||
"""Get a numerical, int32 ID for the group"""
|
"""Get a numerical, int32 ID for the group"""
|
||||||
@ -103,7 +113,10 @@ class Group(models.Model):
|
|||||||
|
|
||||||
SELECT authentik_core_group.*, parents.relative_depth - 1
|
SELECT authentik_core_group.*, parents.relative_depth - 1
|
||||||
FROM authentik_core_group,parents
|
FROM authentik_core_group,parents
|
||||||
WHERE authentik_core_group.parent_id = parents.group_uuid
|
WHERE (
|
||||||
|
authentik_core_group.parent_id = parents.group_uuid and
|
||||||
|
parents.relative_depth > -20
|
||||||
|
)
|
||||||
)
|
)
|
||||||
SELECT group_uuid
|
SELECT group_uuid
|
||||||
FROM parents
|
FROM parents
|
||||||
@ -133,11 +146,12 @@ class UserManager(DjangoUserManager):
|
|||||||
return self._create_user(username, email, password, **extra_fields)
|
return self._create_user(username, email, password, **extra_fields)
|
||||||
|
|
||||||
|
|
||||||
class User(GuardianUserMixin, AbstractUser):
|
class User(SerializerModel, GuardianUserMixin, AbstractUser):
|
||||||
"""Custom User model to allow easier adding of user-based settings"""
|
"""Custom User model to allow easier adding of user-based settings"""
|
||||||
|
|
||||||
uuid = models.UUIDField(default=uuid4, editable=False)
|
uuid = models.UUIDField(default=uuid4, editable=False)
|
||||||
name = models.TextField(help_text=_("User's display name."))
|
name = models.TextField(help_text=_("User's display name."))
|
||||||
|
path = models.TextField(default="users")
|
||||||
|
|
||||||
sources = models.ManyToManyField("Source", through="UserSourceConnection")
|
sources = models.ManyToManyField("Source", through="UserSourceConnection")
|
||||||
ak_groups = models.ManyToManyField("Group", related_name="users")
|
ak_groups = models.ManyToManyField("Group", related_name="users")
|
||||||
@ -147,6 +161,11 @@ class User(GuardianUserMixin, AbstractUser):
|
|||||||
|
|
||||||
objects = UserManager()
|
objects = UserManager()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def default_path() -> str:
|
||||||
|
"""Get the default user path"""
|
||||||
|
return User._meta.get_field("path").default
|
||||||
|
|
||||||
def group_attributes(self, request: Optional[HttpRequest] = None) -> dict[str, Any]:
|
def group_attributes(self, request: Optional[HttpRequest] = None) -> dict[str, Any]:
|
||||||
"""Get a dictionary containing the attributes from all groups the user belongs to,
|
"""Get a dictionary containing the attributes from all groups the user belongs to,
|
||||||
including the users attributes"""
|
including the users attributes"""
|
||||||
@ -158,6 +177,12 @@ class User(GuardianUserMixin, AbstractUser):
|
|||||||
always_merger.merge(final_attributes, self.attributes)
|
always_merger.merge(final_attributes, self.attributes)
|
||||||
return final_attributes
|
return final_attributes
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serializer(self) -> Serializer:
|
||||||
|
from authentik.core.api.users import UserSerializer
|
||||||
|
|
||||||
|
return UserSerializer
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def is_superuser(self) -> bool:
|
def is_superuser(self) -> bool:
|
||||||
"""Get supseruser status based on membership in a group with superuser status"""
|
"""Get supseruser status based on membership in a group with superuser status"""
|
||||||
@ -201,6 +226,8 @@ class User(GuardianUserMixin, AbstractUser):
|
|||||||
mode: str = CONFIG.y("avatars", "none")
|
mode: str = CONFIG.y("avatars", "none")
|
||||||
if mode == "none":
|
if mode == "none":
|
||||||
return DEFAULT_AVATAR
|
return DEFAULT_AVATAR
|
||||||
|
if mode.startswith("attributes."):
|
||||||
|
return get_path_from_dict(self.attributes, mode[11:], default=DEFAULT_AVATAR)
|
||||||
# gravatar uses md5 for their URLs, so md5 can't be avoided
|
# gravatar uses md5 for their URLs, so md5 can't be avoided
|
||||||
mail_hash = md5(self.email.lower().encode("utf-8")).hexdigest() # nosec
|
mail_hash = md5(self.email.lower().encode("utf-8")).hexdigest() # nosec
|
||||||
if mode == "gravatar":
|
if mode == "gravatar":
|
||||||
@ -262,7 +289,7 @@ class Provider(SerializerModel):
|
|||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class Application(PolicyBindingModel):
|
class Application(SerializerModel, PolicyBindingModel):
|
||||||
"""Every Application which uses authentik for authentication/identification/authorization
|
"""Every Application which uses authentik for authentication/identification/authorization
|
||||||
needs an Application record. Other authentication types can subclass this Model to
|
needs an Application record. Other authentication types can subclass this Model to
|
||||||
add custom fields and other properties"""
|
add custom fields and other properties"""
|
||||||
@ -293,6 +320,12 @@ class Application(PolicyBindingModel):
|
|||||||
meta_description = models.TextField(default="", blank=True)
|
meta_description = models.TextField(default="", blank=True)
|
||||||
meta_publisher = models.TextField(default="", blank=True)
|
meta_publisher = models.TextField(default="", blank=True)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serializer(self) -> Serializer:
|
||||||
|
from authentik.core.api.applications import ApplicationSerializer
|
||||||
|
|
||||||
|
return ApplicationSerializer
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def get_meta_icon(self) -> Optional[str]:
|
def get_meta_icon(self) -> Optional[str]:
|
||||||
"""Get the URL to the App Icon image. If the name is /static or starts with http
|
"""Get the URL to the App Icon image. If the name is /static or starts with http
|
||||||
@ -373,6 +406,8 @@ class Source(ManagedModel, SerializerModel, PolicyBindingModel):
|
|||||||
name = models.TextField(help_text=_("Source's display Name."))
|
name = models.TextField(help_text=_("Source's display Name."))
|
||||||
slug = models.SlugField(help_text=_("Internal source name, used in URLs."), unique=True)
|
slug = models.SlugField(help_text=_("Internal source name, used in URLs."), unique=True)
|
||||||
|
|
||||||
|
user_path_template = models.TextField(default="goauthentik.io/sources/%(slug)s")
|
||||||
|
|
||||||
enabled = models.BooleanField(default=True)
|
enabled = models.BooleanField(default=True)
|
||||||
property_mappings = models.ManyToManyField("PropertyMapping", default=None, blank=True)
|
property_mappings = models.ManyToManyField("PropertyMapping", default=None, blank=True)
|
||||||
|
|
||||||
@ -408,6 +443,17 @@ class Source(ManagedModel, SerializerModel, PolicyBindingModel):
|
|||||||
|
|
||||||
objects = InheritanceManager()
|
objects = InheritanceManager()
|
||||||
|
|
||||||
|
def get_user_path(self) -> str:
|
||||||
|
"""Get user path, fallback to default for formatting errors"""
|
||||||
|
try:
|
||||||
|
return self.user_path_template % {
|
||||||
|
"slug": self.slug,
|
||||||
|
}
|
||||||
|
# pylint: disable=broad-except
|
||||||
|
except Exception as exc:
|
||||||
|
LOGGER.warning("Failed to template user path", exc=exc, source=self)
|
||||||
|
return User.default_path()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def component(self) -> str:
|
def component(self) -> str:
|
||||||
"""Return component used to edit this object"""
|
"""Return component used to edit this object"""
|
||||||
@ -427,7 +473,7 @@ class Source(ManagedModel, SerializerModel, PolicyBindingModel):
|
|||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class UserSourceConnection(CreatedUpdatedModel):
|
class UserSourceConnection(SerializerModel, CreatedUpdatedModel):
|
||||||
"""Connection between User and Source."""
|
"""Connection between User and Source."""
|
||||||
|
|
||||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
@ -435,6 +481,11 @@ class UserSourceConnection(CreatedUpdatedModel):
|
|||||||
|
|
||||||
objects = InheritanceManager()
|
objects = InheritanceManager()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serializer(self) -> type[Serializer]:
|
||||||
|
"""Get serializer for this model"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
||||||
unique_together = (("user", "source"),)
|
unique_together = (("user", "source"),)
|
||||||
@ -457,8 +508,9 @@ class ExpiringModel(models.Model):
|
|||||||
def filter_not_expired(cls, **kwargs) -> QuerySet:
|
def filter_not_expired(cls, **kwargs) -> QuerySet:
|
||||||
"""Filer for tokens which are not expired yet or are not expiring,
|
"""Filer for tokens which are not expired yet or are not expiring,
|
||||||
and match filters in `kwargs`"""
|
and match filters in `kwargs`"""
|
||||||
expired = Q(expires__lt=now(), expiring=True)
|
for obj in cls.objects.filter(**kwargs).filter(Q(expires__lt=now(), expiring=True)):
|
||||||
return cls.objects.exclude(expired).filter(**kwargs)
|
obj.delete()
|
||||||
|
return cls.objects.filter(**kwargs)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_expired(self) -> bool:
|
def is_expired(self) -> bool:
|
||||||
@ -488,7 +540,7 @@ class TokenIntents(models.TextChoices):
|
|||||||
INTENT_APP_PASSWORD = "app_password" # nosec
|
INTENT_APP_PASSWORD = "app_password" # nosec
|
||||||
|
|
||||||
|
|
||||||
class Token(ManagedModel, ExpiringModel):
|
class Token(SerializerModel, ManagedModel, ExpiringModel):
|
||||||
"""Token used to authenticate the User for API Access or confirm another Stage like Email."""
|
"""Token used to authenticate the User for API Access or confirm another Stage like Email."""
|
||||||
|
|
||||||
token_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
token_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
||||||
@ -500,6 +552,12 @@ class Token(ManagedModel, ExpiringModel):
|
|||||||
user = models.ForeignKey("User", on_delete=models.CASCADE, related_name="+")
|
user = models.ForeignKey("User", on_delete=models.CASCADE, related_name="+")
|
||||||
description = models.TextField(default="", blank=True)
|
description = models.TextField(default="", blank=True)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serializer(self) -> type[Serializer]:
|
||||||
|
from authentik.core.api.tokens import TokenSerializer
|
||||||
|
|
||||||
|
return TokenSerializer
|
||||||
|
|
||||||
def expire_action(self, *args, **kwargs):
|
def expire_action(self, *args, **kwargs):
|
||||||
"""Handler which is called when this object is expired."""
|
"""Handler which is called when this object is expired."""
|
||||||
from authentik.events.models import Event, EventAction
|
from authentik.events.models import Event, EventAction
|
||||||
|
@ -26,11 +26,11 @@ from authentik.flows.planner import (
|
|||||||
from authentik.flows.views.executor import NEXT_ARG_NAME, SESSION_KEY_GET, SESSION_KEY_PLAN
|
from authentik.flows.views.executor import NEXT_ARG_NAME, SESSION_KEY_GET, SESSION_KEY_PLAN
|
||||||
from authentik.lib.utils.urls import redirect_with_qs
|
from authentik.lib.utils.urls import redirect_with_qs
|
||||||
from authentik.policies.denied import AccessDeniedResponse
|
from authentik.policies.denied import AccessDeniedResponse
|
||||||
from authentik.policies.types import PolicyResult
|
|
||||||
from authentik.policies.utils import delete_none_keys
|
from authentik.policies.utils import delete_none_keys
|
||||||
from authentik.stages.password import BACKEND_INBUILT
|
from authentik.stages.password import BACKEND_INBUILT
|
||||||
from authentik.stages.password.stage import PLAN_CONTEXT_AUTHENTICATION_BACKEND
|
from authentik.stages.password.stage import PLAN_CONTEXT_AUTHENTICATION_BACKEND
|
||||||
from authentik.stages.prompt.stage import PLAN_CONTEXT_PROMPT
|
from authentik.stages.prompt.stage import PLAN_CONTEXT_PROMPT
|
||||||
|
from authentik.stages.user_write.stage import PLAN_CONTEXT_USER_PATH
|
||||||
|
|
||||||
|
|
||||||
class Action(Enum):
|
class Action(Enum):
|
||||||
@ -165,9 +165,9 @@ class SourceFlowManager:
|
|||||||
return self.handle_enroll(connection)
|
return self.handle_enroll(connection)
|
||||||
except FlowNonApplicableException as exc:
|
except FlowNonApplicableException as exc:
|
||||||
self._logger.warning("Flow non applicable", exc=exc)
|
self._logger.warning("Flow non applicable", exc=exc)
|
||||||
return self.error_handler(exc, exc.policy_result)
|
return self.error_handler(exc)
|
||||||
# Default case, assume deny
|
# Default case, assume deny
|
||||||
error = (
|
error = Exception(
|
||||||
_(
|
_(
|
||||||
(
|
(
|
||||||
"Request to authenticate with %(source)s has been denied. Please authenticate "
|
"Request to authenticate with %(source)s has been denied. Please authenticate "
|
||||||
@ -178,14 +178,13 @@ class SourceFlowManager:
|
|||||||
)
|
)
|
||||||
return self.error_handler(error)
|
return self.error_handler(error)
|
||||||
|
|
||||||
def error_handler(
|
def error_handler(self, error: Exception) -> HttpResponse:
|
||||||
self, error: Exception, policy_result: Optional[PolicyResult] = None
|
|
||||||
) -> HttpResponse:
|
|
||||||
"""Handle any errors by returning an access denied stage"""
|
"""Handle any errors by returning an access denied stage"""
|
||||||
response = AccessDeniedResponse(self.request)
|
response = AccessDeniedResponse(self.request)
|
||||||
response.error_message = str(error)
|
response.error_message = str(error)
|
||||||
if policy_result:
|
if isinstance(error, FlowNonApplicableException):
|
||||||
response.policy_result = policy_result
|
response.policy_result = error.policy_result
|
||||||
|
response.error_message = error.messages
|
||||||
return response
|
return response
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
@ -291,5 +290,6 @@ class SourceFlowManager:
|
|||||||
connection,
|
connection,
|
||||||
**{
|
**{
|
||||||
PLAN_CONTEXT_PROMPT: delete_none_keys(self.enroll_info),
|
PLAN_CONTEXT_PROMPT: delete_none_keys(self.enroll_info),
|
||||||
|
PLAN_CONTEXT_USER_PATH: self.source.get_user_path(),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -7,6 +7,12 @@
|
|||||||
<script src="{% static 'dist/admin/AdminInterface.js' %}" type="module"></script>
|
<script src="{% static 'dist/admin/AdminInterface.js' %}" type="module"></script>
|
||||||
<meta name="theme-color" content="#18191a" media="(prefers-color-scheme: dark)">
|
<meta name="theme-color" content="#18191a" media="(prefers-color-scheme: dark)">
|
||||||
<meta name="theme-color" content="#ffffff" media="(prefers-color-scheme: light)">
|
<meta name="theme-color" content="#ffffff" media="(prefers-color-scheme: light)">
|
||||||
|
<script>
|
||||||
|
window.authentik = {};
|
||||||
|
window.authentik.locale = "{{ tenant.default_locale }}";
|
||||||
|
window.authentik.config = JSON.parse('{{ config_json|escapejs }}');
|
||||||
|
window.authentik.tenant = JSON.parse('{{ tenant_json|escapejs }}');
|
||||||
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
|
@ -11,6 +11,9 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
<script>
|
<script>
|
||||||
window.authentik = {};
|
window.authentik = {};
|
||||||
|
window.authentik.locale = "{{ tenant.default_locale }}";
|
||||||
|
window.authentik.config = JSON.parse('{{ config_json|escapejs }}');
|
||||||
|
window.authentik.tenant = JSON.parse('{{ tenant_json|escapejs }}');
|
||||||
window.authentik.flow = {
|
window.authentik.flow = {
|
||||||
"layout": "{{ flow.layout }}",
|
"layout": "{{ flow.layout }}",
|
||||||
};
|
};
|
||||||
|
@ -7,6 +7,12 @@
|
|||||||
<script src="{% static 'dist/user/UserInterface.js' %}" type="module"></script>
|
<script src="{% static 'dist/user/UserInterface.js' %}" type="module"></script>
|
||||||
<meta name="theme-color" content="#151515" media="(prefers-color-scheme: light)">
|
<meta name="theme-color" content="#151515" media="(prefers-color-scheme: light)">
|
||||||
<meta name="theme-color" content="#151515" media="(prefers-color-scheme: dark)">
|
<meta name="theme-color" content="#151515" media="(prefers-color-scheme: dark)">
|
||||||
|
<script>
|
||||||
|
window.authentik = {};
|
||||||
|
window.authentik.locale = "{{ tenant.default_locale }}";
|
||||||
|
window.authentik.config = JSON.parse('{{ config_json|escapejs }}');
|
||||||
|
window.authentik.tenant = JSON.parse('{{ tenant_json|escapejs }}');
|
||||||
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
from django.test.testcases import TestCase
|
from django.test.testcases import TestCase
|
||||||
|
|
||||||
from authentik.core.models import Group, User
|
from authentik.core.models import Group, User
|
||||||
|
from authentik.lib.generators import generate_id
|
||||||
|
|
||||||
|
|
||||||
class TestGroups(TestCase):
|
class TestGroups(TestCase):
|
||||||
@ -9,32 +10,43 @@ class TestGroups(TestCase):
|
|||||||
|
|
||||||
def test_group_membership_simple(self):
|
def test_group_membership_simple(self):
|
||||||
"""Test simple membership"""
|
"""Test simple membership"""
|
||||||
user = User.objects.create(username="user")
|
user = User.objects.create(username=generate_id())
|
||||||
user2 = User.objects.create(username="user2")
|
user2 = User.objects.create(username=generate_id())
|
||||||
group = Group.objects.create(name="group")
|
group = Group.objects.create(name=generate_id())
|
||||||
group.users.add(user)
|
group.users.add(user)
|
||||||
self.assertTrue(group.is_member(user))
|
self.assertTrue(group.is_member(user))
|
||||||
self.assertFalse(group.is_member(user2))
|
self.assertFalse(group.is_member(user2))
|
||||||
|
|
||||||
def test_group_membership_parent(self):
|
def test_group_membership_parent(self):
|
||||||
"""Test parent membership"""
|
"""Test parent membership"""
|
||||||
user = User.objects.create(username="user")
|
user = User.objects.create(username=generate_id())
|
||||||
user2 = User.objects.create(username="user2")
|
user2 = User.objects.create(username=generate_id())
|
||||||
first = Group.objects.create(name="first")
|
first = Group.objects.create(name=generate_id())
|
||||||
second = Group.objects.create(name="second", parent=first)
|
second = Group.objects.create(name=generate_id(), parent=first)
|
||||||
second.users.add(user)
|
second.users.add(user)
|
||||||
self.assertTrue(first.is_member(user))
|
self.assertTrue(first.is_member(user))
|
||||||
self.assertFalse(first.is_member(user2))
|
self.assertFalse(first.is_member(user2))
|
||||||
|
|
||||||
def test_group_membership_parent_extra(self):
|
def test_group_membership_parent_extra(self):
|
||||||
"""Test parent membership"""
|
"""Test parent membership"""
|
||||||
user = User.objects.create(username="user")
|
user = User.objects.create(username=generate_id())
|
||||||
user2 = User.objects.create(username="user2")
|
user2 = User.objects.create(username=generate_id())
|
||||||
first = Group.objects.create(name="first")
|
first = Group.objects.create(name=generate_id())
|
||||||
second = Group.objects.create(name="second", parent=first)
|
second = Group.objects.create(name=generate_id(), parent=first)
|
||||||
third = Group.objects.create(name="third", parent=second)
|
third = Group.objects.create(name=generate_id(), parent=second)
|
||||||
second.users.add(user)
|
second.users.add(user)
|
||||||
self.assertTrue(first.is_member(user))
|
self.assertTrue(first.is_member(user))
|
||||||
self.assertFalse(first.is_member(user2))
|
self.assertFalse(first.is_member(user2))
|
||||||
self.assertFalse(third.is_member(user))
|
self.assertFalse(third.is_member(user))
|
||||||
self.assertFalse(third.is_member(user2))
|
self.assertFalse(third.is_member(user2))
|
||||||
|
|
||||||
|
def test_group_membership_recursive(self):
|
||||||
|
"""Test group membership (recursive)"""
|
||||||
|
user = User.objects.create(username=generate_id())
|
||||||
|
group = Group.objects.create(name=generate_id())
|
||||||
|
group2 = Group.objects.create(name=generate_id(), parent=group)
|
||||||
|
group.users.add(user)
|
||||||
|
group.parent = group2
|
||||||
|
group.save()
|
||||||
|
self.assertTrue(group.is_member(user))
|
||||||
|
self.assertTrue(group2.is_member(user))
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
"""Test Users API"""
|
"""Test Users API"""
|
||||||
|
from json import loads
|
||||||
|
|
||||||
from django.urls.base import reverse
|
from django.urls.base import reverse
|
||||||
from rest_framework.test import APITestCase
|
from rest_framework.test import APITestCase
|
||||||
|
|
||||||
from authentik.core.models import User
|
from authentik.core.models import User
|
||||||
from authentik.core.tests.utils import create_test_admin_user, create_test_flow, create_test_tenant
|
from authentik.core.tests.utils import create_test_admin_user, create_test_flow, create_test_tenant
|
||||||
from authentik.flows.models import FlowDesignation
|
from authentik.flows.models import FlowDesignation
|
||||||
from authentik.lib.generators import generate_key
|
from authentik.lib.config import CONFIG
|
||||||
|
from authentik.lib.generators import generate_id, generate_key
|
||||||
from authentik.stages.email.models import EmailStage
|
from authentik.stages.email.models import EmailStage
|
||||||
from authentik.tenants.models import Tenant
|
from authentik.tenants.models import Tenant
|
||||||
|
|
||||||
@ -149,3 +152,109 @@ class TestUsersAPI(APITestCase):
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, 400)
|
self.assertEqual(response.status_code, 400)
|
||||||
|
|
||||||
|
def test_paths(self):
|
||||||
|
"""Test path"""
|
||||||
|
self.client.force_login(self.admin)
|
||||||
|
response = self.client.get(
|
||||||
|
reverse("authentik_api:user-paths"),
|
||||||
|
)
|
||||||
|
print(response.content)
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertJSONEqual(response.content.decode(), {"paths": ["users"]})
|
||||||
|
|
||||||
|
def test_path_valid(self):
|
||||||
|
"""Test path"""
|
||||||
|
self.client.force_login(self.admin)
|
||||||
|
response = self.client.post(
|
||||||
|
reverse("authentik_api:user-list"),
|
||||||
|
data={"name": generate_id(), "username": generate_id(), "groups": [], "path": "foo"},
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 201)
|
||||||
|
|
||||||
|
def test_path_invalid(self):
|
||||||
|
"""Test path (invalid)"""
|
||||||
|
self.client.force_login(self.admin)
|
||||||
|
response = self.client.post(
|
||||||
|
reverse("authentik_api:user-list"),
|
||||||
|
data={"name": generate_id(), "username": generate_id(), "groups": [], "path": "/foo"},
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 400)
|
||||||
|
self.assertJSONEqual(
|
||||||
|
response.content.decode(), {"path": ["No leading or trailing slashes allowed."]}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.client.force_login(self.admin)
|
||||||
|
response = self.client.post(
|
||||||
|
reverse("authentik_api:user-list"),
|
||||||
|
data={"name": generate_id(), "username": generate_id(), "groups": [], "path": ""},
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 400)
|
||||||
|
self.assertJSONEqual(response.content.decode(), {"path": ["This field may not be blank."]})
|
||||||
|
|
||||||
|
response = self.client.post(
|
||||||
|
reverse("authentik_api:user-list"),
|
||||||
|
data={"name": generate_id(), "username": generate_id(), "groups": [], "path": "foo/"},
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 400)
|
||||||
|
self.assertJSONEqual(
|
||||||
|
response.content.decode(), {"path": ["No leading or trailing slashes allowed."]}
|
||||||
|
)
|
||||||
|
|
||||||
|
response = self.client.post(
|
||||||
|
reverse("authentik_api:user-list"),
|
||||||
|
data={
|
||||||
|
"name": generate_id(),
|
||||||
|
"username": generate_id(),
|
||||||
|
"groups": [],
|
||||||
|
"path": "fos//o",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 400)
|
||||||
|
self.assertJSONEqual(
|
||||||
|
response.content.decode(), {"path": ["No empty segments in user path allowed."]}
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_me(self):
|
||||||
|
"""Test user's me endpoint"""
|
||||||
|
self.client.force_login(self.admin)
|
||||||
|
response = self.client.get(reverse("authentik_api:user-me"))
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
@CONFIG.patch("avatars", "none")
|
||||||
|
def test_avatars_none(self):
|
||||||
|
"""Test avatars none"""
|
||||||
|
self.client.force_login(self.admin)
|
||||||
|
response = self.client.get(reverse("authentik_api:user-me"))
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
body = loads(response.content.decode())
|
||||||
|
self.assertEqual(body["user"]["avatar"], "/static/dist/assets/images/user_default.png")
|
||||||
|
|
||||||
|
@CONFIG.patch("avatars", "gravatar")
|
||||||
|
def test_avatars_gravatar(self):
|
||||||
|
"""Test avatars gravatar"""
|
||||||
|
self.client.force_login(self.admin)
|
||||||
|
response = self.client.get(reverse("authentik_api:user-me"))
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
body = loads(response.content.decode())
|
||||||
|
self.assertIn("gravatar", body["user"]["avatar"])
|
||||||
|
|
||||||
|
@CONFIG.patch("avatars", "foo-%(username)s")
|
||||||
|
def test_avatars_custom(self):
|
||||||
|
"""Test avatars custom"""
|
||||||
|
self.client.force_login(self.admin)
|
||||||
|
response = self.client.get(reverse("authentik_api:user-me"))
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
body = loads(response.content.decode())
|
||||||
|
self.assertEqual(body["user"]["avatar"], f"foo-{self.admin.username}")
|
||||||
|
|
||||||
|
@CONFIG.patch("avatars", "attributes.foo.avatar")
|
||||||
|
def test_avatars_attributes(self):
|
||||||
|
"""Test avatars attributes"""
|
||||||
|
self.admin.attributes = {"foo": {"avatar": "bar"}}
|
||||||
|
self.admin.save()
|
||||||
|
self.client.force_login(self.admin)
|
||||||
|
response = self.client.get(reverse("authentik_api:user-me"))
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
body = loads(response.content.decode())
|
||||||
|
self.assertEqual(body["user"]["avatar"], "bar")
|
||||||
|
@ -11,14 +11,13 @@ from authentik.lib.generators import generate_id
|
|||||||
from authentik.tenants.models import Tenant
|
from authentik.tenants.models import Tenant
|
||||||
|
|
||||||
|
|
||||||
def create_test_flow(designation: FlowDesignation = FlowDesignation.STAGE_CONFIGURATION) -> Flow:
|
def create_test_flow(
|
||||||
|
designation: FlowDesignation = FlowDesignation.STAGE_CONFIGURATION, **kwargs
|
||||||
|
) -> Flow:
|
||||||
"""Generate a flow that can be used for testing"""
|
"""Generate a flow that can be used for testing"""
|
||||||
uid = generate_id(10)
|
uid = generate_id(10)
|
||||||
return Flow.objects.create(
|
return Flow.objects.create(
|
||||||
name=uid,
|
name=uid, title=uid, slug=slugify(uid), designation=designation, **kwargs
|
||||||
title=uid,
|
|
||||||
slug=slugify(uid),
|
|
||||||
designation=designation,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,17 +4,18 @@ from django.contrib.auth.decorators import login_required
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
from django.views.decorators.csrf import ensure_csrf_cookie
|
from django.views.decorators.csrf import ensure_csrf_cookie
|
||||||
from django.views.generic import RedirectView
|
from django.views.generic import RedirectView
|
||||||
from django.views.generic.base import TemplateView
|
|
||||||
|
|
||||||
from authentik.core.views import apps, impersonate
|
from authentik.core.views import apps, impersonate
|
||||||
from authentik.core.views.debug import AccessDeniedView
|
from authentik.core.views.debug import AccessDeniedView
|
||||||
from authentik.core.views.interface import FlowInterfaceView
|
from authentik.core.views.interface import FlowInterfaceView, InterfaceView
|
||||||
from authentik.core.views.session import EndSessionView
|
from authentik.core.views.session import EndSessionView
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path(
|
path(
|
||||||
"",
|
"",
|
||||||
login_required(RedirectView.as_view(pattern_name="authentik_core:if-user")),
|
login_required(
|
||||||
|
RedirectView.as_view(pattern_name="authentik_core:if-user", query_string=True)
|
||||||
|
),
|
||||||
name="root-redirect",
|
name="root-redirect",
|
||||||
),
|
),
|
||||||
path(
|
path(
|
||||||
@ -37,12 +38,12 @@ urlpatterns = [
|
|||||||
# Interfaces
|
# Interfaces
|
||||||
path(
|
path(
|
||||||
"if/admin/",
|
"if/admin/",
|
||||||
ensure_csrf_cookie(TemplateView.as_view(template_name="if/admin.html")),
|
ensure_csrf_cookie(InterfaceView.as_view(template_name="if/admin.html")),
|
||||||
name="if-admin",
|
name="if-admin",
|
||||||
),
|
),
|
||||||
path(
|
path(
|
||||||
"if/user/",
|
"if/user/",
|
||||||
ensure_csrf_cookie(TemplateView.as_view(template_name="if/user.html")),
|
ensure_csrf_cookie(InterfaceView.as_view(template_name="if/user.html")),
|
||||||
name="if-user",
|
name="if-user",
|
||||||
),
|
),
|
||||||
path(
|
path(
|
||||||
@ -56,10 +57,10 @@ urlpatterns = [
|
|||||||
name="if-session-end",
|
name="if-session-end",
|
||||||
),
|
),
|
||||||
# Fallback for WS
|
# Fallback for WS
|
||||||
path("ws/outpost/<uuid:pk>/", TemplateView.as_view(template_name="if/admin.html")),
|
path("ws/outpost/<uuid:pk>/", InterfaceView.as_view(template_name="if/admin.html")),
|
||||||
path(
|
path(
|
||||||
"ws/client/",
|
"ws/client/",
|
||||||
TemplateView.as_view(template_name="if/admin.html"),
|
InterfaceView.as_view(template_name="if/admin.html"),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -1,13 +1,26 @@
|
|||||||
"""Interface views"""
|
"""Interface views"""
|
||||||
|
from json import dumps
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from django.views.generic.base import TemplateView
|
from django.views.generic.base import TemplateView
|
||||||
|
from rest_framework.request import Request
|
||||||
|
|
||||||
|
from authentik.api.v3.config import ConfigView
|
||||||
from authentik.flows.models import Flow
|
from authentik.flows.models import Flow
|
||||||
|
from authentik.tenants.api import CurrentTenantSerializer
|
||||||
|
|
||||||
|
|
||||||
class FlowInterfaceView(TemplateView):
|
class InterfaceView(TemplateView):
|
||||||
|
"""Base interface view"""
|
||||||
|
|
||||||
|
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
|
||||||
|
kwargs["config_json"] = dumps(ConfigView(request=Request(self.request)).get_config().data)
|
||||||
|
kwargs["tenant_json"] = dumps(CurrentTenantSerializer(self.request.tenant).data)
|
||||||
|
return super().get_context_data(**kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class FlowInterfaceView(InterfaceView):
|
||||||
"""Flow interface"""
|
"""Flow interface"""
|
||||||
|
|
||||||
template_name = "if/flow.html"
|
template_name = "if/flow.html"
|
||||||
|
@ -22,8 +22,8 @@ from structlog.stdlib import get_logger
|
|||||||
from authentik.api.decorators import permission_required
|
from authentik.api.decorators import permission_required
|
||||||
from authentik.core.api.used_by import UsedByMixin
|
from authentik.core.api.used_by import UsedByMixin
|
||||||
from authentik.core.api.utils import PassiveSerializer
|
from authentik.core.api.utils import PassiveSerializer
|
||||||
|
from authentik.crypto.apps import MANAGED_KEY
|
||||||
from authentik.crypto.builder import CertificateBuilder
|
from authentik.crypto.builder import CertificateBuilder
|
||||||
from authentik.crypto.managed import MANAGED_KEY
|
|
||||||
from authentik.crypto.models import CertificateKeyPair
|
from authentik.crypto.models import CertificateKeyPair
|
||||||
from authentik.events.models import Event, EventAction
|
from authentik.events.models import Event, EventAction
|
||||||
|
|
||||||
|
@ -1,16 +1,72 @@
|
|||||||
"""authentik crypto app config"""
|
"""authentik crypto app config"""
|
||||||
from importlib import import_module
|
from datetime import datetime
|
||||||
|
from typing import TYPE_CHECKING, Optional
|
||||||
|
|
||||||
from django.apps import AppConfig
|
from authentik.blueprints.manager import ManagedAppConfig
|
||||||
|
from authentik.lib.generators import generate_id
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from authentik.crypto.models import CertificateKeyPair
|
||||||
|
|
||||||
|
MANAGED_KEY = "goauthentik.io/crypto/jwt-managed"
|
||||||
|
|
||||||
|
|
||||||
class AuthentikCryptoConfig(AppConfig):
|
class AuthentikCryptoConfig(ManagedAppConfig):
|
||||||
"""authentik crypto app config"""
|
"""authentik crypto app config"""
|
||||||
|
|
||||||
name = "authentik.crypto"
|
name = "authentik.crypto"
|
||||||
label = "authentik_crypto"
|
label = "authentik_crypto"
|
||||||
verbose_name = "authentik Crypto"
|
verbose_name = "authentik Crypto"
|
||||||
|
default = True
|
||||||
|
|
||||||
def ready(self):
|
def reconcile_load_crypto_tasks(self):
|
||||||
import_module("authentik.crypto.managed")
|
"""Load crypto tasks"""
|
||||||
import_module("authentik.crypto.tasks")
|
self.import_module("authentik.crypto.tasks")
|
||||||
|
|
||||||
|
def _create_update_cert(self, cert: Optional["CertificateKeyPair"] = None):
|
||||||
|
from authentik.crypto.builder import CertificateBuilder
|
||||||
|
from authentik.crypto.models import CertificateKeyPair
|
||||||
|
|
||||||
|
builder = CertificateBuilder()
|
||||||
|
builder.common_name = "goauthentik.io"
|
||||||
|
builder.build(
|
||||||
|
subject_alt_names=["goauthentik.io"],
|
||||||
|
validity_days=360,
|
||||||
|
)
|
||||||
|
if not cert:
|
||||||
|
|
||||||
|
cert = CertificateKeyPair()
|
||||||
|
cert.certificate_data = builder.certificate
|
||||||
|
cert.key_data = builder.private_key
|
||||||
|
cert.name = "authentik Internal JWT Certificate"
|
||||||
|
cert.managed = MANAGED_KEY
|
||||||
|
cert.save()
|
||||||
|
|
||||||
|
def reconcile_managed_jwt_cert(self):
|
||||||
|
"""Ensure managed JWT certificate"""
|
||||||
|
from authentik.crypto.models import CertificateKeyPair
|
||||||
|
|
||||||
|
certs = CertificateKeyPair.objects.filter(managed=MANAGED_KEY)
|
||||||
|
if not certs.exists():
|
||||||
|
self._create_update_cert()
|
||||||
|
return
|
||||||
|
cert: CertificateKeyPair = certs.first()
|
||||||
|
now = datetime.now()
|
||||||
|
if now < cert.certificate.not_valid_before or now > cert.certificate.not_valid_after:
|
||||||
|
self._create_update_cert(cert)
|
||||||
|
|
||||||
|
def reconcile_self_signed(self):
|
||||||
|
"""Create self-signed keypair"""
|
||||||
|
from authentik.crypto.builder import CertificateBuilder
|
||||||
|
from authentik.crypto.models import CertificateKeyPair
|
||||||
|
|
||||||
|
name = "authentik Self-signed Certificate"
|
||||||
|
if CertificateKeyPair.objects.filter(name=name).exists():
|
||||||
|
return
|
||||||
|
builder = CertificateBuilder()
|
||||||
|
builder.build(subject_alt_names=[f"{generate_id()}.self-signed.goauthentik.io"])
|
||||||
|
CertificateKeyPair.objects.create(
|
||||||
|
name="authentik Self-signed Certificate",
|
||||||
|
certificate_data=builder.certificate,
|
||||||
|
key_data=builder.private_key,
|
||||||
|
)
|
||||||
|
@ -1,40 +0,0 @@
|
|||||||
"""Crypto managed objects"""
|
|
||||||
from datetime import datetime
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from authentik.crypto.builder import CertificateBuilder
|
|
||||||
from authentik.crypto.models import CertificateKeyPair
|
|
||||||
from authentik.managed.manager import ObjectManager
|
|
||||||
|
|
||||||
MANAGED_KEY = "goauthentik.io/crypto/jwt-managed"
|
|
||||||
|
|
||||||
|
|
||||||
class CryptoManager(ObjectManager):
|
|
||||||
"""Crypto managed objects"""
|
|
||||||
|
|
||||||
def _create(self, cert: Optional[CertificateKeyPair] = None):
|
|
||||||
builder = CertificateBuilder()
|
|
||||||
builder.common_name = "goauthentik.io"
|
|
||||||
builder.build(
|
|
||||||
subject_alt_names=["goauthentik.io"],
|
|
||||||
validity_days=360,
|
|
||||||
)
|
|
||||||
if not cert:
|
|
||||||
cert = CertificateKeyPair()
|
|
||||||
cert.certificate_data = builder.certificate
|
|
||||||
cert.key_data = builder.private_key
|
|
||||||
cert.name = "authentik Internal JWT Certificate"
|
|
||||||
cert.managed = MANAGED_KEY
|
|
||||||
cert.save()
|
|
||||||
|
|
||||||
def reconcile(self):
|
|
||||||
certs = CertificateKeyPair.objects.filter(managed=MANAGED_KEY)
|
|
||||||
if not certs.exists():
|
|
||||||
self._create()
|
|
||||||
return []
|
|
||||||
cert: CertificateKeyPair = certs.first()
|
|
||||||
now = datetime.now()
|
|
||||||
if now < cert.certificate.not_valid_before or now > cert.certificate.not_valid_after:
|
|
||||||
self._create(cert)
|
|
||||||
return []
|
|
||||||
return []
|
|
@ -5,24 +5,10 @@ from django.db import migrations
|
|||||||
from authentik.lib.generators import generate_id
|
from authentik.lib.generators import generate_id
|
||||||
|
|
||||||
|
|
||||||
def create_self_signed(apps, schema_editor):
|
|
||||||
CertificateKeyPair = apps.get_model("authentik_crypto", "CertificateKeyPair")
|
|
||||||
db_alias = schema_editor.connection.alias
|
|
||||||
from authentik.crypto.builder import CertificateBuilder
|
|
||||||
|
|
||||||
builder = CertificateBuilder()
|
|
||||||
builder.build(subject_alt_names=[f"{generate_id()}.self-signed.goauthentik.io"])
|
|
||||||
CertificateKeyPair.objects.using(db_alias).create(
|
|
||||||
name="authentik Self-signed Certificate",
|
|
||||||
certificate_data=builder.certificate,
|
|
||||||
key_data=builder.private_key,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
("authentik_crypto", "0001_initial"),
|
("authentik_crypto", "0001_initial"),
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [migrations.RunPython(create_self_signed)]
|
operations = []
|
||||||
|
@ -16,15 +16,16 @@ from cryptography.hazmat.primitives.serialization import load_pem_private_key
|
|||||||
from cryptography.x509 import Certificate, load_pem_x509_certificate
|
from cryptography.x509 import Certificate, load_pem_x509_certificate
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
from rest_framework.serializers import Serializer
|
||||||
from structlog.stdlib import get_logger
|
from structlog.stdlib import get_logger
|
||||||
|
|
||||||
from authentik.lib.models import CreatedUpdatedModel
|
from authentik.blueprints.models import ManagedModel
|
||||||
from authentik.managed.models import ManagedModel
|
from authentik.lib.models import CreatedUpdatedModel, SerializerModel
|
||||||
|
|
||||||
LOGGER = get_logger()
|
LOGGER = get_logger()
|
||||||
|
|
||||||
|
|
||||||
class CertificateKeyPair(ManagedModel, CreatedUpdatedModel):
|
class CertificateKeyPair(SerializerModel, ManagedModel, CreatedUpdatedModel):
|
||||||
"""CertificateKeyPair that can be used for signing or encrypting if `key_data`
|
"""CertificateKeyPair that can be used for signing or encrypting if `key_data`
|
||||||
is set, otherwise it can be used to verify remote data."""
|
is set, otherwise it can be used to verify remote data."""
|
||||||
|
|
||||||
@ -44,6 +45,12 @@ class CertificateKeyPair(ManagedModel, CreatedUpdatedModel):
|
|||||||
_private_key: Optional[RSAPrivateKey | EllipticCurvePrivateKey | Ed25519PrivateKey] = None
|
_private_key: Optional[RSAPrivateKey | EllipticCurvePrivateKey | Ed25519PrivateKey] = None
|
||||||
_public_key: Optional[RSAPublicKey | EllipticCurvePublicKey | Ed25519PublicKey] = None
|
_public_key: Optional[RSAPublicKey | EllipticCurvePublicKey | Ed25519PublicKey] = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serializer(self) -> Serializer:
|
||||||
|
from authentik.crypto.api import CertificateKeyPairSerializer
|
||||||
|
|
||||||
|
return CertificateKeyPairSerializer
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def certificate(self) -> Certificate:
|
def certificate(self) -> Certificate:
|
||||||
"""Get python cryptography Certificate instance"""
|
"""Get python cryptography Certificate instance"""
|
||||||
|
@ -1,15 +1,23 @@
|
|||||||
"""authentik events app"""
|
"""authentik events app"""
|
||||||
from importlib import import_module
|
from prometheus_client import Gauge
|
||||||
|
|
||||||
from django.apps import AppConfig
|
from authentik.blueprints.manager import ManagedAppConfig
|
||||||
|
|
||||||
|
GAUGE_TASKS = Gauge(
|
||||||
|
"authentik_system_tasks",
|
||||||
|
"System tasks and their status",
|
||||||
|
["task_name", "task_uid", "status"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class AuthentikEventsConfig(AppConfig):
|
class AuthentikEventsConfig(ManagedAppConfig):
|
||||||
"""authentik events app"""
|
"""authentik events app"""
|
||||||
|
|
||||||
name = "authentik.events"
|
name = "authentik.events"
|
||||||
label = "authentik_events"
|
label = "authentik_events"
|
||||||
verbose_name = "authentik Events"
|
verbose_name = "authentik Events"
|
||||||
|
default = True
|
||||||
|
|
||||||
def ready(self):
|
def reconcile_load_events_signals(self):
|
||||||
import_module("authentik.events.signals")
|
"""Load events signals"""
|
||||||
|
self.import_module("authentik.events.signals")
|
||||||
|
@ -11,11 +11,11 @@ from django.http import HttpRequest, HttpResponse
|
|||||||
from django_otp.plugins.otp_static.models import StaticToken
|
from django_otp.plugins.otp_static.models import StaticToken
|
||||||
from guardian.models import UserObjectPermission
|
from guardian.models import UserObjectPermission
|
||||||
|
|
||||||
from authentik.core.middleware import LOCAL
|
|
||||||
from authentik.core.models import AuthenticatedSession, User
|
from authentik.core.models import AuthenticatedSession, User
|
||||||
from authentik.events.models import Event, EventAction, Notification
|
from authentik.events.models import Event, EventAction, Notification
|
||||||
from authentik.events.signals import EventNewThread
|
from authentik.events.signals import EventNewThread
|
||||||
from authentik.events.utils import model_to_dict
|
from authentik.events.utils import model_to_dict
|
||||||
|
from authentik.flows.models import FlowToken
|
||||||
from authentik.lib.sentry import before_send
|
from authentik.lib.sentry import before_send
|
||||||
from authentik.lib.utils.errors import exception_to_string
|
from authentik.lib.utils.errors import exception_to_string
|
||||||
|
|
||||||
@ -26,6 +26,7 @@ IGNORED_MODELS = [
|
|||||||
AuthenticatedSession,
|
AuthenticatedSession,
|
||||||
StaticToken,
|
StaticToken,
|
||||||
Session,
|
Session,
|
||||||
|
FlowToken,
|
||||||
]
|
]
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
from silk.models import Request, Response, SQLQuery
|
from silk.models import Request, Response, SQLQuery
|
||||||
@ -43,36 +44,46 @@ class AuditMiddleware:
|
|||||||
def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]):
|
def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]):
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
|
|
||||||
|
def connect(self, request: HttpRequest):
|
||||||
|
"""Connect signal for automatic logging"""
|
||||||
|
if not hasattr(request, "user"):
|
||||||
|
return
|
||||||
|
if not getattr(request.user, "is_authenticated", False):
|
||||||
|
return
|
||||||
|
if not hasattr(request, "request_id"):
|
||||||
|
return
|
||||||
|
post_save_handler = partial(self.post_save_handler, user=request.user, request=request)
|
||||||
|
pre_delete_handler = partial(self.pre_delete_handler, user=request.user, request=request)
|
||||||
|
post_save.connect(
|
||||||
|
post_save_handler,
|
||||||
|
dispatch_uid=request.request_id,
|
||||||
|
weak=False,
|
||||||
|
)
|
||||||
|
pre_delete.connect(
|
||||||
|
pre_delete_handler,
|
||||||
|
dispatch_uid=request.request_id,
|
||||||
|
weak=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
def disconnect(self, request: HttpRequest):
|
||||||
|
"""Disconnect signals"""
|
||||||
|
if not hasattr(request, "request_id"):
|
||||||
|
return
|
||||||
|
post_save.disconnect(dispatch_uid=request.request_id)
|
||||||
|
pre_delete.disconnect(dispatch_uid=request.request_id)
|
||||||
|
|
||||||
def __call__(self, request: HttpRequest) -> HttpResponse:
|
def __call__(self, request: HttpRequest) -> HttpResponse:
|
||||||
# Connect signal for automatic logging
|
self.connect(request)
|
||||||
if hasattr(request, "user") and getattr(request.user, "is_authenticated", False):
|
|
||||||
post_save_handler = partial(self.post_save_handler, user=request.user, request=request)
|
|
||||||
pre_delete_handler = partial(
|
|
||||||
self.pre_delete_handler, user=request.user, request=request
|
|
||||||
)
|
|
||||||
post_save.connect(
|
|
||||||
post_save_handler,
|
|
||||||
dispatch_uid=LOCAL.authentik["request_id"],
|
|
||||||
weak=False,
|
|
||||||
)
|
|
||||||
pre_delete.connect(
|
|
||||||
pre_delete_handler,
|
|
||||||
dispatch_uid=LOCAL.authentik["request_id"],
|
|
||||||
weak=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
response = self.get_response(request)
|
response = self.get_response(request)
|
||||||
|
|
||||||
post_save.disconnect(dispatch_uid=LOCAL.authentik["request_id"])
|
self.disconnect(request)
|
||||||
pre_delete.disconnect(dispatch_uid=LOCAL.authentik["request_id"])
|
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def process_exception(self, request: HttpRequest, exception: Exception):
|
def process_exception(self, request: HttpRequest, exception: Exception):
|
||||||
"""Disconnect handlers in case of exception"""
|
"""Disconnect handlers in case of exception"""
|
||||||
post_save.disconnect(dispatch_uid=LOCAL.authentik["request_id"])
|
self.disconnect(request)
|
||||||
pre_delete.disconnect(dispatch_uid=LOCAL.authentik["request_id"])
|
|
||||||
|
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
return
|
return
|
||||||
|
@ -1,29 +1,5 @@
|
|||||||
# Generated by Django 4.0.4 on 2022-05-30 18:08
|
# Generated by Django 4.0.4 on 2022-05-30 18:08
|
||||||
from django.apps.registry import Apps
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
|
||||||
|
|
||||||
from authentik.events.models import TransportMode
|
|
||||||
|
|
||||||
|
|
||||||
def notify_local_transport(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
|
||||||
db_alias = schema_editor.connection.alias
|
|
||||||
NotificationTransport = apps.get_model("authentik_events", "NotificationTransport")
|
|
||||||
NotificationRule = apps.get_model("authentik_events", "NotificationRule")
|
|
||||||
|
|
||||||
local_transport, _ = NotificationTransport.objects.using(db_alias).update_or_create(
|
|
||||||
name="default-local-transport",
|
|
||||||
defaults={"mode": TransportMode.LOCAL},
|
|
||||||
)
|
|
||||||
|
|
||||||
for trigger in NotificationRule.objects.using(db_alias).filter(
|
|
||||||
name__in=[
|
|
||||||
"default-notify-configuration-error",
|
|
||||||
"default-notify-exception",
|
|
||||||
"default-notify-update",
|
|
||||||
]
|
|
||||||
):
|
|
||||||
trigger.transports.add(local_transport)
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
@ -46,5 +22,4 @@ class Migration(migrations.Migration):
|
|||||||
default="local",
|
default="local",
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
migrations.RunPython(notify_local_transport),
|
|
||||||
]
|
]
|
||||||
|
@ -1,130 +1,6 @@
|
|||||||
# Generated by Django 3.1.4 on 2021-01-10 18:57
|
# Generated by Django 3.1.4 on 2021-01-10 18:57
|
||||||
|
|
||||||
from django.apps.registry import Apps
|
|
||||||
from django.db import migrations
|
from django.db import migrations
|
||||||
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
|
||||||
|
|
||||||
from authentik.events.models import EventAction, NotificationSeverity, TransportMode
|
|
||||||
|
|
||||||
|
|
||||||
def notify_configuration_error(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
|
||||||
db_alias = schema_editor.connection.alias
|
|
||||||
Group = apps.get_model("authentik_core", "Group")
|
|
||||||
PolicyBinding = apps.get_model("authentik_policies", "PolicyBinding")
|
|
||||||
EventMatcherPolicy = apps.get_model("authentik_policies_event_matcher", "EventMatcherPolicy")
|
|
||||||
NotificationRule = apps.get_model("authentik_events", "NotificationRule")
|
|
||||||
NotificationTransport = apps.get_model("authentik_events", "NotificationTransport")
|
|
||||||
|
|
||||||
admin_group = (
|
|
||||||
Group.objects.using(db_alias).filter(name="authentik Admins", is_superuser=True).first()
|
|
||||||
)
|
|
||||||
|
|
||||||
policy, _ = EventMatcherPolicy.objects.using(db_alias).update_or_create(
|
|
||||||
name="default-match-configuration-error",
|
|
||||||
defaults={"action": EventAction.CONFIGURATION_ERROR},
|
|
||||||
)
|
|
||||||
trigger, _ = NotificationRule.objects.using(db_alias).update_or_create(
|
|
||||||
name="default-notify-configuration-error",
|
|
||||||
defaults={"group": admin_group, "severity": NotificationSeverity.ALERT},
|
|
||||||
)
|
|
||||||
trigger.transports.set(
|
|
||||||
NotificationTransport.objects.using(db_alias).filter(name="default-email-transport")
|
|
||||||
)
|
|
||||||
trigger.save()
|
|
||||||
PolicyBinding.objects.using(db_alias).update_or_create(
|
|
||||||
target=trigger,
|
|
||||||
policy=policy,
|
|
||||||
defaults={
|
|
||||||
"order": 0,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def notify_update(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
|
||||||
db_alias = schema_editor.connection.alias
|
|
||||||
Group = apps.get_model("authentik_core", "Group")
|
|
||||||
PolicyBinding = apps.get_model("authentik_policies", "PolicyBinding")
|
|
||||||
EventMatcherPolicy = apps.get_model("authentik_policies_event_matcher", "EventMatcherPolicy")
|
|
||||||
NotificationRule = apps.get_model("authentik_events", "NotificationRule")
|
|
||||||
NotificationTransport = apps.get_model("authentik_events", "NotificationTransport")
|
|
||||||
|
|
||||||
admin_group = (
|
|
||||||
Group.objects.using(db_alias).filter(name="authentik Admins", is_superuser=True).first()
|
|
||||||
)
|
|
||||||
|
|
||||||
policy, _ = EventMatcherPolicy.objects.using(db_alias).update_or_create(
|
|
||||||
name="default-match-update",
|
|
||||||
defaults={"action": EventAction.UPDATE_AVAILABLE},
|
|
||||||
)
|
|
||||||
trigger, _ = NotificationRule.objects.using(db_alias).update_or_create(
|
|
||||||
name="default-notify-update",
|
|
||||||
defaults={"group": admin_group, "severity": NotificationSeverity.ALERT},
|
|
||||||
)
|
|
||||||
trigger.transports.set(
|
|
||||||
NotificationTransport.objects.using(db_alias).filter(name="default-email-transport")
|
|
||||||
)
|
|
||||||
trigger.save()
|
|
||||||
PolicyBinding.objects.using(db_alias).update_or_create(
|
|
||||||
target=trigger,
|
|
||||||
policy=policy,
|
|
||||||
defaults={
|
|
||||||
"order": 0,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def notify_exception(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
|
||||||
db_alias = schema_editor.connection.alias
|
|
||||||
Group = apps.get_model("authentik_core", "Group")
|
|
||||||
PolicyBinding = apps.get_model("authentik_policies", "PolicyBinding")
|
|
||||||
EventMatcherPolicy = apps.get_model("authentik_policies_event_matcher", "EventMatcherPolicy")
|
|
||||||
NotificationRule = apps.get_model("authentik_events", "NotificationRule")
|
|
||||||
NotificationTransport = apps.get_model("authentik_events", "NotificationTransport")
|
|
||||||
|
|
||||||
admin_group = (
|
|
||||||
Group.objects.using(db_alias).filter(name="authentik Admins", is_superuser=True).first()
|
|
||||||
)
|
|
||||||
|
|
||||||
policy_policy_exc, _ = EventMatcherPolicy.objects.using(db_alias).update_or_create(
|
|
||||||
name="default-match-policy-exception",
|
|
||||||
defaults={"action": EventAction.POLICY_EXCEPTION},
|
|
||||||
)
|
|
||||||
policy_pm_exc, _ = EventMatcherPolicy.objects.using(db_alias).update_or_create(
|
|
||||||
name="default-match-property-mapping-exception",
|
|
||||||
defaults={"action": EventAction.PROPERTY_MAPPING_EXCEPTION},
|
|
||||||
)
|
|
||||||
trigger, _ = NotificationRule.objects.using(db_alias).update_or_create(
|
|
||||||
name="default-notify-exception",
|
|
||||||
defaults={"group": admin_group, "severity": NotificationSeverity.ALERT},
|
|
||||||
)
|
|
||||||
trigger.transports.set(
|
|
||||||
NotificationTransport.objects.using(db_alias).filter(name="default-email-transport")
|
|
||||||
)
|
|
||||||
trigger.save()
|
|
||||||
PolicyBinding.objects.using(db_alias).update_or_create(
|
|
||||||
target=trigger,
|
|
||||||
policy=policy_policy_exc,
|
|
||||||
defaults={
|
|
||||||
"order": 0,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
PolicyBinding.objects.using(db_alias).update_or_create(
|
|
||||||
target=trigger,
|
|
||||||
policy=policy_pm_exc,
|
|
||||||
defaults={
|
|
||||||
"order": 1,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def transport_email_global(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
|
||||||
db_alias = schema_editor.connection.alias
|
|
||||||
NotificationTransport = apps.get_model("authentik_events", "NotificationTransport")
|
|
||||||
|
|
||||||
NotificationTransport.objects.using(db_alias).update_or_create(
|
|
||||||
name="default-email-transport",
|
|
||||||
defaults={"mode": TransportMode.EMAIL},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
@ -134,14 +10,6 @@ class Migration(migrations.Migration):
|
|||||||
"authentik_events",
|
"authentik_events",
|
||||||
"0010_notification_notificationtransport_notificationrule",
|
"0010_notification_notificationtransport_notificationrule",
|
||||||
),
|
),
|
||||||
("authentik_core", "0016_auto_20201202_2234"),
|
|
||||||
("authentik_policies_event_matcher", "0003_auto_20210110_1907"),
|
|
||||||
("authentik_policies", "0004_policy_execution_logging"),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = []
|
||||||
migrations.RunPython(transport_email_global),
|
|
||||||
migrations.RunPython(notify_configuration_error),
|
|
||||||
migrations.RunPython(notify_update),
|
|
||||||
migrations.RunPython(notify_exception),
|
|
||||||
]
|
|
||||||
|
@ -30,7 +30,7 @@ from authentik.core.middleware import (
|
|||||||
from authentik.core.models import ExpiringModel, Group, PropertyMapping, User
|
from authentik.core.models import ExpiringModel, Group, PropertyMapping, User
|
||||||
from authentik.events.geo import GEOIP_READER
|
from authentik.events.geo import GEOIP_READER
|
||||||
from authentik.events.utils import cleanse_dict, get_user, model_to_dict, sanitize_dict
|
from authentik.events.utils import cleanse_dict, get_user, model_to_dict, sanitize_dict
|
||||||
from authentik.lib.models import DomainlessURLValidator
|
from authentik.lib.models import DomainlessURLValidator, SerializerModel
|
||||||
from authentik.lib.sentry import SentryIgnoredException
|
from authentik.lib.sentry import SentryIgnoredException
|
||||||
from authentik.lib.utils.http import get_client_ip, get_http_session
|
from authentik.lib.utils.http import get_client_ip, get_http_session
|
||||||
from authentik.lib.utils.time import timedelta_from_string
|
from authentik.lib.utils.time import timedelta_from_string
|
||||||
@ -168,7 +168,7 @@ class EventManager(Manager):
|
|||||||
return self.get_queryset().get_events_per_day()
|
return self.get_queryset().get_events_per_day()
|
||||||
|
|
||||||
|
|
||||||
class Event(ExpiringModel):
|
class Event(SerializerModel, ExpiringModel):
|
||||||
"""An individual Audit/Metrics/Notification/Error Event"""
|
"""An individual Audit/Metrics/Notification/Error Event"""
|
||||||
|
|
||||||
event_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
event_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
||||||
@ -273,6 +273,12 @@ class Event(ExpiringModel):
|
|||||||
)
|
)
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serializer(self) -> "Serializer":
|
||||||
|
from authentik.events.api.events import EventSerializer
|
||||||
|
|
||||||
|
return EventSerializer
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def summary(self) -> str:
|
def summary(self) -> str:
|
||||||
"""Return a summary of this event."""
|
"""Return a summary of this event."""
|
||||||
@ -298,7 +304,7 @@ class TransportMode(models.TextChoices):
|
|||||||
EMAIL = "email", _("Email")
|
EMAIL = "email", _("Email")
|
||||||
|
|
||||||
|
|
||||||
class NotificationTransport(models.Model):
|
class NotificationTransport(SerializerModel):
|
||||||
"""Action which is executed when a Rule matches"""
|
"""Action which is executed when a Rule matches"""
|
||||||
|
|
||||||
uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
||||||
@ -448,6 +454,12 @@ class NotificationTransport(models.Model):
|
|||||||
except (SMTPException, ConnectionError, OSError) as exc:
|
except (SMTPException, ConnectionError, OSError) as exc:
|
||||||
raise NotificationTransportError from exc
|
raise NotificationTransportError from exc
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serializer(self) -> "Serializer":
|
||||||
|
from authentik.events.api.notification_transports import NotificationTransportSerializer
|
||||||
|
|
||||||
|
return NotificationTransportSerializer
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return f"Notification Transport {self.name}"
|
return f"Notification Transport {self.name}"
|
||||||
|
|
||||||
@ -465,7 +477,7 @@ class NotificationSeverity(models.TextChoices):
|
|||||||
ALERT = "alert", _("Alert")
|
ALERT = "alert", _("Alert")
|
||||||
|
|
||||||
|
|
||||||
class Notification(models.Model):
|
class Notification(SerializerModel):
|
||||||
"""Event Notification"""
|
"""Event Notification"""
|
||||||
|
|
||||||
uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
|
||||||
@ -476,6 +488,12 @@ class Notification(models.Model):
|
|||||||
seen = models.BooleanField(default=False)
|
seen = models.BooleanField(default=False)
|
||||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serializer(self) -> "Serializer":
|
||||||
|
from authentik.events.api.notifications import NotificationSerializer
|
||||||
|
|
||||||
|
return NotificationSerializer
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
body_trunc = (self.body[:75] + "..") if len(self.body) > 75 else self.body
|
body_trunc = (self.body[:75] + "..") if len(self.body) > 75 else self.body
|
||||||
return f"Notification for user {self.user}: {body_trunc}"
|
return f"Notification for user {self.user}: {body_trunc}"
|
||||||
@ -486,7 +504,7 @@ class Notification(models.Model):
|
|||||||
verbose_name_plural = _("Notifications")
|
verbose_name_plural = _("Notifications")
|
||||||
|
|
||||||
|
|
||||||
class NotificationRule(PolicyBindingModel):
|
class NotificationRule(SerializerModel, PolicyBindingModel):
|
||||||
"""Decide when to create a Notification based on policies attached to this object."""
|
"""Decide when to create a Notification based on policies attached to this object."""
|
||||||
|
|
||||||
name = models.TextField(unique=True)
|
name = models.TextField(unique=True)
|
||||||
@ -518,6 +536,12 @@ class NotificationRule(PolicyBindingModel):
|
|||||||
on_delete=models.SET_NULL,
|
on_delete=models.SET_NULL,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def serializer(self) -> "Serializer":
|
||||||
|
from authentik.events.api.notification_rules import NotificationRuleSerializer
|
||||||
|
|
||||||
|
return NotificationRuleSerializer
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return f"Notification Rule {self.name}"
|
return f"Notification Rule {self.name}"
|
||||||
|
|
||||||
|
@ -8,18 +8,12 @@ from typing import Any, Optional
|
|||||||
from celery import Task
|
from celery import Task
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from prometheus_client import Gauge
|
|
||||||
from structlog.stdlib import get_logger
|
from structlog.stdlib import get_logger
|
||||||
|
|
||||||
|
from authentik.events.apps import GAUGE_TASKS
|
||||||
from authentik.events.models import Event, EventAction
|
from authentik.events.models import Event, EventAction
|
||||||
from authentik.lib.utils.errors import exception_to_string
|
from authentik.lib.utils.errors import exception_to_string
|
||||||
|
|
||||||
GAUGE_TASKS = Gauge(
|
|
||||||
"authentik_system_tasks",
|
|
||||||
"System tasks and their status",
|
|
||||||
["task_name", "task_uid", "status"],
|
|
||||||
)
|
|
||||||
|
|
||||||
LOGGER = get_logger()
|
LOGGER = get_logger()
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""event utilities"""
|
"""event utilities"""
|
||||||
import re
|
import re
|
||||||
from dataclasses import asdict, is_dataclass
|
from dataclasses import asdict, is_dataclass
|
||||||
|
from pathlib import Path
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
@ -22,21 +23,31 @@ from authentik.policies.types import PolicyRequest
|
|||||||
ALLOWED_SPECIAL_KEYS = re.compile("passing", flags=re.I)
|
ALLOWED_SPECIAL_KEYS = re.compile("passing", flags=re.I)
|
||||||
|
|
||||||
|
|
||||||
|
def cleanse_item(key: str, value: Any) -> Any:
|
||||||
|
"""Cleanse a single item"""
|
||||||
|
if isinstance(value, dict):
|
||||||
|
return cleanse_dict(value)
|
||||||
|
if isinstance(value, list):
|
||||||
|
for idx, item in enumerate(value):
|
||||||
|
value[idx] = cleanse_item(key, item)
|
||||||
|
return value
|
||||||
|
try:
|
||||||
|
if SafeExceptionReporterFilter.hidden_settings.search(
|
||||||
|
key
|
||||||
|
) and not ALLOWED_SPECIAL_KEYS.search(key):
|
||||||
|
return SafeExceptionReporterFilter.cleansed_substitute
|
||||||
|
except TypeError: # pragma: no cover
|
||||||
|
return value
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
def cleanse_dict(source: dict[Any, Any]) -> dict[Any, Any]:
|
def cleanse_dict(source: dict[Any, Any]) -> dict[Any, Any]:
|
||||||
"""Cleanse a dictionary, recursively"""
|
"""Cleanse a dictionary, recursively"""
|
||||||
final_dict = {}
|
final_dict = {}
|
||||||
for key, value in source.items():
|
for key, value in source.items():
|
||||||
try:
|
new_value = cleanse_item(key, value)
|
||||||
if SafeExceptionReporterFilter.hidden_settings.search(
|
if new_value is not ...:
|
||||||
key
|
final_dict[key] = new_value
|
||||||
) and not ALLOWED_SPECIAL_KEYS.search(key):
|
|
||||||
final_dict[key] = SafeExceptionReporterFilter.cleansed_substitute
|
|
||||||
else:
|
|
||||||
final_dict[key] = value
|
|
||||||
except TypeError: # pragma: no cover
|
|
||||||
final_dict[key] = value
|
|
||||||
if isinstance(value, dict):
|
|
||||||
final_dict[key] = cleanse_dict(value)
|
|
||||||
return final_dict
|
return final_dict
|
||||||
|
|
||||||
|
|
||||||
@ -69,6 +80,45 @@ def get_user(user: User, original_user: Optional[User] = None) -> dict[str, Any]
|
|||||||
return user_data
|
return user_data
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=too-many-return-statements
|
||||||
|
def sanitize_item(value: Any) -> Any:
|
||||||
|
"""Sanitize a single item, ensure it is JSON parsable"""
|
||||||
|
if is_dataclass(value):
|
||||||
|
# Because asdict calls `copy.deepcopy(obj)` on everything that's not tuple/dict,
|
||||||
|
# and deepcopy doesn't work with HttpRequests (neither django nor rest_framework).
|
||||||
|
# Currently, the only dataclass that actually holds an http request is a PolicyRequest
|
||||||
|
if isinstance(value, PolicyRequest):
|
||||||
|
value.http_request = None
|
||||||
|
value = asdict(value)
|
||||||
|
if isinstance(value, dict):
|
||||||
|
return sanitize_dict(value)
|
||||||
|
if isinstance(value, list):
|
||||||
|
new_values = []
|
||||||
|
for item in value:
|
||||||
|
new_value = sanitize_item(item)
|
||||||
|
if new_value:
|
||||||
|
new_values.append(new_value)
|
||||||
|
return new_values
|
||||||
|
if isinstance(value, (User, AnonymousUser)):
|
||||||
|
return sanitize_dict(get_user(value))
|
||||||
|
if isinstance(value, models.Model):
|
||||||
|
return sanitize_dict(model_to_dict(value))
|
||||||
|
if isinstance(value, UUID):
|
||||||
|
return value.hex
|
||||||
|
if isinstance(value, (HttpRequest, WSGIRequest)):
|
||||||
|
return ...
|
||||||
|
if isinstance(value, City):
|
||||||
|
return GEOIP_READER.city_to_dict(value)
|
||||||
|
if isinstance(value, Path):
|
||||||
|
return str(value)
|
||||||
|
if isinstance(value, type):
|
||||||
|
return {
|
||||||
|
"type": value.__name__,
|
||||||
|
"module": value.__module__,
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
def sanitize_dict(source: dict[Any, Any]) -> dict[Any, Any]:
|
def sanitize_dict(source: dict[Any, Any]) -> dict[Any, Any]:
|
||||||
"""clean source of all Models that would interfere with the JSONField.
|
"""clean source of all Models that would interfere with the JSONField.
|
||||||
Models are replaced with a dictionary of {
|
Models are replaced with a dictionary of {
|
||||||
@ -78,30 +128,7 @@ def sanitize_dict(source: dict[Any, Any]) -> dict[Any, Any]:
|
|||||||
}"""
|
}"""
|
||||||
final_dict = {}
|
final_dict = {}
|
||||||
for key, value in source.items():
|
for key, value in source.items():
|
||||||
if is_dataclass(value):
|
new_value = sanitize_item(value)
|
||||||
# Because asdict calls `copy.deepcopy(obj)` on everything that's not tuple/dict,
|
if new_value is not ...:
|
||||||
# and deepcopy doesn't work with HttpRequests (neither django nor rest_framework).
|
final_dict[key] = new_value
|
||||||
# Currently, the only dataclass that actually holds an http request is a PolicyRequest
|
|
||||||
if isinstance(value, PolicyRequest):
|
|
||||||
value.http_request = None
|
|
||||||
value = asdict(value)
|
|
||||||
if isinstance(value, dict):
|
|
||||||
final_dict[key] = sanitize_dict(value)
|
|
||||||
elif isinstance(value, (User, AnonymousUser)):
|
|
||||||
final_dict[key] = sanitize_dict(get_user(value))
|
|
||||||
elif isinstance(value, models.Model):
|
|
||||||
final_dict[key] = sanitize_dict(model_to_dict(value))
|
|
||||||
elif isinstance(value, UUID):
|
|
||||||
final_dict[key] = value.hex
|
|
||||||
elif isinstance(value, (HttpRequest, WSGIRequest)):
|
|
||||||
continue
|
|
||||||
elif isinstance(value, City):
|
|
||||||
final_dict[key] = GEOIP_READER.city_to_dict(value)
|
|
||||||
elif isinstance(value, type):
|
|
||||||
final_dict[key] = {
|
|
||||||
"type": value.__name__,
|
|
||||||
"module": value.__module__,
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
final_dict[key] = value
|
|
||||||
return final_dict
|
return final_dict
|
||||||
|
@ -3,7 +3,8 @@ from dataclasses import dataclass
|
|||||||
|
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.db.models import Model
|
from django.db.models import Model
|
||||||
from django.http.response import HttpResponseBadRequest, JsonResponse
|
from django.http import HttpResponse
|
||||||
|
from django.http.response import HttpResponseBadRequest
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
from drf_spectacular.types import OpenApiTypes
|
from drf_spectacular.types import OpenApiTypes
|
||||||
@ -19,6 +20,8 @@ from rest_framework.viewsets import ModelViewSet
|
|||||||
from structlog.stdlib import get_logger
|
from structlog.stdlib import get_logger
|
||||||
|
|
||||||
from authentik.api.decorators import permission_required
|
from authentik.api.decorators import permission_required
|
||||||
|
from authentik.blueprints.v1.exporter import FlowExporter
|
||||||
|
from authentik.blueprints.v1.importer import Importer
|
||||||
from authentik.core.api.used_by import UsedByMixin
|
from authentik.core.api.used_by import UsedByMixin
|
||||||
from authentik.core.api.utils import (
|
from authentik.core.api.utils import (
|
||||||
CacheSerializer,
|
CacheSerializer,
|
||||||
@ -29,9 +32,6 @@ from authentik.core.api.utils import (
|
|||||||
from authentik.flows.exceptions import FlowNonApplicableException
|
from authentik.flows.exceptions import FlowNonApplicableException
|
||||||
from authentik.flows.models import Flow
|
from authentik.flows.models import Flow
|
||||||
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlanner, cache_key
|
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlanner, cache_key
|
||||||
from authentik.flows.transfer.common import DataclassEncoder
|
|
||||||
from authentik.flows.transfer.exporter import FlowExporter
|
|
||||||
from authentik.flows.transfer.importer import FlowImporter
|
|
||||||
from authentik.flows.views.executor import SESSION_KEY_HISTORY, SESSION_KEY_PLAN
|
from authentik.flows.views.executor import SESSION_KEY_HISTORY, SESSION_KEY_PLAN
|
||||||
from authentik.lib.views import bad_request_message
|
from authentik.lib.views import bad_request_message
|
||||||
|
|
||||||
@ -73,6 +73,7 @@ class FlowSerializer(ModelSerializer):
|
|||||||
"compatibility_mode",
|
"compatibility_mode",
|
||||||
"export_url",
|
"export_url",
|
||||||
"layout",
|
"layout",
|
||||||
|
"denied_action",
|
||||||
]
|
]
|
||||||
extra_kwargs = {
|
extra_kwargs = {
|
||||||
"background": {"read_only": True},
|
"background": {"read_only": True},
|
||||||
@ -110,8 +111,8 @@ class FlowViewSet(UsedByMixin, ModelViewSet):
|
|||||||
serializer_class = FlowSerializer
|
serializer_class = FlowSerializer
|
||||||
lookup_field = "slug"
|
lookup_field = "slug"
|
||||||
ordering = ["slug", "name"]
|
ordering = ["slug", "name"]
|
||||||
search_fields = ["name", "slug", "designation", "title"]
|
search_fields = ["name", "slug", "designation", "title", "denied_action"]
|
||||||
filterset_fields = ["flow_uuid", "name", "slug", "designation"]
|
filterset_fields = ["flow_uuid", "name", "slug", "designation", "denied_action"]
|
||||||
|
|
||||||
@permission_required(None, ["authentik_flows.view_flow_cache"])
|
@permission_required(None, ["authentik_flows.view_flow_cache"])
|
||||||
@extend_schema(responses={200: CacheSerializer(many=False)})
|
@extend_schema(responses={200: CacheSerializer(many=False)})
|
||||||
@ -162,12 +163,13 @@ class FlowViewSet(UsedByMixin, ModelViewSet):
|
|||||||
)
|
)
|
||||||
@action(detail=False, methods=["POST"], parser_classes=(MultiPartParser,))
|
@action(detail=False, methods=["POST"], parser_classes=(MultiPartParser,))
|
||||||
def import_flow(self, request: Request) -> Response:
|
def import_flow(self, request: Request) -> Response:
|
||||||
"""Import flow from .akflow file"""
|
"""Import flow from .yaml file"""
|
||||||
file = request.FILES.get("file", None)
|
file = request.FILES.get("file", None)
|
||||||
if not file:
|
if not file:
|
||||||
return HttpResponseBadRequest()
|
return HttpResponseBadRequest()
|
||||||
importer = FlowImporter(file.read().decode())
|
importer = Importer(file.read().decode())
|
||||||
valid = importer.validate()
|
valid, _logs = importer.validate()
|
||||||
|
# TODO: return logs
|
||||||
if not valid:
|
if not valid:
|
||||||
return HttpResponseBadRequest()
|
return HttpResponseBadRequest()
|
||||||
successful = importer.apply()
|
successful = importer.apply()
|
||||||
@ -194,11 +196,11 @@ class FlowViewSet(UsedByMixin, ModelViewSet):
|
|||||||
@action(detail=True, pagination_class=None, filter_backends=[])
|
@action(detail=True, pagination_class=None, filter_backends=[])
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def export(self, request: Request, slug: str) -> Response:
|
def export(self, request: Request, slug: str) -> Response:
|
||||||
"""Export flow to .akflow file"""
|
"""Export flow to .yaml file"""
|
||||||
flow = self.get_object()
|
flow = self.get_object()
|
||||||
exporter = FlowExporter(flow)
|
exporter = FlowExporter(flow)
|
||||||
response = JsonResponse(exporter.export(), encoder=DataclassEncoder, safe=False)
|
response = HttpResponse(content=exporter.export_to_string())
|
||||||
response["Content-Disposition"] = f'attachment; filename="{flow.slug}.akflow"'
|
response["Content-Disposition"] = f'attachment; filename="{flow.slug}.yaml"'
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@extend_schema(responses={200: FlowDiagramSerializer()})
|
@extend_schema(responses={200: FlowDiagramSerializer()})
|
||||||
@ -371,7 +373,7 @@ class FlowViewSet(UsedByMixin, ModelViewSet):
|
|||||||
request,
|
request,
|
||||||
_(
|
_(
|
||||||
"Flow not applicable to current user/request: %(messages)s"
|
"Flow not applicable to current user/request: %(messages)s"
|
||||||
% {"messages": str(exc)}
|
% {"messages": exc.messages}
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
return Response(
|
return Response(
|
||||||
|
@ -1,26 +1,36 @@
|
|||||||
"""authentik flows app config"""
|
"""authentik flows app config"""
|
||||||
from importlib import import_module
|
from prometheus_client import Gauge, Histogram
|
||||||
|
|
||||||
from django.apps import AppConfig
|
|
||||||
from django.db.utils import ProgrammingError
|
|
||||||
|
|
||||||
|
from authentik.blueprints.manager import ManagedAppConfig
|
||||||
from authentik.lib.utils.reflection import all_subclasses
|
from authentik.lib.utils.reflection import all_subclasses
|
||||||
|
|
||||||
|
GAUGE_FLOWS_CACHED = Gauge(
|
||||||
|
"authentik_flows_cached",
|
||||||
|
"Cached flows",
|
||||||
|
)
|
||||||
|
HIST_FLOWS_PLAN_TIME = Histogram(
|
||||||
|
"authentik_flows_plan_time",
|
||||||
|
"Duration to build a plan for a flow",
|
||||||
|
["flow_slug"],
|
||||||
|
)
|
||||||
|
|
||||||
class AuthentikFlowsConfig(AppConfig):
|
|
||||||
|
class AuthentikFlowsConfig(ManagedAppConfig):
|
||||||
"""authentik flows app config"""
|
"""authentik flows app config"""
|
||||||
|
|
||||||
name = "authentik.flows"
|
name = "authentik.flows"
|
||||||
label = "authentik_flows"
|
label = "authentik_flows"
|
||||||
mountpoint = "flows/"
|
mountpoint = "flows/"
|
||||||
verbose_name = "authentik Flows"
|
verbose_name = "authentik Flows"
|
||||||
|
default = True
|
||||||
|
|
||||||
def ready(self):
|
def reconcile_load_flows_signals(self):
|
||||||
import_module("authentik.flows.signals")
|
"""Load flows signals"""
|
||||||
try:
|
self.import_module("authentik.flows.signals")
|
||||||
from authentik.flows.models import Stage
|
|
||||||
|
|
||||||
for stage in all_subclasses(Stage):
|
def reconcile_load_stages(self):
|
||||||
_ = stage().type
|
"""Ensure all stages are loaded"""
|
||||||
except ProgrammingError:
|
from authentik.flows.models import Stage
|
||||||
pass
|
|
||||||
|
for stage in all_subclasses(Stage):
|
||||||
|
_ = stage().type
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
"""Challenge helpers"""
|
"""Challenge helpers"""
|
||||||
|
from dataclasses import asdict, is_dataclass
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import TYPE_CHECKING, Optional
|
from typing import TYPE_CHECKING, Optional, TypedDict
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from django.core.serializers.json import DjangoJSONEncoder
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from rest_framework.fields import ChoiceField, DictField
|
from rest_framework.fields import ChoiceField, DictField
|
||||||
from rest_framework.serializers import CharField
|
from rest_framework.serializers import CharField
|
||||||
|
|
||||||
from authentik.core.api.utils import PassiveSerializer
|
from authentik.core.api.utils import PassiveSerializer
|
||||||
from authentik.flows.transfer.common import DataclassEncoder
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from authentik.flows.stage import StageView
|
from authentik.flows.stage import StageView
|
||||||
@ -95,10 +97,17 @@ class AccessDeniedChallenge(WithUserInfoChallenge):
|
|||||||
component = CharField(default="ak-stage-access-denied")
|
component = CharField(default="ak-stage-access-denied")
|
||||||
|
|
||||||
|
|
||||||
|
class PermissionDict(TypedDict):
|
||||||
|
"""Consent Permission"""
|
||||||
|
|
||||||
|
id: str
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
class PermissionSerializer(PassiveSerializer):
|
class PermissionSerializer(PassiveSerializer):
|
||||||
"""Permission used for consent"""
|
"""Permission used for consent"""
|
||||||
|
|
||||||
name = CharField()
|
name = CharField(allow_blank=True)
|
||||||
id = CharField()
|
id = CharField()
|
||||||
|
|
||||||
|
|
||||||
@ -128,6 +137,19 @@ class AutoSubmitChallengeResponse(ChallengeResponse):
|
|||||||
component = CharField(default="ak-stage-autosubmit")
|
component = CharField(default="ak-stage-autosubmit")
|
||||||
|
|
||||||
|
|
||||||
|
class DataclassEncoder(DjangoJSONEncoder):
|
||||||
|
"""Convert any dataclass to json"""
|
||||||
|
|
||||||
|
def default(self, o):
|
||||||
|
if is_dataclass(o):
|
||||||
|
return asdict(o)
|
||||||
|
if isinstance(o, UUID):
|
||||||
|
return str(o)
|
||||||
|
if isinstance(o, Enum):
|
||||||
|
return o.value
|
||||||
|
return super().default(o) # pragma: no cover
|
||||||
|
|
||||||
|
|
||||||
class HttpChallengeResponse(JsonResponse):
|
class HttpChallengeResponse(JsonResponse):
|
||||||
"""Subclass of JsonResponse that uses the `DataclassEncoder`"""
|
"""Subclass of JsonResponse that uses the `DataclassEncoder`"""
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
"""flow exceptions"""
|
"""flow exceptions"""
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from authentik.lib.sentry import SentryIgnoredException
|
from authentik.lib.sentry import SentryIgnoredException
|
||||||
from authentik.policies.types import PolicyResult
|
from authentik.policies.types import PolicyResult
|
||||||
@ -9,6 +10,13 @@ class FlowNonApplicableException(SentryIgnoredException):
|
|||||||
|
|
||||||
policy_result: PolicyResult
|
policy_result: PolicyResult
|
||||||
|
|
||||||
|
@property
|
||||||
|
def messages(self) -> str:
|
||||||
|
"""Get messages from policy result, fallback to generic reason"""
|
||||||
|
if len(self.policy_result.messages) < 1:
|
||||||
|
return _("Flow does not apply to current user (denied by policy).")
|
||||||
|
return "\n".join(self.policy_result.messages)
|
||||||
|
|
||||||
|
|
||||||
class EmptyFlowException(SentryIgnoredException):
|
class EmptyFlowException(SentryIgnoredException):
|
||||||
"""Flow has no stages."""
|
"""Flow has no stages."""
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
"""Apply flow from commandline"""
|
|
||||||
from django.core.management.base import BaseCommand, no_translations
|
|
||||||
|
|
||||||
from authentik.flows.transfer.importer import FlowImporter
|
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand): # pragma: no cover
|
|
||||||
"""Apply flow from commandline"""
|
|
||||||
|
|
||||||
@no_translations
|
|
||||||
def handle(self, *args, **options):
|
|
||||||
"""Apply all flows in order, abort when one fails to import"""
|
|
||||||
for flow_path in options.get("flows", []):
|
|
||||||
with open(flow_path, "r", encoding="utf8") as flow_file:
|
|
||||||
importer = FlowImporter(flow_file.read())
|
|
||||||
valid = importer.validate()
|
|
||||||
if not valid:
|
|
||||||
raise ValueError("Flow invalid")
|
|
||||||
importer.apply()
|
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
|
||||||
parser.add_argument("flows", nargs="+", type=str)
|
|
@ -48,7 +48,7 @@ class FlowPlanProcess(PROCESS_CLASS): # pragma: no cover
|
|||||||
self.return_dict[self.index] = diffs
|
self.return_dict[self.index] = diffs
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand): # pragma: no cover
|
class Command(BaseCommand):
|
||||||
"""Benchmark authentik"""
|
"""Benchmark authentik"""
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
|
@ -47,7 +47,8 @@ class ReevaluateMarker(StageMarker):
|
|||||||
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER
|
from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER
|
||||||
|
|
||||||
LOGGER.debug(
|
LOGGER.debug(
|
||||||
"f(plan_inst)[re-eval marker]: running re-evaluation",
|
"f(plan_inst): running re-evaluation",
|
||||||
|
marker="ReevaluateMarker",
|
||||||
binding=binding,
|
binding=binding,
|
||||||
policy_binding=self.binding,
|
policy_binding=self.binding,
|
||||||
)
|
)
|
||||||
@ -56,13 +57,15 @@ class ReevaluateMarker(StageMarker):
|
|||||||
)
|
)
|
||||||
engine.use_cache = False
|
engine.use_cache = False
|
||||||
engine.request.set_http_request(http_request)
|
engine.request.set_http_request(http_request)
|
||||||
engine.request.context = plan.context
|
engine.request.context["flow_plan"] = plan
|
||||||
|
engine.request.context.update(plan.context)
|
||||||
engine.build()
|
engine.build()
|
||||||
result = engine.result
|
result = engine.result
|
||||||
if result.passing:
|
if result.passing:
|
||||||
return binding
|
return binding
|
||||||
LOGGER.warning(
|
LOGGER.warning(
|
||||||
"f(plan_inst)[re-eval marker]: binding failed re-evaluation",
|
"f(plan_inst): binding failed re-evaluation",
|
||||||
|
marker="ReevaluateMarker",
|
||||||
binding=binding,
|
binding=binding,
|
||||||
messages=result.messages,
|
messages=result.messages,
|
||||||
)
|
)
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user