Skip to content

Commit 5678ab4

Browse files
authored
Sanitize ssm paths (#874)
1 parent 8bcfd51 commit 5678ab4

File tree

12 files changed

+120
-20
lines changed

12 files changed

+120
-20
lines changed

.changeset/chatty-balloons-sing.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@aws-amplify/platform-core': minor
3+
---
4+
5+
Sanitize invalid characters when constructing SSM parameter paths.
6+
Uses the same convention that is used for sanitizing stack names.
7+
8+
**NOTE:** Any secrets created before this change will no longer be found.
9+
Recreate sandbox secrets using `npx amplify sandbox secret set` and recreate branch secrets in the Amplify console.

.changeset/early-maps-live.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@aws-amplify/backend-function': minor
3+
'@aws-amplify/backend-secret': minor
4+
'@aws-amplify/backend': minor
5+
---
6+
7+
Consume parameter resolution changes from @aws-amplify/platform-core

packages/backend-function/src/factory.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ void describe('AmplifyFunctionFactory', () => {
280280
TEST_VAR: 'testValue',
281281
TEST_SECRET: '<value will be resolved during runtime>',
282282
AMPLIFY_SECRET_PATHS: JSON.stringify({
283-
TEST_SECRET: '/amplify/testBackendId/testBranchName/secretValue',
283+
TEST_SECRET:
284+
'/amplify/testBackendId/testBranchName-branch-e482a1c36f/secretValue',
284285
}),
285286
},
286287
},

packages/backend-secret/src/ssm_secret.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const testSecretName = 'testSecretName';
1919
const testSecretValue = 'testSecretValue';
2020
const testSecretLastUpdated = new Date(1234567);
2121
const testSecretVersion = 20;
22-
const testBranchPath = `/amplify/${testBackendId}/${testBranchName}`;
22+
const testBranchPath = `/amplify/${testBackendId}/${testBranchName}-branch-e482a1c36f`;
2323
const testSharedPath = `/amplify/${shared}/${testBackendId}`;
2424
const testBranchSecretFullNamePath = `${testBranchPath}/${testSecretName}`;
2525
const testSharedSecretFullNamePath = `${testSharedPath}/${testSecretName}`;

packages/backend/src/engine/backend-secret/backend_secret_fetcher_factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class BackendSecretFetcherFactory {
5858
const customResourceProps: SecretResourceProps = {
5959
namespace: backendIdentifier.namespace,
6060
name: backendIdentifier.name,
61+
type: backendIdentifier.type,
6162
secretName: secretName,
6263
};
6364

packages/backend/src/engine/backend-secret/backend_secret_fetcher_provider_factory.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Runtime as LambdaRuntime } from 'aws-cdk-lib/aws-lambda';
77
import { Provider } from 'aws-cdk-lib/custom-resources';
88
import { fileURLToPath } from 'url';
99
import { BackendIdentifier } from '@aws-amplify/plugin-types';
10+
import { ParameterPathConversions } from '@aws-amplify/platform-core';
1011

1112
const filename = fileURLToPath(import.meta.url);
1213
const dirname = path.dirname(filename);
@@ -41,13 +42,19 @@ export class BackendSecretFetcherProviderFactory {
4142
handler: 'handler',
4243
});
4344

45+
const backendParameterPrefix =
46+
ParameterPathConversions.toParameterPrefix(backendIdentifier);
47+
const sharedParameterPrefix = ParameterPathConversions.toParameterPrefix(
48+
backendIdentifier.namespace
49+
);
50+
4451
secretLambda.grantPrincipal.addToPrincipalPolicy(
4552
new iam.PolicyStatement({
4653
effect: iam.Effect.ALLOW,
4754
actions: ['ssm:GetParameter'],
4855
resources: [
49-
`arn:aws:ssm:*:*:parameter/amplify/${backendIdentifier.namespace}/${backendIdentifier.name}/*`,
50-
`arn:aws:ssm:*:*:parameter/amplify/shared/${backendIdentifier.namespace}/*`,
56+
`arn:aws:ssm:*:*:parameter${backendParameterPrefix}/*`,
57+
`arn:aws:ssm:*:*:parameter${sharedParameterPrefix}/*`,
5158
],
5259
})
5360
);

packages/backend/src/engine/backend-secret/lambda/backend_secret_fetcher.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ const customResourceEventCommon = {
4242
PhysicalResourceId: 'physicalId',
4343
ResourceType: 'AWS::CloudFormation::CustomResource',
4444
ResourceProperties: {
45-
namespace: testBackendId,
46-
name: testBranchName,
45+
...testBackendIdentifier,
4746
secretName: testSecretName,
4847
ServiceToken: 'token',
4948
},

packages/backend/src/engine/backend-secret/lambda/backend_secret_fetcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const handleCreateUpdateEvent = async (
5656
{
5757
namespace: props.namespace,
5858
name: props.name,
59-
type: 'branch',
59+
type: props.type,
6060
},
6161
{
6262
name: props.secretName,
@@ -74,7 +74,7 @@ export const handleCreateUpdateEvent = async (
7474
}
7575
}
7676

77-
// if the secret is not available in branch path, retrieve it at app-level.
77+
// if the secret is not available in branch path, try retrieving it at the app-level.
7878
if (!secret) {
7979
try {
8080
const resp = await secretClient.getSecret(props.namespace, {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export type SecretResourceProps = {
2-
namespace: string;
3-
name: string;
1+
import { BackendIdentifier } from '@aws-amplify/plugin-types';
2+
3+
export type SecretResourceProps = Omit<BackendIdentifier, 'hash'> & {
44
secretName: string;
55
};

packages/platform-core/src/backend_identifier_conversions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import assert from 'node:assert';
44
import { BackendIdentifier } from '@aws-amplify/plugin-types';
55

66
void describe('toStackName', () => {
7-
void it('removes non-alphanumeric chars from namespace and instance', () => {
7+
void it('removes non-alphanumeric chars from namespace and name', () => {
88
const actual = BackendIdentifierConversions.toStackName({
99
namespace: 't-_e.s,@ t--T@,, !@#$%^&*(){}:":<>?/|\\[] H/I. .S',
1010
name: 't-_h.i,@ ng1-&&%2@@3- _',

0 commit comments

Comments
 (0)