Procházet zdrojové kódy

feat(create): Support SSL enforced PostgreSQL databases (#2905)

HotTotem před 1 rokem
rodič
revize
65b4f3c510

+ 15 - 0
packages/create/src/gather-user-responses.ts

@@ -14,6 +14,7 @@ interface PromptAnswers {
     dbSchema?: string | symbol;
     dbUserName: string | symbol;
     dbPassword: string | symbol;
+    dbSSL?: boolean | symbol;
     superadminIdentifier: string | symbol;
     superadminPassword: string | symbol;
     populateProducts: boolean | symbol;
@@ -72,6 +73,19 @@ export async function gatherUserResponses(
               })
             : '';
     checkCancel(dbSchema);
+    const dbSSL =
+        dbType === 'postgres'
+            ? await select({
+                  message:
+                      'Use SSL to connect to the database? (only enable if you database provider supports SSL)',
+                  options: [
+                      { label: 'no', value: false },
+                      { label: 'yes', value: true },
+                  ],
+                  initialValue: false,
+              })
+            : false;
+    checkCancel(dbSSL);
     const dbUserName = hasConnection
         ? await text({
               message: "What's the database user name?",
@@ -113,6 +127,7 @@ export async function gatherUserResponses(
         dbSchema,
         dbUserName,
         dbPassword,
+        dbSSL,
         superadminIdentifier,
         superadminPassword,
         populateProducts,

+ 15 - 0
packages/create/src/helpers.ts

@@ -352,6 +352,7 @@ async function checkPostgresDbExists(options: any, root: string): Promise<true>
         port: options.port,
         database: options.database,
         schema: options.schema,
+        ssl: options.ssl,
     };
     const client = new Client(connectionOptions);
 
@@ -371,6 +372,8 @@ async function checkPostgresDbExists(options: any, root: string): Promise<true>
             throwDatabaseDoesNotExist(options.database);
         } else if (e.message === 'NO_SCHEMA') {
             throwDatabaseSchemaDoesNotExist(options.database, options.schema);
+        } else if (e.code === '28000') {
+            throwSSLConnectionError(e, options.ssl);
         }
         throwConnectionError(e);
         await client.end();
@@ -389,6 +392,18 @@ function throwConnectionError(err: any) {
     );
 }
 
+function throwSSLConnectionError(err: any, sslEnabled?: any) {
+    throw new Error(
+        'Could not connect to the database. ' +
+            (sslEnabled === undefined
+                ? 'Is your server requiring an SSL connection?'
+                : 'Are you sure your server supports SSL?') +
+            `Please check the connection settings in your Vendure config.\n[${
+                (err.message || err.toString()) as string
+            }]`,
+    );
+}
+
 function throwDatabaseDoesNotExist(name: string) {
     throw new Error(`Database "${name}" does not exist. Please create the database and then try again.`);
 }

+ 3 - 0
packages/create/templates/vendure-config.hbs

@@ -52,6 +52,9 @@ export const config: VendureConfig = {
         {{#if dbSchema}}
         schema: process.env.DB_SCHEMA,
         {{/if}}
+        {{#if dbSSL}}
+        ssl: true,
+        {{/if}}
         {{#if isSQLjs}}
         location: path.join(__dirname, 'vendure.sqlite'),
         autoSave: true,