Skip to content

Instantly share code, notes, and snippets.

@cjbj
Last active November 20, 2018 00:30
Show Gist options
  • Save cjbj/cbf162355bfe75d698b1f6cbb0f86199 to your computer and use it in GitHub Desktop.
Save cjbj/cbf162355bfe75d698b1f6cbb0f86199 to your computer and use it in GitHub Desktop.
'use strict';
/*
In CDB: (insecure, not for production)
alter system set remote_os_authent=true scope=spfile;
In PDB create a user:
create user ops$oracle identified externally;
grant connect,resource to ops$oracle;
alter user ops$oracle default tablespace users;
alter user cj grant connect through ops$oracle;
Then as the 'oracle' OS user:
sqlplus /@localhost/orclpdb
SQL> SELECT SYS_CONTEXT('USERENV', 'PROXY_USER') as p, SYS_CONTEXT('USERENV', 'SESSION_USER') as s FROM DUAL;
P S
---------- ----------
OPS$ORACLE
Using:
sqlplus '[cj]'/@localhost/orclpdb
SQL> SELECT SYS_CONTEXT('USERENV', 'PROXY_USER') as p, SYS_CONTEXT('USERENV', 'SESSION_USER') as s FROM DUAL;
P S
---------- ----------
OPS$ORACLE CJ
The issue980c.js file output is:
Connection pool started
=> Pool connect: {}
[ 'proxy_user: ', 'session_user: OPS$ORACLE' ]
ORA-00942: table or view does not exist
=> Pool connect: { user: '[cj]' }
[ 'proxy_user: OPS$ORACLE', 'session_user: CJ' ]
[ [ 'Ellen' ] ]
*/
const oracledb = require('oracledb');
let pool;
async function init() {
try {
pool = await oracledb.createPool({
connectString: "localhost/orclpdb",
externalAuth: true,
homogeneous: false
});
console.log('Connection pool started');
await dopoolconnect({});
await dopoolconnect({user:"[cj]"});
} catch (err) {
console.error('init() error: ' + err.message);
} finally {
await closePoolAndExit();
}
}
async function dopoolconnect(p) {
console.log('=> Pool connect: ', p);
let connection;
try {
connection = await pool.getConnection(p);
let sql = `select 'proxy_user: ' || sys_context('USERENV', 'PROXY_USER'),
'session_user: ' || SYS_CONTEXT('USERENV', 'SESSION_USER')
from dual`;
let result = await connection.execute(sql);
console.log(result.rows[0]);
result = await connection.execute('select first_name from employees where rownum < 2');
console.log(result.rows);
} catch (err) {
console.error(err.message);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err.message);
}
}
}
}
async function closePoolAndExit() {
try {
let pool = oracledb.getPool();
await pool.close(10);
process.exit(0);
} catch(err) {
process.exit(0);
}
}
process
.on('SIGTERM', closePoolAndExit)
.on('SIGINT', closePoolAndExit);
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment