Skip to content

Commit 65a751c

Browse files
set default request timeout of 10 seconds
1 parent 2ec60f6 commit 65a751c

File tree

6 files changed

+54
-5
lines changed

6 files changed

+54
-5
lines changed

plugins/microsoft-graph-authz/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ dependencies {
7171
runtimeOnly "com.squareup.okio:okio:3.4.0"
7272
runtimeOnly "com.squareup.okio:okio-jvm:3.4.0"
7373
runtimeOnly "io.github.std-uritemplate:std-uritemplate:2.0.0"
74-
runtimeOnly "com.azure:azure-core-http-okhttp:1.12.10"
74+
implementation "com.azure:azure-core-http-okhttp:1.12.10"
7575
implementation "com.google.code.gson:gson:2.10"
7676

7777
testRuntimeOnly "net.minidev:json-smart:2.5.2"

plugins/microsoft-graph-authz/src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
requires com.microsoft.graph.core;
2323
requires kotlin.stdlib;
2424
requires com.google.gson;
25+
requires okhttp3;
26+
requires com.azure.core.http.okhttp;
2527

2628
provides org.elasticsearch.xpack.core.security.SecurityExtension with MicrosoftGraphAuthzPlugin;
2729
}

plugins/microsoft-graph-authz/src/main/java/org/elasticsearch/xpack/security/authz/microsoft/MicrosoftGraphAuthzRealm.java

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

1010
package org.elasticsearch.xpack.security.authz.microsoft;
1111

12+
import com.azure.core.http.HttpClient;
13+
import com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder;
14+
import com.azure.core.http.policy.RetryPolicy;
15+
import com.azure.core.util.HttpClientOptions;
1216
import com.azure.identity.ClientSecretCredentialBuilder;
1317
import com.microsoft.graph.core.requests.BaseGraphRequestAdapter;
1418
import com.microsoft.graph.core.tasks.PageIterator;
@@ -17,6 +21,10 @@
1721
import com.microsoft.graph.serviceclient.GraphServiceClient;
1822
import com.microsoft.kiota.authentication.AzureIdentityAuthenticationProvider;
1923

24+
import com.microsoft.kiota.http.middleware.RetryHandler;
25+
26+
import okhttp3.OkHttpClient;
27+
2028
import org.elasticsearch.action.ActionListener;
2129
import org.elasticsearch.common.Strings;
2230
import org.elasticsearch.common.settings.Setting;
@@ -39,6 +47,7 @@
3947
import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper;
4048
import org.elasticsearch.xpack.core.security.user.User;
4149

50+
import java.time.Duration;
4251
import java.util.ArrayList;
4352
import java.util.List;
4453
import java.util.Map;
@@ -151,12 +160,21 @@ public void lookupUser(String principal, ActionListener<User> listener) {
151160
private static GraphServiceClient buildClient(RealmConfig config) {
152161
final var clientSecret = config.getSetting(MicrosoftGraphAuthzRealmSettings.CLIENT_SECRET);
153162

163+
final var timeout = config.getSetting(MicrosoftGraphAuthzRealmSettings.HTTP_REQUEST_TIMEOUT);
164+
final var httpClient = new OkHttpClient.Builder()
165+
.callTimeout(Duration.ofSeconds(timeout.seconds()))
166+
.addInterceptor(new RetryHandler())
167+
.build();
168+
154169
final var credentialProviderBuilder = new ClientSecretCredentialBuilder().clientId(
155170
config.getSetting(MicrosoftGraphAuthzRealmSettings.CLIENT_ID)
156171
)
157172
.clientSecret(clientSecret.toString())
158173
.tenantId(config.getSetting(MicrosoftGraphAuthzRealmSettings.TENANT_ID))
159-
.authorityHost(config.getSetting(MicrosoftGraphAuthzRealmSettings.ACCESS_TOKEN_HOST));
174+
.authorityHost(config.getSetting(MicrosoftGraphAuthzRealmSettings.ACCESS_TOKEN_HOST))
175+
.httpClient(new OkHttpAsyncHttpClientBuilder(httpClient).build())
176+
.enableUnsafeSupportLogging()
177+
.enableAccountIdentifierLogging();
160178

161179
if (DISABLE_INSTANCE_DISCOVERY) {
162180
credentialProviderBuilder.disableInstanceDiscovery();
@@ -166,7 +184,8 @@ private static GraphServiceClient buildClient(RealmConfig config) {
166184
return new GraphServiceClient(
167185
new BaseGraphRequestAdapter(
168186
new AzureIdentityAuthenticationProvider(credentialProvider, Strings.EMPTY_ARRAY, "https://graph.microsoft.com/.default"),
169-
config.getSetting(MicrosoftGraphAuthzRealmSettings.API_HOST)
187+
config.getSetting(MicrosoftGraphAuthzRealmSettings.API_HOST),
188+
httpClient
170189
)
171190
);
172191
}

plugins/microsoft-graph-authz/src/main/java/org/elasticsearch/xpack/security/authz/microsoft/MicrosoftGraphAuthzRealmSettings.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.elasticsearch.common.settings.SecureString;
1313
import org.elasticsearch.common.settings.Setting;
14+
import org.elasticsearch.core.TimeValue;
1415
import org.elasticsearch.xpack.core.security.authc.RealmSettings;
1516

1617
import java.util.ArrayList;
@@ -45,6 +46,12 @@ public class MicrosoftGraphAuthzRealmSettings {
4546
key -> Setting.simpleString(key, "https://graph.microsoft.com/v1.0", Setting.Property.NodeScope)
4647
);
4748

49+
public static final Setting.AffixSetting<TimeValue> HTTP_REQUEST_TIMEOUT = Setting.affixKeySetting(
50+
RealmSettings.realmSettingPrefix(REALM_TYPE),
51+
"http_request_timeout",
52+
key -> Setting.timeSetting(key, TimeValue.timeValueSeconds(10), Setting.Property.NodeScope)
53+
);
54+
4855
public static List<Setting<?>> getSettings() {
4956
var settings = new ArrayList<Setting<?>>(RealmSettings.getStandardSettings(REALM_TYPE));
5057
settings.add(CLIENT_ID);

x-pack/plugin/security/qa/microsoft-graph-authz-tests/src/javaRestTest/java/org/elasticsearch/xpack/security/authz/microsoft/MicrosoftGraphAuthzPluginIT.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.action.support.GroupedActionListener;
1313
import org.elasticsearch.action.support.PlainActionFuture;
1414
import org.elasticsearch.client.Request;
15+
import org.elasticsearch.client.RestClientBuilder;
1516
import org.elasticsearch.common.Strings;
1617
import org.elasticsearch.common.settings.SecureString;
1718
import org.elasticsearch.common.settings.Settings;
@@ -183,6 +184,16 @@ protected Settings restClientSettings() {
183184
return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build();
184185
}
185186

187+
@Override
188+
protected void configureClient(RestClientBuilder builder, Settings settings) throws IOException {
189+
super.configureClient(builder, settings);
190+
191+
builder.setRequestConfigCallback(requestConfigBuilder -> {
192+
requestConfigBuilder.setSocketTimeout(-1);
193+
return requestConfigBuilder;
194+
});
195+
}
196+
186197
@Override
187198
protected boolean shouldConfigureProjects() {
188199
return false;

x-pack/plugin/security/qa/microsoft-graph-authz-tests/src/javaRestTest/java/org/elasticsearch/xpack/security/authz/microsoft/MicrosoftGraphHttpFixture.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ public String getBaseUrl() {
110110
private void registerGetAccessTokenHandler() {
111111
server.createContext("/" + tenantId + "/oauth2/v2.0/token", exchange -> {
112112
logger.info("Received access token request");
113-
loginCount.incrementAndGet();
113+
final var callCount = loginCount.incrementAndGet();
114+
115+
if (callCount == 1) {
116+
graphError(exchange, RestStatus.GATEWAY_TIMEOUT, "Gateway timed out");
117+
return;
118+
}
114119

115120
if (exchange.getRequestMethod().equals("POST") == false) {
116121
graphError(exchange, RestStatus.METHOD_NOT_ALLOWED, "Expected POST request");
@@ -214,7 +219,12 @@ private void registerGetUserMembershipHandler(TestUser user) {
214219

215220
server.createContext("/v1.0/users/" + user.username() + "/transitiveMemberOf", exchange -> {
216221
logger.info("Received get user membership request [{}]", exchange.getRequestURI());
217-
getGroupMembershipCount.incrementAndGet();
222+
final var callCount = getGroupMembershipCount.incrementAndGet();
223+
224+
if (callCount == 1) {
225+
graphError(exchange, RestStatus.GATEWAY_TIMEOUT, "Gateway timed out");
226+
return;
227+
}
218228

219229
if (exchange.getRequestMethod().equals("GET") == false) {
220230
graphError(exchange, RestStatus.METHOD_NOT_ALLOWED, "Expected GET request");

0 commit comments

Comments
 (0)