Skip to content

Commit f5eeb67

Browse files
authored
feat: support custom SSL certificates in SQL data sources (#1696)
1 parent 90ac407 commit f5eeb67

File tree

7 files changed

+384
-245
lines changed

7 files changed

+384
-245
lines changed

.changeset/perfect-coins-tease.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@aws-amplify/backend-data': minor
3+
'@aws-amplify/schema-generator': minor
4+
---
5+
6+
support custom SSL certificates in SQL data sources

package-lock.json

Lines changed: 240 additions & 240 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"type": "module",
66
"scripts": {
7-
"build": "tsc --build packages/* && tsc --build scripts",
7+
"build": "tsc --build packages/* scripts",
88
"check:api": "npm run update:api && tsx scripts/check_api_extract.ts",
99
"check:dependencies": "tsx scripts/check_dependencies.ts",
1010
"check:package-lock": "tsx scripts/check_package_lock.ts",

packages/backend-data/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"dependencies": {
3030
"@aws-amplify/backend-output-storage": "^1.0.2",
3131
"@aws-amplify/backend-output-schemas": "^1.1.0",
32-
"@aws-amplify/data-construct": "^1.8.0",
32+
"@aws-amplify/data-construct": "^1.9.1",
3333
"@aws-amplify/plugin-types": "^1.0.1",
34-
"@aws-amplify/data-schema-types": "^1.0.0"
34+
"@aws-amplify/data-schema-types": "^1.1.1"
3535
}
3636
}

packages/backend-data/src/convert_schema.test.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,124 @@ void describe('convertSchemaToCDK', () => {
394394
}
395395
);
396396
});
397+
398+
void it('produces expected definition for MySQL schema with custom SSL cert', () => {
399+
const schema = configure({
400+
database: {
401+
engine: 'mysql',
402+
connectionUri: new TestBackendSecret('SQL_CONNECTION_STRING'),
403+
sslCert: new TestBackendSecret('CUSTOM_SSL_CERT'),
404+
},
405+
}).schema({
406+
post: a
407+
.model({
408+
id: a.integer().required(),
409+
title: a.string(),
410+
})
411+
.identifier(['id'])
412+
.authorization((allow) => allow.publicApiKey()),
413+
});
414+
415+
const modified = schema.addQueries({
416+
oddList: a
417+
.query()
418+
.handler(a.handler.inlineSql('SELECT * from post where id % 2 = 1;'))
419+
.returns(a.ref('post'))
420+
.authorization((allow) => allow.publicApiKey()),
421+
});
422+
423+
const convertedDefinition = convertSchemaToCDK(
424+
modified,
425+
secretResolver,
426+
stableBackendIdentifiers
427+
);
428+
429+
assert.equal(
430+
Object.values(convertedDefinition.dataSourceStrategies).length,
431+
1
432+
);
433+
assert.deepEqual(
434+
Object.values(convertedDefinition.dataSourceStrategies)[0],
435+
{
436+
customSqlStatements: {},
437+
/* eslint-disable spellcheck/spell-checker */
438+
dbConnectionConfig: {
439+
connectionUriSsmPath: [
440+
'/amplify/testBackendId/testBranchName-branch-e482a1c36f/SQL_CONNECTION_STRING',
441+
'/amplify/shared/testBackendId/SQL_CONNECTION_STRING',
442+
],
443+
sslCertConfig: {
444+
ssmPath: [
445+
'/amplify/testBackendId/testBranchName-branch-e482a1c36f/CUSTOM_SSL_CERT',
446+
'/amplify/shared/testBackendId/CUSTOM_SSL_CERT',
447+
],
448+
},
449+
},
450+
dbType: 'MYSQL',
451+
name: '00034dcf3444861c3ca5mysql',
452+
vpcConfiguration: undefined,
453+
/* eslint-enable spellcheck/spell-checker */
454+
}
455+
);
456+
});
457+
458+
void it('produces expected definition for Postgresql schema with custom SSL cert', () => {
459+
const schema = configure({
460+
database: {
461+
engine: 'postgresql',
462+
connectionUri: new TestBackendSecret('SQL_CONNECTION_STRING'),
463+
sslCert: new TestBackendSecret('CUSTOM_SSL_CERT'),
464+
},
465+
}).schema({
466+
post: a
467+
.model({
468+
id: a.integer().required(),
469+
title: a.string(),
470+
})
471+
.identifier(['id'])
472+
.authorization((allow) => allow.publicApiKey()),
473+
});
474+
475+
const modified = schema.addQueries({
476+
oddList: a
477+
.query()
478+
.handler(a.handler.inlineSql('SELECT * from post where id % 2 = 1;'))
479+
.returns(a.ref('post'))
480+
.authorization((allow) => allow.publicApiKey()),
481+
});
482+
483+
const convertedDefinition = convertSchemaToCDK(
484+
modified,
485+
secretResolver,
486+
stableBackendIdentifiers
487+
);
488+
489+
assert.equal(
490+
Object.values(convertedDefinition.dataSourceStrategies).length,
491+
1
492+
);
493+
assert.deepEqual(
494+
Object.values(convertedDefinition.dataSourceStrategies)[0],
495+
{
496+
customSqlStatements: {},
497+
/* eslint-disable spellcheck/spell-checker */
498+
dbConnectionConfig: {
499+
connectionUriSsmPath: [
500+
'/amplify/testBackendId/testBranchName-branch-e482a1c36f/SQL_CONNECTION_STRING',
501+
'/amplify/shared/testBackendId/SQL_CONNECTION_STRING',
502+
],
503+
sslCertConfig: {
504+
ssmPath: [
505+
'/amplify/testBackendId/testBranchName-branch-e482a1c36f/CUSTOM_SSL_CERT',
506+
'/amplify/shared/testBackendId/CUSTOM_SSL_CERT',
507+
],
508+
},
509+
},
510+
dbType: 'POSTGRES',
511+
name: '00034dcf3444861c3ca5postgresql',
512+
vpcConfiguration: undefined,
513+
/* eslint-enable spellcheck/spell-checker */
514+
}
515+
);
516+
});
397517
});

packages/backend-data/src/convert_schema.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
AmplifyDataDefinition,
99
type IAmplifyDataDefinition,
1010
type ModelDataSourceStrategy,
11+
type SslCertSsmPathConfig,
1112
type VpcConfig,
1213
} from '@aws-amplify/data-construct';
1314
import type { DataSchema, DataSchemaInput } from './types.js';
@@ -174,17 +175,29 @@ const convertDatabaseConfigurationToDataSourceStrategy = (
174175

175176
const { branchSecretPath, sharedSecretPath } =
176177
backendSecretResolver.resolvePath(configuration.connectionUri);
177-
return {
178+
179+
let sslCertConfig: SslCertSsmPathConfig | undefined;
180+
if (configuration.sslCert) {
181+
const { branchSecretPath, sharedSecretPath } =
182+
backendSecretResolver.resolvePath(configuration.sslCert);
183+
sslCertConfig = {
184+
ssmPath: [branchSecretPath, sharedSecretPath],
185+
};
186+
}
187+
const strategy: ModelDataSourceStrategy = {
178188
dbType,
179189
name:
180190
provisionStrategyName +
181191
(configuration.identifier ?? configuration.engine),
182192
dbConnectionConfig: {
183193
connectionUriSsmPath: [branchSecretPath, sharedSecretPath],
194+
...(sslCertConfig ? { sslCertConfig } : undefined),
184195
},
185196
vpcConfiguration,
186197
customSqlStatements,
187198
};
199+
200+
return strategy;
188201
};
189202

190203
/**

packages/schema-generator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"update:api": "api-extractor run --local"
1818
},
1919
"dependencies": {
20-
"@aws-amplify/graphql-schema-generator": "^0.9.0",
20+
"@aws-amplify/graphql-schema-generator": "^0.9.2",
2121
"@aws-amplify/platform-core": "^1.0.0"
2222
},
2323
"license": "Apache-2.0"

0 commit comments

Comments
 (0)