mirror of https://github.com/hyperledger/besu
BESU-134: Prevent execution of PRIV/EEA methods when privacy is disabled (#226)
* Add warning when enabling PRIV/EEA APIs with privacy disabled * Prevent execution of PRIV/EEA methods when privacy is disabled Signed-off-by: Lucas Saldanha <lucas.saldanha@consensys.net>pull/230/head
parent
5f4bf595f4
commit
f84c3d2111
@ -0,0 +1,43 @@ |
||||
/* |
||||
* 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.privacy.methods.priv; |
||||
|
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; |
||||
import org.hyperledger.besu.ethereum.core.PrivacyParameters; |
||||
|
||||
public abstract class PrivacyApiMethod implements JsonRpcMethod { |
||||
|
||||
protected final PrivacyParameters privacyParameters; |
||||
|
||||
protected PrivacyApiMethod(final PrivacyParameters privacyParameters) { |
||||
this.privacyParameters = privacyParameters; |
||||
} |
||||
|
||||
@Override |
||||
public final JsonRpcResponse response(final JsonRpcRequestContext requestContext) { |
||||
if (privacyParameters.isEnabled()) { |
||||
return doResponse(requestContext); |
||||
} else { |
||||
return new JsonRpcErrorResponse( |
||||
requestContext.getRequest().getId(), JsonRpcError.PRIVACY_NOT_ENABLED); |
||||
} |
||||
} |
||||
|
||||
public abstract JsonRpcResponse doResponse(final JsonRpcRequestContext request); |
||||
} |
@ -0,0 +1,58 @@ |
||||
/* |
||||
* 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.privacy.methods.priv; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
import org.hyperledger.besu.enclave.Enclave; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||
import org.hyperledger.besu.ethereum.core.PrivacyParameters; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.Mock; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class PrivDeletePrivacyGroupTest { |
||||
|
||||
@Mock private Enclave enclave; |
||||
@Mock private PrivacyParameters privacyParameters; |
||||
private PrivDeletePrivacyGroup privDeletePrivacyGroup; |
||||
|
||||
@Before |
||||
public void before() { |
||||
when(privacyParameters.getEnclave()).thenReturn(enclave); |
||||
privDeletePrivacyGroup = new PrivDeletePrivacyGroup(privacyParameters); |
||||
} |
||||
|
||||
@Test |
||||
public void returnPrivacyDisabledErrorWhenPrivacyIsDisabled() { |
||||
when(privacyParameters.isEnabled()).thenReturn(false); |
||||
|
||||
final JsonRpcRequestContext request = |
||||
new JsonRpcRequestContext( |
||||
new JsonRpcRequest("1", "priv_deletePrivacyGroup", new Object[] {})); |
||||
final JsonRpcErrorResponse response = |
||||
(JsonRpcErrorResponse) privDeletePrivacyGroup.response(request); |
||||
|
||||
assertThat(response.getError()).isEqualTo(JsonRpcError.PRIVACY_NOT_ENABLED); |
||||
} |
||||
} |
@ -0,0 +1,57 @@ |
||||
/* |
||||
* 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.privacy.methods.priv; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
import org.hyperledger.besu.enclave.Enclave; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||
import org.hyperledger.besu.ethereum.core.PrivacyParameters; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.Mock; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class PrivFindPrivacyGroupTest { |
||||
|
||||
@Mock private Enclave enclave; |
||||
@Mock private PrivacyParameters privacyParameters; |
||||
private PrivFindPrivacyGroup privFindPrivacyGroup; |
||||
|
||||
@Before |
||||
public void before() { |
||||
when(privacyParameters.getEnclave()).thenReturn(enclave); |
||||
privFindPrivacyGroup = new PrivFindPrivacyGroup(privacyParameters); |
||||
} |
||||
|
||||
@Test |
||||
public void returnPrivacyDisabledErrorWhenPrivacyIsDisabled() { |
||||
when(privacyParameters.isEnabled()).thenReturn(false); |
||||
|
||||
final JsonRpcRequestContext request = |
||||
new JsonRpcRequestContext(new JsonRpcRequest("1", "findPrivacyGroup", new Object[] {})); |
||||
final JsonRpcErrorResponse response = |
||||
(JsonRpcErrorResponse) privFindPrivacyGroup.response(request); |
||||
|
||||
assertThat(response.getError()).isEqualTo(JsonRpcError.PRIVACY_NOT_ENABLED); |
||||
} |
||||
} |
Loading…
Reference in new issue