From 4d360083a386a1ebae2ca0be9fa51b72d85c8e64 Mon Sep 17 00:00:00 2001 From: Lucas Holmquist Date: Mon, 30 Sep 2019 10:27:29 -0400 Subject: [PATCH] docs: update docs with new authUrl setting (#169) --- example.js | 36 ++++++++++++++++++++++++++++++++++++ lib/openshift-rest-client.js | 1 + other.js | 26 ++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 example.js create mode 100644 other.js diff --git a/example.js b/example.js new file mode 100644 index 0000000..2e0bf0e --- /dev/null +++ b/example.js @@ -0,0 +1,36 @@ +const { OpenshiftClient: openshiftRestClient, config } = require('.'); + +// async function genericTryCatch (fn) { +// let result; +// try { +// result = await fn; +// } catch (err) { +// if (err.code === 404) { +// return Promise.reject(err); +// } +// result = err; +// } + +// return result; +// } + +// console.log(config); + +// OpenshiftClient().then(async (client) => { +// // Use the client object to find a list of projects, for example +// let bc = await genericTryCatch(client.apis.build.v1.ns('myproject').buildconfigs('nodejs-rest-http-s2i').delete()); +// console.log(bc); +// }); + +// const openshiftRestClient = require('openshift-rest-client').OpenshiftClient; + +const otherConfig = config.fromKubeconfig(); + +console.log(otherConfig); + +openshiftRestClient().then((client) => { + // Use the client object to find a list of projects, for example + client.apis['project.openshift.io'].v1.project.get().then((response) => { + console.log(response.body); + }); +}); diff --git a/lib/openshift-rest-client.js b/lib/openshift-rest-client.js index 894d8ca..ebc3158 100644 --- a/lib/openshift-rest-client.js +++ b/lib/openshift-rest-client.js @@ -61,6 +61,7 @@ const spec = JSON.parse(zlib.gunzipSync(fs.readFileSync(path.join(__dirname, 'sp * * @param {object} [settings] - settings object for the openshiftClient function * @param {string} [settings.url] - Openshift cluster url + * @param {string} [settings.authUrl] - Openshift Basic auth url * @param {object} [settings.auth] - * @param {string} [settings.auth.username] - username to authenticate to Openshift * @param {string} [settings.auth.password] - password to authenticate to Openshift diff --git a/other.js b/other.js new file mode 100644 index 0000000..239fc0f --- /dev/null +++ b/other.js @@ -0,0 +1,26 @@ + let kubeconfig = settings.config; + if (kubeconfig) { + // A config is being passed in. Check if it is an object + if (typeof kubeconfig === 'object' && kubeconfig.auth) { + // Check for the auth username password + if ('user' in kubeconfig.auth || 'username' in kubeconfig.auth) { + // They are trying the basic auth. + // Get the access token using the username and password + const accessToken = await getTokenFromBasicAuth({ insecureSkipTlsVerify: settings.insecureSkipTlsVerify, url: kubeconfig.url, user: kubeconfig.username || kubeconfig.user, password: kubeconfig.password || kubeconfig.pass }); + + kubeconfig = { + url: kubeconfig.url, + auth: { + bearer: accessToken + }, + insecureSkipTlsVerify: 'insecureSkipTlsVerify' in settings ? Boolean(settings.insecureSkipTlsVerify) : false + }; + } + } + } + + console.log(kubeconfig); + + // TODO: need to be able to pass in the config object here + const client = new Client({ config: kubeconfig || config.fromKubeconfig(), spec, getNames: getNames }); + return client;