mirror of https://github.com/hyperledger/besu
[Refactor] Move graphqloptions to its own class (#6496)
Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>pull/6500/head
parent
8972d6707c
commit
bc36d2c4c2
@ -0,0 +1,112 @@ |
||||
/* |
||||
* Copyright Hyperledger Besu Contributors. |
||||
* |
||||
* 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.cli.options.stable; |
||||
|
||||
import static java.util.Arrays.asList; |
||||
import static org.hyperledger.besu.ethereum.api.graphql.GraphQLConfiguration.DEFAULT_GRAPHQL_HTTP_PORT; |
||||
|
||||
import org.hyperledger.besu.cli.DefaultCommandValues; |
||||
import org.hyperledger.besu.cli.custom.CorsAllowedOriginsProperty; |
||||
import org.hyperledger.besu.cli.util.CommandLineUtils; |
||||
import org.hyperledger.besu.ethereum.api.graphql.GraphQLConfiguration; |
||||
|
||||
import java.util.List; |
||||
|
||||
import com.google.common.base.Strings; |
||||
import org.slf4j.Logger; |
||||
import picocli.CommandLine; |
||||
|
||||
/** Handles configuration options for the GraphQL HTTP service in Besu. */ |
||||
public class GraphQlOptions { |
||||
@CommandLine.Option( |
||||
names = {"--graphql-http-enabled"}, |
||||
description = "Set to start the GraphQL HTTP service (default: ${DEFAULT-VALUE})") |
||||
private final Boolean isGraphQLHttpEnabled = false; |
||||
|
||||
@SuppressWarnings({"FieldCanBeFinal", "FieldMayBeFinal"}) // PicoCLI requires non-final Strings.
|
||||
@CommandLine.Option( |
||||
names = {"--graphql-http-host"}, |
||||
paramLabel = DefaultCommandValues.MANDATORY_HOST_FORMAT_HELP, |
||||
description = "Host for GraphQL HTTP to listen on (default: ${DEFAULT-VALUE})", |
||||
arity = "1") |
||||
private String graphQLHttpHost; |
||||
|
||||
@CommandLine.Option( |
||||
names = {"--graphql-http-port"}, |
||||
paramLabel = DefaultCommandValues.MANDATORY_PORT_FORMAT_HELP, |
||||
description = "Port for GraphQL HTTP to listen on (default: ${DEFAULT-VALUE})", |
||||
arity = "1") |
||||
private final Integer graphQLHttpPort = DEFAULT_GRAPHQL_HTTP_PORT; |
||||
|
||||
@CommandLine.Option( |
||||
names = {"--graphql-http-cors-origins"}, |
||||
description = "Comma separated origin domain URLs for CORS validation (default: none)") |
||||
private final CorsAllowedOriginsProperty graphQLHttpCorsAllowedOrigins = |
||||
new CorsAllowedOriginsProperty(); |
||||
|
||||
/** |
||||
* Validates the GraphQL HTTP options. |
||||
* |
||||
* @param logger Logger instance |
||||
* @param commandLine CommandLine instance |
||||
*/ |
||||
public void validate(final Logger logger, final CommandLine commandLine) { |
||||
CommandLineUtils.checkOptionDependencies( |
||||
logger, |
||||
commandLine, |
||||
"--graphql-http-enabled", |
||||
!isGraphQLHttpEnabled, |
||||
asList("--graphql-http-cors-origins", "--graphql-http-host", "--graphql-http-port")); |
||||
} |
||||
|
||||
/** |
||||
* Creates a GraphQLConfiguration based on the provided options. |
||||
* |
||||
* @param hostsAllowlist List of hosts allowed |
||||
* @param defaultHostAddress Default host address |
||||
* @param timoutSec Timeout in seconds |
||||
* @return A GraphQLConfiguration instance |
||||
*/ |
||||
public GraphQLConfiguration graphQLConfiguration( |
||||
final List<String> hostsAllowlist, final String defaultHostAddress, final Long timoutSec) { |
||||
final GraphQLConfiguration graphQLConfiguration = GraphQLConfiguration.createDefault(); |
||||
graphQLConfiguration.setEnabled(isGraphQLHttpEnabled); |
||||
graphQLConfiguration.setHost( |
||||
Strings.isNullOrEmpty(graphQLHttpHost) ? defaultHostAddress : graphQLHttpHost); |
||||
graphQLConfiguration.setPort(graphQLHttpPort); |
||||
graphQLConfiguration.setHostsAllowlist(hostsAllowlist); |
||||
graphQLConfiguration.setCorsAllowedDomains(graphQLHttpCorsAllowedOrigins); |
||||
graphQLConfiguration.setHttpTimeoutSec(timoutSec); |
||||
return graphQLConfiguration; |
||||
} |
||||
|
||||
/** |
||||
* Checks if GraphQL over HTTP is enabled. |
||||
* |
||||
* @return true if enabled, false otherwise |
||||
*/ |
||||
public Boolean isGraphQLHttpEnabled() { |
||||
return isGraphQLHttpEnabled; |
||||
} |
||||
|
||||
/** |
||||
* Returns the port for GraphQL over HTTP. |
||||
* |
||||
* @return The port number |
||||
*/ |
||||
public Integer getGraphQLHttpPort() { |
||||
return graphQLHttpPort; |
||||
} |
||||
} |
@ -0,0 +1,93 @@ |
||||
/* |
||||
* Copyright Hyperledger Besu Contributors. |
||||
* |
||||
* 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.cli.options; |
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8; |
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.verify; |
||||
|
||||
import org.hyperledger.besu.cli.CommandTestAbstract; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class GraphQlOptionsTest extends CommandTestAbstract { |
||||
@Test |
||||
public void graphQLHttpEnabledPropertyMustBeUsed() { |
||||
parseCommand("--graphql-http-enabled"); |
||||
|
||||
verify(mockRunnerBuilder).graphQLConfiguration(graphQLConfigArgumentCaptor.capture()); |
||||
verify(mockRunnerBuilder).build(); |
||||
|
||||
assertThat(graphQLConfigArgumentCaptor.getValue().isEnabled()).isTrue(); |
||||
|
||||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); |
||||
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); |
||||
} |
||||
|
||||
@Test |
||||
public void graphQLHttpHostAndPortOptionsMustBeUsed() { |
||||
|
||||
final String host = "1.2.3.4"; |
||||
final int port = 1234; |
||||
parseCommand( |
||||
"--graphql-http-enabled", |
||||
"--graphql-http-host", |
||||
host, |
||||
"--graphql-http-port", |
||||
String.valueOf(port)); |
||||
|
||||
verify(mockRunnerBuilder).graphQLConfiguration(graphQLConfigArgumentCaptor.capture()); |
||||
verify(mockRunnerBuilder).build(); |
||||
|
||||
assertThat(graphQLConfigArgumentCaptor.getValue().getHost()).isEqualTo(host); |
||||
assertThat(graphQLConfigArgumentCaptor.getValue().getPort()).isEqualTo(port); |
||||
|
||||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); |
||||
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); |
||||
} |
||||
|
||||
@Test |
||||
public void graphQLHttpHostMayBeLocalhost() { |
||||
|
||||
final String host = "localhost"; |
||||
parseCommand("--graphql-http-enabled", "--graphql-http-host", host); |
||||
|
||||
verify(mockRunnerBuilder).graphQLConfiguration(graphQLConfigArgumentCaptor.capture()); |
||||
verify(mockRunnerBuilder).build(); |
||||
|
||||
assertThat(graphQLConfigArgumentCaptor.getValue().getHost()).isEqualTo(host); |
||||
|
||||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); |
||||
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); |
||||
} |
||||
|
||||
@Test |
||||
public void graphQLHttpHostMayBeIPv6() { |
||||
|
||||
final String host = "2600:DB8::8545"; |
||||
parseCommand("--graphql-http-enabled", "--graphql-http-host", host); |
||||
|
||||
verify(mockRunnerBuilder).graphQLConfiguration(graphQLConfigArgumentCaptor.capture()); |
||||
verify(mockRunnerBuilder).build(); |
||||
|
||||
assertThat(graphQLConfigArgumentCaptor.getValue().getHost()).isEqualTo(host); |
||||
|
||||
assertThat(commandOutput.toString(UTF_8)).isEmpty(); |
||||
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty(); |
||||
} |
||||
} |
Loading…
Reference in new issue