Skip to content

Instantly share code, notes, and snippets.

@AfvanMoopen
Last active December 19, 2022 09:40
Show Gist options
  • Save AfvanMoopen/9ecaaf7af121c984b2625726ce18b516 to your computer and use it in GitHub Desktop.
Save AfvanMoopen/9ecaaf7af121c984b2625726ce18b516 to your computer and use it in GitHub Desktop.
import getopt
import google.cloud.logging
import sys
import re
from google.protobuf.json_format import MessageToDict
from google.cloud import resourcemanager_v3
from google.oauth2 import service_account
SCOPE = ["https://www.googleapis.com/auth/cloud-platform"]
credentials = service_account.Credentials.from_service_account_file("secret.json", scopes = SCOPE)
resource_mgr = resourcemanager_v3.ProjectsClient(credentials=credentials)
# get service_account
options , remainder = getopt.getopt(sys.argv[1:] , "l:")
for opt, arg in options:
if opt in ["-l"]:
srv_acc = arg
assert len(srv_acc) > 0 , "specify Service account"
print(f"The service account specified is {srv_acc}")
def format_output(action):
print("The actions made by the service account's present in the logs are :\n")
for act in action:
match = re.search("\.([\w+]*$)" , act)
print(match[1])
def main():
action = set()
client = google.cloud.logging.Client(credentials=credentials)
Filter = f"protoPayload.authenticationInfo.principalEmail={srv_acc}"
for response in client.list_entries(filter_=Filter):
action.add(response.payload.get('request').get('@type'))
format_output(action)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment