From a1a86f2dcff97172d91821068809abad88b0e9a8 Mon Sep 17 00:00:00 2001 From: Paul English Date: Tue, 19 Jul 2022 09:53:30 +0100 Subject: [PATCH 1/6] feat: got https partially working --- lib/cypress-plugin/index.js | 2 +- lib/modules/createConfig.js | 11 ++++++++--- lib/schemas/config-validation.json | 4 ++++ lib/templates/docker-compose.ejs | 2 +- lib/templates/dockerfile.ejs | 6 ++++++ 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/cypress-plugin/index.js b/lib/cypress-plugin/index.js index 7683ef8..ed6a85c 100644 --- a/lib/cypress-plugin/index.js +++ b/lib/cypress-plugin/index.js @@ -36,7 +36,7 @@ module.exports = async (_on, config) => { watchDir(config.integrationFolder, duplicateTests); } - const baseUrl = packageConfig.port ? `http://localhost:${packageConfig.port}` : 'http://localhost'; + const baseUrl = packageConfig.port ? `http://localhost:${packageConfig.port}` : 'https://localhost:4433'; return { ...config, diff --git a/lib/modules/createConfig.js b/lib/modules/createConfig.js index 186ce30..de85433 100644 --- a/lib/modules/createConfig.js +++ b/lib/modules/createConfig.js @@ -49,7 +49,7 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => { } if (!config.url) { - config.url = config.port ? `http://localhost:${config.port}` : 'http://localhost'; + config.url = config.port ? `http://localhost:${config.port}` : 'https://localhost:4433'; } let volumes = []; @@ -108,6 +108,11 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => { `${packageDir}/plugin:/var/www/html/wp-content/plugins/wp-cypress`, ); + volumes.push( + `${CWD}/${config.sslCertificatesPath[0]}:/var/www/html/localhost.pem`, + `${CWD}/${config.sslCertificatesPath[1]}:/var/www/html/localhost.key`, + ); + if (config.configFile) { volumes.push(`${path.resolve(config.configFile)}:/var/www/html/wp-cypress-config.php`); } @@ -125,8 +130,8 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => { `${packageDir}/lib/templates/docker-compose.ejs`, `${packageDir}/docker-compose.yml`, { - port: config.port || 80, - dbPort: config.dbPort || 3306, + port: config.port || 4433, + dbPort: config.dbPort || 6033, volumes: hasVolumeSupport ? volumes : false, }, ); diff --git a/lib/schemas/config-validation.json b/lib/schemas/config-validation.json index 5425c67..fad1b1b 100644 --- a/lib/schemas/config-validation.json +++ b/lib/schemas/config-validation.json @@ -32,6 +32,10 @@ "type": "number", "errorMessage": "wp.port is not a number" }, + "sslCertificatesPath": { + "type": "array", + "error": "sslCertificatesPath is not a string" + }, "phpVersion": { "type": "number", "errorMessage": "wp.phpVersion is not a number" diff --git a/lib/templates/docker-compose.ejs b/lib/templates/docker-compose.ejs index 80eb3b5..bde9190 100644 --- a/lib/templates/docker-compose.ejs +++ b/lib/templates/docker-compose.ejs @@ -5,7 +5,7 @@ services: - db build: . ports: - - <%= port %>:80 + - <%= port %>:443 <% if (volumes) { %>volumes: <% volumes.forEach((volume) => { %> - <%= volume %> <% }); } %> db: diff --git a/lib/templates/dockerfile.ejs b/lib/templates/dockerfile.ejs index 318d359..315599a 100644 --- a/lib/templates/dockerfile.ejs +++ b/lib/templates/dockerfile.ejs @@ -5,6 +5,11 @@ RUN apt-get update && apt-get install -y wget libpng-dev libjpeg-dev gnupg defau && docker-php-ext-install gd mysqli RUN a2enmod rewrite +RUN a2enmod ssl + +RUN sed -i 's/SSLCertificateFile.*snakeoil\.pem/SSLCertificateFile \/var\/www\/html\/localhost.pem/' /etc/apache2/sites-available/default-ssl.conf +RUN sed -i 's/SSLCertificateKeyFile.*snakeoil\.key/SSLCertificateKeyFile \/var\/www\/html\/localhost.key/' /etc/apache2/sites-available/default-ssl.conf +RUN a2ensite default-ssl COPY update.sh /var/www/html @@ -36,5 +41,6 @@ RUN curl https://github.com/Automattic/vip-go-mu-plugins-built/archive/master.zi <% } %> EXPOSE 80 +EXPOSE 443 CMD ["apache2-foreground"] From beb389e72e15416a4deea9457adae9381aa3fe24 Mon Sep 17 00:00:00 2001 From: Paul English Date: Wed, 20 Jul 2022 10:44:38 +0100 Subject: [PATCH 2/6] feat: made the ssl settings configurable which can be changed using cypress.json --- lib/cypress-plugin/index.js | 5 ++--- lib/modules/createConfig.js | 20 +++++++++++++------- lib/templates/docker-compose.ejs | 3 ++- lib/templates/dockerfile.ejs | 10 ++++++---- lib/utils/getBaseUrl.js | 8 ++++++++ 5 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 lib/utils/getBaseUrl.js diff --git a/lib/cypress-plugin/index.js b/lib/cypress-plugin/index.js index c74a0b6..0d0d581 100644 --- a/lib/cypress-plugin/index.js +++ b/lib/cypress-plugin/index.js @@ -2,6 +2,7 @@ const fs = require('fs-extra'); const path = require('path'); const shell = require('shelljs'); +const getBaseUrl = require('../utils/getBaseUrl'); const watchDir = require('../utils/watch-dir'); const { copyDirSync } = require('../utils/copy'); @@ -36,9 +37,7 @@ module.exports = async (_on, config) => { watchDir(config.integrationFolder, duplicateTests); } - const baseUrl = packageConfig.port - ? `http://localhost:${packageConfig.port}` - : 'https://localhost:4433'; + const baseUrl = getBaseUrl(packageConfig); return { ...config, diff --git a/lib/modules/createConfig.js b/lib/modules/createConfig.js index a7bd580..ce4136c 100644 --- a/lib/modules/createConfig.js +++ b/lib/modules/createConfig.js @@ -4,6 +4,8 @@ const glob = require('glob'); const { get, defaults } = require('lodash'); const Ajv = require('ajv'); +const getBaseUrl = require('../utils/getBaseUrl'); + const createDefaultSeeds = require('./createDefaultSeeds'); const getThemeAndPluginData = require('./getThemeAndPluginData'); const renderTemplate = require('./renderTemplate'); @@ -49,7 +51,7 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => { } if (!config.url) { - config.url = config.port ? `http://localhost:${config.port}` : 'https://localhost:4433'; + config.url = getBaseUrl(config); } let volumes = []; @@ -104,10 +106,12 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => { `${packageDir}/plugin:/var/www/html/wp-content/plugins/wp-cypress`, ); - volumes.push( - `${CWD}/${config.sslCertificatesPath[0]}:/var/www/html/localhost.pem`, - `${CWD}/${config.sslCertificatesPath[1]}:/var/www/html/localhost.key`, - ); + if (config.sslCertificate) { + volumes.push( + `${CWD}/${config.sslCertificate.cert}:/var/www/html/localhost.pem`, + `${CWD}/${config.sslCertificate.key}:/var/www/html/localhost.key`, + ); + } if (config.configFile) { volumes.push(`${path.resolve(config.configFile)}:/var/www/html/wp-cypress-config.php`); @@ -126,8 +130,9 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => { `${packageDir}/lib/templates/docker-compose.ejs`, `${packageDir}/docker-compose.yml`, { - port: config.port || 4433, - dbPort: config.dbPort || 6033, + port: config.port || 80, + dbPort: config.dbPort || 3306, + sslPort: config.sslPort || 4433, volumes: hasVolumeSupport ? volumes : false, }, ); @@ -143,6 +148,7 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => { await renderTemplate(`${packageDir}/lib/templates/dockerfile.ejs`, `${packageDir}/Dockerfile`, { isWpContent: config.wpContent, versions: validVersions, + sslCertificate: config.sslCertificate, vip: config.muPlugins ? config.muPlugins.vip : false, phpVersion: config.phpVersion || 7.4, phpMemoryLimit: config.phpMemoryLimit || '128M', diff --git a/lib/templates/docker-compose.ejs b/lib/templates/docker-compose.ejs index bde9190..5c6915a 100644 --- a/lib/templates/docker-compose.ejs +++ b/lib/templates/docker-compose.ejs @@ -5,7 +5,8 @@ services: - db build: . ports: - - <%= port %>:443 + - <%= port %>:80 + - <%= sslPort %>:443 <% if (volumes) { %>volumes: <% volumes.forEach((volume) => { %> - <%= volume %> <% }); } %> db: diff --git a/lib/templates/dockerfile.ejs b/lib/templates/dockerfile.ejs index 7a682ec..a5013ed 100644 --- a/lib/templates/dockerfile.ejs +++ b/lib/templates/dockerfile.ejs @@ -5,11 +5,13 @@ RUN apt-get update && apt-get install -y wget libpng-dev libjpeg-dev gnupg defau && docker-php-ext-install gd mysqli RUN a2enmod rewrite -RUN a2enmod ssl -RUN sed -i 's/SSLCertificateFile.*snakeoil\.pem/SSLCertificateFile \/var\/www\/html\/localhost.pem/' /etc/apache2/sites-available/default-ssl.conf -RUN sed -i 's/SSLCertificateKeyFile.*snakeoil\.key/SSLCertificateKeyFile \/var\/www\/html\/localhost.key/' /etc/apache2/sites-available/default-ssl.conf -RUN a2ensite default-ssl +<% if (sslCertificate) { %> + RUN a2enmod ssl + RUN sed -i 's/SSLCertificateFile.*snakeoil\.pem/SSLCertificateFile \/var\/www\/html\/localhost.pem/' /etc/apache2/sites-available/default-ssl.conf + RUN sed -i 's/SSLCertificateKeyFile.*snakeoil\.key/SSLCertificateKeyFile \/var\/www\/html\/localhost.key/' /etc/apache2/sites-available/default-ssl.conf + RUN a2ensite default-ssl +<% } %> COPY update.sh /var/www/html diff --git a/lib/utils/getBaseUrl.js b/lib/utils/getBaseUrl.js new file mode 100644 index 0000000..ac7799a --- /dev/null +++ b/lib/utils/getBaseUrl.js @@ -0,0 +1,8 @@ +const getBaseUrl = (config) => { + if (config.sslCertificate) { + return config.sslPort ? `https://localhost:${config.sslPort}` : 'https://localhost:4433'; + } + return config.port ? `http://localhost:${config.port}` : 'https://localhost'; +}; + +module.exports = getBaseUrl; From 6fd4f924facfb2cdf18784cd7617837ec74f62fc Mon Sep 17 00:00:00 2001 From: Paul English Date: Wed, 20 Jul 2022 11:12:43 +0100 Subject: [PATCH 3/6] chore: doc blocked the getBaseUrl utils function to explain what it does --- lib/utils/getBaseUrl.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/utils/getBaseUrl.js b/lib/utils/getBaseUrl.js index ac7799a..a682986 100644 --- a/lib/utils/getBaseUrl.js +++ b/lib/utils/getBaseUrl.js @@ -1,3 +1,9 @@ +/** + * Uses the Cypress config option to return the correct base URL. + * + * @param {object} config the Cypress config object + * @returns the base URL + */ const getBaseUrl = (config) => { if (config.sslCertificate) { return config.sslPort ? `https://localhost:${config.sslPort}` : 'https://localhost:4433'; From 7bb67d32a50e4e0682c7fbe52bc54cd3c6490dd2 Mon Sep 17 00:00:00 2001 From: Paul English Date: Wed, 20 Jul 2022 11:45:45 +0100 Subject: [PATCH 4/6] fix: fixed a bug where https was still being used when no sllCertificate and no port was provided --- lib/utils/getBaseUrl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/getBaseUrl.js b/lib/utils/getBaseUrl.js index a682986..5b12659 100644 --- a/lib/utils/getBaseUrl.js +++ b/lib/utils/getBaseUrl.js @@ -8,7 +8,7 @@ const getBaseUrl = (config) => { if (config.sslCertificate) { return config.sslPort ? `https://localhost:${config.sslPort}` : 'https://localhost:4433'; } - return config.port ? `http://localhost:${config.port}` : 'https://localhost'; + return config.port ? `http://localhost:${config.port}` : 'http://localhost'; }; module.exports = getBaseUrl; From 698fced2668267203032bb746c8469fba320c5bb Mon Sep 17 00:00:00 2001 From: Paul English Date: Wed, 20 Jul 2022 11:49:40 +0100 Subject: [PATCH 5/6] test: added tests for getBaseUrl.js --- lib/utils/__tests__/getBaseUrl.js | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/utils/__tests__/getBaseUrl.js diff --git a/lib/utils/__tests__/getBaseUrl.js b/lib/utils/__tests__/getBaseUrl.js new file mode 100644 index 0000000..d3a2770 --- /dev/null +++ b/lib/utils/__tests__/getBaseUrl.js @@ -0,0 +1,42 @@ +const getBaseUrl = require('../getBaseUrl'); + +describe('getBaseUrl', () => { + test('should be a function.', () => { + expect(typeof getBaseUrl).toBe('function'); + }); + test('should use default port if none provided', () => { + const config = {}; + expect(getBaseUrl(config)).toBe('http://localhost'); + }); + test('should use provided port number', () => { + const port = 8080; + const config = { + port, + }; + expect(getBaseUrl(config)).toBe(`http://localhost:${port}`); + }); + test('should use default SSL port if sslCertificate is provided but sslPort is not provided', () => { + const port = 8080; + const config = { + sslCertificate: { + cert: './localhost.pem', + key: './localhost-key.pem', + }, + port, + }; + expect(getBaseUrl(config)).toBe('https://localhost:4433'); + }); + test('should use provided SSL port number if sslCertificate and sslPort is provided', () => { + const port = 8080; + const sslPort = 3433; + const config = { + sslCertificate: { + cert: './localhost.pem', + key: './localhost-key.pem', + }, + port, + sslPort, + }; + expect(getBaseUrl(config)).toBe(`https://localhost:${sslPort}`); + }); +}); From fa511d7108f35b6fce5797938e8f95dc1e369ffb Mon Sep 17 00:00:00 2001 From: Paul English Date: Thu, 21 Jul 2022 09:27:57 +0100 Subject: [PATCH 6/6] chore: added sslCertificates and sslPort to the config-validation file --- lib/schemas/config-validation.json | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/schemas/config-validation.json b/lib/schemas/config-validation.json index fac682c..db3964a 100644 --- a/lib/schemas/config-validation.json +++ b/lib/schemas/config-validation.json @@ -32,9 +32,22 @@ "type": "number", "errorMessage": "wp.port is not a number" }, - "sslCertificatesPath": { - "type": "array", - "error": "sslCertificatesPath is not a string" + "sslCertificate": { + "type": "object", + "properties": { + "cert": { + "type": "string", + "errorMessage": "wp.sslCertificate.cert is not a string" + }, + "key": { + "type": "string", + "errorMessage": "wp.sslCertificate.key is not a string" + } + } + }, + "sslPort": { + "type": "number", + "errorMessage": "wp.sslPort is not a number" }, "phpVersion": { "type": "number",