Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read SA data & write to cloud logging #1145

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

itodotimothy6
Copy link

An example python script that shows how to use Looker API to extract System Activity logs and use the Logging API to write to Cloud Logging

Fixes #1144 1144 🦕

@jeremytchang jeremytchang self-assigned this Aug 17, 2022
cd sdk-codegen/examples/python/extract-logs-write-to-cloud-logging
```

- Setup Python Virtual environment
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is python virtual environment necessary for development?
Seems like Python version 3.8.2 is what's necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A virtual environment is not required. However, it is generally encouraged to install dependencies in a virtual environment

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sample script is not the place for this. We should only list bare minimum requirements to run the sample script.

- Install dependencies
```
pip install looker-sdk
pip install --upgrade google-cloud-logging
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the --upgrade assume the developer already has google-cloud-logging installed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a mistake. --upgrade option is not required.

## Requirements
- Looker Instance in which you have Admin or `see_system_activity` permission
- Google Cloud Project with Cloud Logging API enabled
- [pyenv](https://github.com/pyenv/pyenv#installation) installed
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add the python version and gcloud to this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


| GCP Audit Log Field | Looker System Actvity Field |
| ----------- | ----------- |
| [logName](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#:~:text=Fields-,logName,-string) | `looker_system_activity_logs` |
Copy link
Collaborator

@jeremytchang jeremytchang Sep 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the GCP services that consume Audit Logs have expectations on the values in the Audit Log?
This example would set precedent and probably be defacto standard for Audit logs coming from Looker. I assume compliance/auditing expects certain values for Audit Logs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes GCP Audit logs have expected values.

However, some of these values do not apply or make sense for a Looker resource. For example, GCP resource always falls under a project/resource but a Looker resource doesn't. A project in Looker means something different. The mapping is on a best effort basis.

Technically the output of this script is not a GCP Audit log (when you filter for GCP Audit log, these logs won't appear). I'm only using the Audit log format in this example for similarity when querying these logs.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know. So this is only for querying standard GCP Audit Logs with Looker System activity Logs included.
However, my main worry is compliance/auditing issues. Like i previously asked, is there any GCP services that consume audit logs that may require specific values? (And have you consulted them on this)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, all GCP resources require certain values in audit logs(E.g Project, organization, IAM details). I already mentioned that some of these values don't apply to Looker because technically Looker is not a GCP resource (yet)

No, I did not consult with the Audit Logs team. This repo is only an example showing a way you can programmatically export System Activity logs today, until the product team integrates Looker as a GCP service. It's my understanding that only the product team can get this type of compliance approval you're referring to.

System Activity logs is NOT a GCP Audit Log. And I'm sure the Audit team will echo that if I consult with them. This is not an official solution, but an example to demonstrate one of the workarounds you could do until the product team provides an official solution to this.

I'm only mapping the SA logs to look like GCP Audit logs since most customers are familiar with the format and I mentioned in the readme that this is a best effort basis


## GCP Audit Log Fields to Looker System Activity Mapping

| GCP Audit Log Field | Looker System Actvity Field |
Copy link
Collaborator

@jeremytchang jeremytchang Sep 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the second column be Looker System Activity Field OR Value? So that the other hardcoded values can be documented here too?

Comment on lines 18 to 37
"event.id",
"event.name",
"event.category",
"event.sudo_user_id",
"event.created_time",
"user.email",
"user.name",
"permission_set.permissions",
"permission_set.name",
"permission_set.id",
"model_set.models",
"model_set.name",
"event_attribute.name",
"event_attribute.value",
"event_attribute.id",
"group.id",
"group.name",
"group.external_group_id",
"model_set.id",
"user.dev_branch_name"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nitpick but can we alphabetize?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -0,0 +1,66 @@
## Overview

A Python script that extracts Looker system/audit logs from [System Activity](https://docs.looker.com/admin-options/system-activity) and exports the Logs to Cloud Logging. This example tries to format the output logs like a [GCP Audit Log](https://cloud.google.com/logging/docs/audit/understanding-audit-logs) as best as possible. See [mapping](#gcp-audit-log-fields-to-looker-system-activity-mapping) for comparison between Looker System Activity Fields and GCP Audit Log Fields
Copy link
Collaborator

@jeremytchang jeremytchang Sep 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First lines should state what it does, not be saved for the note.

A Python script that extracts [System Activity](https://docs.looker.com/admin-options/system-activity) data from the last 10 minutes, formats the data as Audit Logs, and exports the logs to Cloud Logging. The data formatting/mapping is best effort. See [data mapping](#gcp-audit-log-fields-to-looker-system-activity-mapping) below.

Then can keep note as:

**_NOTE:_**  You can schedule this script to run every 10 minutes using a cron job or equivalent to continually create and export logs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

],
filters={"event.created_time": "10 minutes"},
sorts=["event.created_time desc"],
filter_config={"event.created_time": [{
Copy link
Collaborator

@jeremytchang jeremytchang Sep 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be set to null unless i'm misunderstanding behavior or api docs.

https://developers.looker.com/api/explorer/4.0/types/Query/WriteQuery
The filter_config represents the state of the filter UI on the explore page for a given query. When running a query via the Looker UI, this parameter takes precedence over "filters". When creating a query or modifying an existing query, "filter_config" should be set to null. Setting it to any other value could cause unexpected filtering behavior. The format should be considered opaque.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true. having both filters & filter_config is redundant

cd extract-looker-logs/
```

- Setup python virtual environment
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove pyenv setup

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 18 to 37
"event.id",
"event.name",
"event.category",
"event.sudo_user_id",
"event.created_time",
"event_attribute.name",
"event_attribute.value",
"event_attribute.id",
"group.id",
"group.name",
"group.external_group_id",
"model_set.id",
"model_set.models",
"model_set.name",
"permission_set.permissions",
"permission_set.name",
"permission_set.id",
"user.email",
"user.name",
"user.dev_branch_name"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed some alphabetical ordering here.


- Clone the repo
```
git clone https://github.com/itodotimothy6/extract-looker-logs.git
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong repo.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected to the right repo

@itodotimothy6 itodotimothy6 requested a review from a team as a code owner March 12, 2024 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Show how to extract SA logs & write to Logging
2 participants