Skip to content

Commit e71923f

Browse files
committed
Fix sequelize tests to work in secure mode
1 parent 559838b commit e71923f

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

node/sequelize/models/index.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,53 @@
22

33
var fs = require('fs');
44
var Sequelize = require('sequelize-cockroachdb');
5-
var sequelize = new Sequelize(process.env.ADDR, {
6-
dialectOptions: {cockroachdbTelemetryDisabled : true}
7-
});
5+
6+
if (process.env.ADDR === undefined) {
7+
throw new Error("ADDR (database URL) must be specified.");
8+
}
9+
10+
var url = new URL(process.env.ADDR);
11+
var opts = {
12+
dialect: "postgres",
13+
username: url.username,
14+
host: url.hostname,
15+
port: url.port,
16+
database: url.pathname.substring(1), // ignore leading '/'
17+
dialectOptions: {
18+
cockroachdbTelemetryDisabled: true,
19+
ssl: {},
20+
},
21+
logging: false,
22+
};
23+
24+
if (url.password) {
25+
opts.password = url.password
26+
}
27+
if (url.searchParams.has("options")) {
28+
var pgOpts = url.searchParams.get("options")
29+
var cluster = pgOpts.match(/cluster=([^\s]+)/)[1]
30+
opts.database = `${cluster}.${opts.database}`
31+
}
32+
if (url.searchParams.get("sslmode") === "disable") {
33+
delete opts.dialectOptions.ssl
34+
} else {
35+
if (url.searchParams.has("sslrootcert")) {
36+
opts.dialectOptions.ssl.ca = fs.readFileSync(url.searchParams.get("sslrootcert").toString())
37+
}
38+
if (url.searchParams.has("sslcert")) {
39+
opts.dialectOptions.ssl.cert = fs.readFileSync(url.searchParams.get("sslcert").toString())
40+
}
41+
if (url.searchParams.has("sslkey")) {
42+
opts.dialectOptions.ssl.key = fs.readFileSync(url.searchParams.get("sslkey").toString())
43+
}
44+
}
45+
var sequelize = new Sequelize(opts);
846
var DataTypes = Sequelize.DataTypes;
947

1048
if (!Sequelize.supportsCockroachDB) {
1149
throw new Error("CockroachDB dialect for Sequelize not installed");
1250
}
1351

14-
if (process.env.ADDR === undefined) {
15-
throw new Error("ADDR (database URL) must be specified.");
16-
}
17-
1852
module.exports.Customer = sequelize.define('customer', {
1953
name: DataTypes.STRING
2054
}, {

testing/main_test.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -439,15 +439,7 @@ func TestHibernate(t *testing.T) {
439439
}
440440

441441
func TestSequelize(t *testing.T) {
442-
testORMForAuthModesExcept(t,
443-
testInfo{language: "node", orm: "sequelize"},
444-
map[authMode]string{
445-
// Requires bespoke code to actually use SSL, see:
446-
// https://github.com/sequelize/sequelize/issues/10015
447-
//authClientCert: "needs custom SSL setup",
448-
//authPassword: "needs custom SSL setup",
449-
},
450-
)
442+
testORMForAuthModesExcept(t, testInfo{language: "node", orm: "sequelize"}, nothingSkipped())
451443
}
452444

453445
func TestSQLAlchemy(t *testing.T) {
@@ -469,4 +461,3 @@ func TestDjango(t *testing.T) {
469461
func TestActiveRecord(t *testing.T) {
470462
testORMForAuthModesExcept(t, testInfo{language: "ruby", orm: "activerecord"}, nothingSkipped())
471463
}
472-

0 commit comments

Comments
 (0)