This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
[NC-2046] websocket method permissions #870
Merged
lucassaldanha
merged 18 commits into
PegaSysEng:master
from
macfarla:NC-2046-websockets
Feb 18, 2019
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
72f15d1
added new class for authentication utils
macfarla 15c3fc5
formatting
macfarla a27a398
Merge branch 'master' of github.com:PegaSysEng/pantheon into NC-2046-…
macfarla fbc0819
use context.async for login tests
macfarla ad2958c
added tests and websocket now uses auth token
macfarla 308f4b5
Merge branch 'master' of github.com:PegaSysEng/pantheon into NC-2046-…
macfarla b42ff7a
formatting
macfarla caec183
Merge branch 'master' of github.com:PegaSysEng/pantheon into NC-2046-…
macfarla fd79e06
unignore test and fixed permission should be an array
macfarla 60621e9
formatting
macfarla 9b0a8a0
Merge branch 'master' of github.com:PegaSysEng/pantheon into NC-2046-…
macfarla be4ced9
PR comments
macfarla d002b18
PR comments
macfarla 15ddcf3
Merge branch 'master' of github.com:PegaSysEng/pantheon into NC-2046-…
macfarla ff3f3dc
tidy up
macfarla 9f15d63
Merge branch 'master' of github.com:PegaSysEng/pantheon into NC-2046-…
macfarla bc989a3
Merge branch 'master' of github.com:PegaSysEng/pantheon into NC-2046-…
macfarla 806dbfa
Merge branch 'master' into NC-2046-websockets
lucassaldanha 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
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...s/acceptance/dsl/condition/net/ExpectNetVersionPermissionJsonRpcUnauthorizedResponse.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,32 @@ | ||
/* | ||
* Copyright 2018 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. | ||
*/ | ||
package tech.pegasys.pantheon.tests.acceptance.dsl.condition.net; | ||
|
||
import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition; | ||
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node; | ||
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.net.NetVersionTransaction; | ||
|
||
public class ExpectNetVersionPermissionJsonRpcUnauthorizedResponse implements Condition { | ||
|
||
private final NetVersionTransaction transaction; | ||
|
||
public ExpectNetVersionPermissionJsonRpcUnauthorizedResponse( | ||
final NetVersionTransaction transaction) { | ||
this.transaction = transaction; | ||
} | ||
|
||
@Override | ||
public void verify(final Node node) { | ||
node.execute(transaction); | ||
} | ||
} |
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
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
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
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
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
89 changes: 89 additions & 0 deletions
89
.../main/java/tech/pegasys/pantheon/ethereum/jsonrpc/authentication/AuthenticationUtils.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,89 @@ | ||
/* | ||
* Copyright 2019 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. | ||
*/ | ||
package tech.pegasys.pantheon.ethereum.jsonrpc.authentication; | ||
|
||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.ext.auth.User; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class AuthenticationUtils { | ||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
@VisibleForTesting | ||
public static boolean isPermitted( | ||
final Optional<AuthenticationService> authenticationService, | ||
final Optional<User> optionalUser, | ||
final JsonRpcMethod jsonRpcMethod) { | ||
|
||
AtomicBoolean foundMatchingPermission = new AtomicBoolean(); | ||
|
||
if (authenticationService.isPresent()) { | ||
if (optionalUser.isPresent()) { | ||
User user = optionalUser.get(); | ||
for (String perm : jsonRpcMethod.getPermissions()) { | ||
user.isAuthorized( | ||
perm, | ||
(authed) -> { | ||
if (authed.result()) { | ||
LOG.trace( | ||
"user {} authorized : {} via permission {}", | ||
user, | ||
jsonRpcMethod.getName(), | ||
perm); | ||
foundMatchingPermission.set(true); | ||
} | ||
}); | ||
} | ||
} | ||
} else { | ||
// no auth provider configured thus anything is permitted | ||
foundMatchingPermission.set(true); | ||
} | ||
|
||
if (!foundMatchingPermission.get()) { | ||
LOG.trace("user NOT authorized : {}", jsonRpcMethod.getName()); | ||
} | ||
return foundMatchingPermission.get(); | ||
} | ||
|
||
public static void getUser( | ||
final Optional<AuthenticationService> authenticationService, | ||
final String token, | ||
final Handler<Optional<User>> handler) { | ||
try { | ||
if (!authenticationService.isPresent()) { | ||
handler.handle(Optional.empty()); | ||
} else { | ||
authenticationService | ||
.get() | ||
.getJwtAuthProvider() | ||
.authenticate( | ||
new JsonObject().put("jwt", token), | ||
(r) -> { | ||
final User user = r.result(); | ||
handler.handle(Optional.of(user)); | ||
}); | ||
} | ||
} catch (Exception e) { | ||
handler.handle(Optional.empty()); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.
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.
I couldn't find any unit tests for AuthenticationUtils. This will make it harder to change this class in the future as we won't have anything to verify its current behaviour. Do we have unit tests that will assert the behaviour of this class?