35
35
import okhttp3 .RequestBody ;
36
36
import okhttp3 .Response ;
37
37
import org .apache .commons .lang3 .StringUtils ;
38
+ import org .slf4j .Logger ;
39
+ import org .slf4j .LoggerFactory ;
38
40
39
41
import javax .net .ssl .SSLException ;
40
42
import java .io .IOException ;
@@ -65,6 +67,8 @@ public class VaultClient {
65
67
.disableHtmlEscaping ()
66
68
.create ();
67
69
70
+ private final Logger logger = LoggerFactory .getLogger (getClass ());
71
+
68
72
/**
69
73
* Explicit constructor that allows for full control over construction of the Vault client.
70
74
*
@@ -107,6 +111,8 @@ public VaultClient(final UrlResolver vaultUrlResolver,
107
111
*/
108
112
public VaultListResponse list (final String path ) {
109
113
final HttpUrl url = buildUrl (SECRET_PATH_PREFIX , path + "?list=true" );
114
+ logger .debug ("list: requestUrl={}" , url );
115
+
110
116
final Response response = execute (url , HttpMethod .GET , null );
111
117
112
118
if (response .code () == HttpStatus .NOT_FOUND ) {
@@ -132,6 +138,8 @@ public VaultListResponse list(final String path) {
132
138
*/
133
139
public VaultResponse read (final String path ) {
134
140
final HttpUrl url = buildUrl (SECRET_PATH_PREFIX , path );
141
+ logger .debug ("read: requestUrl={}" , url );
142
+
135
143
final Response response = execute (url , HttpMethod .GET , null );
136
144
137
145
if (response .code () != HttpStatus .OK ) {
@@ -151,6 +159,8 @@ public VaultResponse read(final String path) {
151
159
*/
152
160
public void write (final String path , final Map <String , String > data ) {
153
161
final HttpUrl url = buildUrl (SECRET_PATH_PREFIX , path );
162
+ logger .debug ("write: requestUrl={}" , url );
163
+
154
164
final Response response = execute (url , HttpMethod .POST , data );
155
165
156
166
if (response .code () != HttpStatus .NO_CONTENT ) {
@@ -167,6 +177,8 @@ public void write(final String path, final Map<String, String> data) {
167
177
*/
168
178
public void delete (final String path ) {
169
179
final HttpUrl url = buildUrl (SECRET_PATH_PREFIX , path );
180
+ logger .debug ("delete: requestUrl={}" , url );
181
+
170
182
final Response response = execute (url , HttpMethod .DELETE , null );
171
183
172
184
if (response .code () != HttpStatus .NO_CONTENT ) {
@@ -183,6 +195,8 @@ public void delete(final String path) {
183
195
*/
184
196
public VaultClientTokenResponse lookupSelf () {
185
197
final HttpUrl url = buildUrl (AUTH_PATH_PREFIX , "token/lookup-self" );
198
+ logger .debug ("lookupSelf: requestUrl={}" , url );
199
+
186
200
final Response response = execute (url , HttpMethod .GET , null );
187
201
188
202
if (response .code () != HttpStatus .OK ) {
@@ -287,6 +301,8 @@ protected <M> M parseResponseBody(final Response response, final Class<M> respon
287
301
try {
288
302
return gson .fromJson (response .body ().string (), responseClass );
289
303
} catch (IOException |JsonSyntaxException e ) {
304
+ logger .error ("parseResponseBody: responseCode={}, requestUrl={}, response={}" ,
305
+ response .code (), response .request ().url (), responseBodyAsString (response ));
290
306
throw new VaultClientException ("Error parsing the response body from vault, response code: " + response .code (), e );
291
307
}
292
308
}
@@ -303,6 +319,8 @@ protected <M> M parseResponseBody(final Response response, final Type typeOf) {
303
319
try {
304
320
return gson .fromJson (response .body ().string (), typeOf );
305
321
} catch (IOException |JsonSyntaxException e ) {
322
+ logger .error ("parseResponseBody: responseCode={}, requestUrl={}, response={}" ,
323
+ response .code (), response .request ().url (), responseBodyAsString (response ));
306
324
throw new VaultClientException ("Error parsing the response body from vault, response code: " + response .code (), e );
307
325
}
308
326
}
@@ -313,6 +331,9 @@ protected <M> M parseResponseBody(final Response response, final Type typeOf) {
313
331
* @param response Response to parses the error details from
314
332
*/
315
333
protected void parseAndThrowErrorResponse (final Response response ) {
334
+ logger .debug ("parseAndThrowErrorResponse: responseCode={}, requestUrl={}, response={}" ,
335
+ response .code (), response .request ().url (), responseBodyAsString (response ));
336
+
316
337
try {
317
338
ErrorResponse errorResponse = gson .fromJson (response .body ().string (), ErrorResponse .class );
318
339
@@ -322,6 +343,7 @@ protected void parseAndThrowErrorResponse(final Response response) {
322
343
throw new VaultServerException (response .code (), new LinkedList <String >());
323
344
}
324
345
} catch (IOException |JsonSyntaxException e ) {
346
+ logger .error ("ERROR Failed to parse error message, response body received: {}" , responseBodyAsString (response ));
325
347
throw new VaultClientException ("Error parsing the error response body from vault, response code: " + response .code (), e );
326
348
}
327
349
}
@@ -336,4 +358,13 @@ public List<String> getErrors() {
336
358
return errors ;
337
359
}
338
360
}
361
+
362
+ protected String responseBodyAsString (Response response ) {
363
+ try {
364
+ return response .body ().string ();
365
+ } catch (IOException ioe ) {
366
+ logger .debug ("responseBodyAsString: response={}" , gson .toJson (response ));
367
+ return "ERROR failed to print response body as str: " + ioe .getMessage ();
368
+ }
369
+ }
339
370
}
0 commit comments