We have the owner/team of the account documented in Confluence.
My current attempt:
I created 3 tables in my database:
from sqlalchemy import Column, ForeignKey, Integer, String, Table, PrimaryKeyConstraint
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
Base = declarative_base()
# Define the association table for the many-to-many relationship
team_accounts = Table('team_accounts', Base.metadata,
Column('team_id', Integer, ForeignKey('product_teams.team_id')),
Column('account_id', String(12), ForeignKey('aws_accounts.account_id')),
PrimaryKeyConstraint('team_id', 'account_id')
)
class AwsAccount(Base):
__tablename__ = 'aws_accounts'
account_id = Column(String(12), primary_key=True) # AWS Account ID
friendly_name = Column(String(100)) # AWS account alias
team_id = Column(Integer, ForeignKey('product_teams.team_id'))
env = Column(String(100)) # Is the account used as Production, Dev, or Test
team = relationship("ProductTeam", back_populates="accounts")
class ProductTeam(Base):
__tablename__ = 'product_teams'
team_id = Column(Integer, primary_key=True)
team_name = Column(String(100), unique=True) # Name of product team
unit = Column(String(100)) # Business unit
accounts = relationship("AwsAccount", secondary=team_accounts, back_populates="team")
Then I tried to learn how to use dbt to attribute the findings to teams:
{% set aws_accounts_table = 'aws_accounts' %}
{% set product_teams_table = 'product_teams' %}
{% set findings_table = 'aws_inspector2_findings' %}
SELECT
a.account_id,
a.friendly_name AS account_friendly_name,
t.team_id,
t.team_name,
f.arn AS finding_id,
f.severity,
f.description,
f.last_observed_at,
f.first_observed_at,
f.type,
f.exploit_available,
f.title,
f.status
FROM
{{ aws_accounts_table }} a
JOIN
{{ product_teams_table }} t ON a.team_id = t.team_id
JOIN
{{ findings_table }} f ON a.account_id = f.aws_account_id
This did technically work, but I feel like there is a better way to do it that doesn’t require me to basically copy each AWS table. I assume there is a way to do this so that I don’t have to write the same dbt query with different column names for each either. Additionally, this doesn’t let me do a query like:
SELECT aws_inspector2_findings FROM product_teams WHERE team_name = 'team1'