Skip to content
This repository was archived by the owner on Dec 18, 2020. It is now read-only.

Commit 7a1fbdc

Browse files
authored
Add debug logging to help with troubleshooting (#5)
Adds additional logging to help troubleshoot the vault client.
1 parent 60d5bc4 commit 7a1fbdc

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
# limitations under the License.
1515
#
1616

17-
version=1.1.1
17+
version=1.2.0
1818
groupId=com.nike
1919
artifactId=vault-client

src/main/java/com/nike/vault/client/VaultClient.java

+31
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import okhttp3.RequestBody;
3636
import okhttp3.Response;
3737
import org.apache.commons.lang3.StringUtils;
38+
import org.slf4j.Logger;
39+
import org.slf4j.LoggerFactory;
3840

3941
import javax.net.ssl.SSLException;
4042
import java.io.IOException;
@@ -65,6 +67,8 @@ public class VaultClient {
6567
.disableHtmlEscaping()
6668
.create();
6769

70+
private final Logger logger = LoggerFactory.getLogger(getClass());
71+
6872
/**
6973
* Explicit constructor that allows for full control over construction of the Vault client.
7074
*
@@ -107,6 +111,8 @@ public VaultClient(final UrlResolver vaultUrlResolver,
107111
*/
108112
public VaultListResponse list(final String path) {
109113
final HttpUrl url = buildUrl(SECRET_PATH_PREFIX, path + "?list=true");
114+
logger.debug("list: requestUrl={}", url);
115+
110116
final Response response = execute(url, HttpMethod.GET, null);
111117

112118
if (response.code() == HttpStatus.NOT_FOUND) {
@@ -132,6 +138,8 @@ public VaultListResponse list(final String path) {
132138
*/
133139
public VaultResponse read(final String path) {
134140
final HttpUrl url = buildUrl(SECRET_PATH_PREFIX, path);
141+
logger.debug("read: requestUrl={}", url);
142+
135143
final Response response = execute(url, HttpMethod.GET, null);
136144

137145
if (response.code() != HttpStatus.OK) {
@@ -151,6 +159,8 @@ public VaultResponse read(final String path) {
151159
*/
152160
public void write(final String path, final Map<String, String> data) {
153161
final HttpUrl url = buildUrl(SECRET_PATH_PREFIX, path);
162+
logger.debug("write: requestUrl={}", url);
163+
154164
final Response response = execute(url, HttpMethod.POST, data);
155165

156166
if (response.code() != HttpStatus.NO_CONTENT) {
@@ -167,6 +177,8 @@ public void write(final String path, final Map<String, String> data) {
167177
*/
168178
public void delete(final String path) {
169179
final HttpUrl url = buildUrl(SECRET_PATH_PREFIX, path);
180+
logger.debug("delete: requestUrl={}", url);
181+
170182
final Response response = execute(url, HttpMethod.DELETE, null);
171183

172184
if (response.code() != HttpStatus.NO_CONTENT) {
@@ -183,6 +195,8 @@ public void delete(final String path) {
183195
*/
184196
public VaultClientTokenResponse lookupSelf() {
185197
final HttpUrl url = buildUrl(AUTH_PATH_PREFIX, "token/lookup-self");
198+
logger.debug("lookupSelf: requestUrl={}", url);
199+
186200
final Response response = execute(url, HttpMethod.GET, null);
187201

188202
if (response.code() != HttpStatus.OK) {
@@ -287,6 +301,8 @@ protected <M> M parseResponseBody(final Response response, final Class<M> respon
287301
try {
288302
return gson.fromJson(response.body().string(), responseClass);
289303
} catch (IOException|JsonSyntaxException e) {
304+
logger.error("parseResponseBody: responseCode={}, requestUrl={}, response={}",
305+
response.code(), response.request().url(), responseBodyAsString(response));
290306
throw new VaultClientException("Error parsing the response body from vault, response code: " + response.code(), e);
291307
}
292308
}
@@ -303,6 +319,8 @@ protected <M> M parseResponseBody(final Response response, final Type typeOf) {
303319
try {
304320
return gson.fromJson(response.body().string(), typeOf);
305321
} catch (IOException|JsonSyntaxException e) {
322+
logger.error("parseResponseBody: responseCode={}, requestUrl={}, response={}",
323+
response.code(), response.request().url(), responseBodyAsString(response));
306324
throw new VaultClientException("Error parsing the response body from vault, response code: " + response.code(), e);
307325
}
308326
}
@@ -313,6 +331,9 @@ protected <M> M parseResponseBody(final Response response, final Type typeOf) {
313331
* @param response Response to parses the error details from
314332
*/
315333
protected void parseAndThrowErrorResponse(final Response response) {
334+
logger.debug("parseAndThrowErrorResponse: responseCode={}, requestUrl={}, response={}",
335+
response.code(), response.request().url(), responseBodyAsString(response));
336+
316337
try {
317338
ErrorResponse errorResponse = gson.fromJson(response.body().string(), ErrorResponse.class);
318339

@@ -322,6 +343,7 @@ protected void parseAndThrowErrorResponse(final Response response) {
322343
throw new VaultServerException(response.code(), new LinkedList<String>());
323344
}
324345
} catch (IOException|JsonSyntaxException e) {
346+
logger.error("ERROR Failed to parse error message, response body received: {}", responseBodyAsString(response));
325347
throw new VaultClientException("Error parsing the error response body from vault, response code: " + response.code(), e);
326348
}
327349
}
@@ -336,4 +358,13 @@ public List<String> getErrors() {
336358
return errors;
337359
}
338360
}
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+
}
339370
}

0 commit comments

Comments
 (0)