Skip to content

Commit 371606d

Browse files
awolfdenAdam Wolfman
andauthored
Add Audit Logs example application (#44)
* Add Audit Logs example application * Add snackbar when events are fired Co-authored-by: Adam Wolfman <[email protected]>
1 parent 0b782d0 commit 371606d

File tree

25 files changed

+1368
-0
lines changed

25 files changed

+1368
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
pip-wheel-metadata/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
target/
76+
77+
# Jupyter Notebook
78+
.ipynb_checkpoints
79+
80+
# IPython
81+
profile_default/
82+
ipython_config.py
83+
84+
# pyenv
85+
.python-version
86+
87+
# pipenv
88+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
90+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
91+
# install all needed dependencies.
92+
#Pipfile.lock
93+
94+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
95+
__pypackages__/
96+
97+
# Celery stuff
98+
celerybeat-schedule
99+
celerybeat.pid
100+
101+
# SageMath parsed files
102+
*.sage.py
103+
104+
# Environments
105+
.env
106+
.venv
107+
env/
108+
venv/
109+
ENV/
110+
env.bak/
111+
venv.bak/
112+
.vscode/
113+
114+
# Spyder project settings
115+
.spyderproject
116+
.spyproject
117+
118+
# Rope project settings
119+
.ropeproject
120+
121+
# mkdocs documentation
122+
/site
123+
124+
# mypy
125+
.mypy_cache/
126+
.dmypy.json
127+
dmypy.json
128+
129+
# Pyre type checker
130+
.pyre/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 WorkOS
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# python-django-audit-logs-example
2+
3+
An example Django application demonstrating how to use the [WorkOS Python SDK](https://github.com/workos/workos-python) to send and retrieve Audit Log events. This example is not meant to show a real-world example of an Audit Logs implementation, but rather to show concrete examples of how events can be sent using the Python SDK.
4+
5+
## Prerequisites
6+
7+
- Python 3.6+
8+
9+
## Django Project Setup
10+
11+
1. Clone the main git repo for these Python example apps using your preferred secure method (HTTPS or SSH).
12+
13+
```bash
14+
# HTTPS
15+
$ git clone https://github.com/workos/python-django-example-applications.git
16+
```
17+
18+
or
19+
20+
```bash
21+
# SSH
22+
$ git clone [email protected]:workos/python-django-example-applications.git
23+
```
24+
25+
2. Navigate to the Admin Portal example app within the cloned repo.
26+
27+
```bash
28+
$ cd python-django-example-applications/python-django-audit-logs-example
29+
```
30+
31+
3. Create and source a Python virtual environment. You should then see `(env)` at the beginning of your command-line prompt.
32+
33+
```bash
34+
$ python3 -m venv env
35+
$ source env/bin/activate
36+
(env) $
37+
```
38+
39+
4. Install the cloned app's dependencies. If the `pip` command doesn't work, try `pip3` instead.
40+
41+
```bash
42+
(env) $ pip install -r requirements.txt
43+
```
44+
45+
5. Obtain and make note of the following values. In the next step, these will be set as environment variables.
46+
47+
- Your [WorkOS API key](https://dashboard.workos.com/api-keys)
48+
- Your [WorkOS Client ID](https://dashboard.workos.com/configuration)
49+
50+
6. Ensure you're in the root directory for the example app, `python-django-audit-logs-example/`. Create a `.env` file to securely store the environment variables. Open this file with the Nano text editor. (This file is listed in this repo's `.gitignore` file, so your sensitive information will not be checked into version control.)
51+
52+
```bash
53+
(env) $ touch .env
54+
(env) $ nano .env
55+
```
56+
57+
7. Once the Nano text editor opens, you can directly edit the `.env` file by listing the environment variables:
58+
59+
```bash
60+
export WORKOS_API_KEY=<value found in step 6>
61+
export WORKOS_CLIENT_ID=<value found in step 6>
62+
```
63+
64+
To exit the Nano text editor, type `CTRL + x`. When prompted to "Save modified buffer", type `Y`, then press the `Enter` or `Return` key.
65+
66+
8. Source the environment variables so they are accessible to the operating system.
67+
68+
```bash
69+
(env) $ source .env
70+
```
71+
72+
You can ensure the environment variables were set correctly by running the following commands. The output should match the corresponding values.
73+
74+
```bash
75+
(env) $ echo $WORKOS_API_KEY
76+
(env) $ echo $WORKOS_CLIENT_ID
77+
```
78+
79+
9. Run the Django migrations. Again, ensure you're in the `python-django-sso-example/` directory where the `manange.py` file is.
80+
81+
```bash
82+
(env) $ python3 manage.py migrate
83+
```
84+
85+
You should see output like:
86+
87+
```bash
88+
Operations to perform:
89+
Apply all migrations: admin, auth, contenttypes, sessions
90+
Running migrations:
91+
Applying contenttypes.0001_initial... OK
92+
Applying auth.0001_initial... OK
93+
. . .
94+
```
95+
96+
10. The final setup step is to start the server.
97+
98+
```bash
99+
(env) $ python3 manage.py runserver --insecure
100+
```
101+
102+
You'll know the server is running when you see no warnings or errors in the CLI, and output similar to the following is displayed:
103+
104+
```bash
105+
Watching for file changes with StatReloader
106+
Performing system checks...
107+
108+
System check identified no issues (0 silenced).
109+
March 18, 2021 - 04:54:50
110+
Django version 3.1.7, using settings 'workos_django.settings'
111+
Starting development server at http://127.0.0.1:8000/
112+
Quit the server with CONTROL-C.
113+
```
114+
115+
Navigate to `localhost:8000` in your web browser. You should see a place to enter your organization ID. This can be found in the WorkOS Dashboard for the Organization you'd like to send logs to.
116+
117+
You can stop the local Django server for now by entering `CTRL + c` on the command line.
118+
119+
### Audit Logs Setup with WorkOS
120+
121+
11. Follow the [Audit Logs configuration steps](https://workos.com/docs/audit-logs/emit-an-audit-log-event/sign-in-to-your-workos-dashboard-account-and-configure-audit-log-event-schemas) to set up the following 5 events that are sent with this example:
122+
123+
Action title: "user.signed_in" | Target type: "team"
124+
Action title: "user.logged_out" | Target type: "team"
125+
Action title: "user.organization_set" | Target type: "team"
126+
Action title: "user.organization_deleted" | Target type: "team"
127+
Action title: "user.connection_deleted" | Target type: "team"
128+
129+
12. Next, take note of the Organization ID for the Org which you will be sending the Audit Log events for. This ID gets entered into the splash page of the example application.
130+
131+
13. Once you enter the Organization ID and submit it, you will be brought to the page where you'll be able to send the audit log events that were just configured. You'll also notice that the action of setting the Organization triggered an Audit Log already. Click the buttons to send the respective events.
132+
133+
14. To obtain a CSV of the Audit Log events that were sent for the last 30 days, click the "Export Events" button. This will bring you to a new page where you can download the events. Downloading the events is a 2 step process. First you need to create the report by clicking the "Generate CSV" button. Then click the "Access CSV" button to download a CSV of the Audit Log events for the selected Organization for the past 30 days.
134+
135+
## Need help?
136+
137+
If you get stuck and aren't able to resolve the issue by reading our API reference or tutorials, you can reach out to us at [email protected] and we'll lend a hand.

python-django-audit-logs-example/audit_logs/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AuditLogsConfig(AppConfig):
5+
name = "audit_logs"
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from datetime import datetime
2+
3+
user_signed_in = {
4+
"action": "user.signed_in",
5+
"occurred_at": datetime.now().isoformat(),
6+
"actor": {
7+
"type": "user",
8+
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
9+
},
10+
"targets": [
11+
{
12+
"type": "team",
13+
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
14+
},
15+
],
16+
"context": {
17+
"location": "123.123.123.123",
18+
"user_agent": "Chrome/104.0.0.0",
19+
},
20+
}
21+
22+
user_logged_out = {
23+
"action": "user.logged_out",
24+
"occurred_at": datetime.now().isoformat(),
25+
"actor": {
26+
"type": "user",
27+
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
28+
},
29+
"targets": [
30+
{
31+
"type": "team",
32+
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
33+
},
34+
],
35+
"context": {
36+
"location": "123.123.123.123",
37+
"user_agent": "Chrome/104.0.0.0",
38+
},
39+
}
40+
41+
user_organization_set = {
42+
"action": "user.organization_set",
43+
"occurred_at": datetime.now().isoformat(),
44+
"actor": {
45+
"type": "user",
46+
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
47+
},
48+
"targets": [
49+
{
50+
"type": "team",
51+
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
52+
},
53+
],
54+
"context": {
55+
"location": "123.123.123.123",
56+
"user_agent": "Chrome/104.0.0.0",
57+
},
58+
}
59+
60+
user_organization_deleted = {
61+
"action": "user.organization_deleted",
62+
"occurred_at": datetime.now().isoformat(),
63+
"actor": {
64+
"type": "user",
65+
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
66+
},
67+
"targets": [
68+
{
69+
"type": "team",
70+
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
71+
},
72+
],
73+
"context": {
74+
"location": "123.123.123.123",
75+
"user_agent": "Chrome/104.0.0.0",
76+
},
77+
}
78+
79+
user_connection_deleted = {
80+
"action": "user.connection_deleted",
81+
"occurred_at": datetime.now().isoformat(),
82+
"actor": {
83+
"type": "user",
84+
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
85+
},
86+
"targets": [
87+
{
88+
"type": "team",
89+
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
90+
},
91+
],
92+
"context": {
93+
"location": "123.123.123.123",
94+
"user_agent": "Chrome/104.0.0.0",
95+
},
96+
}

python-django-audit-logs-example/audit_logs/migrations/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)