|
| 1 | +/* Copyright (c) 2022, Oracle and/or its affiliates. */ |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * |
| 5 | + * You may not use the identified files except in compliance with the Apache |
| 6 | + * License, Version 2.0 (the "License.") |
| 7 | + * |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * NAME |
| 19 | + * tokenbasedauthpool.js |
| 20 | + * |
| 21 | + * DESCRIPTION |
| 22 | + * This script shows connection pooling with token based authentication to |
| 23 | + * Oracle Autonomous Database from Oracle Compute Infrastructure. It shows |
| 24 | + * how to create a connection pool and update expired tokens. |
| 25 | + * |
| 26 | + * For more information refer to |
| 27 | + * https://oracle.github.io/node-oracledb/doc/api.html#tokenbasedauth |
| 28 | + * |
| 29 | + * PREREQUISITES |
| 30 | + * - node-oracledb 5.4 or later. |
| 31 | + * |
| 32 | + * - Oracle Client libraries 19.14 (or later), or 21.5 (or later). |
| 33 | + * |
| 34 | + * - The Oracle Cloud Infrastructure command line interface (OCI-CLI). The |
| 35 | + * command line interface (CLI) is a tool that enables you to work with |
| 36 | + * Oracle Cloud Infrastructure objects and services at a command line, see |
| 37 | + * https://docs.oracle.com/en-us/iaas/Content/API/Concepts/cliconcepts.htm |
| 38 | + * |
| 39 | + * - Set these environment variables (see the code explanation): |
| 40 | + * NODE_ORACLEDB_ACCESS_TOKEN_LOC |
| 41 | + * NODE_ORACLEDB_CONNECTIONSTRING |
| 42 | + * |
| 43 | + *****************************************************************************/ |
| 44 | + |
| 45 | +const fs = require('fs'); |
| 46 | +const oracledb = require('oracledb'); |
| 47 | +const { execSync } = require('child_process'); |
| 48 | + |
| 49 | +// Execute the OCI-CLI command to generate a token. |
| 50 | +// This should create two files "token" and "oci_db_key.pem" |
| 51 | +// On Linux the default file location is "~/.oci/db-token". You should set |
| 52 | +// NODE_ORACLEDB_ACCESS_TOKEN_LOC to this directory, or to the directory where |
| 53 | +// you move the files. |
| 54 | +try { |
| 55 | + const cmdResult = execSync('oci iam db-token get', { encoding: 'utf-8' }); |
| 56 | + console.log(cmdResult); |
| 57 | +} catch (err) { |
| 58 | + console.log(err); |
| 59 | +} |
| 60 | + |
| 61 | +// User defined function for reading token and private key values generated by |
| 62 | +// the OCI-CLI. |
| 63 | +function getToken() { |
| 64 | + const tokenPath = process.env.NODE_ORACLEDB_ACCESS_TOKEN_LOC + '/token'; |
| 65 | + const privateKeyPath = process.env.NODE_ORACLEDB_ACCESS_TOKEN_LOC + |
| 66 | + '/oci_db_key.pem'; |
| 67 | + |
| 68 | + let token = ''; |
| 69 | + let privateKey = ''; |
| 70 | + try { |
| 71 | + // Read token file |
| 72 | + token = fs.readFileSync(tokenPath, 'utf8'); |
| 73 | + // Read private key file |
| 74 | + const privateKeyFileContents = fs.readFileSync(privateKeyPath, 'utf-8'); |
| 75 | + privateKeyFileContents.split(/\r?\n/).forEach(line => { |
| 76 | + if (line != '-----BEGIN PRIVATE KEY-----' && |
| 77 | + line != '-----END PRIVATE KEY-----') |
| 78 | + privateKey = privateKey.concat(line); |
| 79 | + }); |
| 80 | + } catch (err) { |
| 81 | + console.error(err); |
| 82 | + } |
| 83 | + |
| 84 | + const tokenBasedAuthData = { |
| 85 | + token : token, |
| 86 | + privateKey : privateKey |
| 87 | + }; |
| 88 | + return tokenBasedAuthData; |
| 89 | +} |
| 90 | + |
| 91 | +// User defined callback function which is invoked by node-oracledb when a |
| 92 | +// token expires and the pool needs to create new connections |
| 93 | +function tokenCallback() { |
| 94 | + return getToken(); |
| 95 | +} |
| 96 | + |
| 97 | +async function run() { |
| 98 | + // Get token and private key for initial pool creation. |
| 99 | + const accessTokenData = getToken(); |
| 100 | + |
| 101 | + // Configuration for token based authentication: |
| 102 | + // accessToken: The initial token values |
| 103 | + // accessTokenCallback: A callback function to provide a refreshed token |
| 104 | + // externalAuth: Must be set to true for token based authentication |
| 105 | + // homogeneous: Must be set to true for token based authentication |
| 106 | + // connectString: The NODE_ORACLEDB_CONNECTIONSTRING environment |
| 107 | + // variable set to the Oracle Net alias or connect |
| 108 | + // descriptor of your Oracle Autonomous Database |
| 109 | + const config = { |
| 110 | + accessToken : accessTokenData, |
| 111 | + accessTokenCallback : tokenCallback, |
| 112 | + externalAuth : true, |
| 113 | + homogeneous : true, |
| 114 | + connectString : process.env.NODE_ORACLEDB_CONNECTIONSTRING, |
| 115 | + }; |
| 116 | + |
| 117 | + try { |
| 118 | + |
| 119 | + // Create pool using token based authentication |
| 120 | + await oracledb.createPool(config); |
| 121 | + |
| 122 | + // A real app would call createConnection() multiple times over a long |
| 123 | + // period of time. During this time the pool may grow. If the initial |
| 124 | + // token has expired, node-oracledb will automatically call the |
| 125 | + // accessTokenCallback function allowing you to update the token. |
| 126 | + await createConnection(); |
| 127 | + |
| 128 | + } catch (err) { |
| 129 | + console.error(err); |
| 130 | + } finally { |
| 131 | + await closePoolAndExit(); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +async function createConnection() { |
| 136 | + let connection; |
| 137 | + try { |
| 138 | + // Get a connection from the default pool |
| 139 | + connection = await oracledb.getConnection(); |
| 140 | + const sql = `SELECT TO_CHAR(current_date, 'DD-Mon-YYYY HH24:MI') AS D |
| 141 | + FROM DUAL`; |
| 142 | + const result = await connection.execute(sql); |
| 143 | + console.log("Result is:\n", result); |
| 144 | + } catch (err) { |
| 145 | + console.error(err); |
| 146 | + } finally { |
| 147 | + if (connection) { |
| 148 | + // Put the connection back in the pool |
| 149 | + await connection.close(); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +async function closePoolAndExit() { |
| 155 | + console.log('\nTerminating'); |
| 156 | + try { |
| 157 | + // Get the pool from the pool cache and close it |
| 158 | + await oracledb.getPool().close(0); |
| 159 | + process.exit(0); |
| 160 | + } catch (err) { |
| 161 | + console.error(err.message); |
| 162 | + process.exit(1); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +process |
| 167 | + .once('SIGTERM', closePoolAndExit) |
| 168 | + .once('SIGINT', closePoolAndExit); |
| 169 | + |
| 170 | +run(); |
0 commit comments