Skip to content

Commit

Permalink
Endpoint configuration (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkukday authored Jun 1, 2020
1 parent c4c2ce3 commit 8ee3e93
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ext {

pluginVersion = [
checkstyle : '8.4',
gradle : '3.6.2',
gradle : '3.6.3',
dependencyGraph : '0.5.0',
mapboxSdkVersions: '1.0.1'
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.mapbox.android.telemetry;

import android.os.Bundle;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;


import static org.junit.Assert.assertEquals;

public class ServerInformationInstrumentationTest {

@Test
public void comServerValidInfoTest() {
List<String> configurationList = new ArrayList<String>() {
{
add("qMkoTOaKYcVA8pWPtVKB7Kg46M5eccBMoa9WaEQVedQ=");
}
};
ComServerInformation comServerInformation = new ComServerInformation();
String anyAppInfoHostname = "some.test.url.com";
Bundle bundle = new Bundle();
bundle.putString("com.mapbox.ComEventsServer", anyAppInfoHostname);
comServerInformation.setConfigurationList(configurationList);

ServerInformation serverInformation = comServerInformation.obtainServerInformation(bundle);

assertEquals(anyAppInfoHostname, serverInformation.getHostname());
}

@Test
public void comServerInvalidInfoTest() {
ComServerInformation comServerInformation = new ComServerInformation();
String anyAppInfoHostname = "some.test.url.com";
Bundle bundle = new Bundle();
bundle.putString("com.mapbox.ComEventsServer", anyAppInfoHostname);

ServerInformation serverInformation = comServerInformation.obtainServerInformation(bundle);

assertEquals(serverInformation.getHostname(), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import okhttp3.CertificatePinner;

Expand Down Expand Up @@ -47,20 +46,16 @@ private void addCertificatesPins(Map<String, List<String>> pins, CertificatePinn

private Map<String, List<String>> removeBlacklistedPins(Map<String, List<String>> pins,
CertificateBlacklist certificateBlacklist) {
String key = retrievePinKey(pins);
List<String> hashList = pins.get(key);
if (hashList != null) {
hashList = removeBlacklistedHashes(certificateBlacklist, hashList);
pins.put(key, hashList);
for (Map.Entry<String, List<String>> entry : pins.entrySet()) {
List<String> hashList = entry.getValue();
if (hashList != null) {
hashList = removeBlacklistedHashes(certificateBlacklist, hashList);
pins.put(entry.getKey(), hashList);
}
}
return pins;
}

private String retrievePinKey(Map<String, List<String>> pins) {
Set<String> pinsKey = pins.keySet();
return pinsKey.iterator().next();
}

private List<String> removeBlacklistedHashes(CertificateBlacklist certificateBlacklist, List<String> hashList) {
for (String hash : hashList) {
if (certificateBlacklist.isBlacklisted(hash)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ class ComCertificatePins {
add("PA1lecwXNRXY/Vpy0VN+jQEYChN4hCAF36oB0Ygx3wQ=");
}
});
put("FVQ3CP/SEI8eLPxHJnjyew2P5DTC1OBKK4Y6XkmC0WI=", new ArrayList<String>() {
{
add("qjl/5X6sDeDCP4DEcR4VFPw0qa/El98EU/ZHwY0jTx0=");
add("Xw7GYmoUa7YVrYJj7t7RnqYcO58dRFLYEL7UEOuIlX8=");
}
});
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,25 @@


import android.os.Bundle;
import android.util.Base64;
import android.util.Log;

import androidx.annotation.VisibleForTesting;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

class ComServerInformation implements EnvironmentResolver {
private final String LOG_TAG = "ComServerInformation";
private final String DIGEST = "SHA-256";
private final String KEY_META_DATA_COM_SERVER = "com.mapbox.ComEventsServer";
private List<String> configurationList =
new ArrayList<String>() {
{
add("FVQ3CP/SEI8eLPxHJnjyew2P5DTC1OBKK4Y6XkmC0WI=");
}
};

@Override
public void nextChain(EnvironmentResolver chain) {
Expand All @@ -12,6 +29,32 @@ public void nextChain(EnvironmentResolver chain) {
@Override
public ServerInformation obtainServerInformation(Bundle appMetaData) {
ServerInformation com = new ServerInformation(Environment.COM);
String hostname = appMetaData.getString(KEY_META_DATA_COM_SERVER);
if (!TelemetryUtils.isEmpty(hostname)) {
String hostnameHash = obtainHash(hostname);
if (!TelemetryUtils.isEmpty(hostnameHash)
&& configurationList.contains(hostnameHash)) {
com.setHostname(hostname);
}
}
return com;
}

private String obtainHash(String hostname) {
String hostNameHash = null;
try {
byte[] digest = MessageDigest.getInstance(DIGEST).digest(hostname.getBytes());
hostNameHash = Base64.encodeToString(digest, Base64.NO_WRAP);
} catch (Exception exception) {
Log.d(LOG_TAG, String.format("Hostname error %s", exception.getMessage()));
}

return hostNameHash;
}

@VisibleForTesting
void setConfigurationList(List<String> configurationList) {
this.configurationList = configurationList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ private TelemetryClient buildTelemetryClientCustom(ServerInformation serverInfor
.environment(serverInformation.getEnvironment())
.baseUrl(TelemetryClientSettings.configureUrlHostname(serverInformation.getHostname()))
.build();
return new TelemetryClient(serverInformation.getAccessToken(), userAgent,
String serverToken = serverInformation.getAccessToken();
return new TelemetryClient(serverToken != null ? serverToken : accessToken,
userAgent,
TelemetryUtils.createReformedFullUserAgent(context),
telemetryClientSettings, logger, certificateBlacklist);
telemetryClientSettings,
logger,
certificateBlacklist);
}

private TelemetryClient buildClientFrom(ServerInformation serverInformation, Context context) {
Expand All @@ -61,7 +65,11 @@ private TelemetryClient buildClientFrom(ServerInformation serverInformation, Con
case STAGING:
return buildTelemetryClientCustom(serverInformation, certificateBlacklist, context);
default:
return buildTelemetryClient(environment, certificateBlacklist, context);
if (!TelemetryUtils.isEmpty(serverInformation.getHostname())) {
return buildTelemetryClientCustom(serverInformation, certificateBlacklist, context);
} else {
return buildTelemetryClient(environment, certificateBlacklist, context);
}
}
}
}

0 comments on commit 8ee3e93

Please sign in to comment.