apx-aws-lambda 0.3.4__tar.gz → 0.3.6__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.
Files changed (32) hide show
  1. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/PKG-INFO +3 -3
  2. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/README.md +2 -2
  3. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/lambda_.py +10 -9
  4. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/proxy.py +40 -8
  5. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda.egg-info/PKG-INFO +3 -3
  6. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/pyproject.toml +1 -1
  7. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/tests/test_proxy.py +91 -7
  8. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/tests/test_targets.py +2 -0
  9. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/LICENSE +0 -0
  10. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/__init__.py +0 -0
  11. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/cli.py +0 -0
  12. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/overlays/cloud/aws/lambda/cookiecutter.json +0 -0
  13. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/overlays/cloud/aws/lambda/hooks/post_gen_project.py +0 -0
  14. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/overlays/cloud/aws/lambda/{{cookiecutter.project_slug}}/.github/workflows/deploy.yml +0 -0
  15. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/overlays/cloud/aws/lambda/{{cookiecutter.project_slug}}/DEPLOY.md +0 -0
  16. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/overlays/cloud/aws/lambda/{{cookiecutter.project_slug}}/_lang/python/Makefile +0 -0
  17. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/overlays/cloud/aws/lambda/{{cookiecutter.project_slug}}/_lang/python/lambda_handler.py +0 -0
  18. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/overlays/cloud/aws/lambda/{{cookiecutter.project_slug}}/_lang/python/tools/lambda_requirements.py +0 -0
  19. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/overlays/cloud/aws/lambda/{{cookiecutter.project_slug}}/requirements/policy.json +0 -0
  20. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/overlays/cloud/aws/lambda/{{cookiecutter.project_slug}}/requirements/trust.json +0 -0
  21. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/overlays/cloud/aws/lambda/{{cookiecutter.project_slug}}/samconfig.toml +0 -0
  22. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/overlays/cloud/aws/lambda/{{cookiecutter.project_slug}}/template.yaml +0 -0
  23. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/overlays/index.json +0 -0
  24. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/plugin.py +0 -0
  25. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/shell.py +0 -0
  26. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda/tools.py +0 -0
  27. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda.egg-info/SOURCES.txt +0 -0
  28. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda.egg-info/dependency_links.txt +0 -0
  29. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda.egg-info/entry_points.txt +0 -0
  30. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda.egg-info/requires.txt +0 -0
  31. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/apx_aws_lambda.egg-info/top_level.txt +0 -0
  32. {apx_aws_lambda-0.3.4 → apx_aws_lambda-0.3.6}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: apx-aws-lambda
3
- Version: 0.3.4
3
+ Version: 0.3.6
4
4
  Summary: AWS Lambda for Action Platform: the aws/lambda deploy target (SAM) with its overlay, and tools to read stacks and functions.
5
5
  Author-email: Action Platform <cloud@actionplatform.io>
6
6
  License-Expression: Apache-2.0
@@ -89,7 +89,7 @@ cd apx-aws-lambda
89
89
 
90
90
  The script runs `sam build` + `sam deploy`, prints the stack's `ProxyUrl` and calls `/health`.
91
91
 
92
- **2. Register the app** — from a machine logged in to the platform (`action-platform login`) as someone with `org.manage`:
92
+ **2. Register the app** — happens by itself on the first deploy from the platform by someone with `org.manage`: the proxy answers 404 for an app it does not know, the plugin creates it (the deploy token carries `org.manage`) and goes on. Anyone else gets a readable refusal until a manager deploys once. From a machine logged in to the platform (`action-platform login`) as someone with `org.manage`, or to grant other subjects:
93
93
 
94
94
  ```bash
95
95
  P=https://xxxx.lambda-url.us-east-1.on.aws
@@ -110,7 +110,7 @@ proxy_url = "https://xxxx.lambda-url.us-east-1.on.aws"
110
110
  app = "acme/shop/orders"
111
111
  ```
112
112
 
113
- `stack_name` in `samconfig.toml` must start with `ap-acme-shop-orders` — the deploy role reaches those stacks only, and the target refuses anything else before touching AWS.
113
+ Through the proxy the stack is named `ap-acme-shop-orders-dev` / `-prod` — the deploy role reaches that prefix only, so the target sets `--stack-name` itself and `stack_name` in `samconfig.toml` is ignored; the repository never has to know the organization.
114
114
 
115
115
  **4.** `action-platform deploy`.
116
116
 
@@ -69,7 +69,7 @@ cd apx-aws-lambda
69
69
 
70
70
  The script runs `sam build` + `sam deploy`, prints the stack's `ProxyUrl` and calls `/health`.
71
71
 
72
- **2. Register the app** — from a machine logged in to the platform (`action-platform login`) as someone with `org.manage`:
72
+ **2. Register the app** — happens by itself on the first deploy from the platform by someone with `org.manage`: the proxy answers 404 for an app it does not know, the plugin creates it (the deploy token carries `org.manage`) and goes on. Anyone else gets a readable refusal until a manager deploys once. From a machine logged in to the platform (`action-platform login`) as someone with `org.manage`, or to grant other subjects:
73
73
 
74
74
  ```bash
75
75
  P=https://xxxx.lambda-url.us-east-1.on.aws
@@ -90,7 +90,7 @@ proxy_url = "https://xxxx.lambda-url.us-east-1.on.aws"
90
90
  app = "acme/shop/orders"
91
91
  ```
92
92
 
93
- `stack_name` in `samconfig.toml` must start with `ap-acme-shop-orders` — the deploy role reaches those stacks only, and the target refuses anything else before touching AWS.
93
+ Through the proxy the stack is named `ap-acme-shop-orders-dev` / `-prod` — the deploy role reaches that prefix only, so the target sets `--stack-name` itself and `stack_name` in `samconfig.toml` is ignored; the repository never has to know the organization.
94
94
 
95
95
  **4.** `action-platform deploy`.
96
96
 
@@ -67,7 +67,7 @@ class LambdaTarget(DeployTarget):
67
67
  return None
68
68
 
69
69
  granted = (
70
- proxy.env(ctx)
70
+ proxy.env(ctx, self.region)
71
71
  if proxy is not None
72
72
  else assume_role(ctx, self.role_arn or "", self.session_name, self.region)
73
73
  )
@@ -89,6 +89,12 @@ class LambdaTarget(DeployTarget):
89
89
  return tomllib.loads(path.read_text())
90
90
 
91
91
  def _stack(self, ctx: Context) -> str:
92
+ """The stack: `<prefix>-<stage>` when the proxy granted a prefix — the repository's samconfig.toml never has to know the organization — else samconfig.toml's `stack_name`."""
93
+ prefix = (self.env(ctx) or {}).get("AP_STACK_PREFIX")
94
+
95
+ if prefix:
96
+ return f"{prefix}-{'prod' if ctx.stage == 'prod' else 'dev'}"
97
+
92
98
  params = (
93
99
  self._samconfig(ctx)
94
100
  .get(self._stage(ctx), {})
@@ -138,15 +144,8 @@ class LambdaTarget(DeployTarget):
138
144
  "template.yaml not found: apply the aws/lambda overlay first"
139
145
  )
140
146
 
141
- stack = self._stack(ctx)
147
+ self._stack(ctx)
142
148
  env = self.env(ctx)
143
- prefix = (env or {}).get("AP_STACK_PREFIX")
144
-
145
- if prefix and not stack.startswith(prefix):
146
- raise DeployError(
147
- f"stack_name {stack!r} must start with {prefix!r}: the proxy grants deploy on that prefix only"
148
- )
149
-
150
149
  shell.aws("sts", "get-caller-identity", region=self._region(ctx), env=env)
151
150
  shell.run(
152
151
  [*shell.require("sam", ""), "validate", "--lint"],
@@ -164,6 +163,8 @@ class LambdaTarget(DeployTarget):
164
163
  "deploy",
165
164
  "--no-confirm-changeset",
166
165
  "--no-fail-on-empty-changeset",
166
+ "--stack-name",
167
+ self._stack(ctx),
167
168
  ]
168
169
 
169
170
  if stage != "default":
@@ -13,6 +13,13 @@ from action_platform.remote.client import Remote
13
13
  MIN_PROXY = "0.1.0"
14
14
 
15
15
 
16
+ class ProxyRefused(DeployError):
17
+ def __init__(self, status: int, detail: str, method: str, path: str) -> None:
18
+ super().__init__(f"proxy {method} {path}: {status} {detail}")
19
+ self.status = status
20
+ self.detail = detail
21
+
22
+
16
23
  class ProxyClient:
17
24
  def __init__(self, url: str, app: str, timeout: float = 20.0) -> None:
18
25
  self.url = url.rstrip("/")
@@ -41,7 +48,10 @@ class ProxyClient:
41
48
  def health(self) -> dict:
42
49
  return self._call("GET", "/health")
43
50
 
44
- def credentials(self, ctx: Context, duration: int = 3600) -> dict:
51
+ def credentials(
52
+ self, ctx: Context, duration: int = 3600, region: str | None = None
53
+ ) -> dict:
54
+ """The app's credentials; an app the proxy does not know yet is registered first when the token may (`org.manage`) — the first deploy by an organization manager creates the roles."""
45
55
  health = self.health()
46
56
 
47
57
  if tuple_of(health.get("version", "0")) < tuple_of(MIN_PROXY):
@@ -49,20 +59,42 @@ class ProxyClient:
49
59
  f"proxy {self.url} is {health.get('version')}; this plugin needs {MIN_PROXY} or newer — update the proxy stack"
50
60
  )
51
61
 
62
+ token = self.token(ctx)
63
+
64
+ try:
65
+ return self._credentials(token, duration)
66
+ except ProxyRefused as e:
67
+ if e.status != 404:
68
+ raise
69
+
70
+ try:
71
+ self.create(region, token=token)
72
+ except ProxyRefused as e:
73
+ if e.status == 403:
74
+ raise DeployError(
75
+ f"the proxy does not know {self.app} yet, and this deploy may not register it ({e.detail}): "
76
+ "deploy once as an organization manager, or run `action-platform aws-lambda proxy create`"
77
+ ) from e
78
+
79
+ raise
80
+
81
+ return self._credentials(token, duration)
82
+
83
+ def _credentials(self, token: str, duration: int) -> dict:
52
84
  return self._call(
53
85
  "POST",
54
86
  f"/apps/{self.app}/credentials",
55
87
  {"duration": duration},
56
- token=self.token(ctx),
88
+ token=token,
57
89
  )
58
90
 
59
- def create(self, region: str | None = None) -> dict:
60
- """Admin: both roles for the app, granted to the app itself — the caller's token must carry `org.manage`."""
91
+ def create(self, region: str | None = None, token: str | None = None) -> dict:
92
+ """Admin: both roles for the app, granted to the app itself — the token must carry `org.manage`."""
61
93
  return self._call(
62
94
  "POST",
63
95
  f"/apps/{self.app}",
64
96
  {"region": region} if region else {},
65
- token=self.token(),
97
+ token=token or self.token(),
66
98
  )
67
99
 
68
100
  def show(self) -> dict:
@@ -79,8 +111,8 @@ class ProxyClient:
79
111
  token=self.token(),
80
112
  )
81
113
 
82
- def env(self, ctx: Context) -> dict[str, str]:
83
- data = self.credentials(ctx)
114
+ def env(self, ctx: Context, region: str | None = None) -> dict[str, str]:
115
+ data = self.credentials(ctx, region=region)
84
116
 
85
117
  return {
86
118
  "AWS_ACCESS_KEY_ID": data["access_key_id"],
@@ -119,7 +151,7 @@ class ProxyClient:
119
151
  except ValueError:
120
152
  pass
121
153
 
122
- raise DeployError(f"proxy {method} {path}: {e.code} {detail}") from e
154
+ raise ProxyRefused(e.code, str(detail), method, path) from e
123
155
  except urllib.error.URLError as e:
124
156
  raise DeployError(f"proxy {self.url} unreachable: {e.reason}") from e
125
157
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: apx-aws-lambda
3
- Version: 0.3.4
3
+ Version: 0.3.6
4
4
  Summary: AWS Lambda for Action Platform: the aws/lambda deploy target (SAM) with its overlay, and tools to read stacks and functions.
5
5
  Author-email: Action Platform <cloud@actionplatform.io>
6
6
  License-Expression: Apache-2.0
@@ -89,7 +89,7 @@ cd apx-aws-lambda
89
89
 
90
90
  The script runs `sam build` + `sam deploy`, prints the stack's `ProxyUrl` and calls `/health`.
91
91
 
92
- **2. Register the app** — from a machine logged in to the platform (`action-platform login`) as someone with `org.manage`:
92
+ **2. Register the app** — happens by itself on the first deploy from the platform by someone with `org.manage`: the proxy answers 404 for an app it does not know, the plugin creates it (the deploy token carries `org.manage`) and goes on. Anyone else gets a readable refusal until a manager deploys once. From a machine logged in to the platform (`action-platform login`) as someone with `org.manage`, or to grant other subjects:
93
93
 
94
94
  ```bash
95
95
  P=https://xxxx.lambda-url.us-east-1.on.aws
@@ -110,7 +110,7 @@ proxy_url = "https://xxxx.lambda-url.us-east-1.on.aws"
110
110
  app = "acme/shop/orders"
111
111
  ```
112
112
 
113
- `stack_name` in `samconfig.toml` must start with `ap-acme-shop-orders` — the deploy role reaches those stacks only, and the target refuses anything else before touching AWS.
113
+ Through the proxy the stack is named `ap-acme-shop-orders-dev` / `-prod` — the deploy role reaches that prefix only, so the target sets `--stack-name` itself and `stack_name` in `samconfig.toml` is ignored; the repository never has to know the organization.
114
114
 
115
115
  **4.** `action-platform deploy`.
116
116
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "apx-aws-lambda"
3
- version = "0.3.4"
3
+ version = "0.3.6"
4
4
  description = "AWS Lambda for Action Platform: the aws/lambda deploy target (SAM) with its overlay, and tools to read stacks and functions."
5
5
  readme = "README.md"
6
6
  license = "Apache-2.0"
@@ -99,6 +99,83 @@ class ProxyClientTest(unittest.TestCase):
99
99
 
100
100
  self.assertIn("403 nope", str(caught.exception))
101
101
 
102
+ def test_an_unknown_app_is_registered_on_the_way_when_the_token_may(self):
103
+ fake = FakeProxy({"POST /apps/acme/shop/orders": {"created": True}})
104
+ credentials = "POST /apps/acme/shop/orders/credentials"
105
+
106
+ def answer(request, timeout=None):
107
+ if (
108
+ f"{request.get_method()} {request.full_url.split('proxy.test', 1)[1]}"
109
+ == credentials
110
+ and credentials not in fake.answers
111
+ ):
112
+ fake.answers[credentials] = GRANTED
113
+ fake.requests.append(
114
+ (
115
+ request.get_method(),
116
+ "/apps/acme/shop/orders/credentials",
117
+ None,
118
+ None,
119
+ )
120
+ )
121
+ raise urllib.error.HTTPError(
122
+ request.full_url,
123
+ 404,
124
+ "no",
125
+ {},
126
+ io.BytesIO(b'{"error":"no app acme/shop/orders; create it first"}'),
127
+ )
128
+
129
+ return fake(request, timeout)
130
+
131
+ with mock.patch("urllib.request.urlopen", answer):
132
+ env = ProxyClient("https://proxy.test", "acme/shop/orders").env(
133
+ self.ctx(), "eu-west-1"
134
+ )
135
+
136
+ self.assertEqual(env["AWS_ACCESS_KEY_ID"], "AKIA")
137
+ self.assertEqual(
138
+ [(m, p, b) for m, p, b, _ in fake.requests[1:]],
139
+ [
140
+ ("POST", "/apps/acme/shop/orders/credentials", None),
141
+ ("POST", "/apps/acme/shop/orders", {"region": "eu-west-1"}),
142
+ ("POST", "/apps/acme/shop/orders/credentials", {"duration": 3600}),
143
+ ],
144
+ )
145
+
146
+ def test_an_unknown_app_the_token_may_not_register_is_a_readable_error(self):
147
+ fake = FakeProxy({})
148
+
149
+ def answer(request, timeout=None):
150
+ path = request.full_url.split("proxy.test", 1)[1]
151
+
152
+ if path.endswith("/credentials"):
153
+ raise urllib.error.HTTPError(
154
+ request.full_url,
155
+ 404,
156
+ "no",
157
+ {},
158
+ io.BytesIO(b'{"error":"no app; create it first"}'),
159
+ )
160
+
161
+ if path == "/apps/acme/shop/orders":
162
+ raise urllib.error.HTTPError(
163
+ request.full_url,
164
+ 403,
165
+ "no",
166
+ {},
167
+ io.BytesIO(b'{"error":"org.manage is required"}'),
168
+ )
169
+
170
+ return fake(request, timeout)
171
+
172
+ with mock.patch("urllib.request.urlopen", answer):
173
+ with self.assertRaises(DeployError) as caught:
174
+ ProxyClient("https://proxy.test", "acme/shop/orders").env(self.ctx())
175
+
176
+ self.assertIn("organization manager", str(caught.exception))
177
+ self.assertIn("org.manage is required", str(caught.exception))
178
+
102
179
  def test_an_old_proxy_is_refused(self):
103
180
  fake = FakeProxy({}, version="0.0.1")
104
181
 
@@ -130,24 +207,31 @@ class ProxyClientTest(unittest.TestCase):
130
207
  with self.assertRaises(DeployError):
131
208
  ProxyClient("https://proxy.test", "orders")
132
209
 
133
- def test_the_target_refuses_a_stack_outside_the_prefix(self):
210
+ def test_the_stack_follows_the_prefix_the_proxy_granted(self):
134
211
  (self.root / "samconfig.toml").write_text(SAMCONFIG.format(stack="shop-dev"))
135
212
  (self.root / "template.yaml").write_text("Resources: {}\n")
136
213
  fake = FakeProxy({"POST /apps/acme/shop/orders/credentials": GRANTED})
137
214
  target = LambdaTarget(proxy_url="https://proxy.test", app="acme/shop/orders")
215
+ calls: list[list[str]] = []
216
+
217
+ def run(args, cwd=None, env=None):
218
+ calls.append(args)
219
+
220
+ return "{}"
138
221
 
139
222
  with (
140
223
  mock.patch("urllib.request.urlopen", fake),
141
224
  mock.patch.multiple(
142
- shell,
143
- run=lambda *a, **k: "{}",
144
- require=lambda tool, hint: [f"/usr/bin/{tool}"],
225
+ shell, run=run, require=lambda tool, hint: [f"/usr/bin/{tool}"]
145
226
  ),
146
227
  ):
147
- with self.assertRaises(DeployError) as caught:
148
- target.preflight(self.ctx())
228
+ target.preflight(self.ctx())
229
+ target.deploy(self.ctx())
149
230
 
150
- self.assertIn("must start with 'ap-acme-shop-orders'", str(caught.exception))
231
+ deploy = next(c for c in calls if c[1] == "deploy")
232
+ self.assertEqual(
233
+ deploy[deploy.index("--stack-name") + 1], "ap-acme-shop-orders-dev"
234
+ )
151
235
 
152
236
  def test_the_target_deploys_with_the_granted_credentials(self):
153
237
  (self.root / "samconfig.toml").write_text(
@@ -79,6 +79,8 @@ class LambdaTargetTest(unittest.TestCase):
79
79
  "deploy",
80
80
  "--no-confirm-changeset",
81
81
  "--no-fail-on-empty-changeset",
82
+ "--stack-name",
83
+ "shop-prod",
82
84
  "--config-env",
83
85
  "prod",
84
86
  ],
File without changes
File without changes