feat(allowUnauthorized): accept self-signed certificates (#62)
Related to vatesfr/xo-web#2138
This commit is contained in:
parent
a348585e76
commit
7481874ba2
@ -37,6 +37,7 @@
|
||||
"debug": "^2.6.6",
|
||||
"event-to-promise": "^0.8.0",
|
||||
"exec-promise": "^0.6.1",
|
||||
"http-request-plus": "^0.1.1",
|
||||
"json-rpc-protocol": "^0.11.2",
|
||||
"kindof": "^2.0.0",
|
||||
"lodash": "^4.17.4",
|
||||
@ -45,7 +46,6 @@
|
||||
"ms": "^1.0.0",
|
||||
"promise-toolbox": "^0.8.2",
|
||||
"pw": "0.0.4",
|
||||
"superagent": "^3.5.2",
|
||||
"xmlrpc": "^1.3.2",
|
||||
"xo-collection": "^0.4.1"
|
||||
},
|
||||
|
@ -36,9 +36,10 @@ const usage = 'Usage: xen-api <url> <user> [<password>]'
|
||||
|
||||
const main = async args => {
|
||||
const opts = minimist(args, {
|
||||
boolean: ['help', 'read-only', 'verbose'],
|
||||
boolean: ['allow-unauthorized', 'help', 'read-only', 'verbose'],
|
||||
|
||||
alias: {
|
||||
'allow-unauthorized': 'au',
|
||||
debounce: 'd',
|
||||
help: 'h',
|
||||
'read-only': 'ro',
|
||||
@ -72,6 +73,7 @@ const main = async args => {
|
||||
|
||||
const xapi = createClient({
|
||||
url,
|
||||
allowUnauthorized: opts.au,
|
||||
auth: { user, password },
|
||||
debounce: opts.debounce != null ? +opts.debounce : null,
|
||||
readOnly: opts.ro
|
||||
|
@ -76,20 +76,15 @@ export const wrapError = error => new XapiError(error)
|
||||
|
||||
// ===================================================================
|
||||
|
||||
const URL_RE = /^(?:http(s)?:\/*)?([^/]+?)(?::([0-9]+))?\/?$/
|
||||
const URL_RE = /^(?:(https?:)\/*)?([^/]+?)(?::([0-9]+))?\/?$/
|
||||
const parseUrl = url => {
|
||||
const matches = URL_RE.exec(url)
|
||||
if (!matches) {
|
||||
throw new Error('invalid URL: ' + url)
|
||||
}
|
||||
|
||||
url = {
|
||||
hostname: matches[2],
|
||||
port: matches[3],
|
||||
protocol: matches[1] === 's' ? 'https:' : 'http:'
|
||||
}
|
||||
|
||||
return url
|
||||
const [ , protocol = 'https:', hostname, port ] = matches
|
||||
return { protocol, hostname, port }
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
@ -139,6 +134,7 @@ export class Xapi extends EventEmitter {
|
||||
constructor (opts) {
|
||||
super()
|
||||
|
||||
this._allowUnauthorized = opts.allowUnauthorized
|
||||
this._auth = opts.auth
|
||||
this._pool = null
|
||||
this._readOnly = Boolean(opts.readOnly)
|
||||
@ -174,7 +170,10 @@ export class Xapi extends EventEmitter {
|
||||
|
||||
set _url (url) {
|
||||
this.__url = url
|
||||
this._call = autoTransport({ url })
|
||||
this._call = autoTransport({
|
||||
allowUnauthorized: this._allowUnauthorized,
|
||||
url
|
||||
})
|
||||
}
|
||||
|
||||
get readOnly () {
|
||||
|
@ -1,23 +1,21 @@
|
||||
import superagent from 'superagent'
|
||||
import httpRequestPlus from 'http-request-plus'
|
||||
import { format, parse } from 'json-rpc-protocol'
|
||||
|
||||
export default ({ url: { hostname, port, protocol } }) => {
|
||||
const href = `${protocol}//${hostname}${
|
||||
port != null ? `:${port}` : ''
|
||||
}/jsonrpc`
|
||||
export default ({ allowUnauthorized, url }) => {
|
||||
return (method, args) => httpRequestPlus.post(url, {
|
||||
rejectUnauthorized: !allowUnauthorized,
|
||||
body: format.request(0, method, args),
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
path: '/jsonrpc'
|
||||
}).readAll('utf8').then(text => {
|
||||
const response = parse(text)
|
||||
if (response.type === 'response') {
|
||||
return response.result
|
||||
}
|
||||
|
||||
return (method, args) =>
|
||||
superagent.post(href)
|
||||
.accept('application/json')
|
||||
.parse(superagent.parse.text) // no smart parsing from superagent
|
||||
.send(format.request(0, method, args))
|
||||
.type('application/json')
|
||||
.then(({ text }) => {
|
||||
const response = parse(text)
|
||||
if (response.type === 'response') {
|
||||
return response.result
|
||||
}
|
||||
|
||||
throw response.error
|
||||
})
|
||||
throw response.error
|
||||
})
|
||||
}
|
||||
|
@ -64,7 +64,10 @@ const parseResult = result => {
|
||||
}
|
||||
}
|
||||
|
||||
export default ({ url: { hostname, path, port, protocol } }) => {
|
||||
export default ({
|
||||
allowUnauthorized,
|
||||
url: { hostname, path, port, protocol }
|
||||
}) => {
|
||||
const client = (
|
||||
protocol === 'https:'
|
||||
? createSecureClient
|
||||
@ -73,7 +76,7 @@ export default ({ url: { hostname, path, port, protocol } }) => {
|
||||
host: hostname,
|
||||
path: '/json',
|
||||
port,
|
||||
rejectUnauthorized: false
|
||||
rejectUnauthorized: !allowUnauthorized
|
||||
})
|
||||
const call = promisify(client.methodCall, client)
|
||||
|
||||
|
@ -1069,10 +1069,6 @@ commander@^2.8.1:
|
||||
dependencies:
|
||||
graceful-readlink ">= 1.0.0"
|
||||
|
||||
component-emitter@^1.2.0:
|
||||
version "1.2.1"
|
||||
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.2.1.tgz#137918d6d78283f7df7a6b7c5a63e140e69425e6"
|
||||
|
||||
concat-map@0.0.1:
|
||||
version "0.0.1"
|
||||
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
|
||||
@ -1101,10 +1097,6 @@ convert-source-map@^1.1.0, convert-source-map@^1.4.0:
|
||||
version "1.5.0"
|
||||
resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.5.0.tgz#9acd70851c6d5dfdd93d9282e5edf94a03ff46b5"
|
||||
|
||||
cookiejar@^2.0.6:
|
||||
version "2.1.1"
|
||||
resolved "https://registry.yarnpkg.com/cookiejar/-/cookiejar-2.1.1.tgz#41ad57b1b555951ec171412a81942b1e8200d34a"
|
||||
|
||||
core-js@^2.4.0:
|
||||
version "2.4.1"
|
||||
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.4.1.tgz#4de911e667b0eae9124e34254b53aea6fc618d3e"
|
||||
@ -1580,7 +1572,7 @@ expand-range@^1.8.1:
|
||||
dependencies:
|
||||
fill-range "^2.1.0"
|
||||
|
||||
extend@^3.0.0, extend@~3.0.0:
|
||||
extend@~3.0.0:
|
||||
version "3.0.1"
|
||||
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.1.tgz#a755ea7bc1adfcc5a31ce7e762dbaadc5e636444"
|
||||
|
||||
@ -1693,7 +1685,7 @@ forever-agent@~0.6.1:
|
||||
version "0.6.1"
|
||||
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
|
||||
|
||||
form-data@^2.1.1, form-data@~2.1.1:
|
||||
form-data@~2.1.1:
|
||||
version "2.1.4"
|
||||
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.1.4.tgz#33c183acf193276ecaa98143a69e94bfee1750d1"
|
||||
dependencies:
|
||||
@ -1701,10 +1693,6 @@ form-data@^2.1.1, form-data@~2.1.1:
|
||||
combined-stream "^1.0.5"
|
||||
mime-types "^2.1.12"
|
||||
|
||||
formidable@^1.1.1:
|
||||
version "1.1.1"
|
||||
resolved "https://registry.yarnpkg.com/formidable/-/formidable-1.1.1.tgz#96b8886f7c3c3508b932d6bd70c4d3a88f35f1a9"
|
||||
|
||||
fs-readdir-recursive@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/fs-readdir-recursive/-/fs-readdir-recursive-1.0.0.tgz#8cd1745c8b4f8a29c8caec392476921ba195f560"
|
||||
@ -1900,6 +1888,14 @@ html-encoding-sniffer@^1.0.1:
|
||||
dependencies:
|
||||
whatwg-encoding "^1.0.1"
|
||||
|
||||
http-request-plus@^0.1.1:
|
||||
version "0.1.1"
|
||||
resolved "https://registry.yarnpkg.com/http-request-plus/-/http-request-plus-0.1.1.tgz#f12e5f00d4808d5863145e9dde7b3e3274bb5e0c"
|
||||
dependencies:
|
||||
is-redirect "^1.0.0"
|
||||
lodash "^4.17.4"
|
||||
promise-toolbox "^0.8.2"
|
||||
|
||||
http-signature@~1.1.0:
|
||||
version "1.1.1"
|
||||
resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.1.1.tgz#df72e267066cd0ac67fb76adf8e134a8fbcf91bf"
|
||||
@ -2093,6 +2089,10 @@ is-property@^1.0.0:
|
||||
version "1.0.2"
|
||||
resolved "https://registry.yarnpkg.com/is-property/-/is-property-1.0.2.tgz#57fe1c4e48474edd65b09911f26b1cd4095dda84"
|
||||
|
||||
is-redirect@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/is-redirect/-/is-redirect-1.0.0.tgz#1d03dded53bd8db0f30c26e4f95d36fc7c87dc24"
|
||||
|
||||
is-regex@^1.0.3:
|
||||
version "1.0.4"
|
||||
resolved "https://registry.yarnpkg.com/is-regex/-/is-regex-1.0.4.tgz#5517489b547091b0930e095654ced25ee97e9491"
|
||||
@ -2639,10 +2639,6 @@ merge@^1.1.3:
|
||||
version "1.2.0"
|
||||
resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.0.tgz#7531e39d4949c281a66b8c5a6e0265e8b05894da"
|
||||
|
||||
methods@^1.1.1:
|
||||
version "1.1.2"
|
||||
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
|
||||
|
||||
micromatch@^2.1.5, micromatch@^2.3.11:
|
||||
version "2.3.11"
|
||||
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-2.3.11.tgz#86677c97d1720b363431d04d0d15293bd38c1565"
|
||||
@ -2671,10 +2667,6 @@ mime-types@^2.1.12, mime-types@~2.1.7:
|
||||
dependencies:
|
||||
mime-db "~1.27.0"
|
||||
|
||||
mime@^1.3.4:
|
||||
version "1.3.4"
|
||||
resolved "https://registry.yarnpkg.com/mime/-/mime-1.3.4.tgz#115f9e3b6b3daf2959983cb38f149a2d40eb5d53"
|
||||
|
||||
minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3:
|
||||
version "3.0.4"
|
||||
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
|
||||
@ -3033,7 +3025,7 @@ pw@0.0.4:
|
||||
version "0.0.4"
|
||||
resolved "https://registry.yarnpkg.com/pw/-/pw-0.0.4.tgz#8015982ef8bebfd9d8eb8c795e751774871fde46"
|
||||
|
||||
qs@^6.1.0, qs@~6.4.0:
|
||||
qs@~6.4.0:
|
||||
version "6.4.0"
|
||||
resolved "https://registry.yarnpkg.com/qs/-/qs-6.4.0.tgz#13e26d28ad6b0ffaa91312cd3bf708ed351e7233"
|
||||
|
||||
@ -3078,7 +3070,7 @@ read-pkg@^1.0.0:
|
||||
normalize-package-data "^2.3.2"
|
||||
path-type "^1.0.0"
|
||||
|
||||
readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@^2.0.6, readable-stream@^2.1.4, readable-stream@^2.2.2:
|
||||
readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.1.4, readable-stream@^2.2.2:
|
||||
version "2.2.9"
|
||||
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.9.tgz#cf78ec6f4a6d1eb43d26488cac97f042e74b7fc8"
|
||||
dependencies:
|
||||
@ -3462,21 +3454,6 @@ strip-json-comments@~2.0.1:
|
||||
version "2.0.1"
|
||||
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
|
||||
|
||||
superagent@^3.5.2:
|
||||
version "3.5.2"
|
||||
resolved "https://registry.yarnpkg.com/superagent/-/superagent-3.5.2.tgz#3361a3971567504c351063abeaae0faa23dbf3f8"
|
||||
dependencies:
|
||||
component-emitter "^1.2.0"
|
||||
cookiejar "^2.0.6"
|
||||
debug "^2.2.0"
|
||||
extend "^3.0.0"
|
||||
form-data "^2.1.1"
|
||||
formidable "^1.1.1"
|
||||
methods "^1.1.1"
|
||||
mime "^1.3.4"
|
||||
qs "^6.1.0"
|
||||
readable-stream "^2.0.5"
|
||||
|
||||
supports-color@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7"
|
||||
@ -3704,10 +3681,14 @@ window-size@0.1.0:
|
||||
version "0.1.0"
|
||||
resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d"
|
||||
|
||||
wordwrap@0.0.2, wordwrap@~0.0.2:
|
||||
wordwrap@0.0.2:
|
||||
version "0.0.2"
|
||||
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f"
|
||||
|
||||
wordwrap@~0.0.2:
|
||||
version "0.0.3"
|
||||
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107"
|
||||
|
||||
wordwrap@~1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
|
||||
|
Loading…
Reference in New Issue
Block a user