1919
2020package org .apache .ranger .audit .destination ;
2121
22- import com .sun .jersey .api .client .ClientResponse ;
23- import com .sun .jersey .api .client .WebResource ;
2422import org .apache .commons .lang3 .StringUtils ;
2523import org .apache .hadoop .conf .Configuration ;
2624import org .apache .hadoop .security .UserGroupInformation ;
2725import org .apache .http .HttpStatus ;
2826import org .apache .ranger .audit .model .AuditEventBase ;
2927import org .apache .ranger .audit .model .AuthzAuditEvent ;
3028import org .apache .ranger .audit .provider .MiscUtil ;
29+ import org .apache .ranger .plugin .authn .DefaultJwtProvider ;
3130import org .apache .ranger .plugin .util .RangerRESTClient ;
3231import org .slf4j .Logger ;
3332import org .slf4j .LoggerFactory ;
3433
34+ import javax .ws .rs .client .Entity ;
35+ import javax .ws .rs .client .WebTarget ;
36+ import javax .ws .rs .core .MediaType ;
37+ import javax .ws .rs .core .Response ;
38+
3539import java .security .PrivilegedExceptionAction ;
3640import java .util .Collection ;
3741import java .util .HashMap ;
@@ -85,6 +89,10 @@ public void init(Properties props, String propPrefix) {
8589
8690 this .restClient = new RangerRESTClient (url , sslConfigFileName , config );
8791
92+ if (AUTH_TYPE_JWT .equalsIgnoreCase (authType )) {
93+ this .restClient .setJwtProvider (new DefaultJwtProvider ("ranger.plugin.policy.rest.client" , config ));
94+ }
95+
8896 this .restClient .setRestClientConnTimeOutMs (connTimeoutMs );
8997 this .restClient .setRestClientReadTimeOutMs (readTimeoutMs );
9098 this .restClient .setMaxRetryAttempts (maxRetryAttempts );
@@ -180,6 +188,8 @@ private boolean sendBatch(Collection<AuditEventBase> events, RangerRESTClient re
180188 queryParams .put (QUERY_PARAM_APP_ID , appId );
181189 }
182190
191+ Response response = null ;
192+
183193 try {
184194 final UserGroupInformation user = MiscUtil .getUGILoginUser ();
185195 final boolean isSecureMode = isKerberosAuthenticated ();
@@ -190,10 +200,8 @@ private boolean sendBatch(Collection<AuditEventBase> events, RangerRESTClient re
190200 LOG .debug ("Sending audit batch of {} events. SecureMode: {}, User: {}" , events .size (), isSecureMode , user != null ? user .getUserName () : "null" );
191201 }
192202
193- final ClientResponse response ;
194-
195203 if (isSecureMode ) {
196- response = MiscUtil .executePrivilegedAction ((PrivilegedExceptionAction <ClientResponse >) () -> {
204+ response = MiscUtil .executePrivilegedAction ((PrivilegedExceptionAction <Response >) () -> {
197205 try {
198206 return postAuditEvents (restClient , queryParams , events );
199207 } catch (Exception e ) {
@@ -210,7 +218,7 @@ private boolean sendBatch(Collection<AuditEventBase> events, RangerRESTClient re
210218
211219 if (status == HttpStatus .SC_OK ) {
212220 if (LOG .isDebugEnabled ()) {
213- LOG .debug ("Audit batch sent successfully. {} events delivered. Response: {}" , events .size (), response .getEntity (String .class ));
221+ LOG .debug ("Audit batch sent successfully. {} events delivered. Response: {}" , events .size (), response .readEntity (String .class ));
214222 }
215223
216224 ret = true ;
@@ -219,7 +227,7 @@ private boolean sendBatch(Collection<AuditEventBase> events, RangerRESTClient re
219227
220228 try {
221229 if (response .hasEntity ()) {
222- errorBody = response .getEntity (String .class );
230+ errorBody = response .readEntity (String .class );
223231 }
224232 } catch (Exception e ) {
225233 LOG .debug ("Failed to read error response body" , e );
@@ -242,27 +250,33 @@ private boolean sendBatch(Collection<AuditEventBase> events, RangerRESTClient re
242250 LOG .error ("Failed to send audit batch of {} events. Error: {}" , events .size (), e .getMessage (), e );
243251
244252 ret = false ;
253+ } finally {
254+ if (response != null ) {
255+ try {
256+ response .close ();
257+ } catch (Exception e ) {
258+ LOG .debug ("Error closing HTTP response" , e );
259+ }
260+ }
245261 }
246262
247263 return ret ;
248264 }
249265
250- private ClientResponse postAuditEvents (RangerRESTClient restClient , Map <String , String > params , Collection <AuditEventBase > events ) {
266+ private Response postAuditEvents (RangerRESTClient restClient , Map <String , String > params , Collection <AuditEventBase > events ) {
251267 LOG .debug ("Posting {} audit events to {}" , events .size (), REST_RELATIVE_PATH_POST );
252268
253- WebResource webResource = restClient .getResource (REST_RELATIVE_PATH_POST );
269+ WebTarget target = restClient .getResource (REST_RELATIVE_PATH_POST );
254270
255271 if (params != null && !params .isEmpty ()) {
256272 for (Map .Entry <String , String > entry : params .entrySet ()) {
257- webResource = webResource .queryParam (entry .getKey (), entry .getValue ());
273+ target = target .queryParam (entry .getKey (), entry .getValue ());
258274 }
259275 }
260276
261- return webResource
262- .accept ("application/json" )
263- .type ("application/json" )
264- .entity (events )
265- .post (ClientResponse .class );
277+ return target .request (MediaType .APPLICATION_JSON_TYPE )
278+ .accept (MediaType .APPLICATION_JSON_TYPE )
279+ .post (Entity .entity (events , MediaType .APPLICATION_JSON_TYPE ));
266280 }
267281
268282 private static Configuration createRESTClientConfiguration (Properties props , String propPrefix , String authType ) {
0 commit comments