Search in sources :

Example 1 with NatInitializationException

use of org.hyperledger.besu.nat.core.exception.NatInitializationException in project besu by hyperledger.

the class UpnpNatManager method doStart.

/**
 * Start the manager. Must not be in started state.
 *
 * @throws IllegalStateException if already started.
 */
@Override
public synchronized void doStart() throws NatInitializationException {
    LOG.info("Starting UPnP Service");
    try {
        upnpService.startup();
        upnpService.getRegistry().addListener(registryListener);
        initiateExternalIpQuery();
    } catch (Exception e) {
        throw new NatInitializationException("Failed start UPnP nat service.", e);
    }
}
Also used : NatInitializationException(org.hyperledger.besu.nat.core.exception.NatInitializationException) NatInitializationException(org.hyperledger.besu.nat.core.exception.NatInitializationException)

Example 2 with NatInitializationException

use of org.hyperledger.besu.nat.core.exception.NatInitializationException in project besu by hyperledger.

the class DockerNatManagerTest method assertThatExternalIPIsEqualToDefaultHostIfIpDetectorCannotRetrieveIP.

@Test
public void assertThatExternalIPIsEqualToDefaultHostIfIpDetectorCannotRetrieveIP() throws ExecutionException, InterruptedException {
    final NatManager natManager = new DockerNatManager(hostBasedIpDetector, advertisedHost, p2pPort, rpcHttpPort);
    when(hostBasedIpDetector.detectAdvertisedIp()).thenReturn(Optional.empty());
    try {
        natManager.start();
    } catch (NatInitializationException e) {
        Assertions.fail(e.getMessage());
    }
    assertThat(natManager.queryExternalIPAddress().get()).isEqualTo(advertisedHost);
}
Also used : NatInitializationException(org.hyperledger.besu.nat.core.exception.NatInitializationException) NatManager(org.hyperledger.besu.nat.core.NatManager) Test(org.junit.Test)

Example 3 with NatInitializationException

use of org.hyperledger.besu.nat.core.exception.NatInitializationException in project besu by hyperledger.

the class KubernetesNatManager method doStart.

@Override
protected void doStart() throws NatInitializationException {
    LOG.info("Starting kubernetes NAT manager.");
    try {
        KubeConfig.registerAuthenticator(new GCPAuthenticator());
        LOG.debug("Trying to update information using Kubernetes client SDK.");
        final ApiClient client = ClientBuilder.cluster().build();
        // set the global default api-client to the in-cluster one from above
        Configuration.setDefaultApiClient(client);
        // the CoreV1Api loads default api-client from global configuration.
        final CoreV1Api api = new CoreV1Api();
        // invokes the CoreV1Api client
        final V1Service service = api.listServiceForAllNamespaces(null, null, null, null, null, null, null, null, null, null).getItems().stream().filter(v1Service -> v1Service.getMetadata().getName().contains(besuServiceNameFilter)).findFirst().orElseThrow(() -> new NatInitializationException("Service not found"));
        updateUsingBesuService(service);
    } catch (Exception e) {
        throw new NatInitializationException(e.getMessage(), e);
    }
}
Also used : GCPAuthenticator(io.kubernetes.client.util.authenticators.GCPAuthenticator) NatInitializationException(org.hyperledger.besu.nat.core.exception.NatInitializationException) V1Service(io.kubernetes.client.openapi.models.V1Service) ApiClient(io.kubernetes.client.openapi.ApiClient) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) NatInitializationException(org.hyperledger.besu.nat.core.exception.NatInitializationException)

Example 4 with NatInitializationException

use of org.hyperledger.besu.nat.core.exception.NatInitializationException in project besu by hyperledger.

the class DockerNatManager method doStart.

@Override
protected void doStart() throws NatInitializationException {
    LOG.info("Starting docker NAT manager.");
    try {
        ipDetector.detectAdvertisedIp().ifPresent(ipFound -> internalAdvertisedHost = ipFound);
        buildForwardedPorts();
    } catch (Exception e) {
        throw new NatInitializationException("Unable to retrieve IP from docker");
    }
}
Also used : NatInitializationException(org.hyperledger.besu.nat.core.exception.NatInitializationException) NatInitializationException(org.hyperledger.besu.nat.core.exception.NatInitializationException)

Example 5 with NatInitializationException

use of org.hyperledger.besu.nat.core.exception.NatInitializationException in project besu by hyperledger.

the class KubernetesNatManager method updateUsingBesuService.

@VisibleForTesting
void updateUsingBesuService(final V1Service service) throws RuntimeException {
    try {
        LOG.info("Found Besu service: {}", service.getMetadata().getName());
        internalAdvertisedHost = getIpDetector(service).detectAdvertisedIp().orElseThrow(() -> new NatInitializationException("Unable to retrieve IP from service"));
        LOG.info("Setting host IP to: {}.", internalAdvertisedHost);
        final String internalHost = queryLocalIPAddress().get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
        service.getSpec().getPorts().forEach(v1ServicePort -> {
            try {
                final NatServiceType natServiceType = NatServiceType.fromString(v1ServicePort.getName());
                forwardedPorts.add(new NatPortMapping(natServiceType, natServiceType.equals(NatServiceType.DISCOVERY) ? NetworkProtocol.UDP : NetworkProtocol.TCP, internalHost, internalAdvertisedHost, v1ServicePort.getPort(), v1ServicePort.getTargetPort().getIntValue()));
            } catch (IllegalStateException e) {
                LOG.warn("Ignored unknown Besu port: {}", e.getMessage());
            }
        });
    } catch (Exception e) {
        throw new RuntimeException("Failed update information using pod metadata : " + e.getMessage(), e);
    }
}
Also used : NatInitializationException(org.hyperledger.besu.nat.core.exception.NatInitializationException) NatPortMapping(org.hyperledger.besu.nat.core.domain.NatPortMapping) NatServiceType(org.hyperledger.besu.nat.core.domain.NatServiceType) NatInitializationException(org.hyperledger.besu.nat.core.exception.NatInitializationException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

NatInitializationException (org.hyperledger.besu.nat.core.exception.NatInitializationException)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)1 V1Service (io.kubernetes.client.openapi.models.V1Service)1 GCPAuthenticator (io.kubernetes.client.util.authenticators.GCPAuthenticator)1 NatManager (org.hyperledger.besu.nat.core.NatManager)1 NatPortMapping (org.hyperledger.besu.nat.core.domain.NatPortMapping)1 NatServiceType (org.hyperledger.besu.nat.core.domain.NatServiceType)1 Test (org.junit.Test)1