Skip to content
This repository has been archived by the owner on Jan 20, 2025. It is now read-only.

fix: add timeout for did web resolver #42

Closed
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
Expand Up @@ -26,6 +26,7 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.eclipse.tractusx.ssi.lib.did.web.util.Constants;
Expand All @@ -40,9 +41,20 @@
@RequiredArgsConstructor
public class DidWebDocumentResolver implements DidDocumentResolver {

private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(30);

private final HttpClient client;
private final DidWebParser parser;
private final boolean enforceHttps;
private final Duration timeout;

public DidWebDocumentResolver(HttpClient client, DidWebParser parser) {
this(client, parser, true, DEFAULT_TIMEOUT);
}

public DidWebDocumentResolver(HttpClient client, DidWebParser parser, boolean enforceHttps) {
this(client, parser, enforceHttps, DEFAULT_TIMEOUT);
}

@Override
public DidMethod getSupportedMethod() {
Expand All @@ -57,7 +69,7 @@ public DidDocument resolve(Did did) {

final URI uri = parser.parse(did, enforceHttps);

final HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build();
final HttpRequest request = HttpRequest.newBuilder().uri(uri).timeout(timeout).GET().build();

try {
final HttpResponse<String> response =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/********************************************************************************
* Copyright (c) 2021,2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

package org.eclipse.tractusx.ssi.lib.lib.did.web;

import java.net.ServerSocket;
import java.net.http.HttpClient;
import java.time.Duration;
import lombok.SneakyThrows;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebDocumentResolver;
import org.eclipse.tractusx.ssi.lib.did.web.util.DidWebParser;
import org.eclipse.tractusx.ssi.lib.model.did.Did;
import org.eclipse.tractusx.ssi.lib.model.did.DidParser;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class DidWebDocumentResolverTest {

@Test
@SneakyThrows
// When the MIW tries to resolve a DID document that is not protected by TLS, but the DID resolve
// is configured to
// enforce HTTPS, the MIW will hang without timeout.
// This test is to ensure that the MIW does not hang anymore when trying to resolve a DID document
// that is not protected by TLS.
public void testTimeout() {

final HttpClient httpClient = HttpClient.newHttpClient();
final DidWebParser didParser = new DidWebParser();
final boolean enforceHttps = true;
final Duration timeout = Duration.ofMillis(100);
final DidWebDocumentResolver didWebDocumentResolver =
new DidWebDocumentResolver(httpClient, didParser, enforceHttps, timeout);

// open a local port to create an endpoint that is not protected by TLS
try (var ss = new ServerSocket(0)) {
final int port = ss.getLocalPort();
final Did did = DidParser.parse("did:web:localhost%3A" + port);
Assertions.assertThrows(RuntimeException.class, () -> didWebDocumentResolver.resolve(did));
}
}
}
Loading