-
Notifications
You must be signed in to change notification settings - Fork 879
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
jwt auth on websockets #4039
Merged
Merged
jwt auth on websockets #4039
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fac85f7
integration test covering websocket subscription without auth
jflo fd3f980
uses authenticated user on websocket handler when auth enabled and su…
jflo 6671999
sonarlint fixes and copyright correction
jflo 56e6350
Merge branch 'main' into 3990-jwt-on-websockets
garyschulte 55b7ab6
moved test specific class to test sources
jflo 0b29634
Merge branch 'main' into 3990-jwt-on-websockets
jflo 7271d06
Merge branch 'main' into 3990-jwt-on-websockets
garyschulte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
integration test covering websocket subscription without auth
Signed-off-by: Justin Florentine <justin+github@florentine.us>
- Loading branch information
commit fac85f7e71c6eef0af033bd8948a64a7dbe87677
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...yperledger/besu/ethereum/api/jsonrpc/internal/response/MutableJsonRpcSuccessResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://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.hyperledger.besu.ethereum.api.jsonrpc.internal.response; | ||
|
||
import java.util.Objects; | ||
|
||
import com.fasterxml.jackson.annotation.JsonGetter; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
import com.fasterxml.jackson.annotation.JsonSetter; | ||
|
||
@JsonPropertyOrder({"jsonrpc", "id", "result"}) | ||
public class MutableJsonRpcSuccessResponse { | ||
|
||
private Object id; | ||
private Object result; | ||
private Object version; | ||
|
||
public MutableJsonRpcSuccessResponse() { | ||
this.id = null; | ||
this.result = null; | ||
} | ||
|
||
public MutableJsonRpcSuccessResponse(final Object id, final Object result) { | ||
this.id = id; | ||
this.result = result; | ||
} | ||
|
||
public MutableJsonRpcSuccessResponse(final Object id) { | ||
this.id = id; | ||
this.result = "Success"; | ||
} | ||
|
||
@JsonGetter("id") | ||
public Object getId() { | ||
return id; | ||
} | ||
|
||
@JsonGetter("result") | ||
public Object getResult() { | ||
return result; | ||
} | ||
|
||
@JsonSetter("id") | ||
public void setId(final Object id) { | ||
this.id = id; | ||
} | ||
|
||
@JsonSetter("result") | ||
public void setResult(final Object result) { | ||
this.result = result; | ||
} | ||
|
||
@JsonGetter("jsonrpc") | ||
public Object getVersion() { | ||
return version; | ||
} | ||
|
||
@JsonSetter("jsonrpc") | ||
public void setVersion(final Object version) { | ||
this.version = version; | ||
} | ||
|
||
@JsonIgnore | ||
public JsonRpcResponseType getType() { | ||
return JsonRpcResponseType.SUCCESS; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
final MutableJsonRpcSuccessResponse that = (MutableJsonRpcSuccessResponse) o; | ||
return Objects.equals(id, that.id) && Objects.equals(result, that.result); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, result); | ||
} | ||
} |
245 changes: 245 additions & 0 deletions
245
...api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/websocket/JsonRpcJWTTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
/* | ||
* Copyright ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://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.hyperledger.besu.ethereum.api.jsonrpc.websocket; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.fail; | ||
|
||
import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcService; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.authentication.AuthenticationService; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.authentication.EngineAuthService; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.health.HealthService; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.health.HealthService.HealthCheck; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.health.HealthService.ParamSource; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.MutableJsonRpcSuccessResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketMethodsFactory; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.SubscriptionManager; | ||
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler; | ||
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; | ||
import org.hyperledger.besu.nat.NatService; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import io.vertx.core.Vertx; | ||
import io.vertx.core.http.HttpClient; | ||
import io.vertx.core.http.HttpClientOptions; | ||
import io.vertx.core.http.WebSocket; | ||
import io.vertx.core.http.WebSocketConnectOptions; | ||
import io.vertx.core.json.Json; | ||
import io.vertx.ext.unit.Async; | ||
import io.vertx.ext.unit.TestContext; | ||
import io.vertx.ext.unit.junit.VertxUnitRunner; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(VertxUnitRunner.class) | ||
public class JsonRpcJWTTest { | ||
|
||
@ClassRule public static final TemporaryFolder folder = new TemporaryFolder(); | ||
public static final String HOSTNAME = "127.0.0.1"; | ||
|
||
protected static Vertx vertx; | ||
|
||
private final JsonRpcConfiguration jsonRpcConfiguration = | ||
JsonRpcConfiguration.createEngineDefault(); | ||
private JsonRpcService jsonRpcService; | ||
private HttpClient httpClient; | ||
private Optional<AuthenticationService> jwtAuth; | ||
private HealthService healthy; | ||
private EthScheduler scheduler; | ||
private Path bufferDir; | ||
private Map<String, JsonRpcMethod> websocketMethods; | ||
|
||
@Before | ||
public void initServerAndClient() { | ||
jsonRpcConfiguration.setPort(0); | ||
jsonRpcConfiguration.setHostsAllowlist(List.of("*")); | ||
try { | ||
jsonRpcConfiguration.setAuthenticationPublicKeyFile( | ||
new File(this.getClass().getResource("jwt.hex").toURI())); | ||
} catch (URISyntaxException e) { | ||
fail("couldn't load jwt key from jwt.hex in classpath"); | ||
} | ||
vertx = Vertx.vertx(); | ||
|
||
websocketMethods = | ||
new WebSocketMethodsFactory( | ||
new SubscriptionManager(new NoOpMetricsSystem()), new HashMap<>()) | ||
.methods(); | ||
|
||
bufferDir = null; | ||
try { | ||
bufferDir = Files.createTempDirectory("JsonRpcJWTTest").toAbsolutePath(); | ||
} catch (IOException e) { | ||
fail("can't create tempdir", e); | ||
} | ||
|
||
jwtAuth = | ||
Optional.of( | ||
new EngineAuthService( | ||
vertx, | ||
Optional.ofNullable(jsonRpcConfiguration.getAuthenticationPublicKeyFile()), | ||
bufferDir)); | ||
|
||
healthy = | ||
new HealthService( | ||
new HealthCheck() { | ||
@Override | ||
public boolean isHealthy(final ParamSource paramSource) { | ||
return true; | ||
} | ||
}); | ||
|
||
scheduler = new EthScheduler(1, 1, 1, new NoOpMetricsSystem()); | ||
} | ||
|
||
@After | ||
public void after() {} | ||
|
||
@Test | ||
public void unauthenticatedWebsocketAllowedWithoutJWTAuth(final TestContext context) { | ||
|
||
jsonRpcService = | ||
new JsonRpcService( | ||
vertx, | ||
bufferDir, | ||
jsonRpcConfiguration, | ||
new NoOpMetricsSystem(), | ||
new NatService(Optional.empty(), true), | ||
websocketMethods, | ||
Optional.empty(), | ||
scheduler, | ||
Optional.empty(), | ||
healthy, | ||
healthy); | ||
|
||
jsonRpcService.start().join(); | ||
|
||
final InetSocketAddress inetSocketAddress = jsonRpcService.socketAddress(); | ||
int listenPort = inetSocketAddress.getPort(); | ||
|
||
final HttpClientOptions httpClientOptions = | ||
new HttpClientOptions().setDefaultHost(HOSTNAME).setDefaultPort(listenPort); | ||
|
||
httpClient = vertx.createHttpClient(httpClientOptions); | ||
|
||
WebSocketConnectOptions wsOpts = new WebSocketConnectOptions(); | ||
wsOpts.setPort(listenPort); | ||
wsOpts.setHost(HOSTNAME); | ||
wsOpts.setURI("/"); | ||
|
||
final Async async = context.async(); | ||
httpClient.webSocket( | ||
wsOpts, | ||
connected -> { | ||
if (connected.failed()) { | ||
connected.cause().printStackTrace(); | ||
} | ||
assertThat(connected.succeeded()).isTrue(); | ||
WebSocket ws = connected.result(); | ||
|
||
JsonRpcRequest req = | ||
new JsonRpcRequest("2.0", "eth_subscribe", List.of("syncing").toArray()); | ||
ws.frameHandler( | ||
resp -> { | ||
assertThat(resp.isText()).isTrue(); | ||
MutableJsonRpcSuccessResponse messageReply = | ||
Json.decodeValue(resp.textData(), MutableJsonRpcSuccessResponse.class); | ||
assertThat(messageReply.getResult()).isEqualTo("0x1"); | ||
async.complete(); | ||
}); | ||
ws.writeTextMessage(Json.encode(req)); | ||
}); | ||
|
||
async.awaitSuccess(10000); | ||
jsonRpcService.stop(); | ||
httpClient.close(); | ||
} | ||
|
||
@Test | ||
public void httpRequestWithDefaultHeaderAndValidJWTIsAccepted(final TestContext context) { | ||
|
||
jsonRpcService = | ||
new JsonRpcService( | ||
vertx, | ||
bufferDir, | ||
jsonRpcConfiguration, | ||
new NoOpMetricsSystem(), | ||
new NatService(Optional.empty(), true), | ||
websocketMethods, | ||
Optional.empty(), | ||
scheduler, | ||
jwtAuth, | ||
healthy, | ||
healthy); | ||
|
||
jsonRpcService.start().join(); | ||
|
||
final InetSocketAddress inetSocketAddress = jsonRpcService.socketAddress(); | ||
int listenPort = inetSocketAddress.getPort(); | ||
|
||
final HttpClientOptions httpClientOptions = | ||
new HttpClientOptions().setDefaultHost(HOSTNAME).setDefaultPort(listenPort); | ||
|
||
httpClient = vertx.createHttpClient(httpClientOptions); | ||
|
||
WebSocketConnectOptions wsOpts = new WebSocketConnectOptions(); | ||
wsOpts.setPort(listenPort); | ||
wsOpts.setHost(HOSTNAME); | ||
wsOpts.setURI("/"); | ||
wsOpts.addHeader("Origin", "localhost"); | ||
wsOpts.addHeader( | ||
"Authorization", "Bearer " + ((EngineAuthService) jwtAuth.get()).createToken()); | ||
|
||
final Async async = context.async(); | ||
httpClient.webSocket( | ||
wsOpts, | ||
connected -> { | ||
if (connected.failed()) { | ||
connected.cause().printStackTrace(); | ||
} | ||
assertThat(connected.succeeded()).isTrue(); | ||
WebSocket ws = connected.result(); | ||
JsonRpcRequest req = new JsonRpcRequest("1", "admin_nodeInfo", new Object[0]); | ||
ws.frameHandler( | ||
resp -> { | ||
assertThat(resp.isText()).isTrue(); | ||
System.out.println(resp.textData()); | ||
async.complete(); | ||
}); | ||
ws.writeTextMessage(Json.encode(req)); | ||
}); | ||
|
||
async.awaitSuccess(10000); | ||
jsonRpcService.stop(); | ||
httpClient.close(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
ethereum/api/src/test/resources/org/hyperledger/besu/ethereum/api/jsonrpc/websocket/jwt.hex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
9465710175a93a3f2d67b0cb98d92d44ead4d1126a12233571884de92a8edc76 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be moved to src/test ? It looks like it is only used as a deserialization target in tests