From f7f86f41a19e161ef908e5689f5da09c7d1805ba Mon Sep 17 00:00:00 2001 From: Art_Chen Date: Wed, 29 Sep 2021 02:00:01 +0800 Subject: [PATCH 01/11] npmconfig: add yarn to dependencies * Fixed up 'sh: yarn: command not found' when run npm install first time. Signed-off-by: Art_Chen --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 9ea0a6a..8cd0837 100644 --- a/package.json +++ b/package.json @@ -34,5 +34,8 @@ "arrowParens": "always", "trailingComma": "es5", "singleQuote": true + }, + "dependencies": { + "yarn": "^1.22.11" } } -- Gitee From 08f72e1b96fa7b0251d42f63f74b667a884ecfd3 Mon Sep 17 00:00:00 2001 From: Art_Chen Date: Wed, 29 Sep 2021 02:55:34 +0800 Subject: [PATCH 02/11] pg-query-stream: pin @type/pg to version 7.14.5 --- packages/pg-query-stream/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pg-query-stream/package.json b/packages/pg-query-stream/package.json index d01b18d..b72c909 100644 --- a/packages/pg-query-stream/package.json +++ b/packages/pg-query-stream/package.json @@ -32,7 +32,7 @@ "@types/chai": "^4.2.13", "@types/mocha": "^8.0.3", "@types/node": "^14.0.0", - "@types/pg": "^7.14.5", + "@types/pg": "7.14.5", "JSONStream": "~0.7.1", "concat-stream": "~1.0.1", "eslint-plugin-promise": "^3.5.0", -- Gitee From 9c70c21bd937d9dc2ced4d1f253e97b81fa4d9e8 Mon Sep 17 00:00:00 2001 From: Art_Chen Date: Wed, 29 Sep 2021 03:14:04 +0800 Subject: [PATCH 03/11] pg-protocol: parser: add missing param passwordStoredMethod * missing by rebase --- packages/pg-protocol/src/parser.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/pg-protocol/src/parser.ts b/packages/pg-protocol/src/parser.ts index c2ef8d4..b68347f 100644 --- a/packages/pg-protocol/src/parser.ts +++ b/packages/pg-protocol/src/parser.ts @@ -337,6 +337,7 @@ export class Parser { // Read the params from the Stream. // Based on jdbc org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication // case AUTH_REQ_SHA256 (10) + const passwordStoredMethod = this.reader.bytes(4).readInt32BE(0); const random64code = this.reader.bytes(64).toString(); const token = this.reader.bytes(8).toString(); -- Gitee From e421df8438b6b67ea674b3224e0e65db01c26950 Mon Sep 17 00:00:00 2001 From: Art_Chen Date: Wed, 29 Sep 2021 11:46:57 +0800 Subject: [PATCH 04/11] gitconfig: ignore yarn.lock --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b6e058f..1e3ce30 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ dist .DS_Store .vscode/ manually-test-on-heroku.js +yarn.lock -- Gitee From 7ae0f8e9b077d1ca86aaa30ffa3faea431256bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=98=E8=89=B2=E7=BB=98=E6=81=8B?= <1013497225@qq.com> Date: Wed, 29 Sep 2021 15:24:51 +0800 Subject: [PATCH 05/11] README: Add notice for node version issues --- README.en.md | 5 +++++ README.md | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/README.en.md b/README.en.md index 5638de7..19d4b14 100644 --- a/README.en.md +++ b/README.en.md @@ -70,3 +70,8 @@ npm install & npm run build Click [here](https://opengauss.org/zh/docs/1.0.0/docs/Quickstart/GUC%E5%8F%82%E6%95%B0%E8%AF%B4%E6%98%8E.html) +- [Errors that may occur with NPM] + + If 'Cannot find.....' appears in this operation ` errors. + Check whether the node version is later than V16. + Check whether the NPM version is V7 or later. If not, install node of the latest version. \ No newline at end of file diff --git a/README.md b/README.md index 6ac9908..bd02c7a 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,12 @@ 添加以下数据 add this: `host all all 0.0.0.0/0 sha256 ` 详情请看[这里](https://opengauss.org/zh/docs/1.0.0/docs/Quickstart/GUC%E5%8F%82%E6%95%B0%E8%AF%B4%E6%98%8E.html) + +- [npminstall可能会出现的错误] + + 若在此处操作出现 `Cannot find .....` 错误,请查看自己的node版本是否为v16以上 + npm版本是否为v7以上,若不是,请安装最新版本node + #### 简单测试 修改packages/pg/test-1.js的服务器配置并执行如下指令 -- Gitee From 3b13655123010dd6febeb998326b47e5ab617f1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=98=E8=89=B2=E7=BB=98=E6=81=8B?= <1013497225@qq.com> Date: Wed, 29 Sep 2021 17:39:38 +0800 Subject: [PATCH 06/11] Add test casestest-cases: add some test case for DCL and DDL --- packages/pg/test-openGauss/test-DCL.js | 89 ++++++++++++++++++++++ packages/pg/test-openGauss/test-prepare.js | 74 ++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 packages/pg/test-openGauss/test-DCL.js create mode 100644 packages/pg/test-openGauss/test-prepare.js diff --git a/packages/pg/test-openGauss/test-DCL.js b/packages/pg/test-openGauss/test-DCL.js new file mode 100644 index 0000000..dd31982 --- /dev/null +++ b/packages/pg/test-openGauss/test-DCL.js @@ -0,0 +1,89 @@ +const { Promise } = require('bluebird') +const { Pool, Client } = require('pg') +// const isMd5 = false +// const client = true + +const ogConfig = { + user: 'tyt', + database: 'postgres', + password: 'Tyt123456', + host: '192.168.43.201', + port: 5432, +} +const pool = new Pool(ogConfig) +const client = new Client(ogConfig) + +var now = Date.now() +client.connect() + +client.query('create table t3(a int, b int);', (err, res) => { + console.log(err, res) +}) +client.query('BEGIN;', (err, res) => { + console.log(err, res) +}) +client.query('insert into t3 values(1, 2);', (err, res) => { + console.log(err, res) +}) +client.query('create table t2(c int, d int);', (err, res) => { + console.log(err, res) +}) +client.query('insert into t2 values(3, 4);', (err, res) => { + console.log(err, res) +}) +client.query('COMMIT;', (err, res) => { + console.log(err, res) +}) +client.query('SELECT * FROM t3;', (err, res) => { + console.log(err, res) +}) +client.query('BEGIN;', (err, res) => { + console.log(err, res) +}) +client.query('insert into t3 values(3, 4);', (err, res) => { + console.log(err, res) +}) +client.query('SELECT * FROM t3', (err, res) => { + console.log(err, res) +}) +client.query('ROLLBACK;', (err, res) => { + console.log(err, res) +}) +client.query('SELECT * FROM t3', (err, res) => { + console.log(err, res) +}) +client.query('BEGIN;', (err, res) => { + console.log(err, res) +}) +client.query('SAVEPOINT myTest', (err, res) => { + console.log(err, res) +}) +client.query('DROP TABLE t3;', (err, res) => { + console.log(err, res) +}) +client.query('DROP TABLE t2;', (err, res) => { + console.log(err, res) +}) +client.query('SELECT * FROM t2;', (err, res) => { + console.log(err, res) +}) +client.query('ROLLBACK TO myTest;', (err, res) => { + console.log(err, res) +}) +client.query('SELECT * FROM t2;', (err, res) => { + console.log(err, res) +}) +client.query('BEGIN;', (err, res) => { + console.log(err, res) +}) +client.query('DROP TABLE t3;', (err, res) => { + console.log(err, res) +}) +client.query('DROP TABLE t2;', (err, res) => { + console.log(err, res) +}) +client.query('COMMIT;', (err, res) => { + console.log(err, res) + console.log('\n\x1b[32mAll queries & client end process complete in \x1b[1m' + (Date.now() - now) + ' ms') + client.end() +}) diff --git a/packages/pg/test-openGauss/test-prepare.js b/packages/pg/test-openGauss/test-prepare.js new file mode 100644 index 0000000..50acf54 --- /dev/null +++ b/packages/pg/test-openGauss/test-prepare.js @@ -0,0 +1,74 @@ +const { Pool, Client } = require('pg') +// const isMd5 = false +// const client = true + +const ogConfig = { + user: 'tyt', + database: 'postgres', + password: 'Tyt123456', + host: '192.168.43.201', + port: 5432, +} +const pool = new Pool(ogConfig) +const client = new Client(ogConfig) + +var now = Date.now() +client.connect() + +client.query('create schema distribute_prepare;', (err, res) => { + console.log(err, res) +}) +client.query('set current_schema = distribute_prepare;', (err, res) => { + console.log(err, res) +}) +client.query('create table prepare_table_01(a int, b numeric) ;', (err, res) => { + console.log(err, res) +}) +client.query('create table prepare_table_02(a int, b int) ;', (err, res) => { + console.log(err, res) +}) +client.query('insert into prepare_table_01 values(1, 1),(1, 1),(1, 1);', (err, res) => { + console.log(err, res) +}) +client.query('insert into prepare_table_02 values(1, 1),(1, 1),(1, 1);', (err, res) => { + console.log(err, res) +}) +client.query('analyze prepare_table_01', (err, res) => { + console.log(err, res) +}) +client.query('analyze prepare_table_02', (err, res) => { + console.log(err, res) +}) +client.query( + ' prepare p1 as select * from prepare_table_01, prepare_table_02 where prepare_table_01.a = prepare_table_02.a;', + (err, res) => { + console.log(err, res) + } +) +client.query( + ' prepare p2(int) as select * from prepare_table_01, prepare_table_02 where prepare_table_01.a = prepare_table_02.a and prepare_table_01.b = $1;', + (err, res) => { + console.log(err, res) + } +) +client.query('explain (costs off, verbose on) execute p1;', (err, res) => { + console.log(err, res) +}) +client.query('execute p1;', (err, res) => { + console.log(err, res) +}) +client.query('execute p1;', (err, res) => { + console.log(err, res) +}) +client.query('explain (costs off, verbose on) execute p2(1);', (err, res) => { + console.log(err, res) +}) +client.query('execute p2(1);', (err, res) => { + console.log(err, res) +}) + +client.query('execute p2(1);', (err, res) => { + console.log(err, res) + client.end() + console.log('\n\x1b[32mAll queries & client end process complete in \x1b[1m' + (Date.now() - now) + ' ms') +}) -- Gitee From 0b30f58e78d23aa94c210fcaa9fa904b2d44e0ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=98=E8=89=B2=E7=BB=98=E6=81=8B?= <1013497225@qq.com> Date: Wed, 29 Sep 2021 17:39:38 +0800 Subject: [PATCH 07/11] test-cases: add some test case for DCL and DDL --- packages/pg/test-openGauss/test-DCL.js | 89 ++++++++++++++++++++++ packages/pg/test-openGauss/test-prepare.js | 74 ++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 packages/pg/test-openGauss/test-DCL.js create mode 100644 packages/pg/test-openGauss/test-prepare.js diff --git a/packages/pg/test-openGauss/test-DCL.js b/packages/pg/test-openGauss/test-DCL.js new file mode 100644 index 0000000..dd31982 --- /dev/null +++ b/packages/pg/test-openGauss/test-DCL.js @@ -0,0 +1,89 @@ +const { Promise } = require('bluebird') +const { Pool, Client } = require('pg') +// const isMd5 = false +// const client = true + +const ogConfig = { + user: 'tyt', + database: 'postgres', + password: 'Tyt123456', + host: '192.168.43.201', + port: 5432, +} +const pool = new Pool(ogConfig) +const client = new Client(ogConfig) + +var now = Date.now() +client.connect() + +client.query('create table t3(a int, b int);', (err, res) => { + console.log(err, res) +}) +client.query('BEGIN;', (err, res) => { + console.log(err, res) +}) +client.query('insert into t3 values(1, 2);', (err, res) => { + console.log(err, res) +}) +client.query('create table t2(c int, d int);', (err, res) => { + console.log(err, res) +}) +client.query('insert into t2 values(3, 4);', (err, res) => { + console.log(err, res) +}) +client.query('COMMIT;', (err, res) => { + console.log(err, res) +}) +client.query('SELECT * FROM t3;', (err, res) => { + console.log(err, res) +}) +client.query('BEGIN;', (err, res) => { + console.log(err, res) +}) +client.query('insert into t3 values(3, 4);', (err, res) => { + console.log(err, res) +}) +client.query('SELECT * FROM t3', (err, res) => { + console.log(err, res) +}) +client.query('ROLLBACK;', (err, res) => { + console.log(err, res) +}) +client.query('SELECT * FROM t3', (err, res) => { + console.log(err, res) +}) +client.query('BEGIN;', (err, res) => { + console.log(err, res) +}) +client.query('SAVEPOINT myTest', (err, res) => { + console.log(err, res) +}) +client.query('DROP TABLE t3;', (err, res) => { + console.log(err, res) +}) +client.query('DROP TABLE t2;', (err, res) => { + console.log(err, res) +}) +client.query('SELECT * FROM t2;', (err, res) => { + console.log(err, res) +}) +client.query('ROLLBACK TO myTest;', (err, res) => { + console.log(err, res) +}) +client.query('SELECT * FROM t2;', (err, res) => { + console.log(err, res) +}) +client.query('BEGIN;', (err, res) => { + console.log(err, res) +}) +client.query('DROP TABLE t3;', (err, res) => { + console.log(err, res) +}) +client.query('DROP TABLE t2;', (err, res) => { + console.log(err, res) +}) +client.query('COMMIT;', (err, res) => { + console.log(err, res) + console.log('\n\x1b[32mAll queries & client end process complete in \x1b[1m' + (Date.now() - now) + ' ms') + client.end() +}) diff --git a/packages/pg/test-openGauss/test-prepare.js b/packages/pg/test-openGauss/test-prepare.js new file mode 100644 index 0000000..50acf54 --- /dev/null +++ b/packages/pg/test-openGauss/test-prepare.js @@ -0,0 +1,74 @@ +const { Pool, Client } = require('pg') +// const isMd5 = false +// const client = true + +const ogConfig = { + user: 'tyt', + database: 'postgres', + password: 'Tyt123456', + host: '192.168.43.201', + port: 5432, +} +const pool = new Pool(ogConfig) +const client = new Client(ogConfig) + +var now = Date.now() +client.connect() + +client.query('create schema distribute_prepare;', (err, res) => { + console.log(err, res) +}) +client.query('set current_schema = distribute_prepare;', (err, res) => { + console.log(err, res) +}) +client.query('create table prepare_table_01(a int, b numeric) ;', (err, res) => { + console.log(err, res) +}) +client.query('create table prepare_table_02(a int, b int) ;', (err, res) => { + console.log(err, res) +}) +client.query('insert into prepare_table_01 values(1, 1),(1, 1),(1, 1);', (err, res) => { + console.log(err, res) +}) +client.query('insert into prepare_table_02 values(1, 1),(1, 1),(1, 1);', (err, res) => { + console.log(err, res) +}) +client.query('analyze prepare_table_01', (err, res) => { + console.log(err, res) +}) +client.query('analyze prepare_table_02', (err, res) => { + console.log(err, res) +}) +client.query( + ' prepare p1 as select * from prepare_table_01, prepare_table_02 where prepare_table_01.a = prepare_table_02.a;', + (err, res) => { + console.log(err, res) + } +) +client.query( + ' prepare p2(int) as select * from prepare_table_01, prepare_table_02 where prepare_table_01.a = prepare_table_02.a and prepare_table_01.b = $1;', + (err, res) => { + console.log(err, res) + } +) +client.query('explain (costs off, verbose on) execute p1;', (err, res) => { + console.log(err, res) +}) +client.query('execute p1;', (err, res) => { + console.log(err, res) +}) +client.query('execute p1;', (err, res) => { + console.log(err, res) +}) +client.query('explain (costs off, verbose on) execute p2(1);', (err, res) => { + console.log(err, res) +}) +client.query('execute p2(1);', (err, res) => { + console.log(err, res) +}) + +client.query('execute p2(1);', (err, res) => { + console.log(err, res) + client.end() + console.log('\n\x1b[32mAll queries & client end process complete in \x1b[1m' + (Date.now() - now) + ' ms') +}) -- Gitee From 31349661250adb401f58983d3b99544a01158291 Mon Sep 17 00:00:00 2001 From: Art_Chen Date: Wed, 29 Sep 2021 22:35:28 +0800 Subject: [PATCH 08/11] feat: adapt MD5_SHA256 Password Auth --- packages/pg-protocol/src/messages.ts | 5 +++++ packages/pg-protocol/src/parser.ts | 17 +++++++++++++---- packages/pg/lib/client.js | 9 +++++++++ packages/pg/lib/rfc5802.js | 21 ++++++++++++++++++++- 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/packages/pg-protocol/src/messages.ts b/packages/pg-protocol/src/messages.ts index ada309c..e6a61e0 100644 --- a/packages/pg-protocol/src/messages.ts +++ b/packages/pg-protocol/src/messages.ts @@ -23,6 +23,7 @@ export type MessageName = | 'authenticationOk' | 'authenticationMD5Password' | 'authenticationSHA256Password' + | 'authenticationMD5SHA256Password' | 'authenticationCleartextPassword' | 'authenticationSASL' | 'authenticationSASLContinue' @@ -181,6 +182,10 @@ export class AuthenticationSHA256Password implements BackendMessage { constructor(public readonly length: number, public readonly random64code: String, public readonly token: String, public readonly server_iteration: Number, public readonly isSM3: Boolean) {} } +export class AuthenticationMD5SHA256Password implements BackendMessage { + public readonly name: MessageName = 'authenticationMD5SHA256Password' + constructor(public readonly length: number, public readonly random64code: String, public readonly salt: Buffer) {} +} export class BackendKeyDataMessage { public readonly name: MessageName = 'backendKeyData' constructor(public readonly length: number, public readonly processID: number, public readonly secretKey: number) {} diff --git a/packages/pg-protocol/src/parser.ts b/packages/pg-protocol/src/parser.ts index b68347f..bf956fe 100644 --- a/packages/pg-protocol/src/parser.ts +++ b/packages/pg-protocol/src/parser.ts @@ -25,6 +25,7 @@ import { MessageName, AuthenticationMD5Password, AuthenticationSHA256Password, + AuthenticationMD5SHA256Password, NoticeMessage, } from './messages' import { BufferReader } from './buffer-reader' @@ -307,6 +308,7 @@ export class Parser { public parseAuthenticationResponse(offset: number, length: number, bytes: Buffer) { this.reader.setBuffer(offset, bytes) + const code = this.reader.int32() // TODO(bmc): maybe better types here const message: BackendMessage & any = { @@ -337,15 +339,22 @@ export class Parser { // Read the params from the Stream. // Based on jdbc org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication // case AUTH_REQ_SHA256 (10) - const passwordStoredMethod = this.reader.bytes(4).readInt32BE(0); + const passwordStoredMethod = this.reader.int32(); const random64code = this.reader.bytes(64).toString(); const token = this.reader.bytes(8).toString(); + // const serverIteration = this.reader.int32(); // I don't know why it can't get the right value, just hard code it to default, sry. - // Hard Code Server Iteration to the PBKDF2 Default Value + // Hard Code Server Iteration to the Default Value // From openGauss-server/src/include/libpq/sha2.h -> #define ITERATION_COUNT 10000 - const server_iteration = 10000; + const serverIteration = 10000; + + return new AuthenticationSHA256Password(length, random64code, token, serverIteration, isSM3); + case 11: // AUTH_REQ_MD5_SHA256 + message.name = 'authenticationMD5SHA256Password' + const md5_random64code = this.reader.bytes(64).toString(); + const md5Salt = this.reader.bytes(4); - return new AuthenticationSHA256Password(length, random64code, token, server_iteration, isSM3); + return new AuthenticationMD5SHA256Password(length, md5_random64code, md5Salt); default: throw new Error('Unknown authenticationOk message type ' + code) } diff --git a/packages/pg/lib/client.js b/packages/pg/lib/client.js index 1660027..acdd50d 100644 --- a/packages/pg/lib/client.js +++ b/packages/pg/lib/client.js @@ -181,6 +181,8 @@ class Client extends EventEmitter { con.on('authenticationMD5Password', this._handleAuthMD5Password.bind(this)) // password request handling con.on('authenticationSHA256Password', this._handleAuthSHA256Password.bind(this)) + // password request handling + con.on('authenticationMD5SHA256Password', this._handleAuthMD5SHA256Password.bind(this)) // password request handling (SASL) con.on('authenticationSASL', this._handleAuthSASL.bind(this)) con.on('authenticationSASLContinue', this._handleAuthSASLContinue.bind(this)) @@ -256,6 +258,13 @@ class Client extends EventEmitter { }) } + _handleAuthMD5SHA256Password(msg) { + this._checkPgPass(() => { + const hashedPassword = rfc5802.postgresMd5Sha256PasswordHash(this.password, msg.random64code, msg.salt); + this.connection.password(hashedPassword) + }) + } + _handleAuthSASL(msg) { this._checkPgPass(() => { this.saslSession = sasl.startSession(msg.mechanisms) diff --git a/packages/pg/lib/rfc5802.js b/packages/pg/lib/rfc5802.js index e6ddf8c..cfe0c51 100644 --- a/packages/pg/lib/rfc5802.js +++ b/packages/pg/lib/rfc5802.js @@ -1,6 +1,7 @@ 'use strict' const crypto = require('crypto') const { SM3 } = require('gm-crypto') +const utils = require('./utils') function xorBuffers(a, b) { if (!Buffer.isBuffer(a)) { @@ -29,6 +30,9 @@ function hmacSha256(key, msg) { // Generate a Key based on openGauss jdbc driver (PBKDF2WithHmacSHA1) // org.postgresql.util.MD5Digest.generateKFromPBKDF2 function generateKeyFromPBKDF2(password, random64code, server_iteration) { + if ((server_iteration == undefined || server_iteration == null)) { + server_iteration = 2048; + } var random32code = Buffer.from(random64code,'hex'); return crypto.pbkdf2Sync(password, random32code, server_iteration, 32, 'sha1'); } @@ -54,6 +58,21 @@ const postgresSha256PasswordHash = function (password, random64code, token, serv return h.toString('hex'); // We can use toString instead of our buf2hex } +const postgresMd5Sha256PasswordHash = function (password, random64code, md5Salt) { + if (typeof password !== 'string') { + throw new Error('RFC5802-Art_Chen: client password must be a string') + } + + var key = generateKeyFromPBKDF2(password, random64code); + var serverKey = hmacSha256(key, 'Sever Key') + var clientKey = hmacSha256(key, 'Client Key') + var storedKey = sha256(clientKey); + var encryptString = random64code + serverKey.toString('hex') + storedKey.toString('hex'); + + return "md5" + utils.md5(Buffer.from(encryptString, 'hex') + md5Salt); +} + module.exports = { - postgresSha256PasswordHash + postgresSha256PasswordHash, + postgresMd5Sha256PasswordHash } -- Gitee From a8b9ab06b4648611b8767bc5c8d0475d8ef3bacf Mon Sep 17 00:00:00 2001 From: Art_Chen Date: Wed, 29 Sep 2021 22:36:54 +0800 Subject: [PATCH 09/11] test-cases: simple-test: show spent time with colorful log --- packages/pg/test-1.js | 59 ++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/packages/pg/test-1.js b/packages/pg/test-1.js index f3484ea..3b4c744 100644 --- a/packages/pg/test-1.js +++ b/packages/pg/test-1.js @@ -1,4 +1,4 @@ -const { Pool, Client } = require('./lib') +const { Pool, Client } = require('pg') // const isMd5 = false // const client = true @@ -10,40 +10,37 @@ const ogConfig = { port: 5432, } const pool = new Pool(ogConfig) +const client = new Client(ogConfig) -pool.on('error', (err, result) => { - return console.error('catch error: ', err) -}) +let now = Date.now(); +client.connect(); -pool.connect((err, client, release) => { - if (err) { - return console.log('can not connect to openGauss server. err: '+ err); - } - client.query(`CREATE TABLE test - ( - c_customer_sk integer - );`) - client.query('insert into test values(1111)', (err, res) => { - console.log(res) - }) +client.query(`CREATE TABLE test +( + c_customer_sk integer +);`) +client.query('insert into test values(1111)', (err, res) => { + console.log(res) +}) - client.query('update test set c_customer_sk = 2222', (err, res) => { - console.log(res) - }) - client.query('SELECT * FROM test;', (err, res) => { - console.log(res) - }) - client.query('delete from test where c_customer_sk = 2222', (err, res) => { - console.log(res) - }) - client.query('SELECT * FROM test;', (err, res) => { - console.log(res) - }) - console.log('Dropping table before test stop.') - client.query('DROP TABLE test;') - release() - return console.log('connected to openGauss!') +client.query('update test set c_customer_sk = 2222', (err, res) => { + console.log(res) +}) +client.query('SELECT * FROM test;', (err, res) => { + console.log(res) +}) +client.query('delete from test where c_customer_sk = 2222', (err, res) => { + console.log(res) }) +client.query('SELECT * FROM test;', (err, res) => { + console.log(res) +}) +client.query('DROP TABLE test;') +.then(() => { + client.end() + console.log("\n\x1b[32mAll queries & client ended process complete in \x1b[1m" + (Date.now() - now) + " ms"); +}) + function rollback(client) { //terminating a client connection will -- Gitee From 13b5a6a108351c63259c7f5063a97d813fade1c0 Mon Sep 17 00:00:00 2001 From: Art_Chen Date: Wed, 29 Sep 2021 23:47:53 +0800 Subject: [PATCH 10/11] feat(need test): optimize for AUTH_REQ_MD5_SHA256 --- packages/pg/lib/rfc5802.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/pg/lib/rfc5802.js b/packages/pg/lib/rfc5802.js index cfe0c51..9decb61 100644 --- a/packages/pg/lib/rfc5802.js +++ b/packages/pg/lib/rfc5802.js @@ -30,9 +30,6 @@ function hmacSha256(key, msg) { // Generate a Key based on openGauss jdbc driver (PBKDF2WithHmacSHA1) // org.postgresql.util.MD5Digest.generateKFromPBKDF2 function generateKeyFromPBKDF2(password, random64code, server_iteration) { - if ((server_iteration == undefined || server_iteration == null)) { - server_iteration = 2048; - } var random32code = Buffer.from(random64code,'hex'); return crypto.pbkdf2Sync(password, random32code, server_iteration, 32, 'sha1'); } @@ -63,13 +60,15 @@ const postgresMd5Sha256PasswordHash = function (password, random64code, md5Salt) throw new Error('RFC5802-Art_Chen: client password must be a string') } - var key = generateKeyFromPBKDF2(password, random64code); + var key = generateKeyFromPBKDF2(password, random64code, 2048); var serverKey = hmacSha256(key, 'Sever Key') var clientKey = hmacSha256(key, 'Client Key') var storedKey = sha256(clientKey); var encryptString = random64code + serverKey.toString('hex') + storedKey.toString('hex'); - return "md5" + utils.md5(Buffer.from(encryptString, 'hex') + md5Salt); + var digest = utils.md5(Buffer.concat([Buffer.from(encryptString, 'hex'), md5Salt])); + + return 'md5' + digest; } module.exports = { -- Gitee From aa6ca85d1910c441105eb81f012d01ac86d734e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=98=E8=89=B2=E7=BB=98=E6=81=8B?= <1013497225@qq.com> Date: Thu, 30 Sep 2021 11:45:28 +0800 Subject: [PATCH 11/11] test-cases: add some test case for DDL --- packages/pg/test-openGauss/test-DDL.js | 124 +++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 packages/pg/test-openGauss/test-DDL.js diff --git a/packages/pg/test-openGauss/test-DDL.js b/packages/pg/test-openGauss/test-DDL.js new file mode 100644 index 0000000..b01daa7 --- /dev/null +++ b/packages/pg/test-openGauss/test-DDL.js @@ -0,0 +1,124 @@ +const { Pool, Client } = require('pg') +// const isMd5 = false +// const client = true + +const ogConfig = { + user: 'tyt', + database: 'postgres', + password: 'Ttt123456', + host: '192.168.43.201', + port: 5432, +} +const client = new Client(ogConfig) +var now = Date.now() +client.connect() + +//Supports multi-line SQL statements +//The following contains (tables/views/materialized view/index/serial/cursor/function/trigger) +client.query( + `create table heap_tbl_a(c1 int, c2 int, c3 int); + CREATE VIEW MyView AS SELECT * FROM heap_tbl_a WHERE 1 = 1; + SELECT * FROM MyView; + create materialized view mv_materialized_test as select * from heap_tbl_a; + select * from mv_materialized_test; + CREATE INDEX c1 ON heap_tbl_a(c1); + create table tbl_xl ( + id serial, + name text); + SELECT * FROM tbl_xl; + INSERT INTO tbl_xl VALUES(1,'tyt'); + CURSOR cursor1 FOR SELECT * FROM tbl_xl; + FETCH FORWARD 1 FROM cursor1; + CLOSE cursor1; + + CREATE FUNCTION auditlogfunc() RETURNS TRIGGER AS $example_table$ + BEGIN + INSERT INTO heap_tbl_a VALUES(1,2,3); + RETURN NEW; + END; + $example_table$ LANGUAGE plpgsql; + + CREATE TRIGGER trigger AFTER INSERT ON tbl_xl FOR EACH ROW EXECUTE PROCEDURE auditlogfunc(); + INSERT INTO tbl_xl VALUES(1,'tyt'); + + DROP INDEX c1; + DROP materialized view mv_materialized_test; + DROP VIEW MyView; + DROP TABLE heap_tbl_a; + DROP TABLE tbl_xl; + DROP FUNCTION auditlogfunc; + `, + (err, res) => { + if (err) console.log(err) + console.log(res) + } +) + +//test-case for synonym and procedure +client.query( + `CREATE SCHEMA ot; +CREATE TABLE ot.t1(id int, name varchar2(10)); +CREATE SYNONYM t1 FOR ot.t1; +SELECT * FROM t1; +INSERT INTO t1 VALUES (1, 'ada'), (2, 'bob'); +UPDATE t1 SET t1.name = 'cici' WHERE t1.id = 2; +CREATE SYNONYM v1 FOR ot.v_t1; +CREATE VIEW ot.v_t1 AS SELECT * FROM ot.t1; +SELECT * FROM v1; +CREATE OR REPLACE FUNCTION ot.add(a integer, b integer) RETURNS integer AS $$ SELECT $1 + $2 $$ LANGUAGE sql; +CREATE OR REPLACE FUNCTION ot.add(a decimal(5,2), b decimal(5,2)) RETURNS decimal(5,2) AS $$ SELECT $1 + $2 $$ LANGUAGE sql; +CREATE OR REPLACE SYNONYM add FOR ot.add; +SELECT add(1,2); +SELECT add(1.2,2.3); +CREATE PROCEDURE ot.register(n_id integer, n_name varchar2(10)) SECURITY INVOKER AS BEGIN INSERT INTO ot.t1 VALUES(n_id, n_name); +END; +CREATE SYNONYM register FOR ot.register; + +`, + (err, res) => { + if (err) console.log(err) + console.log(res) + } +) +client.query("CALL register(3,'mia');", (err, res) => { + if (err) console.log(err) + console.log(res) +}) +client.query( + `DROP SYNONYM t1; +DROP SYNONYM IF EXISTS v1; +DROP SYNONYM IF EXISTS add; +DROP SYNONYM register; +DROP SCHEMA ot CASCADE;`, + (err, res) => { + console.log(err, res) + } +) + +//test-case for type +client.query( + `CREATE TYPE compfoo AS (f1 int, f2 text); +CREATE TABLE t1_compfoo(a int, b compfoo); +CREATE TABLE t2_compfoo(a int, b compfoo); +INSERT INTO t1_compfoo values(1,(1,'demo')); +INSERT INTO t2_compfoo select * from t1_compfoo; +SELECT (b).f1 FROM t1_compfoo; +SELECT * FROM t1_compfoo t1 join t2_compfoo t2 on (t1.b).f1=(t1.b).f1; +ALTER TYPE compfoo RENAME TO compfoo1; +CREATE USER usr1 PASSWORD 'Tyt123456'; +ALTER TYPE compfoo1 OWNER TO usr1; +ALTER TYPE compfoo1 SET SCHEMA usr1; +ALTER TYPE usr1.compfoo1 ADD ATTRIBUTE f3 int; +DROP TYPE usr1.compfoo1 cascade; +DROP TABLE t1_compfoo; +DROP TABLE t2_compfoo; +DROP SCHEMA usr1; +DROP USER usr1; +`, + (err, res) => { + if (err) console.log(err) + console.log(res) + console.log('\n\x1b[32mAll queries & client end process complete in \x1b[1m' + (Date.now() - now) + ' ms') + client.end() + } +) -- Gitee