|
| 1 | +# python-flask-audit-logs-example |
| 2 | + |
| 3 | +An example Flask 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 | +## Flask 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-flask-example-applications.git |
| 16 | + ``` |
| 17 | + |
| 18 | + or |
| 19 | + |
| 20 | + ```bash |
| 21 | + # SSH |
| 22 | + $ git clone [email protected]:workos/python-flask-example-applications.git |
| 23 | + ``` |
| 24 | + |
| 25 | +2. Navigate to the Audit Logs app within the cloned repo. |
| 26 | + |
| 27 | + ```bash |
| 28 | + $ cd python-flask-example-applications/python-flask-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. |
| 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-flask-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 | + WORKOS_API_KEY=<value found in step 6> |
| 61 | + WORKOS_CLIENT_ID=<value found in step 6> |
| 62 | + APP_SECRET_KEY=<any string value you\'d like> |
| 63 | + ``` |
| 64 | + |
| 65 | + To exit the Nano text editor, type `CTRL + x`. When prompted to "Save modified buffer", type `Y`, then press the `Enter` or `Return` key. |
| 66 | + |
| 67 | +8. Source the environment variables so they are accessible to the operating system. |
| 68 | + |
| 69 | + ```bash |
| 70 | + (env) $ source .env |
| 71 | + ``` |
| 72 | + |
| 73 | + You can ensure the environment variables were set correctly by running the following commands. The output should match the corresponding values. |
| 74 | + |
| 75 | + ```bash |
| 76 | + (env) $ echo $WORKOS_API_KEY |
| 77 | + (env) $ echo $WORKOS_CLIENT_ID |
| 78 | + ``` |
| 79 | + |
| 80 | +9. The final setup step is to start the server. |
| 81 | + |
| 82 | +```bash |
| 83 | +(env) $ flask run |
| 84 | +``` |
| 85 | + |
| 86 | +If you are using macOS Monterey and port 5000 is not available and you'll need to start the app on a different port with this slightly different command. |
| 87 | + |
| 88 | +```bash |
| 89 | +(env) $ flask run -p 5001 |
| 90 | +``` |
| 91 | + |
| 92 | +You'll know the server is running when you see no errors in the CLI, and output similar to the following is displayed: |
| 93 | + |
| 94 | +```bash |
| 95 | +* Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them. |
| 96 | +* Environment: production |
| 97 | +WARNING: This is a development server. Do not use it in a production deployment. |
| 98 | +Use a production WSGI server instead. |
| 99 | +* Debug mode: off |
| 100 | +* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) |
| 101 | +``` |
| 102 | + |
| 103 | +Navigate to `localhost:5000`, or `localhost:5001` depending on which port you launched the server, in your web browser. You should see the WorkOS logo and a place to enter your Organization ID. The next steps occur in your WorkOS dashboard. |
| 104 | + |
| 105 | +You can stop the local Flask server for now by entering `CTRL + c` on the command line. |
| 106 | + |
| 107 | +## Audit Logs Setup with WorkOS |
| 108 | + |
| 109 | +10. 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: |
| 110 | + |
| 111 | +Action title: "user.signed_in" | Target type: "team" |
| 112 | +Action title: "user.logged_out" | Target type: "team" |
| 113 | +Action title: "user.organization_set" | Target type: "team" |
| 114 | +Action title: "user.organization_deleted" | Target type: "team" |
| 115 | +Action title: "user.connection_deleted" | Target type: "team" |
| 116 | + |
| 117 | +11. 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. |
| 118 | + |
| 119 | +12. 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. |
| 120 | + |
| 121 | +13. 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. |
| 122 | + |
| 123 | +## Need help? |
| 124 | + |
| 125 | +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