From 8ee3e93c3bcfc07af3fb2f778b804590eda2c61f Mon Sep 17 00:00:00 2001 From: Neeraja Kukday Date: Mon, 1 Jun 2020 10:19:31 -0700 Subject: [PATCH] Endpoint configuration (#477) --- gradle/dependencies.gradle | 2 +- .../ServerInformationInstrumentationTest.java | 44 +++++++++++++++++++ .../telemetry/CertificatePinnerFactory.java | 17 +++---- .../android/telemetry/ComCertificatePins.java | 6 +++ .../telemetry/ComServerInformation.java | 43 ++++++++++++++++++ .../telemetry/TelemetryClientFactory.java | 14 ++++-- 6 files changed, 111 insertions(+), 15 deletions(-) create mode 100644 libtelemetry/src/androidTest/java/com/mapbox/android/telemetry/ServerInformationInstrumentationTest.java diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index 0698888d0..9e25b1333 100644 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -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' ] diff --git a/libtelemetry/src/androidTest/java/com/mapbox/android/telemetry/ServerInformationInstrumentationTest.java b/libtelemetry/src/androidTest/java/com/mapbox/android/telemetry/ServerInformationInstrumentationTest.java new file mode 100644 index 000000000..0c625f4c0 --- /dev/null +++ b/libtelemetry/src/androidTest/java/com/mapbox/android/telemetry/ServerInformationInstrumentationTest.java @@ -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 configurationList = new ArrayList() { + { + 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); + } +} diff --git a/libtelemetry/src/main/java/com/mapbox/android/telemetry/CertificatePinnerFactory.java b/libtelemetry/src/main/java/com/mapbox/android/telemetry/CertificatePinnerFactory.java index dc4e3a538..e067be471 100644 --- a/libtelemetry/src/main/java/com/mapbox/android/telemetry/CertificatePinnerFactory.java +++ b/libtelemetry/src/main/java/com/mapbox/android/telemetry/CertificatePinnerFactory.java @@ -4,7 +4,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; import okhttp3.CertificatePinner; @@ -47,20 +46,16 @@ private void addCertificatesPins(Map> pins, CertificatePinn private Map> removeBlacklistedPins(Map> pins, CertificateBlacklist certificateBlacklist) { - String key = retrievePinKey(pins); - List hashList = pins.get(key); - if (hashList != null) { - hashList = removeBlacklistedHashes(certificateBlacklist, hashList); - pins.put(key, hashList); + for (Map.Entry> entry : pins.entrySet()) { + List hashList = entry.getValue(); + if (hashList != null) { + hashList = removeBlacklistedHashes(certificateBlacklist, hashList); + pins.put(entry.getKey(), hashList); + } } return pins; } - private String retrievePinKey(Map> pins) { - Set pinsKey = pins.keySet(); - return pinsKey.iterator().next(); - } - private List removeBlacklistedHashes(CertificateBlacklist certificateBlacklist, List hashList) { for (String hash : hashList) { if (certificateBlacklist.isBlacklisted(hash)) { diff --git a/libtelemetry/src/main/java/com/mapbox/android/telemetry/ComCertificatePins.java b/libtelemetry/src/main/java/com/mapbox/android/telemetry/ComCertificatePins.java index 5b0d6c5c4..ba12ae468 100644 --- a/libtelemetry/src/main/java/com/mapbox/android/telemetry/ComCertificatePins.java +++ b/libtelemetry/src/main/java/com/mapbox/android/telemetry/ComCertificatePins.java @@ -73,6 +73,12 @@ class ComCertificatePins { add("PA1lecwXNRXY/Vpy0VN+jQEYChN4hCAF36oB0Ygx3wQ="); } }); + put("FVQ3CP/SEI8eLPxHJnjyew2P5DTC1OBKK4Y6XkmC0WI=", new ArrayList() { + { + add("qjl/5X6sDeDCP4DEcR4VFPw0qa/El98EU/ZHwY0jTx0="); + add("Xw7GYmoUa7YVrYJj7t7RnqYcO58dRFLYEL7UEOuIlX8="); + } + }); } }; } \ No newline at end of file diff --git a/libtelemetry/src/main/java/com/mapbox/android/telemetry/ComServerInformation.java b/libtelemetry/src/main/java/com/mapbox/android/telemetry/ComServerInformation.java index 2bc6015ce..5b868da6f 100644 --- a/libtelemetry/src/main/java/com/mapbox/android/telemetry/ComServerInformation.java +++ b/libtelemetry/src/main/java/com/mapbox/android/telemetry/ComServerInformation.java @@ -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 configurationList = + new ArrayList() { + { + add("FVQ3CP/SEI8eLPxHJnjyew2P5DTC1OBKK4Y6XkmC0WI="); + } + }; @Override public void nextChain(EnvironmentResolver chain) { @@ -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 configurationList) { + this.configurationList = configurationList; + } + } diff --git a/libtelemetry/src/main/java/com/mapbox/android/telemetry/TelemetryClientFactory.java b/libtelemetry/src/main/java/com/mapbox/android/telemetry/TelemetryClientFactory.java index fab9765ed..208500a0a 100644 --- a/libtelemetry/src/main/java/com/mapbox/android/telemetry/TelemetryClientFactory.java +++ b/libtelemetry/src/main/java/com/mapbox/android/telemetry/TelemetryClientFactory.java @@ -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) { @@ -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); + } } } }