|
| 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. |
0 commit comments