sgmap - AWS Security Group Mapping Tool
sgmapは、AWS のセキュリティグループの接続関係を可視化するための CLI ツールおよびライブラリです。指定した VPC 内のセキュリティグループ間の接続関係を mermaid 記法のフローチャートまたは JSON 形式で出力します。
機能
- 指定した VPC 内のすべてのセキュリティグループの接続関係を分析
- 特定のセキュリティグループのみを分析するオプション
- mermaid 記法のフローチャートまたは JSON 形式での出力
- セキュリティグループ間の接続プロトコルとポート情報の表示
- CLI ツールとしての使用だけでなく、Python ライブラリとしても利用可能
インストール
sgmap は PyPI で公開されています。
pip を使用したインストール
pip install sgmap
ソースからのインストール
git clone https://github.com/youyo/sgmap.git
cd sgmap
pip install -e .
使用方法
CLI ツールとしての使用方法
# VPC内のすべてのセキュリティグループを分析し、mermaid記法で出力
sgmap --vpc-id vpc-12345678
# 特定のセキュリティグループのみを分析
sgmap --vpc-id vpc-12345678 --security-group-id sg-87654321
# JSON形式で出力
sgmap --vpc-id vpc-12345678 --json
# VPCを含めてセキュリティグループの接続を表示
sgmap --vpc-id vpc-12345678 --with-vpc
オプション
--vpc-id,-v(必須): 分析対象の VPC ID--security-group-id,-s(オプション): 特定のセキュリティグループ ID を指定して分析--json,-j(フラグ): JSON 形式で出力(デフォルトは mermaid 記法)--with-vpc(フラグ): mermaid ダイアグラムに VPC を含める(デフォルトではセキュリティグループとその接続のみを表示)
ライブラリとしての使用方法
sgmap は Python ライブラリとしても利用できます。以下は基本的な使用例です:
import sgmap
# VPC内のセキュリティグループ情報を取得
vpc_and_sgs = sgmap.get_security_groups('vpc-12345678')
# 特定のセキュリティグループのみを取得する場合
# vpc_and_sgs = sgmap.get_security_groups('vpc-12345678', 'sg-87654321')
# セキュリティグループの接続関係を分析
connections = sgmap.analyze_security_group_connections(vpc_and_sgs)
# mermaid記法のダイアグラムを生成
mermaid_diagram = sgmap.generate_mermaid_diagram(connections)
print(mermaid_diagram)
# JSON形式の出力を生成
json_output = sgmap.generate_json_output(connections)
print(json_output)
利用可能な関数
get_security_groups(vpc_id, security_group_id=None): 指定した VPC 内のセキュリティグループ情報を取得analyze_security_group_connections(vpc_and_sgs): セキュリティグループの接続関係を分析generate_mermaid_diagram(connections): mermaid 記法のダイアグラムを生成generate_json_output(connections): JSON 形式の出力を生成
出力例
mermaid 記法の出力例
flowchart LR
SG_sg_12345["Web Server\n(sg-12345)"]
SG_sg_67890["Database\n(sg-67890)"]
SG_sg_12345 -->|tcp/80| SG_sg_67890
JSON 形式の出力例
{
"sg-12345": {
"name": "Web Server",
"inbound": [],
"outbound": [
{
"id": "sg-67890",
"name": "Database",
"protocol": "tcp",
"from_port": 80,
"to_port": 80
}
]
},
"sg-67890": {
"name": "Database",
"inbound": [
{
"id": "sg-12345",
"name": "Web Server",
"protocol": "tcp",
"from_port": 80,
"to_port": 80
}
],
"outbound": []
}
}
必要条件
- Python 3.6 以上
- boto3
- click
- AWS 認証情報(環境変数、~/.aws/credentials など)
開発者向け情報
開発環境のセットアップ
git clone https://github.com/youyo/sgmap.git
cd sgmap
pip install -e ".[dev]"
リリース方法
このプロジェクトは GitHub Actions を使用して PyPI にパッケージを公開しています。タグのバージョンがそのまま PyPI のパッケージバージョンとして使用されます。以下の手順でリリースを行います:
-
バージョンタグを作成します(例:
v0.1.0)git tag -a v0.1.0 -m "Release version 0.1.0" git push origin v0.1.0
注意:
- タグの形式は必ず
vで始まり、その後にX.Y.Z形式のバージョン番号が続く形式(例:v0.1.0、v1.2.3)にしてください。 - タグのバージョン番号(
vを除いた部分)がそのまま PyPI のパッケージバージョンとして使用されます。例えば、v0.2.3というタグを作成すると、PyPI では0.2.3というバージョンでパッケージが公開されます。 pyproject.tomlの設定により、タグのバージョンがそのまま使用され、余分なサフィックス(.post1.dev0など)が付かないようになっています。- この形式に従わないタグはリリースプロセスで問題が発生する可能性があります。
- タグの形式は必ず
-
GitHub Actions が自動的にパッケージをビルドし、PyPI に公開します。
ローカルでのビルド
pip install -r requirements-dev.txt
python -m build
ライセンス
MIT
作者
youyo
Metadata
Release files for sgmap 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sgmap-0.3.1.tar.gz | 25.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sgmap-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 33.0 kB
Release files / sgmap-0.3.1.tar.gz
| Download URL | sgmap-0.3.1.tar.gz |
|---|---|
| Size | 25.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2a4cf183df73911d7870180361b450edc8dd63e7154d2456169035fa76cc9675
|
|
BLAKE2b-256 checksum How to use checksums |
feabf6be74fa39cee7f90f141241ce736efc2c4cce8e4a6944ecc31f79c37f6a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 20, 2025.
Transparency logRelease files / sgmap-0.3.1-py3-none-any.whl
| Download URL | sgmap-0.3.1-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
56001c2a627c74b8e0793596c89da2c25af29ac945b79fef9cdae5e78a52feae
|
|
BLAKE2b-256 checksum How to use checksums |
7ebdcf17ad3333a774e72327a79c1ac3fc2a46b79d7e2acb51188d813938b917
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 20, 2025.
Transparency log