magic-pocket-cli 0.44.0__tar.gz → 0.46.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/.gitignore +0 -2
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/PKG-INFO +2 -2
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/cloudfront_cli.py +65 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/waf_cli.py +117 -4
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/migrations.py +101 -15
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/cloudfront.py +64 -27
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/cloudfront_waf.py +35 -2
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/cloudfront_waf.yaml +44 -5
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pyproject.toml +2 -2
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/__init__.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/__init__.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/aws_auth.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/backup_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/build_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/cleanup_deprecated_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/cloudfront_keys_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/cloudfront_waf_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/container_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/deploy_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/destroy_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/dsql_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/image_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/inbound_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/interaction.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/main_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/migrate_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/neon_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/permissions_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/rds_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/removed_flags.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/resource_helper.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/runtime_config_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/s3_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/status_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/store_url_helper.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/tidb_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/upstash_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/url_helper.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/vpc_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/django_cli.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/mediator.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/__init__.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/__init__.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/backup_common.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/backup_legacy.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/builders/__init__.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/builders/codebuild.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/builders/context_check.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/builders/depot.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/builders/docker.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/builders/dockerignore.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/cloudformation.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/ecr.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/efs.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/iam_roles.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/lambdahandler.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/poll.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/s3_utils.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/stack_backed.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/stage_roles.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/state.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/backup.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/cloudfront_acm.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/cloudfront_keys.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/container.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/dsql.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/inbound.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/inbound_domain.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/inbound_template.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/neon.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/rds.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/s3.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/sqs_alert.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/tidb.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/upstash.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/vpc.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/cf_function_api_host.js +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/cf_function_basic_auth_prelude.js +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/cf_function_cache_control.js +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/cf_function_deploy_hash_strip.js +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/cf_function_host_redirect_prelude.js +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/cf_function_spa_auth.js +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/cf_function_spa_fallback.js +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/cloudfront.yaml +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/cloudfront_acm.yaml +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/cloudfront_keys.yaml +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/container.yaml +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/vpc.yaml +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/init/django-dotenv.env +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/init/django-settings.py +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/init/pocket.Dockerfile +0 -0
- {magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/init/pocket_simple.toml +0 -0
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: magic-pocket-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.46.0
|
|
4
4
|
Summary: CLI and deploy tools for magic-pocket.
|
|
5
5
|
Requires-Python: >=3.10
|
|
6
6
|
Requires-Dist: awscrt>=0.19.0
|
|
7
7
|
Requires-Dist: click>=8.1.7
|
|
8
8
|
Requires-Dist: deepdiff>=6.7.1
|
|
9
9
|
Requires-Dist: jinja2>=3.1.3
|
|
10
|
-
Requires-Dist: magic-pocket<0.
|
|
10
|
+
Requires-Dist: magic-pocket<0.47,>=0.46.0
|
|
11
11
|
Requires-Dist: pathspec>=1.0.4
|
|
12
12
|
Requires-Dist: python-on-whales>=0.68.0
|
|
13
13
|
Requires-Dist: pyyaml>=6.0.1
|
|
@@ -115,6 +115,71 @@ def upload(stage, name, skip_build):
|
|
|
115
115
|
echo.success("アップロード完了")
|
|
116
116
|
|
|
117
117
|
|
|
118
|
+
@cloudfront.command()
|
|
119
|
+
@click.option("--stage", envvar="POCKET_DEPLOY_STAGE", prompt=True)
|
|
120
|
+
@click.option("--name", default=None)
|
|
121
|
+
@click.option(
|
|
122
|
+
"--route",
|
|
123
|
+
"path_pattern",
|
|
124
|
+
default=None,
|
|
125
|
+
help="対象 route の path_pattern (省略時は build / upload_dir を持つ全 route)",
|
|
126
|
+
)
|
|
127
|
+
@click.option("--dry-run", is_flag=True, default=False, help="削除せず一覧だけ表示")
|
|
128
|
+
@click.option(
|
|
129
|
+
"--yes", "-y", is_flag=True, default=False, help="確認プロンプトをスキップ"
|
|
130
|
+
)
|
|
131
|
+
def prune(stage, name, path_pattern, dry_run, yes):
|
|
132
|
+
"""ローカルの成果物に無い S3 key (旧版) を削除する。
|
|
133
|
+
|
|
134
|
+
delete_stale = false (content_hash の既定) で残した旧版の掃除用。ビルドは
|
|
135
|
+
実行しないので、upload_dir を最新のビルドにしてから実行する。
|
|
136
|
+
"""
|
|
137
|
+
targets = _collect_stale_keys(stage, name, path_pattern)
|
|
138
|
+
total = sum(len(keys) for _, keys in targets)
|
|
139
|
+
if not total:
|
|
140
|
+
echo.success("削除対象の旧版はありません")
|
|
141
|
+
return
|
|
142
|
+
if dry_run:
|
|
143
|
+
echo.info("--dry-run のため削除しません (%d 件)" % total)
|
|
144
|
+
return
|
|
145
|
+
if not yes:
|
|
146
|
+
click.confirm("上記 %d 件を削除しますか?" % total, abort=True)
|
|
147
|
+
for cf, keys in targets:
|
|
148
|
+
cf.delete_objects(keys)
|
|
149
|
+
echo.success("%d 件を削除しました" % total)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _collect_stale_keys(
|
|
153
|
+
stage, name, path_pattern
|
|
154
|
+
) -> list[tuple[CloudFront, list[str]]]:
|
|
155
|
+
"""route ごとの旧版 key を表示しつつ集める。
|
|
156
|
+
|
|
157
|
+
--route が何にも当たらなければエラー。
|
|
158
|
+
"""
|
|
159
|
+
targets: list[tuple[CloudFront, list[str]]] = []
|
|
160
|
+
matched = False
|
|
161
|
+
for cf in get_cloudfront_resources(stage, name):
|
|
162
|
+
for route in cf.context.uploadable_routes:
|
|
163
|
+
if path_pattern is not None and route.path_pattern != path_pattern:
|
|
164
|
+
continue
|
|
165
|
+
matched = True
|
|
166
|
+
keys = cf.stale_keys(route)
|
|
167
|
+
echo.info(
|
|
168
|
+
"[%s] %s: 旧版 %d 件"
|
|
169
|
+
% (cf.context.name, route.path_pattern or "/*", len(keys))
|
|
170
|
+
)
|
|
171
|
+
for key in keys:
|
|
172
|
+
echo.log(" s3://%s/%s" % (cf.context.bucket_name, key))
|
|
173
|
+
if keys:
|
|
174
|
+
targets.append((cf, keys))
|
|
175
|
+
if path_pattern is not None and not matched:
|
|
176
|
+
raise click.ClickException(
|
|
177
|
+
"route '%s' はアップロード対象 (build / upload_dir を持つ route)"
|
|
178
|
+
" にありません" % path_pattern
|
|
179
|
+
)
|
|
180
|
+
return targets
|
|
181
|
+
|
|
182
|
+
|
|
118
183
|
@cloudfront.command()
|
|
119
184
|
@click.option("--stage", envvar="POCKET_DEPLOY_STAGE", prompt=True)
|
|
120
185
|
@click.option("--name", default=None)
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
"""`pocket waf ip ...`
|
|
1
|
+
"""`pocket waf ip ...` / `pocket waf ratelimit ...` — WAF の運用 CLI。
|
|
2
|
+
|
|
3
|
+
`pocket waf ip ...` は IPSet を CFN を介さず直接更新する。
|
|
2
4
|
|
|
3
5
|
WebACL / IPSet 自体は CFn (CloudFrontWafStack) が us-east-1 に作成し、
|
|
4
6
|
Addresses は CFn template 上は常に空。実際の CIDR 一覧はこの CLI が
|
|
@@ -14,9 +16,10 @@ from typing import TYPE_CHECKING
|
|
|
14
16
|
import boto3
|
|
15
17
|
import click
|
|
16
18
|
|
|
17
|
-
from pocket.context import CloudFrontContext, Context
|
|
19
|
+
from pocket.context import CloudFrontContext, CloudFrontWafRateLimitContext, Context
|
|
18
20
|
from pocket.utils import echo
|
|
19
21
|
from pocket_cli.resources.aws.cloudformation import CloudFrontWafStack
|
|
22
|
+
from pocket_cli.resources.cloudfront_waf import format_rate_limit
|
|
20
23
|
|
|
21
24
|
if TYPE_CHECKING:
|
|
22
25
|
from mypy_boto3_wafv2 import WAFV2Client
|
|
@@ -32,7 +35,12 @@ def ip():
|
|
|
32
35
|
pass
|
|
33
36
|
|
|
34
37
|
|
|
35
|
-
|
|
38
|
+
@waf.group()
|
|
39
|
+
def ratelimit():
|
|
40
|
+
"""rate_limits (rate-based rule) の運用。"""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _get_waf_cf_context(stage: str, name: str) -> CloudFrontContext:
|
|
36
44
|
context = Context.from_toml(stage=stage)
|
|
37
45
|
if not context.cloudfront:
|
|
38
46
|
raise click.ClickException("cloudfront is not configured for this stage")
|
|
@@ -43,7 +51,12 @@ def _get_cf_context(stage: str, name: str) -> CloudFrontContext:
|
|
|
43
51
|
raise click.ClickException(
|
|
44
52
|
"cloudfront '%s' has no [cloudfront.%s.waf] block" % (name, name)
|
|
45
53
|
)
|
|
46
|
-
|
|
54
|
+
return cf_ctx
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _get_cf_context(stage: str, name: str) -> CloudFrontContext:
|
|
58
|
+
cf_ctx = _get_waf_cf_context(stage, name)
|
|
59
|
+
if cf_ctx.waf is None or not cf_ctx.waf.enable_ip_set:
|
|
47
60
|
raise click.ClickException(
|
|
48
61
|
"cloudfront '%s' is configured with [cloudfront.%s.waf]"
|
|
49
62
|
" enable_ip_set = false, so there is no IPSet to operate on."
|
|
@@ -188,3 +201,103 @@ def ip_clear(stage, name, yes):
|
|
|
188
201
|
)
|
|
189
202
|
_write_ip_set(client, set_name, set_id, [], lock)
|
|
190
203
|
echo.success("IPSet を全削除しました (deny-all 状態)。")
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
# --- ratelimit ------------------------------------------------------------------
|
|
207
|
+
|
|
208
|
+
RATE_LIMIT_RULE_PREFIX = "ratelimit-"
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _get_web_acl_meta(cf_ctx: CloudFrontContext) -> tuple[str, str]:
|
|
212
|
+
"""CFn stack output の WebACLArn から WebACL の (Name, Id) を取得する。
|
|
213
|
+
|
|
214
|
+
ARN は `arn:aws:wafv2:us-east-1:<acct>:global/webacl/<name>/<id>`。
|
|
215
|
+
"""
|
|
216
|
+
stack = CloudFrontWafStack(cf_ctx)
|
|
217
|
+
output = stack.output
|
|
218
|
+
if not output:
|
|
219
|
+
raise click.ClickException(
|
|
220
|
+
"WAF stack '%s' が見つかりません。先に `pocket deploy` を実行してください。"
|
|
221
|
+
% stack.name
|
|
222
|
+
)
|
|
223
|
+
arn = output.get("WebACLArn")
|
|
224
|
+
if not arn:
|
|
225
|
+
raise click.ClickException(
|
|
226
|
+
"WAF stack output に WebACLArn がありません: %s" % stack.name
|
|
227
|
+
)
|
|
228
|
+
acl_name, acl_id = arn.rsplit("/", 2)[-2:]
|
|
229
|
+
return acl_name, acl_id
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def _deployed_rate_limit_rules(
|
|
233
|
+
client: WAFV2Client, acl_name: str, acl_id: str
|
|
234
|
+
) -> list[tuple[str, CloudFrontWafRateLimitContext]]:
|
|
235
|
+
"""deploy 済み WebACL から rate rule を読む (ローカルの宣言ではなく実態を見る)。
|
|
236
|
+
|
|
237
|
+
pocket.toml を変えて deploy していない間や並べ替えの直後に、別の rule の
|
|
238
|
+
IP を誤った説明で表示しないため。
|
|
239
|
+
"""
|
|
240
|
+
acl = client.get_web_acl(Name=acl_name, Scope="CLOUDFRONT", Id=acl_id)["WebACL"]
|
|
241
|
+
rules: list[tuple[str, CloudFrontWafRateLimitContext]] = []
|
|
242
|
+
for rule in sorted(acl.get("Rules", []), key=lambda r: r["Priority"]):
|
|
243
|
+
if not rule["Name"].startswith(RATE_LIMIT_RULE_PREFIX):
|
|
244
|
+
continue
|
|
245
|
+
statement = rule["Statement"].get("RateBasedStatement")
|
|
246
|
+
if statement is None:
|
|
247
|
+
continue
|
|
248
|
+
path = None
|
|
249
|
+
scope_down = statement.get("ScopeDownStatement", {}).get("ByteMatchStatement")
|
|
250
|
+
if scope_down is not None:
|
|
251
|
+
search = scope_down["SearchString"]
|
|
252
|
+
text = search.decode() if isinstance(search, bytes) else str(search)
|
|
253
|
+
if scope_down.get("PositionalConstraint") == "STARTS_WITH":
|
|
254
|
+
text += "*"
|
|
255
|
+
path = text
|
|
256
|
+
action = "block" if "Block" in rule.get("Action", {}) else "count"
|
|
257
|
+
rules.append(
|
|
258
|
+
(
|
|
259
|
+
rule["Name"],
|
|
260
|
+
CloudFrontWafRateLimitContext(
|
|
261
|
+
limit=statement["Limit"],
|
|
262
|
+
window_sec=statement.get("EvaluationWindowSec", 300),
|
|
263
|
+
action=action,
|
|
264
|
+
path=path,
|
|
265
|
+
),
|
|
266
|
+
)
|
|
267
|
+
)
|
|
268
|
+
return rules
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
@ratelimit.command("list")
|
|
272
|
+
@click.option("--stage", envvar="POCKET_DEPLOY_STAGE", prompt=True)
|
|
273
|
+
@click.option("--name", required=True, help="CloudFront name (pocket.toml の key)")
|
|
274
|
+
def ratelimit_list(stage, name):
|
|
275
|
+
"""deploy 済みの rate rule ごとに、レート超過として追跡中の IP を表示する。
|
|
276
|
+
|
|
277
|
+
WAF が「超過中」として扱っている IP の一覧 (managed keys) で、action が
|
|
278
|
+
block なら 429 で止めている IP、count なら止めずに数えているだけの IP。
|
|
279
|
+
超過の検知と解除には数分の遅れがある。
|
|
280
|
+
"""
|
|
281
|
+
cf_ctx = _get_waf_cf_context(stage, name)
|
|
282
|
+
acl_name, acl_id = _get_web_acl_meta(cf_ctx)
|
|
283
|
+
client = _wafv2_client()
|
|
284
|
+
rules = _deployed_rate_limit_rules(client, acl_name, acl_id)
|
|
285
|
+
if not rules:
|
|
286
|
+
echo.warning(
|
|
287
|
+
"WebACL '%s' に rate rule がありません。[cloudfront.%s.waf] の"
|
|
288
|
+
" rate_limits を宣言して deploy してください。" % (acl_name, name)
|
|
289
|
+
)
|
|
290
|
+
return
|
|
291
|
+
for rule_name, rule in rules:
|
|
292
|
+
keys = client.get_rate_based_statement_managed_keys(
|
|
293
|
+
Scope="CLOUDFRONT", WebACLName=acl_name, WebACLId=acl_id, RuleName=rule_name
|
|
294
|
+
)
|
|
295
|
+
addresses = list(keys.get("ManagedKeysIPV4", {}).get("Addresses", [])) + list(
|
|
296
|
+
keys.get("ManagedKeysIPV6", {}).get("Addresses", [])
|
|
297
|
+
)
|
|
298
|
+
echo.info(format_rate_limit(rule_name, rule))
|
|
299
|
+
if not addresses:
|
|
300
|
+
click.echo(" (レート超過として追跡中の IP はありません)")
|
|
301
|
+
continue
|
|
302
|
+
for address in addresses:
|
|
303
|
+
click.echo(" %s" % address)
|
|
@@ -30,11 +30,14 @@ from dataclasses import dataclass
|
|
|
30
30
|
from typing import TYPE_CHECKING, Callable
|
|
31
31
|
|
|
32
32
|
import boto3
|
|
33
|
+
from botocore.exceptions import WaiterError
|
|
33
34
|
|
|
34
35
|
from pocket.utils import echo
|
|
35
36
|
from pocket_cli.cli import interaction
|
|
36
37
|
|
|
37
38
|
if TYPE_CHECKING:
|
|
39
|
+
from mypy_boto3_cloudformation import CloudFormationClient
|
|
40
|
+
|
|
38
41
|
from pocket.context import Context
|
|
39
42
|
|
|
40
43
|
|
|
@@ -141,16 +144,88 @@ def legacy_ecr_repo_name(context: Context) -> str:
|
|
|
141
144
|
return f"{resource_prefix}lambda"
|
|
142
145
|
|
|
143
146
|
|
|
144
|
-
def
|
|
145
|
-
"""旧命名の container stack
|
|
147
|
+
def legacy_container_stack_status(context: Context) -> str | None:
|
|
148
|
+
"""旧命名の container stack の StackStatus。削除済み / 無ければ None。"""
|
|
146
149
|
if not context.general:
|
|
147
|
-
return
|
|
150
|
+
return None
|
|
148
151
|
cfn = boto3.client("cloudformation", region_name=context.general.region)
|
|
149
152
|
try:
|
|
150
|
-
cfn.describe_stacks(StackName=legacy_container_stack_name(context))
|
|
153
|
+
response = cfn.describe_stacks(StackName=legacy_container_stack_name(context))
|
|
151
154
|
except cfn.exceptions.ClientError:
|
|
152
|
-
return
|
|
153
|
-
return
|
|
155
|
+
return None
|
|
156
|
+
return response["Stacks"][0]["StackStatus"]
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def legacy_container_stack_exists(context: Context) -> bool:
|
|
160
|
+
"""旧命名の container stack が (削除済み以外の状態で) 存在するか。"""
|
|
161
|
+
return legacy_container_stack_status(context) is not None
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
# 旧 stack の削除完了を待つ上限 (秒)。Lambda / API Gateway / scheduler / SQS の
|
|
165
|
+
# 削除は通常数分で終わる。超えたら deploy を止めずに警告して先へ進む
|
|
166
|
+
LEGACY_STACK_DELETE_TIMEOUT = 300
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _legacy_stack_failure_reasons(cfn: CloudFormationClient, stack_name: str) -> str:
|
|
170
|
+
"""DELETE_FAILED の stack から失敗したリソースと理由だけを取り出す。"""
|
|
171
|
+
lines: list[str] = []
|
|
172
|
+
for page in cfn.get_paginator("list_stack_resources").paginate(
|
|
173
|
+
StackName=stack_name
|
|
174
|
+
):
|
|
175
|
+
for resource in page["StackResourceSummaries"]:
|
|
176
|
+
if resource["ResourceStatus"].endswith("FAILED"):
|
|
177
|
+
lines.append(
|
|
178
|
+
" - %s (%s): %s"
|
|
179
|
+
% (
|
|
180
|
+
resource["LogicalResourceId"],
|
|
181
|
+
resource["ResourceType"],
|
|
182
|
+
resource.get("ResourceStatusReason", "理由なし"),
|
|
183
|
+
)
|
|
184
|
+
)
|
|
185
|
+
return "\n".join(lines) if lines else " (失敗したリソースの情報なし)"
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _delete_legacy_container_stack(cfn: CloudFormationClient, stack_name: str):
|
|
189
|
+
"""旧 stack を削除し、完了まで待つ。失敗しても deploy は止めない。
|
|
190
|
+
|
|
191
|
+
完了を待たずに success で終えると、CloudFormation 側の一過性エラーで
|
|
192
|
+
DELETE_FAILED に落ちた stack に誰も気付けない (kikiwake prod、KN1799)。
|
|
193
|
+
失敗は理由付きで警告し、次回 deploy の再検出 (= 再プロンプト) に委ねる。
|
|
194
|
+
"""
|
|
195
|
+
cfn.delete_stack(StackName=stack_name)
|
|
196
|
+
echo.log(
|
|
197
|
+
"旧 stack の削除を開始しました (最大 %d 秒待ちます)。"
|
|
198
|
+
% LEGACY_STACK_DELETE_TIMEOUT
|
|
199
|
+
)
|
|
200
|
+
delay = 10
|
|
201
|
+
try:
|
|
202
|
+
cfn.get_waiter("stack_delete_complete").wait(
|
|
203
|
+
StackName=stack_name,
|
|
204
|
+
WaiterConfig={
|
|
205
|
+
"Delay": delay,
|
|
206
|
+
"MaxAttempts": LEGACY_STACK_DELETE_TIMEOUT // delay,
|
|
207
|
+
},
|
|
208
|
+
)
|
|
209
|
+
except WaiterError as error:
|
|
210
|
+
if error.kwargs.get("reason") == "Max attempts exceeded":
|
|
211
|
+
echo.warning(
|
|
212
|
+
"旧 stack '%s' の削除が %d 秒以内に終わりませんでした。deploy は"
|
|
213
|
+
"このまま続けます。完了したか後で確認してください:\n"
|
|
214
|
+
" aws cloudformation describe-stacks --stack-name %s"
|
|
215
|
+
% (stack_name, LEGACY_STACK_DELETE_TIMEOUT, stack_name)
|
|
216
|
+
)
|
|
217
|
+
return
|
|
218
|
+
echo.danger(
|
|
219
|
+
"旧 stack '%s' の削除が失敗しました (DELETE_FAILED)。deploy 自体は"
|
|
220
|
+
"成功していますが、旧 stack の scheduler / SQS が残っている可能性が"
|
|
221
|
+
"あります。失敗したリソース:\n%s\n"
|
|
222
|
+
"CloudFormation の一過性エラーなら次回 deploy の再プロンプト、または"
|
|
223
|
+
"次で再実行すると消えることがあります:\n"
|
|
224
|
+
" aws cloudformation delete-stack --stack-name %s"
|
|
225
|
+
% (stack_name, _legacy_stack_failure_reasons(cfn, stack_name), stack_name)
|
|
226
|
+
)
|
|
227
|
+
return
|
|
228
|
+
echo.success("旧 stack '%s' を削除しました。" % stack_name)
|
|
154
229
|
|
|
155
230
|
|
|
156
231
|
def legacy_ecr_repo_exists(context: Context) -> bool:
|
|
@@ -182,19 +257,30 @@ def _cleanup_legacy_container_resources(context: Context):
|
|
|
182
257
|
return
|
|
183
258
|
region = context.general.region
|
|
184
259
|
legacy_stack_name = legacy_container_stack_name(context)
|
|
185
|
-
|
|
260
|
+
legacy_status = legacy_container_stack_status(context)
|
|
261
|
+
if legacy_status is not None:
|
|
186
262
|
cfn = boto3.client("cloudformation", region_name=region)
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
263
|
+
if legacy_status == "DELETE_FAILED":
|
|
264
|
+
echo.warning(
|
|
265
|
+
"旧形式の container stack '%s' が前回の削除に失敗したまま"
|
|
266
|
+
"残っています (DELETE_FAILED)。失敗したリソース:\n%s\n"
|
|
267
|
+
"一過性のエラーなら削除の再実行で消えます。"
|
|
268
|
+
% (
|
|
269
|
+
legacy_stack_name,
|
|
270
|
+
_legacy_stack_failure_reasons(cfn, legacy_stack_name),
|
|
271
|
+
)
|
|
272
|
+
)
|
|
273
|
+
else:
|
|
274
|
+
echo.warning(
|
|
275
|
+
"旧形式の container stack '%s' が残っています (0.29.0 の "
|
|
276
|
+
"multi-container 化で stack 名が {slug}-container-{name} に"
|
|
277
|
+
"変わりました)。旧 stack の scheduler / SQS が動き続けるため、"
|
|
278
|
+
"削除を推奨します。" % legacy_stack_name
|
|
279
|
+
)
|
|
193
280
|
if interaction.confirm(
|
|
194
281
|
"旧 stack '%s' を削除しますか?" % legacy_stack_name, default=True
|
|
195
282
|
):
|
|
196
|
-
cfn
|
|
197
|
-
echo.log("旧 stack の削除を開始しました (完了待ちはしません)。")
|
|
283
|
+
_delete_legacy_container_stack(cfn, legacy_stack_name)
|
|
198
284
|
if not legacy_ecr_repo_exists(context):
|
|
199
285
|
return
|
|
200
286
|
legacy_repo = legacy_ecr_repo_name(context)
|
|
@@ -319,21 +319,17 @@ class CloudFront:
|
|
|
319
319
|
|
|
320
320
|
内容が一致するオブジェクトは skip する (差分アップロード)。数千ファイル
|
|
321
321
|
規模の配信アセットを upload_dir に置いても deploy 時間が伸びない。
|
|
322
|
+
|
|
323
|
+
今回の成果物に無い key は route.delete_stale のときだけ消す (content_hash
|
|
324
|
+
は既定で残す。RouteContext.delete_stale のコメント参照。残した旧版は
|
|
325
|
+
`pocket resource cloudfront prune` で消せる)。
|
|
322
326
|
"""
|
|
323
|
-
s3_prefix = (route
|
|
324
|
-
if not route.upload_dir:
|
|
325
|
-
raise RuntimeError("route.upload_dir is not set")
|
|
326
|
-
local_dir = Path(route.upload_dir)
|
|
327
|
+
s3_prefix = self._s3_prefix(route)
|
|
327
328
|
existing = self._list_objects(s3_prefix)
|
|
328
|
-
|
|
329
|
+
local_files = self._local_files(route, s3_prefix)
|
|
329
330
|
uploaded = 0
|
|
330
331
|
skipped = 0
|
|
331
|
-
for file in
|
|
332
|
-
if file.is_dir():
|
|
333
|
-
continue
|
|
334
|
-
relative = file.relative_to(local_dir)
|
|
335
|
-
s3_key = s3_prefix + "/" + str(relative)
|
|
336
|
-
uploaded_keys.add(s3_key)
|
|
332
|
+
for s3_key, file in local_files.items():
|
|
337
333
|
if _is_unchanged(file, existing.get(s3_key)):
|
|
338
334
|
skipped += 1
|
|
339
335
|
continue
|
|
@@ -341,7 +337,15 @@ class CloudFront:
|
|
|
341
337
|
"ContentType": mimetypes.guess_type(str(file))[0]
|
|
342
338
|
or "application/octet-stream"
|
|
343
339
|
}
|
|
344
|
-
if route.
|
|
340
|
+
if route.is_content_hash:
|
|
341
|
+
# viewer-response Function が viewer 向けに付ける値と同じ
|
|
342
|
+
# (cf_function_cache_control.js)。オブジェクト側にも付けないと
|
|
343
|
+
# behavior の cache policy (DefaultTTL 1 日) で edge が 1 日で
|
|
344
|
+
# 切れ、内容 hash 付きの名前なのに origin へ取りに戻る (KN2009)
|
|
345
|
+
extra_args["CacheControl"] = (
|
|
346
|
+
"public, max-age=%d, immutable" % route.versioned_max_age
|
|
347
|
+
)
|
|
348
|
+
elif route.is_spa:
|
|
345
349
|
if file.suffix in (".html", ".htm"):
|
|
346
350
|
extra_args["CacheControl"] = "no-cache, no-store"
|
|
347
351
|
else:
|
|
@@ -355,13 +359,51 @@ class CloudFront:
|
|
|
355
359
|
)
|
|
356
360
|
uploaded += 1
|
|
357
361
|
echo.log("アップロード: s3://%s/%s" % (self.context.bucket_name, s3_key))
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
362
|
+
stale = [key for key in existing if key not in local_files]
|
|
363
|
+
if route.delete_stale:
|
|
364
|
+
deleted = self.delete_objects(stale)
|
|
365
|
+
echo.info(
|
|
366
|
+
"%d ファイルをアップロードしました (skip %d / 削除 %d、prefix: %s)"
|
|
367
|
+
% (uploaded, skipped, deleted, s3_prefix)
|
|
368
|
+
)
|
|
369
|
+
else:
|
|
370
|
+
deleted = 0
|
|
371
|
+
echo.info(
|
|
372
|
+
"%d ファイルをアップロードしました"
|
|
373
|
+
" (skip %d / 旧版を保持 %d、prefix: %s)"
|
|
374
|
+
% (uploaded, skipped, len(stale), s3_prefix)
|
|
375
|
+
)
|
|
363
376
|
return bool(uploaded or deleted)
|
|
364
377
|
|
|
378
|
+
@staticmethod
|
|
379
|
+
def _s3_prefix(route: RouteContext) -> str:
|
|
380
|
+
return (route.origin_path + route.path_pattern.rstrip("/*")).lstrip("/")
|
|
381
|
+
|
|
382
|
+
@staticmethod
|
|
383
|
+
def _local_files(route: RouteContext, s3_prefix: str) -> dict[str, Path]:
|
|
384
|
+
"""route の upload_dir 配下のファイルを {S3 key: ローカル path} で返す。"""
|
|
385
|
+
if not route.upload_dir:
|
|
386
|
+
raise RuntimeError("route.upload_dir is not set")
|
|
387
|
+
local_dir = Path(route.upload_dir)
|
|
388
|
+
files: dict[str, Path] = {}
|
|
389
|
+
for file in local_dir.rglob("*"):
|
|
390
|
+
if file.is_dir():
|
|
391
|
+
continue
|
|
392
|
+
files[s3_prefix + "/" + str(file.relative_to(local_dir))] = file
|
|
393
|
+
return files
|
|
394
|
+
|
|
395
|
+
def stale_keys(self, route: RouteContext) -> list[str]:
|
|
396
|
+
"""route の S3 prefix 配下で、ローカルの upload_dir に無い key (旧版) を返す。
|
|
397
|
+
|
|
398
|
+
ビルドは実行しないので、upload_dir が最新の成果物であることは呼び手の責任
|
|
399
|
+
(`upload --skip-build` と同じ前提)。
|
|
400
|
+
"""
|
|
401
|
+
s3_prefix = self._s3_prefix(route)
|
|
402
|
+
local_files = self._local_files(route, s3_prefix)
|
|
403
|
+
return sorted(
|
|
404
|
+
key for key in self._list_objects(s3_prefix) if key not in local_files
|
|
405
|
+
)
|
|
406
|
+
|
|
365
407
|
def _list_objects(self, prefix: str) -> dict[str, str]:
|
|
366
408
|
"""prefix 配下の {key: ETag} を返す (ETag は前後の `"` を除去済み)。"""
|
|
367
409
|
objects: dict[str, str] = {}
|
|
@@ -373,16 +415,11 @@ class CloudFront:
|
|
|
373
415
|
objects[obj["Key"]] = obj["ETag"].strip('"')
|
|
374
416
|
return objects
|
|
375
417
|
|
|
376
|
-
def
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
if key not in uploaded_keys:
|
|
382
|
-
self.s3_client.delete_object(Bucket=self.context.bucket_name, Key=key)
|
|
383
|
-
deleted += 1
|
|
384
|
-
echo.log("削除: s3://%s/%s" % (self.context.bucket_name, key))
|
|
385
|
-
return deleted
|
|
418
|
+
def delete_objects(self, keys: list[str]) -> int:
|
|
419
|
+
for key in keys:
|
|
420
|
+
self.s3_client.delete_object(Bucket=self.context.bucket_name, Key=key)
|
|
421
|
+
echo.log("削除: s3://%s/%s" % (self.context.bucket_name, key))
|
|
422
|
+
return len(keys)
|
|
386
423
|
|
|
387
424
|
def _invalidate(self, routes: list[RouteContext]):
|
|
388
425
|
"""変更があった route の path_pattern だけを invalidate する。
|
|
@@ -8,7 +8,19 @@ from pocket_cli.resources.aws.cloudformation import CloudFrontWafStack
|
|
|
8
8
|
from pocket_cli.resources.aws.stack_backed import StackBackedResource
|
|
9
9
|
|
|
10
10
|
if TYPE_CHECKING:
|
|
11
|
-
from pocket.context import CloudFrontContext
|
|
11
|
+
from pocket.context import CloudFrontContext, CloudFrontWafRateLimitContext
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def format_rate_limit(name: str, rule: CloudFrontWafRateLimitContext) -> str:
|
|
15
|
+
"""rate rule を 1 行で表す (deploy 表示と `pocket waf ratelimit list` 共通)。"""
|
|
16
|
+
path = rule.path or "(全リクエスト)"
|
|
17
|
+
return "%s: %d req / %d 秒, path=%s, action=%s" % (
|
|
18
|
+
name,
|
|
19
|
+
rule.limit,
|
|
20
|
+
rule.window_sec,
|
|
21
|
+
path,
|
|
22
|
+
rule.action,
|
|
23
|
+
)
|
|
12
24
|
|
|
13
25
|
|
|
14
26
|
class CloudFrontWaf(StackBackedResource):
|
|
@@ -55,6 +67,7 @@ class CloudFrontWaf(StackBackedResource):
|
|
|
55
67
|
def create(self, mediator: Mediator | None = None): # type: ignore[override]
|
|
56
68
|
echo.log("WAF (IPSet + WebACL) を作成中 (us-east-1)...")
|
|
57
69
|
self._ensure_allow_secrets(mediator)
|
|
70
|
+
self._echo_rate_limits()
|
|
58
71
|
self._echo_allow_rules()
|
|
59
72
|
self._create_stack()
|
|
60
73
|
if self.context.waf is None:
|
|
@@ -67,10 +80,18 @@ class CloudFrontWaf(StackBackedResource):
|
|
|
67
80
|
% (self.context.name, self.context.stage)
|
|
68
81
|
)
|
|
69
82
|
else:
|
|
70
|
-
echo.info(
|
|
83
|
+
echo.info(
|
|
84
|
+
"WAF を作成しました (IP allowlist 無効。rate_limits %d 件、"
|
|
85
|
+
"managed rules %d 件)。"
|
|
86
|
+
% (
|
|
87
|
+
len(self.context.waf.rate_limits),
|
|
88
|
+
len(self.context.waf.managed_rule_groups),
|
|
89
|
+
)
|
|
90
|
+
)
|
|
71
91
|
|
|
72
92
|
def update(self, mediator: Mediator | None = None): # type: ignore[override]
|
|
73
93
|
self._ensure_allow_secrets(mediator)
|
|
94
|
+
self._echo_rate_limits()
|
|
74
95
|
self._echo_allow_rules()
|
|
75
96
|
self._update_stack()
|
|
76
97
|
|
|
@@ -122,6 +143,18 @@ class CloudFrontWaf(StackBackedResource):
|
|
|
122
143
|
values[key] = value
|
|
123
144
|
self._allow_secret_values = values
|
|
124
145
|
|
|
146
|
+
def _echo_rate_limits(self):
|
|
147
|
+
"""rate_limits は全ルールの先頭で効くので、deploy のたびに一覧を可視化する。"""
|
|
148
|
+
waf = self.context.waf
|
|
149
|
+
if not waf or not waf.rate_limits:
|
|
150
|
+
return
|
|
151
|
+
echo.info(
|
|
152
|
+
"WAF rate_limits (%d 件、全ルールの先頭で評価。超過した送信元 IP は"
|
|
153
|
+
" allow_rules / allowlist 済みでも対象):" % len(waf.rate_limits)
|
|
154
|
+
)
|
|
155
|
+
for i, rule in enumerate(waf.rate_limits):
|
|
156
|
+
echo.info(" %s" % format_rate_limit("ratelimit-%d" % i, rule))
|
|
157
|
+
|
|
125
158
|
def _echo_allow_rules(self):
|
|
126
159
|
"""allow_rules は WAF を弱める宣言なので、deploy のたびに一覧を可視化する。"""
|
|
127
160
|
waf = self.context.waf
|
|
@@ -13,8 +13,14 @@ Description: WAFv2 IPSet + WebACL for CloudFront (us-east-1)
|
|
|
13
13
|
# - DefaultAction = Allow
|
|
14
14
|
# - IPSet / ip-allow Rule は出力しない
|
|
15
15
|
# - managed_rule_groups (必須) で「許可ベース + 怪しいリクエストのみ block」
|
|
16
|
+
# - waf.rate_limits:
|
|
17
|
+
# - 全ルールの先頭 (priority 0..r-1) で評価される rate-based rule。超過していない
|
|
18
|
+
# 送信元には何もせず次へ進む。先頭に置くのは、allow_rules の秘密ヘッダ持ちや
|
|
19
|
+
# allowlist 済み IP からの異常な頻度のアクセスも止めるため (KN1870)
|
|
20
|
+
# - block は CustomResponse 429 (allowlist 外の 403 と区別するため)
|
|
21
|
+
# - 集計キーは送信元 IP 固定 (CloudFront 配下では viewer IP)
|
|
16
22
|
# - waf.allow_rules:
|
|
17
|
-
# - IPSet / managed rules より先 (priority
|
|
23
|
+
# - IPSet / managed rules より先 (priority r..r+n-1) に評価される allow
|
|
18
24
|
# - header rule の SearchString は managed secret 値の焼き込み
|
|
19
25
|
# (enable_origin_verify の OriginCustomHeaders と同じ exposure class)
|
|
20
26
|
|
|
@@ -50,9 +56,42 @@ Resources:
|
|
|
50
56
|
CloudWatchMetricsEnabled: true
|
|
51
57
|
MetricName: "{{ slug }}-waf"
|
|
52
58
|
Rules:
|
|
59
|
+
# {% for rule in waf.rate_limits %}
|
|
60
|
+
- Name: "ratelimit-{{ loop.index0 }}"
|
|
61
|
+
Priority: {{ loop.index0 }}
|
|
62
|
+
Action:
|
|
63
|
+
# {% if rule.action == "block" %}
|
|
64
|
+
Block:
|
|
65
|
+
CustomResponse:
|
|
66
|
+
ResponseCode: 429
|
|
67
|
+
# {% else %}
|
|
68
|
+
Count: {}
|
|
69
|
+
# {% endif %}
|
|
70
|
+
Statement:
|
|
71
|
+
RateBasedStatement:
|
|
72
|
+
Limit: {{ rule.limit }}
|
|
73
|
+
EvaluationWindowSec: {{ rule.window_sec }}
|
|
74
|
+
AggregateKeyType: IP
|
|
75
|
+
# {% if rule.path %}
|
|
76
|
+
ScopeDownStatement:
|
|
77
|
+
ByteMatchStatement:
|
|
78
|
+
FieldToMatch:
|
|
79
|
+
UriPath: {}
|
|
80
|
+
PositionalConstraint: "{{ rule.path_positional_constraint }}"
|
|
81
|
+
SearchString: "{{ rule.path_search_string }}"
|
|
82
|
+
TextTransformations:
|
|
83
|
+
- Priority: 0
|
|
84
|
+
Type: URL_DECODE
|
|
85
|
+
# {% endif %}
|
|
86
|
+
VisibilityConfig:
|
|
87
|
+
SampledRequestsEnabled: true
|
|
88
|
+
CloudWatchMetricsEnabled: true
|
|
89
|
+
MetricName: "{{ slug }}-waf-ratelimit-{{ loop.index0 }}"
|
|
90
|
+
# {% endfor %}
|
|
91
|
+
# {% set rate_offset = waf.rate_limits|length %}
|
|
53
92
|
# {% for rule in waf.allow_rules %}
|
|
54
93
|
- Name: "allow-{{ loop.index0 }}"
|
|
55
|
-
Priority: {{ loop.index0 }}
|
|
94
|
+
Priority: {{ rate_offset + loop.index0 }}
|
|
56
95
|
Action:
|
|
57
96
|
Allow: {}
|
|
58
97
|
Statement:
|
|
@@ -66,7 +105,7 @@ Resources:
|
|
|
66
105
|
SearchString: "{{ rule.path_search_string }}"
|
|
67
106
|
TextTransformations:
|
|
68
107
|
- Priority: 0
|
|
69
|
-
Type:
|
|
108
|
+
Type: URL_DECODE
|
|
70
109
|
- ByteMatchStatement:
|
|
71
110
|
FieldToMatch:
|
|
72
111
|
SingleHeader:
|
|
@@ -84,7 +123,7 @@ Resources:
|
|
|
84
123
|
SearchString: "{{ rule.path_search_string }}"
|
|
85
124
|
TextTransformations:
|
|
86
125
|
- Priority: 0
|
|
87
|
-
Type:
|
|
126
|
+
Type: URL_DECODE
|
|
88
127
|
# {% else %}
|
|
89
128
|
ByteMatchStatement:
|
|
90
129
|
FieldToMatch:
|
|
@@ -101,7 +140,7 @@ Resources:
|
|
|
101
140
|
CloudWatchMetricsEnabled: true
|
|
102
141
|
MetricName: "{{ slug }}-waf-allow-{{ loop.index0 }}"
|
|
103
142
|
# {% endfor %}
|
|
104
|
-
# {% set priority_offset = waf.allow_rules|length %}
|
|
143
|
+
# {% set priority_offset = rate_offset + waf.allow_rules|length %}
|
|
105
144
|
# {% if waf.enable_ip_set %}
|
|
106
145
|
- Name: ip-allow
|
|
107
146
|
Priority: {{ priority_offset }}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "magic-pocket-cli"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.46.0"
|
|
4
4
|
description = "CLI and deploy tools for magic-pocket."
|
|
5
5
|
requires-python = ">=3.10"
|
|
6
6
|
dependencies = [
|
|
7
7
|
# lib は同 minor に束縛する。0.x では minor が breaking の単位のため、
|
|
8
8
|
# 上限なしだと cli を pin した消費者にも lib の破壊的変更が届いてしまう
|
|
9
|
-
"magic-pocket>=0.
|
|
9
|
+
"magic-pocket>=0.46.0,<0.47",
|
|
10
10
|
"click>=8.1.7",
|
|
11
11
|
"jinja2>=3.1.3",
|
|
12
12
|
"python-on-whales>=0.68.0",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/cli/cleanup_deprecated_cli.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/backup_common.py
RENAMED
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/backup_legacy.py
RENAMED
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/builders/__init__.py
RENAMED
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/builders/codebuild.py
RENAMED
|
File without changes
|
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/builders/depot.py
RENAMED
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/builders/docker.py
RENAMED
|
File without changes
|
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/cloudformation.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/lambdahandler.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/aws/stack_backed.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/resources/inbound_template.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/cloudformation/vpc.yaml
RENAMED
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/init/django-dotenv.env
RENAMED
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/init/django-settings.py
RENAMED
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/init/pocket.Dockerfile
RENAMED
|
File without changes
|
{magic_pocket_cli-0.44.0 → magic_pocket_cli-0.46.0}/pocket_cli/templates/init/pocket_simple.toml
RENAMED
|
File without changes
|