@@ -11,6 +11,7 @@ const { packageDirectory } = require('../components/packageComponent.ts');
1111const { encode } = require ( 'cbor-x' ) ;
1212const { getHdbPid } = require ( '../utility/processManagement/processManagement.js' ) ;
1313const { initConfig, getConfigPath } = require ( '../config/configUtils.js' ) ;
14+ const { loadCredentials } = require ( './cliCredentials.js' ) ;
1415
1516const OP_ALIASES = { deploy : 'deploy_component' , package : 'package_component' } ;
1617
@@ -56,15 +57,29 @@ function buildRequest() {
5657 return req ;
5758}
5859
60+ /**
61+ * Resolves the target URL from various sources.
62+ * @param {Object } req The request object.
63+ * @param {Object } allCredentials Stored credentials.
64+ * @returns {string|null } The resolved target URL.
65+ */
66+ function resolveTarget ( req , allCredentials ) {
67+ return (
68+ req . target ||
69+ process . env . HARPER_CLI_TARGET ||
70+ process . env . CLI_TARGET ||
71+ ( allCredentials && allCredentials . last_target )
72+ ) ;
73+ }
74+
5975/**
6076 * Using a unix domain socket will send a request to hdb operations API server
6177 * @param req
6278 * @returns {Promise<void> }
6379 */
64- async function cliOperations ( req ) {
65- if ( ! req . target ) {
66- req . target = process . env . HARPER_CLI_TARGET || process . env . CLI_TARGET ;
67- }
80+ async function cliOperations ( req , skipResponseLog = false ) {
81+ const allCredentials = loadCredentials ( ) ;
82+ req . target = resolveTarget ( req , allCredentials ) ;
6883 let target ;
6984 if ( req . target ) {
7085 try {
@@ -76,15 +91,17 @@ async function cliOperations(req) {
7691 throw error ;
7792 }
7893 }
94+ const resolvedTarget = `${ target . protocol } //${ target . hostname } ${ target . port ? ':' + target . port : '' } ` ;
7995 target = {
8096 protocol : target . protocol ,
8197 hostname : target . hostname ,
8298 port : target . port ,
8399 username : req . username || target . username || process . env . HARPER_CLI_USERNAME || process . env . CLI_TARGET_USERNAME ,
84100 password : req . password || target . password || process . env . HARPER_CLI_PASSWORD || process . env . CLI_TARGET_PASSWORD ,
85101 rejectUnauthorized : req . rejectUnauthorized ,
102+ resolvedTarget,
86103 } ;
87- console . error ( `Connecting to ${ target . protocol } // ${ target . hostname } : ${ target . port } ` ) ;
104+ console . error ( `Connecting to ${ resolvedTarget } ` ) ;
88105 } else {
89106 // if we aren't doing a targeted operation (like deploy), we initialize the config and verify that local harper
90107 // is running and that we can communicate with it.
@@ -110,6 +127,17 @@ async function cliOperations(req) {
110127 options . headers = { 'Content-Type' : 'application/json' } ;
111128 if ( target ?. username ) {
112129 options . headers . Authorization = `Basic ${ Buffer . from ( `${ target . username } :${ target . password } ` ) . toString ( 'base64' ) } ` ;
130+ } else if ( allCredentials ) {
131+ let tokens = null ;
132+ if ( target && allCredentials . targets && allCredentials . targets [ target . resolvedTarget ] ) {
133+ tokens = allCredentials . targets [ target . resolvedTarget ] ;
134+ } else if ( allCredentials . operation_token ) {
135+ tokens = allCredentials ;
136+ }
137+
138+ if ( tokens && tokens . operation_token ) {
139+ options . headers . Authorization = `Bearer ${ tokens . operation_token } ` ;
140+ }
113141 }
114142 if ( req . cborEncode ) {
115143 options . headers [ 'Content-Type' ] = 'application/cbor' ;
@@ -141,7 +169,13 @@ async function cliOperations(req) {
141169 process . exit ( 1 ) ;
142170 }
143171
144- console . log ( responseLog ) ;
172+ if ( ! skipResponseLog ) {
173+ console . log ( responseLog ) ;
174+ }
175+
176+ if ( target ) {
177+ responseData . resolvedTarget = target . resolvedTarget ;
178+ }
145179
146180 return responseData ;
147181 } catch ( err ) {
0 commit comments