Skip to content

Commit

Permalink
ssl configs for mysql (#532)
Browse files Browse the repository at this point in the history
* ssl configs for mysql

* docker file revert

* updated cert file

* updated cert file

* updated mysql version

* updated mysql version

* update docker file

* fix ssl certificate issue

* update docker file

* update docker file

* update docker file

* update docker file

* update docker file

* update docker file

* lint

* sh256 certficates

* sh256 certficates

* updated readme

Co-authored-by: alphmth <>
  • Loading branch information
jaytist authored Nov 3, 2022
1 parent a62aab0 commit 5c6ca18
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 5 deletions.
22 changes: 22 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ services:
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_DATABASE: keyv_test
MYSQL_USER: mysql
keyv_mysql_1:
image: "mysql/mysql-server:8.0.17"
command: [ "mysqld",
"--character-set-server=utf8mb4",
"--collation-server=utf8mb4_unicode_ci",
"--require_secure_transport=ON",
"--bind-address=0.0.0.0",
"--ssl-ca=/etc/certs/ca.pem",
"--ssl-cert=/etc/certs/server-cert.pem",
"--ssl-key=/etc/certs/server-key.pem",
"--default_authentication_plugin=mysql_native_password" ]
ports:
- "3307:3306"
volumes:
- type: bind
source: ./packages/mysql/test/certs
target: /etc/certs/
restart: always
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_DATABASE: keyv_test
MYSQL_ROOT_HOST: '%'
keyv_redis:
image: redis:latest
environment:
Expand Down
20 changes: 20 additions & 0 deletions packages/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ const keyv = new Keyv('mysql://user:pass@localhost:3306/dbname', {
});
```

## SSL

```
const fs = require('fs');
const path = require('path');
const KeyvMysql = require('@keyv/mysql');
const options = {
ssl: {
rejectUnauthorized: false,
ca: fs.readFileSync(path.join(__dirname, '/certs/ca.pem')).toString(),
key: fs.readFileSync(path.join(__dirname, '/certs/client-key.pem')).toString(),
cert: fs.readFileSync(path.join(__dirname, '/certs/client-cert.pem')).toString(),
},
};
const keyv = new KeyvMysql({uri, ...options});
```

**Note:** Some MySQL/MariaDB installations won't allow a key size longer than 767 bytes. If you get an error on table creation try reducing `keySize` to 191 or lower. [#5](https://github.com/jaredwray/keyv-sql/issues/5)

## License
Expand Down
10 changes: 9 additions & 1 deletion packages/mysql/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const EventEmitter = require('events');
const mysql = require('mysql2/promise');
const {pool, endPool} = require('./pool.js');

const keyvMysqlKeys = new Set(['uri', 'dialect', 'connect']);

class KeyvMysql extends EventEmitter {
constructor(options) {
super();
Expand All @@ -15,8 +17,14 @@ class KeyvMysql extends EventEmitter {
options = {dialect: 'mysql',
uri: 'mysql://localhost', ...options};

const mysqlOptions = Object.fromEntries(
Object.entries(options).filter(
([k]) => !keyvMysqlKeys.has(k),
),
);

options.connect = () => Promise.resolve()
.then(() => pool(options.uri))
.then(() => pool(options.uri, mysqlOptions))
.then(connection => sql => connection.execute(sql)
.then(data => data[0]));

Expand Down
11 changes: 7 additions & 4 deletions packages/mysql/src/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@ const mysql = require('mysql2');
let pool;
let globalUri;

const pools = uri => {
const pools = (uri, options = {}) => {
if (globalUri !== uri) {
pool = undefined;
globalUri = uri;
}

pool = pool || mysql.createPool(uri);
pool = pool || mysql.createPool({uri, ...options});
return pool.promise();
};

const endPool = () => {
pool.end();
if (pool) {
pool.end();
}

globalUri = undefined;
};

module.exports = {
pool: uri => pools(uri),
pool: (uri, options) => pools(uri, options),
endPool,
};
28 changes: 28 additions & 0 deletions packages/mysql/test/certs/ca.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN CERTIFICATE-----
MIIE1DCCArwCCQDV/5eP+QZlaTANBgkqhkiG9w0BAQsFADAsMQswCQYDVQQGEwJV
UzELMAkGA1UECAwCTnkxEDAOBgNVBAMMB2NhLWNlcnQwHhcNMj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icXcwvOPxXrDxv/glLfxe4TYVaN9JgL7tR9MNaKM4
eRtLX4C62DVI1IuMsAP4B3zXWF6BviIvZd8iAlpE5hWV/aoyZ5ZB62ifHdOpypb4
KFeMlS+OExGa9SObPKjmhP6H97du6Tbk+o80qd7YdG7f37KiCUmuL9x8oc1cC1/L
Vu72mQUWPjt2dy7Pp2mqEklrcznnMPQGFEhpthnsZkGkrqGL/FXn+xpzCmpkDTfV
p2yOSWDbMJ1PKzDYWjslD5dK4ktOZz9Ce41OThDqHUKRy7wWEpFlOuP6nwX089Od
C+RtTNsO+0zCNqElMqLOQ6dPf9RuwnvJ0kkgUmi25jKvKPtTH4KZk2mezCJlTw+R
RbLpGTRZGpok+QfEB9EhnhXyvN10SfLMDpdtqAzvQ7y+FOojCZRF9JGeU6C1YSM0
lAh35usiQrF2Pshvdfht4gSop2K4nJX87YaTlbhKVm1CaZGwp9mVWA==
-----END CERTIFICATE-----
23 changes: 23 additions & 0 deletions packages/mysql/test/certs/client-cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-----BEGIN CERTIFICATE-----
MIID0DCCAbgCAQEwDQYJKoZIhvcNAQELBQAwLDELMAkGA1UEBhMCVVMxCzAJBgNV
BAgMAk55MRAwDgYDVQQDDAdjYS1jZXJ0MB4XDTIyMTAzMTA4NDMxOVoXDTMyMDkw
ODA4NDMxOVowMDELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAk55MRQwEgYDVQQDDAtj
bGllbnQtY2VydDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMkOoZ4m
OYWDXmx7vlju0tLozQt70P5NtVfRnmkPkty+sB5BYrT0bf5A1ZxQDJgC5X6u5gqz
SdwJp7/4xMPJF63vzIlqmgfvxvHfEPZcVz1zkx/mLr4m+EsMGXQ7OCHtL96+kXvb
Qt53IxCpL62M8iWembdfF1qwoIfuH/RmoaF/SBa3HvOQlU/df5tUuWtjz3R9k9ni
X5sKRoem46HlBTTJsRJIyYv6x9H0Y3l/A7VzzdBB+qVEmaoCqFY73Oy/S1UZcHyI
0mDpXmKNpP4wB7HowePsCbQ8KbCxTyxWk5Fg+ssXI4QcEsx82hFT4J9MXJLNCaYo
/f3MdKJrlLxA7dkCAwEAATANBgkqhkiG9w0BAQsFAAOCAgEAxFqK9EST7tBXnRCW
KJ5HN90utXQKlptJGLwZtofSfGcTTLw8nDrce636Mdq20GpZUuB9Ri/lo146oYOX
Zkdot+dxdJatXc2MH5SSecH/P1d57vU7kQnESpQZ1gq3uHQxGUGD/NPvGQ4jGZSC
RZdDbmGHoCF1OjEF7ZsELTGGGiuGaIPvGGPlEn1hCnmGGXx1T4GT4p236F7zgFh7
QGKLYTbwRL4dXJqVyTq/x6tWEK35uQswRxNqaHAWmQ//T8h/+jCzyFcTOgm/gKkT
WXPtiVjDc/2RYCjkLxAwdcAglh2MtXXPfOP5ZfbYQ8t2Ok6DkSodpYHa1EMqCj/I
vDNcxA+3SV0No9zJW0MBgUHtV0WE7Fx8Cm3XioYg3iYunq0OrwwvNvL4Yl3JGsjH
fgZyBQWSe1RL86BttSae/lisM1308TiXEG01hp57bNzfQSHHJm9B/o2caNkPwAcV
YdyKY3+JLxZiUCSOfP9ChtO8EWTbBJtbMXUfKWQLA8zW5onegWXTjIPukr4vwzLU
6cXGFcWPKWX6PiU7WS7t3dGRvw1aHEuElIqkEuYLSsaXpLwDHlekzpFRqVWGiU3q
27eUZnxYi6CBUuGzmNlYmFymaen7ghnKlJJw3iVp6LK7zXQKU5CWie6hg3/zZjyC
L1/3njEtThz3d1AKsKfUvbUDw3o=
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions packages/mysql/test/certs/client-key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAyQ6hniY5hYNebHu+WO7S0ujNC3vQ/k21V9GeaQ+S3L6wHkFi
tPRt/kDVnFAMmALlfq7mCrNJ3Amnv/jEw8kXre/MiW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oxjygdK9Ggdrj3pomQSOGu4XeBp8MBHqymaFzGo+1+HG+HlJQ8CHeNQDOO8INdgG
TDgvQibQlFOAe+FZTpycuQKBgBvpiOIa8acovl6oLIv4XnXjclxHeJ/yyvTHKuKJ
ZGmkXTO97nqqJs4UMLfb6pJhsaTrFgjWIQMyo/5A4O8KZuOAl1DY9XLEwmzqopej
AJAFouCclDn5YbqSJoP2NnVjM/e+LzbKXEdFRecpgHDaVgfYLKywUaPOcIoCmT7e
qM+hAoGAX56H5QvDj6HcOjWhQ+/nH7bjvaoOylLwqWU760qLIF/8tt6BgkqxR8Rh
5wtVFu004CsUCHkMqTR4pOi/HqIHz7Hto9cwo2cl7AFkU8fO8mt2N7vLrjEeYgm4
Jb1MVCjeMk9I7JyeXRKPW67008jhWfHbz5ckjMxID3c/ROpP5bA=
-----END RSA PRIVATE KEY-----
23 changes: 23 additions & 0 deletions packages/mysql/test/certs/server-cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-----BEGIN CERTIFICATE-----
MIID0DCCAbgCAQEwDQYJKoZIhvcNAQELBQAwLDELMAkGA1UEBhMCVVMxCzAJBgNV
BAgMAk55MRAwDgYDVQQDDAdjYS1jZXJ0MB4XDTIyMTAzMTA4NDIzNVoXDTMyMDkw
ODA4NDIzNVowMDELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAk55MRQwEgYDVQQDDAtz
ZXJ2ZXItY2VydDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAPpAmWlK
g7jAGN2k82RWZf9NaGapC1WxSQ2TyWgHPZYNbadYxGYXfdtqTKqin8+kbKiWxU2p
fKuzD60JyIqxytiEDeIhyL+I+8dMqA+wJpYEGvGoDTOZcU6sm5/jCLmJVDR6W52/
i3eBRzADRvrBuCCe9dVCMh/DVRpGSIZsrZwePankLSOLJq5MyBUj+Z8ZuQaMrUbk
Dg9kLaCmhCAdMuOhLUm/0TkR56B98/8CKz4RVTi0KQq02BEaGUUwjaQlOgrPe8he
7Kl1dW8MyYbV/cuxzmD7o2wDOmSbfU6a7b22N+0Ko1FJIQEhqbGY2tGkrA40YW3L
qeenirJpCN9GRJkCAwEAATANBgkqhkiG9w0BAQsFAAOCAgEAC1xeV+Po8OOyoOjk
u7AmOnzZ1iP2QUfhHOyS04WqYewSR4hULfbyZSLiU/AvRtoBbd2RunWVBaJj1Nyt
HcaAjbVs/UMinDj5U9CEIVnRSzGWAVSFi725otrdaHarXnU+E+lc180i8YHMVe+a
/6wOz/jUmQdboOjgpU26eNhrJ/8NqwykZ2J97ovTRBT56ika88uEr3UeGcvJ5exk
yipxDreCatWP4pt6PzWASi1CzsRUUUrdXplc4ochGNtmk+faoLbMOwQTDEpR9mVm
vaNmOXiBzkNsB79t3/XFPPgmiuWuunFm4OjScOdB8pTyGWHWMfGzKXDM1XkbP2Ti
Kv3Y9fsW884xdv6K0qrpZ0VTY6RHrQxeFzEzScE0pq0wz+aipAwmlFiL4tU4XUxL
ZqcvoNW/XGez5mjn/26EjsoS99ASO+1fCl+LrRVTa603NFnDcNlGh62zupqSueql
Ngv126wYomb7HluBE38oEJqBzcHknKlYUOoQgnhQjOk/rNKre4yBSWmgAAvFuxPW
X5XIdjQg01vHq8xvyxrDKJu3OkwgF8+UTFCTRzjuqrrrBYnXLFmWvRJRZvJ/q5Kw
ySemKaojQECeS9xWD92nKp+gMBcnztH7uXQGZFP0v4zrW7JJ9uXNhFxTIOpNeRpC
+q04voBcpsDhDtTX74SSaHAQiJk=
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions packages/mysql/test/certs/server-key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA+kCZaUqDuMAY3aTzZFZl/01oZqkLVbFJDZPJaAc9lg1tp1jE
Zhd922pMqqKfz6RsqJbFTal8q7MPrQnIirHK2IQN4iHIv4j7x0yoD7AmlgQa8agN
M5lxTqybn+MIuYlUNHpbnb+Ld4FHM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2Y8rDaPH2f1/UcECgYEAho+2alZHn3UtkPLRXea9BUvDyPo/W35/nkwj
2VR/2b88Ue30kJNHElTRpqR05grurwtCdkejmAQypXi2R8RLU1XmT5OAf848hp1d
SkLyCsXWrvlTQPCuS/zEca/v8nmjmQpAktcaq4AEEJN7ayMZwCbu8qnX3TwQW8OL
B1v6G8ECgYAl9vCnHFOivH0xQI82uHvMqKX/TYK9VpKEY01stDOcDA4nvj+4N2oe
9WrP1s5wtmnUnCCv2dFHWFl3FEjCYy8aeGIzV6BJawjkpPsYdlRoetcJcKKepLyp
vJJnjFzXKMv9SZq58KxeXcKnyg/gxxeMRsmArZ2/6p/usoZn7GE8ZQ==
-----END RSA PRIVATE KEY-----
32 changes: 32 additions & 0 deletions packages/mysql/test/ssl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fs = require('fs');
const path = require('path');
const test = require('ava');
const KeyvMysql = require('this');
const {endPool} = require('../src/pool.js');

const options = {
ssl: {
rejectUnauthorized: false,
ca: fs.readFileSync(path.join(__dirname, '/certs/ca.pem')).toString(),
key: fs.readFileSync(path.join(__dirname, '/certs/client-key.pem')).toString(),
cert: fs.readFileSync(path.join(__dirname, '/certs/client-cert.pem')).toString(),
},
};

test.serial('throws if ssl is not used', async t => {
try {
const keyv = new KeyvMysql({uri: 'mysql://root@localhost:3307/keyv_test'});
await keyv.get('foo');
t.fail();
} catch {
t.pass();
} finally {
endPool();
}
});

test.serial('set with ssl ', async t => {
const keyv = new KeyvMysql({uri: 'mysql://root@localhost:3307/keyv_test', ...options});
await keyv.set('key', 'value');
t.is(await keyv.get('key'), 'value');
});

0 comments on commit 5c6ca18

Please sign in to comment.