Skip to content

Commit 0f344f8

Browse files
authored
Merge pull request #1181 from microsoft/littleaj/fix_constr_uribuilder
Fix uribuilder in connection string parsing
2 parents 7b01f10 + 42ad303 commit 0f344f8

File tree

3 files changed

+89
-15
lines changed

3 files changed

+89
-15
lines changed

core/src/main/java/com/microsoft/applicationinsights/internal/config/connection/ConnectionString.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.microsoft.applicationinsights.TelemetryConfiguration;
77
import com.microsoft.applicationinsights.internal.logger.InternalLogger;
88
import org.apache.commons.lang3.StringUtils;
9-
import org.apache.http.client.utils.URIBuilder;
109

1110
import java.net.URI;
1211
import java.net.URISyntaxException;
@@ -81,16 +80,17 @@ private static void mapToConnectionConfiguration(Map<String, String> kvps, Telem
8180
if (!Strings.isNullOrEmpty(snapshotEndpoint)) {
8281
config.getEndpointProvider().setSnapshotEndpoint(toUriOrThrow(snapshotEndpoint, Keywords.SNAPSHOT_ENDPOINT));
8382
}
84-
8583
}
8684

85+
8786
private static URI toUriOrThrow(String uri, String field) throws InvalidConnectionStringException {
8887
try {
89-
final URIBuilder builder = new URIBuilder(uri);
90-
if (Strings.isNullOrEmpty(builder.getScheme())) {
91-
builder.setScheme("https");
88+
URI result = new URI(uri);
89+
final String scheme = result.getScheme();
90+
if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
91+
throw new InvalidConnectionStringException(field+" must specify supported protocol, either 'http' or 'https': \""+uri+"\"");
9292
}
93-
return builder.build();
93+
return result;
9494
} catch (URISyntaxException e) {
9595
throw new InvalidConnectionStringException(field + " is invalid: \"" + uri + "\"", e);
9696
}

core/src/main/java/com/microsoft/applicationinsights/internal/config/connection/EndpointProvider.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
import com.google.common.annotations.VisibleForTesting;
44
import com.microsoft.applicationinsights.internal.config.connection.ConnectionString.Defaults;
5-
import org.apache.http.client.utils.URIBuilder;
65

76
import java.net.URI;
87
import java.net.URISyntaxException;
9-
import java.util.concurrent.atomic.AtomicReference;
108

119
public class EndpointProvider {
1210
@VisibleForTesting static final String INGESTION_URI_PATH = "v2/track";
@@ -33,25 +31,42 @@ public EndpointProvider() {
3331
}
3432

3533
private URI buildIngestionUri(URI baseUri) throws URISyntaxException {
36-
return new URIBuilder(baseUri).setPath(INGESTION_URI_PATH).build();
34+
return buildUri(baseUri, INGESTION_URI_PATH);
3735
}
3836

3937
private URI buildLiveUri(URI baseUri) throws URISyntaxException {
40-
return new URIBuilder(baseUri).setPath(LIVE_URI_PATH).build();
38+
return buildUri(baseUri, LIVE_URI_PATH);
4139
}
4240

4341
public URI getIngestionEndpointURL() {
4442
return ingestionEndpointURL;
4543
}
4644

47-
public URI getAppIdEndpointURL(String instrumentationKey) {
45+
public synchronized URI getAppIdEndpointURL(String instrumentationKey) {
46+
return buildAppIdUri(instrumentationKey);
47+
}
48+
49+
private URI buildAppIdUri(String instrumentationKey) {
4850
try {
49-
return new URIBuilder(ingestionEndpoint).setPath(API_PROFILES_APP_ID_URI_PREFIX +instrumentationKey+ API_PROFILES_APP_ID_URI_SUFFIX).build();
51+
return buildUri(ingestionEndpoint, API_PROFILES_APP_ID_URI_PREFIX +instrumentationKey+ API_PROFILES_APP_ID_URI_SUFFIX);
5052
} catch (URISyntaxException e) {
5153
throw new IllegalArgumentException("Invalid instrumentationKey: "+instrumentationKey);
5254
}
5355
}
5456

57+
URI buildUri(URI baseUri, String appendPath) throws URISyntaxException {
58+
String uriString = baseUri.toString();
59+
if (!uriString.endsWith("/")) {
60+
uriString = uriString + "/";
61+
}
62+
63+
if (appendPath.startsWith("/")) {
64+
appendPath = appendPath.substring(1);
65+
}
66+
67+
return new URI(uriString + appendPath);
68+
}
69+
5570
public URI getIngestionEndpoint() {
5671
return ingestionEndpoint;
5772
}

core/src/test/java/com/microsoft/applicationinsights/internal/config/connection/ConnectionStringParsingTests.java

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ public void appIdUrlIsConstructedWithIkeyFromIngestionEndpoint() {
5454
assertEquals(URI.create(host+"/"+EndpointProvider.API_PROFILES_APP_ID_URI_PREFIX+ikey+EndpointProvider.API_PROFILES_APP_ID_URI_SUFFIX), ep.getAppIdEndpointURL(ikey));
5555
}
5656

57+
@Test
58+
public void appIdUrlWithPathKeepsIt() {
59+
EndpointProvider ep = new EndpointProvider();
60+
String ikey = "fake-ikey";
61+
String url = "http://123.com/path/321";
62+
ep.setIngestionEndpoint(URI.create(url));
63+
assertEquals(URI.create(url+"/"+EndpointProvider.API_PROFILES_APP_ID_URI_PREFIX+ikey+EndpointProvider.API_PROFILES_APP_ID_URI_SUFFIX), ep.getAppIdEndpointURL(ikey));
64+
65+
ep.setIngestionEndpoint(URI.create(url+"/"));
66+
assertEquals(URI.create(url+"/"+EndpointProvider.API_PROFILES_APP_ID_URI_PREFIX+ikey+EndpointProvider.API_PROFILES_APP_ID_URI_SUFFIX), ep.getAppIdEndpointURL(ikey));
67+
}
68+
5769
@Test
5870
public void ikeyWithSuffix() throws Exception {
5971
final String ikey = "fake-ikey";
@@ -70,6 +82,38 @@ public void ikeyWithSuffix() throws Exception {
7082
assertEquals(expectedLiveEndpoint, config.getEndpointProvider().getLiveEndpointURL());
7183
}
7284

85+
@Test
86+
public void suffixWithPathRetainsThePath() throws Exception {
87+
final String ikey = "fake-ikey";
88+
final String suffix = "ai.example.com/my-proxy-app/doProxy";
89+
final String cs = "InstrumentationKey="+ikey+";EndpointSuffix="+suffix;
90+
final URI expectedIngestionEndpoint = URI.create("https://"+EndpointPrefixes.INGESTION_ENDPOINT_PREFIX+"."+suffix);
91+
final URI expectedIngestionEndpointURL = URI.create("https://"+EndpointPrefixes.INGESTION_ENDPOINT_PREFIX+"."+suffix + "/" + EndpointProvider.INGESTION_URI_PATH);
92+
final URI expectedLiveEndpoint = URI.create("https://"+EndpointPrefixes.LIVE_ENDPOINT_PREFIX+"."+suffix + "/" + EndpointProvider.LIVE_URI_PATH);
93+
94+
ConnectionString.parseInto(cs, config);
95+
assertEquals(ikey, config.getInstrumentationKey());
96+
assertEquals(expectedIngestionEndpoint, config.getEndpointProvider().getIngestionEndpoint());
97+
assertEquals(expectedIngestionEndpointURL, config.getEndpointProvider().getIngestionEndpointURL());
98+
assertEquals(expectedLiveEndpoint, config.getEndpointProvider().getLiveEndpointURL());
99+
}
100+
101+
@Test
102+
public void suffixSupportsPort() throws Exception {
103+
final String ikey = "fake-ikey";
104+
final String suffix = "ai.example.com:9999";
105+
final String cs = "InstrumentationKey="+ikey+";EndpointSuffix="+suffix;
106+
final URI expectedIngestionEndpoint = URI.create("https://"+EndpointPrefixes.INGESTION_ENDPOINT_PREFIX+"."+suffix);
107+
final URI expectedIngestionEndpointURL = URI.create("https://"+EndpointPrefixes.INGESTION_ENDPOINT_PREFIX+"."+suffix + "/" + EndpointProvider.INGESTION_URI_PATH);
108+
final URI expectedLiveEndpoint = URI.create("https://"+EndpointPrefixes.LIVE_ENDPOINT_PREFIX+"."+suffix + "/" + EndpointProvider.LIVE_URI_PATH);
109+
110+
ConnectionString.parseInto(cs, config);
111+
assertEquals(ikey, config.getInstrumentationKey());
112+
assertEquals(expectedIngestionEndpoint, config.getEndpointProvider().getIngestionEndpoint());
113+
assertEquals(expectedIngestionEndpointURL, config.getEndpointProvider().getIngestionEndpointURL());
114+
assertEquals(expectedLiveEndpoint, config.getEndpointProvider().getLiveEndpointURL());
115+
}
116+
73117
@Test
74118
public void ikeyWithExplicitEndpoints() throws Exception {
75119
final String ikey = "fake-ikey";
@@ -197,15 +241,30 @@ public void orderDoesNotMatter() throws Exception {
197241
}
198242

199243
@Test
200-
public void endpointWithNoSchemeIsHttps() throws Exception {
244+
public void endpointWithNoSchemeIsInvalid() throws Exception {
245+
exception.expect(InvalidConnectionStringException.class);
246+
exception.expectMessage(containsString("IngestionEndpoint"));
201247
ConnectionString.parseInto("InstrumentationKey=fake-ikey;IngestionEndpoint=my-ai.example.com", config);
202-
assertEquals("https", config.getEndpointProvider().getIngestionEndpoint().getScheme());
248+
}
249+
250+
@Test
251+
public void endpointWithPathMissingSchemeIsInvalid() throws Exception {
252+
exception.expect(InvalidConnectionStringException.class);
253+
exception.expectMessage(containsString("IngestionEndpoint"));
254+
ConnectionString.parseInto("InstrumentationKey=fake-ikey;IngestionEndpoint=my-ai.example.com/path/prefix", config);
255+
}
256+
257+
@Test
258+
public void endpointWithPortMissingSchemeIsInvalid() throws Exception {
259+
exception.expect(InvalidConnectionStringException.class);
260+
exception.expectMessage(containsString("IngestionEndpoint"));
261+
ConnectionString.parseInto("InstrumentationKey=fake-ikey;IngestionEndpoint=my-ai.example.com:9999", config);
203262
}
204263

205264
@Test
206265
public void httpEndpointKeepsScheme() throws Exception {
207266
ConnectionString.parseInto("InstrumentationKey=fake-ikey;IngestionEndpoint=http://my-ai.example.com", config);
208-
assertEquals("http", config.getEndpointProvider().getIngestionEndpoint().getScheme());
267+
assertEquals(URI.create("http://my-ai.example.com"), config.getEndpointProvider().getIngestionEndpoint());
209268
}
210269

211270
@Test

0 commit comments

Comments
 (0)