{ "cells": [ { "cell_type": "markdown", "id": "012d8071", "metadata": {}, "source": [ "# Audit Log Configuration" ] }, { "cell_type": "markdown", "id": "014ab9f9", "metadata": {}, "source": [ "**Table of contents**\n", "- Overview\n", "\n", "- Setup\n", "\n", " - Authentication Token\n", "\n", "- Create Audit Log Configuration\n", "\n", " - Output Description\n", "\n", "- Get Audit Log Configuration\n", "\n", " - Output Description\n", "\n", "- Related Links" ] }, { "cell_type": "markdown", "id": "1d4efbd2", "metadata": {}, "source": [ "## Overview\n", "\n", "This notebook demonstrates how to use the admin audit log configuration APIs to update and retrieve audit logging preferences for your organization.\n", "\n", "These endpoints are intended for administrative users and allow you to:\n", "\n", "- Update request and response logging configuration\n", "- View the configuration set\n", "\n", "The APIs require an admin token. If the authenticated user is not an admin, the service returns `403 Forbidden`." ] }, { "cell_type": "markdown", "id": "82cabae9", "metadata": {}, "source": [ "## Setup\n", "\n", "Ensure that Python 3+ is installed on your system.\n", "\n", "Note: To run this notebook, add your credentials to `../../../auth/secrets.ini` and `../../../auth/config.ini`.\n", "\n", "Example `secrets.ini` format:\n", "\n", "```\n", "[EAPI]\n", "api.pat_token = \n", "api.tenant_id = \n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "05d144d4", "metadata": {}, "outputs": [], "source": [ "# Install the prerequisite Python packages\n", "%pip install pandas configparser IPython requests" ] }, { "cell_type": "code", "execution_count": 1, "id": "f8e5d19f", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import configparser\n", "import requests\n", "import json\n", "from IPython.display import display as display_summary" ] }, { "cell_type": "markdown", "id": "4cc9a95c", "metadata": {}, "source": [ "### Authentication Token\n", "\n", "Run the following code snippet to generate a Bearer Token using your PAT token. These admin endpoints require an authenticated admin user." ] }, { "cell_type": "code", "execution_count": null, "id": "99a7f2e6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Authentication Success\n" ] } ], "source": [ "config = configparser.RawConfigParser()\n", "config.read(['../../../auth/secrets.ini','../../../auth/config.ini'])\n", "\n", "EAPI_PAT_TOKEN = config.get('EAPI', 'api.pat_token')\n", "EAPI_TENANT_ID = config.get('EAPI', 'api.tenant_id')\n", "\n", "EAPI_AUTH_ENDPOINT = config.get('EAPI', 'api.auth_endpoint')\n", "EAPI_BASE_URL = config.get('EAPI', 'api.base_url')\n", "\n", "EAPI_AUTH_CLIENT_ID = 'saascore-' + EAPI_TENANT_ID\n", "EAPI_CLIENT_ID = 'ghgemissions-' + EAPI_TENANT_ID\n", "\n", "auth_request_headers: dict = {}\n", "auth_request_headers[\"X-IBM-Client-Id\"] = EAPI_AUTH_CLIENT_ID\n", "auth_request_headers[\"X-IBM-Envizi-Pat\"] = EAPI_PAT_TOKEN\n", "\n", "verify = True\n", "\n", "auth_url = f\"{EAPI_AUTH_ENDPOINT}\"\n", " \n", "response = requests.post(url = auth_url,\n", " headers = auth_request_headers,\n", " verify = verify\n", " )\n", "if response.status_code == 200:\n", " jwt_token = response.text\n", " print(\"Authentication Success\")\n", "else: \n", " print(\"Authentication Failed\")\n", " print(response.text)" ] }, { "cell_type": "markdown", "id": "81c8dcae", "metadata": {}, "source": [ "## Create Audit Log Configuration\n", "\n", "Use `PUT /admin/audit-log` to update the audit logging preferences for the authenticated user's organization.\n", "\n", "The request body accepts two boolean values:\n", "\n", "- `logRequest`: Boolean value that enables or disables request payload logging.\n", "- `logResponse`: Boolean value that enables or disables response payload logging." ] }, { "cell_type": "code", "execution_count": 11, "id": "c16827b7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Status Code: 200\n" ] }, { "data": { "text/html": [ "\n", "\n", "\n", " \n", " \n", " \n", " logRequest\n", " logResponse\n", " message\n", " \n", " \n", " \n", " \n", " 0\n", " True\n", " False\n", " Audit log configuration created successfully\n", " \n", " \n", "\n", "" ], "text/plain": [ " logRequest logResponse message\n", "0 True False Audit log configuration created successfully" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "PUT_AUDIT_LOG_ENDPOINT = f\"{EAPI_BASE_URL}/admin/audit-log\"\n", "\n", "payload = {\n", " 'logRequest': True,\n", " 'logResponse': False\n", "}\n", "\n", "request_headers: dict = {}\n", "request_headers['Content-Type'] = 'application/json'\n", "request_headers['x-ibm-client-id'] = EAPI_CLIENT_ID\n", "request_headers['Authorization'] = 'Bearer ' + jwt_token\n", "\n", "response = requests.put(\n", " PUT_AUDIT_LOG_ENDPOINT,\n", " headers=request_headers,\n", " data=json.dumps(payload)\n", ")\n", "\n", "print(f'Status Code: {response.status_code}')\n", "\n", "if response.text:\n", " response_json = response.json()\n", " display_summary(pd.json_normalize(response_json))\n", "else:\n", " print('Empty Response')" ] }, { "cell_type": "markdown", "id": "3b635f62", "metadata": {}, "source": [ "### Output Description\n", "\n", "logRequest - Boolean flag indicating whether request payload logging is enabled.\n", "\n", "logResponse - Boolean flag indicating whether response payload logging is enabled.\n", "\n", "message - Response message. On success, the API returns: `Audit log configuration created successfully` or `Audit log configuration updated successfully`. If no changes were made, returns: `No change in audit log configuration`.\n", "\n", "Possible HTTP status codes:\n", "\n", "- `200` - Configuration updated successfully\n", "- `400` - Invalid request parameters\n", "- `403` - User is not authorized to access the admin endpoint\n", "- `409` - No change in audit log configuration (conflict)\n", "- `500` - Internal server error" ] }, { "cell_type": "markdown", "id": "c303752d", "metadata": {}, "source": [ "## Get Audit Log Configuration\n", "\n", "Use `GET /admin/audit-log` to retrieve the current audit logging configuration for the authenticated user's organization.\n", "\n", "The response returns two boolean flags:\n", "\n", "- `logRequest`: whether request payload logging is enabled\n", "- `logResponse`: whether response payload logging is enabled" ] }, { "cell_type": "code", "execution_count": 9, "id": "ba67dde3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Status Code: 200\n" ] }, { "data": { "text/html": [ "\n", "\n", "\n", " \n", " \n", " \n", " logRequest\n", " logResponse\n", " \n", " \n", " \n", " \n", " 0\n", " True\n", " False\n", " \n", " \n", "\n", "" ], "text/plain": [ " logRequest logResponse\n", "0 True False" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "GET_AUDIT_LOG_ENDPOINT = f\"{EAPI_BASE_URL}/admin/audit-log\"\n", "\n", "request_headers: dict = {}\n", "request_headers['Content-Type'] = 'application/json'\n", "request_headers['x-ibm-client-id'] = EAPI_CLIENT_ID\n", "request_headers['Authorization'] = 'Bearer ' + jwt_token\n", "\n", "response = requests.get(\n", " GET_AUDIT_LOG_ENDPOINT,\n", " headers=request_headers\n", ")\n", "\n", "print(f'Status Code: {response.status_code}')\n", "\n", "if response.text:\n", " response_json = response.json()\n", " display_summary(pd.json_normalize(response_json))\n", "else:\n", " print('Empty Response')" ] }, { "cell_type": "markdown", "id": "b9621a68", "metadata": {}, "source": [ "### Output Description\n", "\n", "logRequest - Boolean flag indicating whether incoming request bodies are logged for audit purposes.\n", "\n", "logResponse - Boolean flag indicating whether outgoing response bodies are logged for audit purposes.\n", "\n", "Possible error responses:\n", "\n", "- `403` - User is not authorized to access the admin endpoint\n", "- `404` - Audit log configuration was not found for the organization\n", "- `400` - Bad request\n", "- `500` - Internal server error" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Related Links\n", "\n", "[Emissions API Developer Guide](https://developer.ibm.com/apis/catalog/ghgemissions--ibm-envizi-emissions-api/Introduction)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.10" } }, "nbformat": 4, "nbformat_minor": 5 }