@@ -55,10 +55,7 @@ CertLogic.prototype.checkCertWithPassword = function(options, callback) {
5555 this . managementUri , options . org , options . env , this . vaultName ) ;
5656 request ( {
5757 uri : uri ,
58- auth : {
59- username : options . username ,
60- password : options . password
61- }
58+ auth : generateCredentialsObject ( options )
6259 } , function ( err , res , body ) {
6360 err = translateError ( err , res ) ;
6461 if ( err ) {
@@ -77,10 +74,7 @@ CertLogic.prototype.checkPrivateCert = function(options, callback) {
7774
7875 request ( {
7976 uri : uri ,
80- auth : {
81- username : options . username ,
82- password : options . password
83- }
77+ auth : generateCredentialsObject ( options )
8478 } , function ( err , res ) {
8579 err = translateError ( err , res ) ;
8680 if ( err ) {
@@ -108,45 +102,38 @@ CertLogic.prototype.installPrivateCert = function(options, callback) {
108102 const privateKey = keys . serviceKey ;
109103 const publicKey = keys . certificate ;
110104 const async = require ( 'async' ) ;
111-
112- pem . getPublicKey ( publicKey , function ( err , key ) {
113- async . series (
114- [
115- function ( cb ) {
116- if ( ! options . force ) { return cb ( ) ; }
117- deleteVault ( options . username , options . password , managementUri , options . org , options . env , vaultName , cb ) ;
118- } ,
119- function ( cb ) {
120- console . log ( 'creating vault' ) ;
121- console . log ( 'adding private_key' ) ;
122- console . log ( 'adding public_key' ) ;
123- var entries = [
124- {
125- 'name' :'private_key' ,
126- 'value' : privateKey
127- } ,
128- {
129- 'name' : 'public_key' ,
130- 'value' : publicKey
131- } ,
132- {
133- 'name' : 'public_key1' ,
134- 'value' : key . publicKey
135- }
136- ]
137- createVault ( options . username , options . password , managementUri , options . org , options . env , vaultName , entries , cb ) ;
138- }
139- ] ,
140- function ( err ) {
141- if ( err ) {
142- callback ( err ) ;
143- } else {
144- callback ( null , publicKey ) ;
145- }
105+ async . series (
106+ [
107+ function ( cb ) {
108+ if ( ! options . force ) { return cb ( ) ; }
109+ deleteVault ( generateCredentialsObject ( options ) , managementUri , options . org , options . env , vaultName , cb ) ;
110+ } ,
111+ function ( cb ) {
112+ console . log ( 'creating vault' ) ;
113+ console . log ( 'adding private_key' ) ;
114+ console . log ( 'adding public_key' ) ;
115+ var entries = [
116+ {
117+ 'name' :'private_key' ,
118+ 'value' : privateKey
119+ } ,
120+ {
121+ 'name' : 'public_key' ,
122+ 'value' : publicKey
123+ }
124+ ]
125+ createVault ( generateCredentialsObject ( options ) , managementUri , options . org , options . env , vaultName , entries , cb ) ;
146126 }
127+ ] ,
128+ function ( err ) {
129+ if ( err ) {
130+ callback ( err ) ;
131+ } else {
132+ callback ( null , publicKey ) ;
133+ }
134+ }
147135 ) ;
148136 } ) ;
149- } ) ;
150137}
151138
152139CertLogic . prototype . installCertWithPassword = function ( options , callback ) {
@@ -162,45 +149,38 @@ CertLogic.prototype.installCertWithPassword = function(options, callback) {
162149 const publicKey = keys . certificate ;
163150
164151 const async = require ( 'async' ) ;
165-
166- pem . getPublicKey ( publicKey , function ( err , key ) {
167- async . series (
168- [
169- function ( cb ) {
170- if ( ! options . force ) { return cb ( ) ; }
171- deleteVault ( options . username , options . password , managementUri , options . org , options . env , vaultName , cb ) ;
172- } ,
173- function ( cb ) {
174- console . log ( 'creating vault' ) ;
175- console . log ( 'adding private_key' ) ;
176- console . log ( 'adding public_key' ) ;
177- var entries = [
178- {
179- 'name' :'private_key' ,
180- 'value' : privateKey
181- } ,
182- {
183- 'name' : 'public_key' ,
184- 'value' : publicKey
185- } ,
186- {
187- 'name' : 'public_key1' ,
188- 'value' : key . publicKey
189- }
190- ]
191- createVault ( options . username , options . password , managementUri , options . org , options . env , vaultName , entries , cb ) ;
192- }
193- ] ,
194- function ( err ) {
195- if ( err ) {
196- callback ( err ) ;
197- } else {
198- callback ( null , publicKey ) ;
199- }
152+ async . series (
153+ [
154+ function ( cb ) {
155+ if ( ! options . force ) { return cb ( ) ; }
156+ deleteVault ( generateCredentialsObject ( options ) , managementUri , options . org , options . env , vaultName , cb ) ;
157+ } ,
158+ function ( cb ) {
159+ console . log ( 'creating vault' ) ;
160+ console . log ( 'adding private_key' ) ;
161+ console . log ( 'adding public_key' ) ;
162+ var entries = [
163+ {
164+ 'name' :'private_key' ,
165+ 'value' : privateKey
166+ } ,
167+ {
168+ 'name' : 'public_key' ,
169+ 'value' : publicKey
170+ }
171+ ]
172+ createVault ( generateCredentialsObject ( options ) , managementUri , options . org , options . env , vaultName , entries , cb ) ;
173+ }
174+ ] ,
175+ function ( err ) {
176+ if ( err ) {
177+ callback ( err ) ;
178+ } else {
179+ callback ( null , publicKey ) ;
200180 }
181+ }
201182 ) ;
202183 } ) ;
203- } ) ;
204184}
205185
206186
@@ -239,10 +219,7 @@ CertLogic.prototype.generateKeysWithPassword = function generateKeysWithPassword
239219 request ( {
240220 uri : credentialUrl ,
241221 method : 'POST' ,
242- auth : {
243- username : options . username ,
244- password : options . password
245- } ,
222+ auth : generateCredentialsObject ( options ) ,
246223 json : keys
247224 } , function ( err , res ) {
248225 err = translateError ( err , res ) ;
@@ -308,7 +285,7 @@ CertLogic.prototype.deleteCertWithPassword = function deleteCertWithPassword(opt
308285 const managementUri = this . managementUri ;
309286 const vaultName = this . vaultName ;
310287
311- deleteVault ( options . username , options . password , managementUri , options . org , options . env , vaultName , function ( err ) {
288+ deleteVault ( generateCredentialsObject ( options ) , managementUri , options . org , options . env , vaultName , function ( err ) {
312289 if ( err ) {
313290 cb ( err ) ;
314291 } else {
@@ -336,18 +313,15 @@ function createCert(cb) {
336313 pem . createCertificate ( options , cb ) ;
337314}
338315
339- function deleteVault ( username , password , managementUri , organization , environment , vaultName , cb ) {
316+ function deleteVault ( credentials , managementUri , organization , environment , vaultName , cb ) {
340317 console . log ( 'deleting vault' ) ;
341318
342319 var uri = util . format ( '%s/v1/organizations/%s/environments/%s/keyvaluemaps/%s' , managementUri , organization , environment , vaultName ) ;
343320
344321 request ( {
345322 uri : uri ,
346323 method : 'DELETE' ,
347- auth : {
348- username : username ,
349- password : password
350- }
324+ auth : credentials
351325 } , function ( err , res ) {
352326 err = translateError ( err , res ) ;
353327 if ( isApigeeError ( err , ERR_STORE_MISSING ) ) {
@@ -361,7 +335,7 @@ function deleteVault(username, password, managementUri, organization, environmen
361335
362336}
363337
364- function createVault ( username , password , managementUri , organization , environment , vaultName , entries , cb ) {
338+ function createVault ( credentials , managementUri , organization , environment , vaultName , entries , cb ) {
365339
366340 var storageOpts = {
367341 name : vaultName ,
@@ -373,10 +347,7 @@ function createVault(username, password, managementUri, organization, environmen
373347 request ( {
374348 uri : uri ,
375349 method : 'POST' ,
376- auth : {
377- username : username ,
378- password : password
379- } ,
350+ auth : credentials ,
380351 json : storageOpts
381352 } , function ( err , res ) {
382353 err = translateError ( err , res ) ;
@@ -436,3 +407,15 @@ function getPublicKeyPrivate(authUri, cb) {
436407 } ) ;
437408}
438409
410+ function generateCredentialsObject ( options ) {
411+ if ( options . token ) {
412+ return {
413+ 'bearer' : options . token
414+ } ;
415+ } else {
416+ return {
417+ user : options . username ,
418+ pass : options . password
419+ } ;
420+ }
421+ }
0 commit comments