5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ZOZOAdvent Calendar 2023

Day 7

Google Cloudの監査ログから未使用Service Accountを見つける方法

Last updated at Posted at 2023-12-06

Google CloudでService Accountを作成したものの、色々な事情で使われなくなることもあります。
未使用のService Accountが残っているとセキュリティ的な問題に繋がりかねないので、定期的な棚卸しが必要です。
以下のクエリを使うと監査ログから直近30日間で利用実績のないService Accountを抽出できます。

なお、ここで参照しているテーブルたちの作成方法については以下の記事をご覧下さい。

with service_accounts as (
  select
    json_value(resource.data, '$.email') as email,
  from <Cloud Asset Inventoryresource情報>
  where asset_type = 'iam.googleapis.com/ServiceAccount'
), audit_logs as (
  select
    date(timestamp, 'Asia/Tokyo') as date,
    protopayload_auditlog.authenticationInfo.principalEmail as email,
  from <Audit LogActivityログ>
  union all
  select
    date(timestamp, 'Asia/Tokyo') as date,
    protopayload_auditlog.authenticationInfo.principalEmail as email,
  from <Audit LogData Accessログ>
), usage_counts as (
  select
    email,
    count(*) as count
  from audit_logs
  where date > current_date('Asia/Tokyo') - interval 30 day
  group by email
)

select
  email,
  ifnull(count, 0) as count,
from service_accounts
left join usage_counts using(email)
order by count asc
5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?