Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Communication Network Traversal] Migrate to test proxy #35673

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "java",
"TagPrefix": "java/communication/azure-communication-networktraversal",
"Tag": "java/communication/azure-communication-networktraversal_b1c1957d62"
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<jacoco.skip>false</jacoco.skip>
<!-- Configures the Java 9+ run to perform the required module exports, opens, and reads that are necessary for testing but shouldn't be part of the module-info. -->
<javaModulesSurefireArgLine>
--add-opens com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED
--add-opens com.azure.communication.common/com.azure.communication.common.implementation=ALL-UNNAMED
</javaModulesSurefireArgLine>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,36 @@

import com.azure.communication.common.implementation.CommunicationConnectionString;
import com.azure.communication.identity.CommunicationIdentityClientBuilder;
import com.azure.core.credential.AccessToken;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.credential.TokenCredential;
import com.azure.core.credential.TokenRequestContext;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpPipelineNextPolicy;
import com.azure.core.http.HttpResponse;
import com.azure.core.test.TestBase;
import com.azure.core.test.TestMode;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.test.models.CustomMatcher;
import com.azure.core.test.models.TestProxySanitizer;
import com.azure.core.test.models.TestProxySanitizerType;
import com.azure.core.test.utils.MockTokenCredential;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.identity.DefaultAzureCredentialBuilder;
import reactor.core.publisher.Mono;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.StringJoiner;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommunicationRelayClientTestBase extends TestBase {
protected static final TestMode TEST_MODE = initializeTestMode();
import java.util.stream.Collectors;

public class CommunicationRelayClientTestBase extends TestProxyTestBase {
protected static final String CONNECTION_STRING = Configuration.getGlobalConfiguration()
.get("COMMUNICATION_LIVETEST_DYNAMIC_CONNECTION_STRING", "endpoint=https://REDACTED.communication.azure.com/;accesskey=QWNjZXNzS2V5");

private static final StringJoiner JSON_PROPERTIES_TO_REDACT
= new StringJoiner("\":\"|\"", "\"", "\":\"")
.add("token")
.add("id")
.add("credential");
private static final StringJoiner JSON_PROPERTIES_TO_REDACT_ARRAY
= new StringJoiner("\":\\[\"|\"", "\"", "\":\\[\"")
.add("urls");

private static final Pattern JSON_PROPERTY_VALUE_REDACTION_PATTERN
= Pattern.compile(String.format("(?:%s)(.*?)(?:\",|\"})", JSON_PROPERTIES_TO_REDACT.toString()),
Pattern.CASE_INSENSITIVE);

private static final Pattern JSON_PROPERTY_ARRAY_REDACTION_PATTERN
= Pattern.compile(String.format("(?:%s)(.*?)(?:\"\\],|\"\\]})", JSON_PROPERTIES_TO_REDACT_ARRAY.toString()),
Pattern.CASE_INSENSITIVE);
private static final List<String> JSON_PROPERTIES_TO_REDACT
= new ArrayList<>(Arrays.asList("token", "id", "credential"));
private static List<TestProxySanitizer> addBodyKeySanitizer() {
return JSON_PROPERTIES_TO_REDACT.stream().map(key -> new TestProxySanitizer(String.format("$..%s", key), null,
"REDACTED",
TestProxySanitizerType.BODY_KEY)).collect(Collectors.toList());
}

protected CommunicationRelayClientBuilder createClientBuilder(HttpClient httpClient) {
CommunicationRelayClientBuilder builder = new CommunicationRelayClientBuilder();
Expand All @@ -64,12 +48,9 @@ protected CommunicationRelayClientBuilder createClientBuilder(HttpClient httpCli
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);

if (getTestMode() == TestMode.RECORD) {
List<Function<String, String>> redactors = new ArrayList<>();
redactors.add(data -> redact(data, JSON_PROPERTY_VALUE_REDACTION_PATTERN.matcher(data), "REDACTED"));
redactors.add(data -> redact(data, JSON_PROPERTY_ARRAY_REDACTION_PATTERN.matcher(data), "redacted.skype.com:3400"));
builder.addPolicy(interceptorManager.getRecordPolicy(redactors));
builder.addPolicy(interceptorManager.getRecordPolicy());
}

addSanitizersAndMatchers();
return builder;
}

Expand All @@ -81,106 +62,57 @@ protected CommunicationRelayClientBuilder createClientBuilderUsingManagedIdentit

builder
.endpoint(communicationEndpoint)
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);
.httpClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient);

if (getTestMode() == TestMode.PLAYBACK) {
builder.credential(new FakeCredentials());
builder.credential(new MockTokenCredential());
} else {
builder.credential(new DefaultAzureCredentialBuilder().build());
}

if (getTestMode() == TestMode.RECORD) {
List<Function<String, String>> redactors = new ArrayList<>();
redactors.add(data -> redact(data, JSON_PROPERTY_VALUE_REDACTION_PATTERN.matcher(data), "REDACTED"));
redactors.add(data -> redact(data, JSON_PROPERTY_ARRAY_REDACTION_PATTERN.matcher(data), "redacted.skype.com:3400"));
builder.addPolicy(interceptorManager.getRecordPolicy(redactors));
builder.addPolicy(interceptorManager.getRecordPolicy());
}

return builder;
}

protected CommunicationIdentityClientBuilder createClientIdentityBuilderUsingManagedIdentity(HttpClient httpClient) {
CommunicationIdentityClientBuilder builder = new CommunicationIdentityClientBuilder();

CommunicationConnectionString communicationConnectionString = new CommunicationConnectionString(CONNECTION_STRING);
String communicationEndpoint = communicationConnectionString.getEndpoint();

builder
.endpoint(communicationEndpoint)
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);

if (getTestMode() == TestMode.PLAYBACK) {
builder.credential(new FakeCredentials());
} else {
builder.credential(new DefaultAzureCredentialBuilder().build());
}

if (getTestMode() == TestMode.RECORD) {
List<Function<String, String>> redactors = new ArrayList<>();
redactors.add(data -> redact(data, JSON_PROPERTY_VALUE_REDACTION_PATTERN.matcher(data), "REDACTED"));
builder.addPolicy(interceptorManager.getRecordPolicy(redactors));
}

addSanitizersAndMatchers();
return builder;
}

protected CommunicationRelayClientBuilder createClientBuilderUsingConnectionString(HttpClient httpClient) {
CommunicationRelayClientBuilder builder = new CommunicationRelayClientBuilder();
builder
.connectionString(CONNECTION_STRING)
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);
.httpClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient);

if (getTestMode() == TestMode.RECORD) {
List<Function<String, String>> redactors = new ArrayList<>();
redactors.add(data -> redact(data, JSON_PROPERTY_VALUE_REDACTION_PATTERN.matcher(data), "REDACTED"));
redactors.add(data -> redact(data, JSON_PROPERTY_ARRAY_REDACTION_PATTERN.matcher(data), "redacted.skype.com:3400"));
builder.addPolicy(interceptorManager.getRecordPolicy(redactors));
builder.addPolicy(interceptorManager.getRecordPolicy());
}

addSanitizersAndMatchers();
return builder;
}

protected CommunicationIdentityClientBuilder createIdentityClientBuilder(HttpClient httpClient) {
CommunicationIdentityClientBuilder builder = new CommunicationIdentityClientBuilder();
builder
.connectionString(CONNECTION_STRING)
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);
.httpClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient);

if (getTestMode() == TestMode.RECORD) {
List<Function<String, String>> redactors = new ArrayList<>();
redactors.add(data -> redact(data, JSON_PROPERTY_VALUE_REDACTION_PATTERN.matcher(data), "REDACTED"));
redactors.add(data -> redact(data, JSON_PROPERTY_ARRAY_REDACTION_PATTERN.matcher(data), "redacted.skype.com:3400"));
builder.addPolicy(interceptorManager.getRecordPolicy(redactors));
builder.addPolicy(interceptorManager.getRecordPolicy());
}

addSanitizersAndMatchers();
return builder;
}

private static TestMode initializeTestMode() {
ClientLogger logger = new ClientLogger(CommunicationRelayClientTestBase.class);
String azureTestMode = Configuration.getGlobalConfiguration().get("AZURE_TEST_MODE");
if (azureTestMode != null) {
System.out.println("azureTestMode: " + azureTestMode);
try {
return TestMode.valueOf(azureTestMode.toUpperCase(Locale.US));
} catch (IllegalArgumentException var3) {
logger.error("Could not parse '{}' into TestEnum. Using 'Playback' mode.", azureTestMode);
return TestMode.PLAYBACK;
}
} else {
logger.info("Environment variable '{}' has not been set yet. Using 'Playback' mode.", "AZURE_TEST_MODE");
return TestMode.PLAYBACK;
}
private void addSanitizersAndMatchers() {
interceptorManager.addMatchers(Arrays.asList(new CustomMatcher().setHeadersKeyOnlyMatch(
Arrays.asList("x-ms-hmac-string-to-sign-base64", "x-ms-content-sha", "x-ms-content-sha256"))));
interceptorManager.addSanitizers(addBodyKeySanitizer());
}

protected CommunicationRelayClientBuilder addLoggingPolicy(CommunicationRelayClientBuilder builder, String testName) {
return builder.addPolicy((context, next) -> logHeaders(testName, next));
}

protected CommunicationIdentityClientBuilder addLoggingPolicyIdentity(CommunicationIdentityClientBuilder builder, String testName) {
return builder.addPolicy((context, next) -> logHeaders(testName, next));
}

private Mono<HttpResponse> logHeaders(String testName, HttpPipelineNextPolicy next) {
return next.process()
.flatMap(httpResponse -> {
Expand All @@ -192,22 +124,4 @@ private Mono<HttpResponse> logHeaders(String testName, HttpPipelineNextPolicy ne
return Mono.just(bufferedResponse);
});
}

static class FakeCredentials implements TokenCredential {
@Override
public Mono<AccessToken> getToken(TokenRequestContext tokenRequestContext) {
return Mono.just(new AccessToken("someFakeToken", OffsetDateTime.MAX));
}
}

private String redact(String content, Matcher matcher, String replacement) {
while (matcher.find()) {
String captureGroup = matcher.group(1);
if (!CoreUtils.isNullOrEmpty(captureGroup)) {
content = content.replace(matcher.group(1), replacement);
}
}

return content;
}
}

This file was deleted.

This file was deleted.

Loading