From 41f8b4964b13bb9be1aa6270ad7777fde1bc9c44 Mon Sep 17 00:00:00 2001 From: Nikos Linakis Date: Mon, 8 Jan 2018 14:59:44 +0200 Subject: [PATCH 1/3] Use proper retrofit2 async execution --- .../tony19/loggly/ILogglyRestService.java | 21 ------- .../github/tony19/loggly/LogglyClient.java | 60 +++++++++---------- 2 files changed, 29 insertions(+), 52 deletions(-) diff --git a/loggly-client/src/main/java/com/github/tony19/loggly/ILogglyRestService.java b/loggly-client/src/main/java/com/github/tony19/loggly/ILogglyRestService.java index b48593c..b571c5d 100644 --- a/loggly-client/src/main/java/com/github/tony19/loggly/ILogglyRestService.java +++ b/loggly-client/src/main/java/com/github/tony19/loggly/ILogglyRestService.java @@ -16,7 +16,6 @@ package com.github.tony19.loggly; import retrofit2.Call; -import retrofit2.Callback; import retrofit2.http.Body; import retrofit2.http.Header; import retrofit2.http.POST; @@ -40,15 +39,6 @@ @POST("inputs/{token}") Call log(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body String message); - /** - * Posts a single log event to Loggly's REST endpoint - * @param token Loggly customer token - * @param tags CSV of tags - * @param message log event to be posted - * @param callback callback to be invoked on completion of the post - */ - @POST("inputs/{token}") - Call log(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body String message, Callback callback); /** * Posts several log events at once to Loggly's bulk REST endpoint @@ -62,15 +52,4 @@ @POST("bulk/{token}") Call logBulk(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body String messages); - /** - * Posts several log events at once to Loggly's bulk REST endpoint - * @param token Loggly customer token - * @param tags CSV of tags - * @param messages log event messages, each delimited by new-line - * The text is parsed for a log event in each line. - * e.g., "Hello\nWorld" would create two log events. - * @param callback callback to be invoked on completion of the post - */ - @POST("bulk/{token}") - Call logBulk(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body String messages, Callback callback); } diff --git a/loggly-client/src/main/java/com/github/tony19/loggly/LogglyClient.java b/loggly-client/src/main/java/com/github/tony19/loggly/LogglyClient.java index bab4f33..c644f70 100644 --- a/loggly-client/src/main/java/com/github/tony19/loggly/LogglyClient.java +++ b/loggly-client/src/main/java/com/github/tony19/loggly/LogglyClient.java @@ -15,14 +15,14 @@ */ package com.github.tony19.loggly; +import java.util.Arrays; +import java.util.Collection; + import retrofit2.Call; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; -import java.util.Arrays; -import java.util.Collection; - /** * Loggly client * @@ -116,20 +116,18 @@ public boolean log(String message) { public void log(String message, final Callback callback) { if (message == null) return; - loggly.log(token, - tags, - message, - new retrofit2.Callback() { - @Override - public void onResponse(Call call, Response response) { - callback.success(); - } - - @Override - public void onFailure(Call call, Throwable throwable) { - callback.failure(throwable.getMessage()); - } - }); + Call call = loggly.log(token, tags, message); + call.enqueue(new retrofit2.Callback() { + @Override + public void onResponse(Call call, Response response) { + callback.success(); + } + + @Override + public void onFailure(Call call, Throwable throwable) { + callback.failure(throwable.getMessage()); + } + }); } /** @@ -171,23 +169,23 @@ public boolean logBulk(Collection messages) { public void logBulk(Collection messages, final Callback callback) { if (messages == null) return; + + String parcel = joinStrings(messages); if (parcel.isEmpty()) return; - loggly.logBulk(token, - tags, - parcel, - new retrofit2.Callback() { - @Override - public void onResponse(Call call, Response response) { - callback.success(); - } - - @Override - public void onFailure(Call call, Throwable throwable) { - callback.failure(throwable.getMessage()); - } - }); + Call call = loggly.logBulk(token, tags, parcel); + call.enqueue(new retrofit2.Callback() { + @Override + public void onResponse(Call call, Response response) { + callback.success(); + } + + @Override + public void onFailure(Call call, Throwable throwable) { + callback.failure(throwable.getMessage()); + } + }); } /** From 51d44ee86932e6fd92826c7d0c0cf37eaa3fcb8f Mon Sep 17 00:00:00 2001 From: Nikos Linakis Date: Wed, 10 Oct 2018 17:41:20 +0300 Subject: [PATCH 2/3] Sanitize tags from invalid characters --- .../github/tony19/loggly/LogglyClient.java | 22 ++++++++++++++++++- .../tony19/loggly/LogglyClientTest.java | 21 +++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/loggly-client/src/main/java/com/github/tony19/loggly/LogglyClient.java b/loggly-client/src/main/java/com/github/tony19/loggly/LogglyClient.java index c644f70..cb52cab 100644 --- a/loggly-client/src/main/java/com/github/tony19/loggly/LogglyClient.java +++ b/loggly-client/src/main/java/com/github/tony19/loggly/LogglyClient.java @@ -80,7 +80,7 @@ public void setTags(String... tags) { if (!first) { builder.append(","); } - builder.append(t); + builder.append(sanitizeTag(t)); } first = false; } @@ -210,4 +210,24 @@ private String joinStrings(Collection messages) { } return b.toString(); } + + /** + * Sanitize the tag based on the restrictions described in + * https://www.loggly.com/docs/tags/. + * Sanitation works by replacing invalid characters with the _ (underscore) character. + * + * @param tag tag to be sanitized + * @return the tag without invalid characters + */ + private String sanitizeTag(String tag) { + // replace invalid characters with _ + tag = tag.replaceAll("[^A-Za-z0-9_*,.\\-]", "_"); + + // don't allow non-alphanumeric values starting the tag + if (Character.isLetterOrDigit(tag.charAt(0))) { + return tag; + } + + return tag.substring(1); + } } diff --git a/loggly-client/src/test/java/com/github/tony19/loggly/LogglyClientTest.java b/loggly-client/src/test/java/com/github/tony19/loggly/LogglyClientTest.java index f694a8b..6a88f0d 100644 --- a/loggly-client/src/test/java/com/github/tony19/loggly/LogglyClientTest.java +++ b/loggly-client/src/test/java/com/github/tony19/loggly/LogglyClientTest.java @@ -15,18 +15,19 @@ */ package com.github.tony19.loggly; -import retrofit2.Call; import org.junit.Before; import org.junit.Rule; -import org.junit.rules.ExpectedException; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; -import static org.junit.Assert.assertThat; +import retrofit2.Call; + import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.isNull; import static org.mockito.Mockito.mock; @@ -138,4 +139,18 @@ public void emptyTagsResultInNoTags() { loggly.logBulk("event"); Mockito.verify(restApi).logBulk(TOKEN, NO_TAGS, "event\n"); } + + @Test + public void invalidTagsResultInNoTags() { + loggly.setTags("", " ", " ,", ", , ,, "); + loggly.logBulk("event"); + Mockito.verify(restApi).logBulk(TOKEN, NO_TAGS, "event\n"); + } + + @Test + public void invalidTagsAreSentToLogglySanitized() { + loggly.setTags("_startInvalid", "middle@invalid.com", "%how_many$*3"); + loggly.logBulk("event"); + Mockito.verify(restApi).logBulk(TOKEN, "startInvalid,middle_invalid.com,how_many_*3", "event\n"); + } } From 97bdc0d083b7c4ff80855b6e8d35b844e37c9efd Mon Sep 17 00:00:00 2001 From: Nikos Linakis Date: Wed, 10 Oct 2018 17:47:10 +0300 Subject: [PATCH 3/3] sanitize tags from invalid characters --- .../github/tony19/loggly/LogglyClient.java | 22 ++++++++++++++++++- .../tony19/loggly/LogglyClientTest.java | 21 +++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/loggly-client/src/main/java/com/github/tony19/loggly/LogglyClient.java b/loggly-client/src/main/java/com/github/tony19/loggly/LogglyClient.java index bab4f33..9a43def 100644 --- a/loggly-client/src/main/java/com/github/tony19/loggly/LogglyClient.java +++ b/loggly-client/src/main/java/com/github/tony19/loggly/LogglyClient.java @@ -80,7 +80,7 @@ public void setTags(String... tags) { if (!first) { builder.append(","); } - builder.append(t); + builder.append(sanitizeTag(t)); } first = false; } @@ -212,4 +212,24 @@ private String joinStrings(Collection messages) { } return b.toString(); } + + /** + * Sanitize the tag based on the restrictions described in + * https://www.loggly.com/docs/tags/. + * Sanitation works by replacing invalid characters with the _ (underscore) character. + * + * @param tag tag to be sanitized + * @return the tag without invalid characters + */ + private String sanitizeTag(String tag) { + // replace invalid characters with _ + tag = tag.replaceAll("[^A-Za-z0-9_*,.\\-]", "_"); + + // don't allow non-alphanumeric values starting the tag + if (Character.isLetterOrDigit(tag.charAt(0))) { + return tag; + } + + return tag.substring(1); + } } diff --git a/loggly-client/src/test/java/com/github/tony19/loggly/LogglyClientTest.java b/loggly-client/src/test/java/com/github/tony19/loggly/LogglyClientTest.java index f694a8b..6a88f0d 100644 --- a/loggly-client/src/test/java/com/github/tony19/loggly/LogglyClientTest.java +++ b/loggly-client/src/test/java/com/github/tony19/loggly/LogglyClientTest.java @@ -15,18 +15,19 @@ */ package com.github.tony19.loggly; -import retrofit2.Call; import org.junit.Before; import org.junit.Rule; -import org.junit.rules.ExpectedException; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; -import static org.junit.Assert.assertThat; +import retrofit2.Call; + import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.isNull; import static org.mockito.Mockito.mock; @@ -138,4 +139,18 @@ public void emptyTagsResultInNoTags() { loggly.logBulk("event"); Mockito.verify(restApi).logBulk(TOKEN, NO_TAGS, "event\n"); } + + @Test + public void invalidTagsResultInNoTags() { + loggly.setTags("", " ", " ,", ", , ,, "); + loggly.logBulk("event"); + Mockito.verify(restApi).logBulk(TOKEN, NO_TAGS, "event\n"); + } + + @Test + public void invalidTagsAreSentToLogglySanitized() { + loggly.setTags("_startInvalid", "middle@invalid.com", "%how_many$*3"); + loggly.logBulk("event"); + Mockito.verify(restApi).logBulk(TOKEN, "startInvalid,middle_invalid.com,how_many_*3", "event\n"); + } }