@@ -9,12 +9,13 @@ module.exports = class AuditLog2RESTv2 extends AuditLogService {
9
9
// credentials stuff
10
10
const { credentials } = this . options
11
11
if ( ! credentials ) throw new Error ( 'No or malformed credentials for "audit-log"' )
12
- if ( credentials . uaa ) {
13
- this . _oauth2 = true
14
- this . _tokens = new Map ( )
15
- this . _providerTenant = credentials . uaa . tenantid
16
- } else {
12
+ if ( ! credentials . uaa ) {
13
+ this . _plan = 'standard'
17
14
this . _auth = 'Basic ' + Buffer . from ( credentials . user + ':' + credentials . password ) . toString ( 'base64' )
15
+ } else {
16
+ this . _plan = credentials . url . match ( / 6 0 8 1 / ) ? 'premium' : 'oauth2'
17
+ this . _tokens = new Map ( )
18
+ this . _provider = credentials . uaa . tenantid
18
19
}
19
20
this . _vcap = process . env . VCAP_APPLICATION ? JSON . parse ( process . env . VCAP_APPLICATION ) : null
20
21
@@ -49,21 +50,23 @@ module.exports = class AuditLog2RESTv2 extends AuditLogService {
49
50
const { _tokens : tokens } = this
50
51
if ( tokens . has ( tenant ) ) return tokens . get ( tenant )
51
52
52
- const url = this . options . credentials . uaa . url + '/oauth/token'
53
- const data = {
54
- grant_type : 'client_credentials' ,
55
- response_type : 'token' ,
56
- client_id : this . options . credentials . uaa . clientid ,
57
- client_secret : this . options . credentials . uaa . clientsecret
53
+ const { uaa } = this . options . credentials
54
+ const url = ( uaa . certurl || uaa . url ) + '/oauth/token'
55
+ const data = { grant_type : 'client_credentials' , response_type : 'token' , client_id : uaa . clientid }
56
+ const options = { headers : { 'content-type' : 'application/x-www-form-urlencoded' } }
57
+ if ( tenant !== this . _provider ) options . headers [ 'x-zid' ] = tenant
58
+ // certificate or secret?
59
+ if ( uaa [ 'credential-type' ] === 'x509' ) {
60
+ options . agent = new https . Agent ( { cert : uaa . certificate , key : uaa . key } )
61
+ } else {
62
+ data . client_secret = uaa . clientsecret
58
63
}
59
64
const urlencoded = Object . keys ( data ) . reduce ( ( acc , cur ) => {
60
65
acc += ( acc ? '&' : '' ) + cur + '=' + data [ cur ]
61
66
return acc
62
67
} , '' )
63
- const headers = { 'content-type' : 'application/x-www-form-urlencoded' }
64
- if ( tenant !== this . _providerTenant ) headers [ 'x-zid' ] = tenant
65
68
try {
66
- const { access_token, expires_in } = await _post ( url , urlencoded , headers )
69
+ const { access_token, expires_in } = await _post ( url , urlencoded , options )
67
70
tokens . set ( tenant , access_token )
68
71
// remove token from cache 60 seconds before it expires
69
72
setTimeout ( ( ) => tokens . delete ( tenant ) , ( expires_in - 60 ) * 1000 )
@@ -84,21 +87,21 @@ module.exports = class AuditLog2RESTv2 extends AuditLogService {
84
87
headers . XS_AUDIT_APP = this . _vcap . application_name
85
88
}
86
89
let url
87
- if ( this . _oauth2 ) {
88
- url = this . options . credentials . url + PATHS . OAUTH2 [ path ]
89
- data . tenant ??= this . _providerTenant //> if request has no tenant, stay in provider account
90
- headers . authorization = 'Bearer ' + ( await this . _getToken ( data . tenant ) )
91
- data . tenant = data . tenant === this . _providerTenant ? '$PROVIDER' : '$SUBSCRIBER'
92
- } else {
90
+ if ( this . _plan === 'standard' ) {
93
91
url = this . options . credentials . url + PATHS . STANDARD [ path ]
94
92
headers . authorization = this . _auth
93
+ } else {
94
+ url = this . options . credentials . url + PATHS . OAUTH2 [ path ]
95
+ data . tenant ??= this . _provider //> if request has no tenant, stay in provider account
96
+ headers . authorization = 'Bearer ' + ( await this . _getToken ( data . tenant ) )
97
+ data . tenant = data . tenant === this . _provider ? '$PROVIDER' : '$SUBSCRIBER'
95
98
}
96
99
if ( LOG . _debug ) {
97
100
const _headers = Object . assign ( { } , headers , { authorization : headers . authorization . split ( ' ' ) [ 0 ] + ' ***' } )
98
101
LOG . debug ( `sending audit log to ${ url } with tenant "${ data . tenant } ", user "${ data . user } ", and headers` , _headers )
99
102
}
100
103
try {
101
- await _post ( url , data , headers )
104
+ await _post ( url , data , { headers } )
102
105
} catch ( err ) {
103
106
LOG . _trace && LOG . trace ( 'error during log send:' , err )
104
107
// 429 (rate limit) is not unrecoverable
@@ -143,9 +146,10 @@ const PATHS = {
143
146
144
147
const https = require ( 'https' )
145
148
146
- async function _post ( url , data , headers ) {
149
+ async function _post ( url , data , options ) {
150
+ options . method ??= 'POST'
147
151
return new Promise ( ( resolve , reject ) => {
148
- const req = https . request ( url , { method : 'POST' , headers } , res => {
152
+ const req = https . request ( url , options , res => {
149
153
const chunks = [ ]
150
154
res . on ( 'data' , chunk => chunks . push ( chunk ) )
151
155
res . on ( 'end' , ( ) => {
0 commit comments