# Snowflake Query Audit Logs

Snowflake query audit logs is a feature that audits queries that users run natively in Snowflake and presents them in a universal format as Immuta audit logs. Immuta uses the Snowflake `QUERY_HISTORY` and `ACCESS_HISTORY` tables and translates them into the audit logs that can be viewed at query events in the Immuta UI. Immuta audits the activity of Immuta users on Immuta data sources.

## Requirements

* Snowflake Enterprise Edition or higher
* [Snowflake integration with query audit enabled](https://documentation.immuta.com/saas/configuration/integrations/snowflake/how-to-guides/enterprise)
* [Snowflake tables registered as Immuta data sources](https://documentation.immuta.com/saas/configuration/integrations/data-and-integrations/registering-metadata/register-data-sources/snowflake-tutorial)
* [Snowflake users registered as Immuta users](https://documentation.immuta.com/saas/configuration/people/users-index/how-to-guides/managing-personas-and-permissions): Note that the users' [Snowflake usernames must be mapped to Immuta](https://documentation.immuta.com/saas/configuration/people/users-index/how-to-guides/external-user-mapping). Without this, Immuta will not know the users are Immuta users and will not collect audit events for their data access activity.

{% hint style="info" %}
**Store audit logs**

By default, Snowflake audit logs expire after 90 days. [Export the universal audit model (UAM) logs to S3 or ADLS Gen2](https://documentation.immuta.com/saas/developer-guides/the-immuta-cli/cli-reference-guide), and store audit logs outside of Immuta in order to retain the audit logs long-term.
{% endhint %}

## Audit frequency

Immuta collects audit records once an hour by default, and the frequency can be [changed at any time from the Immuta app settings page](https://documentation.immuta.com/saas/configuration/application-configuration/how-to-guides/config-builder-guide#snowflake-audit-sync-schedule). The frequency is a global setting based on integration type, so organizations with multiple Snowflake integrations will have the same audit frequency for all of them. The more frequent the audit records are ingested, the more current the audit records. However, there could be performance and cost impacts from the frequent jobs.

To manually request query audit ingestion, click **Load Audit Events** on the Immuta audit page.

## Audit schema

Each audit message from the Immuta platform will be a one-line JSON object containing the properties listed below.

| Property                                                 | Description                                                                                                                                                                                      | Example                                                             |
| -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------- |
| **action**                                               | The action associated with the audit log.                                                                                                                                                        | `QUERY`                                                             |
| actor.**type**                                           | The Immuta user type of the actor who made the query.                                                                                                                                            | `USER_ACTOR`                                                        |
| actor.**id**                                             | The Immuta user ID of the actor who made the query.                                                                                                                                              | `taylor@snowflake.com`                                              |
| actor.**name**                                           | The Immuta name of the user who made the query.                                                                                                                                                  | `Taylor`                                                            |
| actor.**identityProvider**                               | The IAM the user is registered in. `bim` is the built-in Immuta IAM.                                                                                                                             | `bim`                                                               |
| **sessionId**                                            | The session ID of the user who performed the action.                                                                                                                                             | `01ee14d9-cab3-1ef6-9cc4-f0c315a53788`                              |
| **actionStatus**                                         | Indicates whether or not the user was granted access to the data. Possible values are `UNAUTHORIZED`, `FAILURE`, or `SUCCESS`.                                                                   | `SUCCESS`                                                           |
| **actionStatusReason**                                   | When available, the reason from Unity Catalog that the user’s query was denied.                                                                                                                  | `null` if **actionStatus** is `SUCCESS`                             |
| **eventTimestamp**                                       | The time the query occurred.                                                                                                                                                                     | `2023-06-27T11:03:59.000Z`                                          |
| **id**                                                   | The unique ID of the audit record.                                                                                                                                                               | `9f542dfd-5099-4362-a72d-8377306db3b8`                              |
| **userAgent**                                            | Client information of the user who made the query.                                                                                                                                               | `Snowflake Web App`                                                 |
| **tenantId**                                             | The Immuta SaaS tenant ID.                                                                                                                                                                       | `your-immuta.com`                                                   |
| **targetType**                                           | The type of targets affected by the query; this value will always be `DATASOURCE`.                                                                                                               | `DATASOURCE`                                                        |
| **targets**                                              | A list of the targets affected by the query.                                                                                                                                                     | *See the example below*                                             |
| auditPayload.**type**                                    | The type of audit record; this value will always be: `QueryAuditPayload`.                                                                                                                        | `QueryAuditPayload`                                                 |
| auditPayload.**queryId**                                 | The unique ID of the query. If the query joins multiple tables, each table will appear as a separate log, but all will have the same query ID.                                                   | `01ee14da-517a-1670-afce-0c3e0fdcf7d4`                              |
| auditPayload.**query**                                   | The command text of the query that was run in the integration. Immuta truncates the query text to the first 2048 characters.                                                                     | `SELECT VERSION AS 'version' FROM 'sample-data'.'__immuta_version'` |
| auditPayload.**startTime**                               | The date and time the query started in UTC.                                                                                                                                                      | `2023-06-27T11:03:59.000Z`                                          |
| auditPayload.**duration**                                | The time the query took in seconds.                                                                                                                                                              | `0.557`                                                             |
| auditPayload.**errorCode**                               | The `errorCode` for the denied query.                                                                                                                                                            | `null` if **actionStatus** is `SUCCESS`                             |
| auditPayload.technologyContext.**type**                  | The technology the query was made in.                                                                                                                                                            | `SnowflakeContext`                                                  |
| auditPayload.technologyContext.**host**                  | The host that the integration is connected to.                                                                                                                                                   | `your-hostname.snowflake.computing.com`                             |
| auditPayload.technologyContext.**snowflakeUsername**     | The user's Snowflake username.                                                                                                                                                                   | `taylor@snowflake.com`                                              |
| auditPayload.technologyContext.**rowsProduced**          | The number of rows returned in the query. *Note that rows produced will show `0` for cached queries.*                                                                                            | `3`                                                                 |
| auditPayload.technologyContext.**roleName**              | The Snowflake role the user used to make the query.                                                                                                                                              | `ACCOUNTADMIN`                                                      |
| auditPayload.technologyContext.**warehouseId**           | The ID of the warehouse where the query was made.                                                                                                                                                | `null`                                                              |
| auditPayload.technologyContext.**warehouseName**         | The name of the warehouse where the query was made.                                                                                                                                              | `null`                                                              |
| auditPayload.technologyContext.**clusterNumber**         | The number of the cluster where the query was made.                                                                                                                                              | `0`                                                                 |
| auditPayload.**objectsAccessed**                         | An array of the data sources accessed in the query.                                                                                                                                              | *See example below.*                                                |
| auditPayload.objectsAccessed.**name**                    | The name of the data source accessed in the query.                                                                                                                                               | `\"tpch\".\"tiny\".\"customer\"`                                    |
| auditPayload.objectsAccessed.**datasourceId**            | The Immuta data source ID.                                                                                                                                                                       | `17`                                                                |
| auditPayload.objectsAccessed.**databaseName**            | The name of the Snowflake database.                                                                                                                                                              | `tpch`                                                              |
| auditPayload.objectsAccessed.**schemaName**              | The name of the Snowflake schema.                                                                                                                                                                | `tiny`                                                              |
| auditPayload.objectsAccessed.**type**                    | Specifies if the queried data source is a table or view.                                                                                                                                         | `TABLE`                                                             |
| auditPayload.objectsAccessed.**columns**                 | An array of the columns accessed in the query.                                                                                                                                                   | *See example below.*                                                |
| auditPayload.objectsAccessed.columns.**name**            | The name of the column.                                                                                                                                                                          | `custkey`                                                           |
| auditPayload.objectsAccessed.columns.**tags**            | An array of the tags on the column.                                                                                                                                                              | *See example below.*                                                |
| auditPayload.objectsAccessed.columns.**securityProfile** | Details about the sensitivity of the column. Available when [classification frameworks are configured](https://documentation.immuta.com/saas/configuration/tags/data-classification).            | *See example below.*                                                |
| auditPayload.objectsAccessed.columns.**inferred**        | If `true`, the column accessed has been determined by Immuta using query parsing; `false` if it is explicitly provided.                                                                          | `false`                                                             |
| auditPayload.objectsAccessed.**securityProfile**         | A classification for all the columns accessed together. Available when [classification frameworks are configured](https://documentation.immuta.com/saas/configuration/tags/data-classification). | *See example below.*                                                |
| auditPayload.securityProfile.sensitivity.**score**       | The sensitivity score of the query. Classification must be configured for this field.                                                                                                            | `INDETERMINATE`                                                     |
| **receivedTimestamp**                                    | The timestamp of when the audit event was received and stored by Immuta.                                                                                                                         | `2023-06-27T15:18:22.314Z`                                          |

## Example audit record

```json
{
  "action": "QUERY",
  "actor": {
    "type": "USER_ACTOR",
    "name": "Taylor",
    "id": "taylor@snowflake.com",
    "identityProvider": "bim",
    "impersonatedBy": null
  },
  "sessionId": "18245308848957358",
  "actionStatus": "SUCCESS",
  "actionStatusReason": null,
  "eventTimestamp": "2023-03-21T13:39:45.040598-04:00",
  "id": "abcd",
  "userAgent": "Snowflake Web App",
  "tenantId": "your-immuta.com",
  "targetType": "DATASOURCE",
  "targets": [
    {
      "type": "DATASOURCE",
      "id": "2034",
      "name": "University Art Gallery Exhibition",
      "technology": "SNOWFLAKE"
    }
  ],
  "relatedResources": [],
  "auditPayload": {
    "type": "QueryAuditPayload",
    "queryId": "01a9c8f5-0602-eeb3-0040-d203014c166a",
    "query": "SELECT T2.theme ,  T1.date ,  T1.attendance FROM \"DETECT_DEMO_DB\".\"UNIVERSITY_ART_GALLERY\".\"EXHIBITION_RECORD\" AS T1 JOIN \"DETECT_DEMO_DB\".\"UNIVERSITY_ART_GALLERY\".\"EXHIBITION\" AS T2 ON T1.exhibition_id  =  T2.exhibition_id WHERE T2.year  =  2004",
    "startTime": "2023-03-21T13:39:45.040598-04:00",
    "endTime": "2023-03-21T13:05:07.040598-04:00",
    "duration": 163,
    "errorCode": null,
    "technologyContext": {
      "type": "SnowflakeContext",
      "host": "example.snowflakecomputing.com",
      "clientIp": "xxx.xx.xx.xx",
      "snowflakeUsername": "taylor@snowflake.com",
      "rowsProduced": 3,
      "roleName": "ACCOUNTADMIN",
      "warehouseId": "3",
      "warehouseName": "DETECT_WH",
      "clusterNumber": 1
    },
    "objectsAccessed": [
      {
        "name": "DB.PUBLIC.CASE",
        "datasourceId": "3",
        "databaseName": "DB",
        "schemaName": "PUBLIC",
        "type": "TABLE",
        "columns": [
          {
            "name": "DOB",
            "tags": [],
            "securityProfile": {
              "sensitivity": {
                "score": "INDETERMINATE"
              }
            },
            "inferred": false
          },
          {
            "name": "COUNTRY",
            "tags": [],
            "securityProfile": {
              "sensitivity": {
                "score": "INDETERMINATE"
              }
            },
            "inferred": false
          },
          {
            "name": "DEPARTMENT",
            "tags": [],
            "securityProfile": {
              "sensitivity": {
                "score": "INDETERMINATE"
              }
            },
            "inferred": false
          },
          {
            "name": "FIRSTNAME",
            "tags": [],
            "securityProfile": {
              "sensitivity": {
                "score": "INDETERMINATE"
              }
            },
            "inferred": false
          },
          {
            "name": "SSN",
            "tags": [],
            "securityProfile": {
              "sensitivity": {
                "score": "INDETERMINATE"
              }
            },
            "inferred": false
          },
          {
            "name": "ID",
            "tags": [],
            "securityProfile": {
              "sensitivity": {
                "score": "INDETERMINATE"
              }
            },
            "inferred": false
          },
          {
            "name": "COMPANY",
            "tags": [],
            "securityProfile": {
              "sensitivity": {
                "score": "INDETERMINATE"
              }
            },
            "inferred": false
          },
          {
            "name": "LASTNAME",
            "tags": [],
            "securityProfile": {
              "sensitivity": {
                "score": "INDETERMINATE"
              }
            },
            "inferred": false
          }
        ],
        "tags": [],
        "securityProfile": {
          "sensitivity": {
            "score": "INDETERMINATE"
          }
        }
      }
    ],
    "securityProfile": {
      "sensitivity": {
        "score": "NONSENSITIVE"
      }
    }
  },
  "receivedTimestamp": "2023-03-22T13:22:04.979644-04:00"
}
```
