@f5devcentral/atg-shared-utilities
Advanced tools
Comparing version 0.4.11 to 0.5.0
@@ -15,2 +15,12 @@ # Changelog | ||
## [0.5.0] 2022-12-08 | ||
### Added | ||
### Fixed | ||
- Get primary admin user from system for http requests to port 8100 | ||
### Changed | ||
### Removed | ||
## [0.4.11] 2022-11-22 | ||
@@ -17,0 +27,0 @@ ### Added |
{ | ||
"name": "@f5devcentral/atg-shared-utilities", | ||
"version": "0.4.11", | ||
"version": "0.5.0", | ||
"scripts": { | ||
@@ -5,0 +5,0 @@ "lint": "eslint .", |
@@ -23,2 +23,4 @@ /** | ||
const tmshUtil = require('./tmshUtils'); | ||
/* eslint-disable no-console */ | ||
@@ -51,9 +53,2 @@ const DEBUG = false; | ||
if (reqOpts.protocol === 'http:') { | ||
protocol = http; | ||
if (reqOpts.port === 8100 && !reqOpts.auth) { | ||
reqOpts.auth = 'admin:'; | ||
} | ||
} | ||
if ((typeof body !== 'undefined') | ||
@@ -73,52 +68,65 @@ && (['GET', 'HEAD'].indexOf(reqOpts.method) < 0)) { | ||
} | ||
return new Promise((resolve, reject) => { | ||
const request = protocol.request(reqOpts, (response) => { | ||
let buffer = ''; | ||
response.on('data', (chunk) => { | ||
buffer += chunk; | ||
}); | ||
response.on('end', () => { | ||
if (DEBUG) { | ||
console.log(`\nResponse from https://${reqOpts.host}/${reqOpts.path}:`); | ||
} | ||
if (buffer.length === 0) { | ||
resolve(''); | ||
return; | ||
} | ||
if (response.statusCode === 204) { | ||
let promise = Promise.resolve(); | ||
if (reqOpts.protocol === 'http:') { | ||
protocol = http; | ||
if (reqOpts.port === 8100 && !reqOpts.auth) { | ||
promise = getPrimaryAdminUser() | ||
.then((primaryAdminUser) => { | ||
reqOpts.auth = `${primaryAdminUser}:`; | ||
}); | ||
} | ||
} | ||
return promise | ||
.then(() => new Promise((resolve, reject) => { | ||
const request = protocol.request(reqOpts, (response) => { | ||
let buffer = ''; | ||
response.on('data', (chunk) => { | ||
buffer += chunk; | ||
}); | ||
response.on('end', () => { | ||
if (DEBUG) { | ||
console.log(response.statusCode); | ||
console.log(`\nResponse from https://${reqOpts.host}/${reqOpts.path}:`); | ||
} | ||
resolve(''); | ||
return; | ||
} | ||
if (buffer.length === 0) { | ||
resolve(''); | ||
return; | ||
} | ||
if (response.statusCode === 204) { | ||
if (DEBUG) { | ||
console.log(response.statusCode); | ||
} | ||
resolve(''); | ||
return; | ||
} | ||
if (response.statusCode >= 400) { | ||
reject(new Error(`${response.statusCode} ${buffer}`)); | ||
} | ||
try { | ||
if (DEBUG) { | ||
console.log(response.statusCode, JSON.parse(buffer)); | ||
if (response.statusCode >= 400) { | ||
reject(new Error(`${response.statusCode} ${buffer}`)); | ||
} | ||
resolve(JSON.parse(buffer)); | ||
} catch (error) { | ||
reject(new Error(`body is not JSON: ${buffer}`)); | ||
} | ||
try { | ||
if (DEBUG) { | ||
console.log(response.statusCode, JSON.parse(buffer)); | ||
} | ||
resolve(JSON.parse(buffer)); | ||
} catch (error) { | ||
reject(new Error(`body is not JSON: ${buffer}`)); | ||
} | ||
}); | ||
}); | ||
}); | ||
request.on('error', (e) => { | ||
reject(e); | ||
}); | ||
request.on('error', (e) => { | ||
reject(e); | ||
}); | ||
if (jsonBody) { | ||
try { | ||
request.write(jsonBody); | ||
} catch (err) { | ||
reject(err); | ||
if (jsonBody) { | ||
try { | ||
request.write(jsonBody); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
} | ||
} | ||
request.end(); | ||
}); | ||
request.end(); | ||
})); | ||
} | ||
@@ -135,2 +143,13 @@ | ||
function getPrimaryAdminUser() { | ||
return tmshUtil.executeTmshCommand('list sys db systemauth.primaryadminuser') | ||
.then((result) => { | ||
if (!result || !result.value) { | ||
return Promise.reject(new Error('Unable to get primary admin user')); | ||
} | ||
const adminUser = result.value.replace(/"/g, ''); | ||
return Promise.resolve(adminUser); | ||
}); | ||
} | ||
module.exports = Request; |
@@ -20,2 +20,3 @@ /** | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
const https = require('https'); | ||
@@ -26,5 +27,7 @@ const sinon = require('sinon'); | ||
const request = require('../../src/request'); | ||
const tmshUtil = require('../../src/tmshUtils'); | ||
describe('Request', () => { | ||
beforeEach(() => { | ||
sinon.spy(http, 'request'); | ||
sinon.spy(https, 'request'); | ||
@@ -41,2 +44,81 @@ nock('https://localhost') | ||
describe('auth', () => { | ||
beforeEach(() => { | ||
sinon.stub(tmshUtil, 'executeTmshCommand').resolves( | ||
{ | ||
value: '"myPrimaryAdminUser"' | ||
} | ||
); | ||
nock('http://localhost:8100') | ||
.get('/foo') | ||
.reply(200, {}); | ||
nock('https://localhost:8100') | ||
.get('/foo') | ||
.reply(200, {}); | ||
nock('http://localhost:8101') | ||
.get('/foo') | ||
.reply(200, {}); | ||
}); | ||
it('should set auth to primary admin user when using port 8100 with http', () => request.send( | ||
{ | ||
protocol: 'http:', | ||
host: 'localhost', | ||
port: 8100, | ||
method: 'GET', | ||
path: '/foo' | ||
} | ||
) | ||
.then(() => { | ||
assert.strictEqual(http.request.calledOnce, true); | ||
assert.deepEqual(http.request.getCall(0).args[0].auth, 'myPrimaryAdminUser:'); | ||
})); | ||
it('should not touch auth when auth is specified', () => request.send( | ||
{ | ||
protocol: 'http:', | ||
host: 'localhost', | ||
port: 8100, | ||
method: 'GET', | ||
path: '/foo', | ||
auth: 'admin:' | ||
} | ||
) | ||
.then(() => { | ||
assert.strictEqual(http.request.calledOnce, true); | ||
assert.deepEqual(http.request.getCall(0).args[0].auth, 'admin:'); | ||
})); | ||
it('should not touch auth when not using port 8100 with http', () => request.send( | ||
{ | ||
protocol: 'http:', | ||
host: 'localhost', | ||
port: 8101, | ||
method: 'GET', | ||
path: '/foo' | ||
} | ||
) | ||
.then(() => { | ||
assert.strictEqual(http.request.calledOnce, true); | ||
assert.deepEqual(http.request.getCall(0).args[0].auth, undefined); | ||
})); | ||
it('should not touch auth when using port 8100 with https', () => request.send( | ||
{ | ||
protocol: 'https:', | ||
host: 'localhost', | ||
port: 8100, | ||
method: 'GET', | ||
path: '/foo' | ||
} | ||
) | ||
.then(() => { | ||
assert.strictEqual(https.request.calledOnce, true); | ||
assert.deepEqual(https.request.getCall(0).args[0].auth, undefined); | ||
})); | ||
}); | ||
it('should set Content-Length header if there is a body', () => { | ||
@@ -43,0 +125,0 @@ const body = { |
@@ -27,2 +27,3 @@ /** | ||
const secureVault = require('../../src/secureVault'); | ||
const tmshUtil = require('../../src/tmshUtils'); | ||
@@ -33,2 +34,10 @@ chai.use(chaiAsPromised); | ||
describe('SecureVault', () => { | ||
beforeEach(() => { | ||
sinon.stub(tmshUtil, 'executeTmshCommand').resolves( | ||
{ | ||
value: '"admin"' | ||
} | ||
); | ||
}); | ||
afterEach(() => { | ||
@@ -35,0 +44,0 @@ sinon.restore(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
123100
22
2566
8