Search in sources :

Example 1 with AdvertisedSocketAddress

use of org.neo4j.helpers.AdvertisedSocketAddress in project neo4j by neo4j.

the class Settings method advertisedAddress.

public static BaseSetting<AdvertisedSocketAddress> advertisedAddress(String name, Setting<ListenSocketAddress> listenAddressSetting) {
    return new ScopeAwareSetting<AdvertisedSocketAddress>() {

        @Override
        protected String provideName() {
            return name;
        }

        @Override
        public String getDefaultValue() {
            return default_advertised_address.getDefaultValue() + ":" + LISTEN_SOCKET_ADDRESS.apply(listenAddressSetting.getDefaultValue()).socketAddress().getPort();
        }

        @Override
        public AdvertisedSocketAddress from(Configuration config) {
            return config.get(this);
        }

        @Override
        public AdvertisedSocketAddress apply(Function<String, String> config) {
            ListenSocketAddress listenSocketAddress = listenAddressSetting.apply(config);
            String hostname = default_advertised_address.apply(config);
            int port = listenSocketAddress.socketAddress().getPort();
            String name = name();
            String value = config.apply(name);
            return SocketAddressFormat.socketAddress(name, value, hostname, port, AdvertisedSocketAddress::new);
        }

        @Override
        public void withScope(Function<String, String> scopingRule) {
            super.withScope(scopingRule);
            listenAddressSetting.withScope(scopingRule);
        }

        @Override
        public String valueDescription() {
            return ADVERTISED_SOCKET_ADDRESS.toString();
        }
    };
}
Also used : BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) Configuration(org.neo4j.graphdb.config.Configuration) ListenSocketAddress(org.neo4j.helpers.ListenSocketAddress) ScopeAwareSetting(org.neo4j.graphdb.config.ScopeAwareSetting) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress)

Example 2 with AdvertisedSocketAddress

use of org.neo4j.helpers.AdvertisedSocketAddress in project neo4j by neo4j.

the class BoltKernelExtension method newInstance.

@Override
public Lifecycle newInstance(KernelContext context, Dependencies dependencies) throws Throwable {
    Config config = dependencies.config();
    GraphDatabaseService gdb = dependencies.db();
    GraphDatabaseAPI api = (GraphDatabaseAPI) gdb;
    LogService logService = dependencies.logService();
    Clock clock = dependencies.clock();
    Log log = logService.getInternalLog(WorkerFactory.class);
    LifeSupport life = new LifeSupport();
    JobScheduler scheduler = dependencies.scheduler();
    InternalLoggerFactory.setDefaultFactory(new Netty4LoggerFactory(logService.getInternalLogProvider()));
    Authentication authentication = authentication(dependencies.authManager(), dependencies.userManagerSupplier());
    BoltFactory boltFactory = life.add(new BoltFactoryImpl(api, dependencies.usageData(), logService, dependencies.txBridge(), authentication, dependencies.sessionTracker(), config));
    WorkerFactory workerFactory = createWorkerFactory(boltFactory, scheduler, dependencies, logService, clock);
    List<ProtocolInitializer> connectors = config.enabledBoltConnectors().stream().map((connConfig) -> {
        ListenSocketAddress listenAddress = config.get(connConfig.listen_address);
        AdvertisedSocketAddress advertisedAddress = config.get(connConfig.advertised_address);
        SslContext sslCtx;
        boolean requireEncryption;
        final BoltConnector.EncryptionLevel encryptionLevel = config.get(connConfig.encryption_level);
        switch(encryptionLevel) {
            case REQUIRED:
                // Encrypted connections are mandatory, a self-signed certificate may be generated.
                requireEncryption = true;
                sslCtx = createSslContext(config, log, advertisedAddress);
                break;
            case OPTIONAL:
                // Encrypted connections are optional, a self-signed certificate may be generated.
                requireEncryption = false;
                sslCtx = createSslContext(config, log, advertisedAddress);
                break;
            case DISABLED:
                // Encryption is turned off, no self-signed certificate will be generated.
                requireEncryption = false;
                sslCtx = null;
                break;
            default:
                // In the unlikely event that we happen to fall through to the default option here,
                // there is a mismatch between the BoltConnector.EncryptionLevel enum and the options
                // handled in this switch statement. In this case, we'll log a warning and default to
                // disabling encryption, since this mirrors the functionality introduced in 3.0.
                log.warn(format("Unhandled encryption level %s - assuming DISABLED.", encryptionLevel.name()));
                requireEncryption = false;
                sslCtx = null;
                break;
        }
        final Map<Long, BiFunction<Channel, Boolean, BoltProtocol>> versions = newVersions(logService, workerFactory);
        return new SocketTransport(listenAddress, sslCtx, requireEncryption, logService.getInternalLogProvider(), versions);
    }).collect(toList());
    if (connectors.size() > 0 && !config.get(GraphDatabaseSettings.disconnected)) {
        life.add(new NettyServer(scheduler.threadFactory(boltNetworkIO), connectors));
        log.info("Bolt Server extension loaded.");
        for (ProtocolInitializer connector : connectors) {
            logService.getUserLog(WorkerFactory.class).info("Bolt enabled on %s.", connector.address());
        }
    }
    return life;
}
Also used : Service(org.neo4j.helpers.Service) UsageData(org.neo4j.udc.UsageData) Log(org.neo4j.logging.Log) Authentication(org.neo4j.bolt.security.auth.Authentication) BiFunction(java.util.function.BiFunction) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) KernelContext(org.neo4j.kernel.impl.spi.KernelContext) SocketTransport(org.neo4j.bolt.transport.SocketTransport) BoltProtocol(org.neo4j.bolt.transport.BoltProtocol) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) KeyStoreFactory(org.neo4j.bolt.security.ssl.KeyStoreFactory) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress) GeneralSecurityException(java.security.GeneralSecurityException) ProtocolInitializer(org.neo4j.bolt.transport.NettyServer.ProtocolInitializer) Map(java.util.Map) KeyStoreInformation(org.neo4j.bolt.security.ssl.KeyStoreInformation) Groups.boltNetworkIO(org.neo4j.kernel.impl.util.JobScheduler.Groups.boltNetworkIO) BoltConnectionDescriptor(org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor) BoltFactory(org.neo4j.bolt.v1.runtime.BoltFactory) BoltConnector(org.neo4j.kernel.configuration.BoltConnector) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) LogService(org.neo4j.kernel.impl.logging.LogService) String.format(java.lang.String.format) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Netty4LoggerFactory(org.neo4j.bolt.transport.Netty4LoggerFactory) Settings.derivedSetting(org.neo4j.kernel.configuration.Settings.derivedSetting) List(java.util.List) Description(org.neo4j.configuration.Description) BoltFactoryImpl(org.neo4j.bolt.v1.runtime.BoltFactoryImpl) KernelExtensionFactory(org.neo4j.kernel.extension.KernelExtensionFactory) WorkerFactory(org.neo4j.bolt.v1.runtime.WorkerFactory) BasicAuthentication(org.neo4j.bolt.security.auth.BasicAuthentication) GraphDatabaseSettings(org.neo4j.graphdb.factory.GraphDatabaseSettings) Settings.pathSetting(org.neo4j.kernel.configuration.Settings.pathSetting) Internal(org.neo4j.configuration.Internal) Monitors(org.neo4j.kernel.monitoring.Monitors) HashMap(java.util.HashMap) JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) Configuration(org.neo4j.graphdb.config.Configuration) ListenSocketAddress(org.neo4j.helpers.ListenSocketAddress) BoltProtocolV1(org.neo4j.bolt.v1.transport.BoltProtocolV1) Certificates(org.neo4j.bolt.security.ssl.Certificates) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) NettyServer(org.neo4j.bolt.transport.NettyServer) ThreadedWorkerFactory(org.neo4j.bolt.v1.runtime.concurrent.ThreadedWorkerFactory) Lifecycle(org.neo4j.kernel.lifecycle.Lifecycle) Config(org.neo4j.kernel.configuration.Config) SslContext(io.netty.handler.ssl.SslContext) BoltConnectionTracker(org.neo4j.kernel.api.bolt.BoltConnectionTracker) Setting(org.neo4j.graphdb.config.Setting) IOException(java.io.IOException) PATH(org.neo4j.kernel.configuration.Settings.PATH) File(java.io.File) Channel(io.netty.channel.Channel) UserManagerSupplier(org.neo4j.kernel.api.security.UserManagerSupplier) BoltWorker(org.neo4j.bolt.v1.runtime.BoltWorker) Collectors.toList(java.util.stream.Collectors.toList) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) Clock(java.time.Clock) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) MonitoredWorkerFactory(org.neo4j.bolt.v1.runtime.MonitoredWorkerFactory) AuthManager(org.neo4j.kernel.api.security.AuthManager) Config(org.neo4j.kernel.configuration.Config) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress) Clock(java.time.Clock) BoltFactory(org.neo4j.bolt.v1.runtime.BoltFactory) WorkerFactory(org.neo4j.bolt.v1.runtime.WorkerFactory) ThreadedWorkerFactory(org.neo4j.bolt.v1.runtime.concurrent.ThreadedWorkerFactory) MonitoredWorkerFactory(org.neo4j.bolt.v1.runtime.MonitoredWorkerFactory) NettyServer(org.neo4j.bolt.transport.NettyServer) BoltFactoryImpl(org.neo4j.bolt.v1.runtime.BoltFactoryImpl) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) SslContext(io.netty.handler.ssl.SslContext) ProtocolInitializer(org.neo4j.bolt.transport.NettyServer.ProtocolInitializer) JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Log(org.neo4j.logging.Log) SocketTransport(org.neo4j.bolt.transport.SocketTransport) Channel(io.netty.channel.Channel) BoltProtocol(org.neo4j.bolt.transport.BoltProtocol) Authentication(org.neo4j.bolt.security.auth.Authentication) BasicAuthentication(org.neo4j.bolt.security.auth.BasicAuthentication) ListenSocketAddress(org.neo4j.helpers.ListenSocketAddress) Netty4LoggerFactory(org.neo4j.bolt.transport.Netty4LoggerFactory) Map(java.util.Map) HashMap(java.util.HashMap) LogService(org.neo4j.kernel.impl.logging.LogService)

Example 3 with AdvertisedSocketAddress

use of org.neo4j.helpers.AdvertisedSocketAddress in project neo4j by neo4j.

the class GraphDatabaseSettingsTest method groupToScopeSetting.

@Test
public void groupToScopeSetting() throws Exception {
    // given
    String hostname = "my_other_host";
    int port = 9999;
    String scoping = "bla";
    Map<String, String> config = stringMap(GraphDatabaseSettings.default_advertised_address.name(), hostname, new BoltConnector(scoping).advertised_address.name(), ":" + port);
    // when
    BoltConnector boltConnector = new BoltConnector(scoping);
    Setting<AdvertisedSocketAddress> advertised_address = boltConnector.advertised_address;
    AdvertisedSocketAddress advertisedSocketAddress = advertised_address.apply(config::get);
    // then
    assertEquals(hostname, advertisedSocketAddress.getHostname());
    assertEquals(port, advertisedSocketAddress.getPort());
}
Also used : BoltConnector(org.neo4j.kernel.configuration.BoltConnector) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress) Test(org.junit.Test)

Example 4 with AdvertisedSocketAddress

use of org.neo4j.helpers.AdvertisedSocketAddress in project neo4j by neo4j.

the class AdvertisedAddressSettingsTest method shouldCombineDefaultHostnameWithExplicitPortWhenOnlyAPortProvided.

@Test
public void shouldCombineDefaultHostnameWithExplicitPortWhenOnlyAPortProvided() throws Exception {
    // given
    Map<String, String> config = stringMap(GraphDatabaseSettings.default_advertised_address.name(), "server1.example.com", advertised_address_setting.name(), ":4000");
    // when
    AdvertisedSocketAddress advertisedSocketAddress = advertised_address_setting.apply(config::get);
    // then
    assertEquals("server1.example.com", advertisedSocketAddress.getHostname());
    assertEquals(4000, advertisedSocketAddress.getPort());
}
Also used : AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress) Test(org.junit.Test)

Example 5 with AdvertisedSocketAddress

use of org.neo4j.helpers.AdvertisedSocketAddress in project neo4j by neo4j.

the class AnyGroupFilterTest method shouldReturnServersMatchingAnyGroup.

@Test
public void shouldReturnServersMatchingAnyGroup() throws Exception {
    // given
    AnyGroupFilter groupFilter = new AnyGroupFilter(asSet("china-west", "europe"));
    ServerInfo serverA = new ServerInfo(new AdvertisedSocketAddress("bolt", 1), new MemberId(UUID.randomUUID()), asSet("china-west"));
    ServerInfo serverB = new ServerInfo(new AdvertisedSocketAddress("bolt", 2), new MemberId(UUID.randomUUID()), asSet("europe"));
    ServerInfo serverC = new ServerInfo(new AdvertisedSocketAddress("bolt", 3), new MemberId(UUID.randomUUID()), asSet("china", "china-west"));
    ServerInfo serverD = new ServerInfo(new AdvertisedSocketAddress("bolt", 4), new MemberId(UUID.randomUUID()), asSet("china-west", "china"));
    ServerInfo serverE = new ServerInfo(new AdvertisedSocketAddress("bolt", 5), new MemberId(UUID.randomUUID()), asSet("china-east", "asia"));
    ServerInfo serverF = new ServerInfo(new AdvertisedSocketAddress("bolt", 6), new MemberId(UUID.randomUUID()), asSet("europe-west"));
    ServerInfo serverG = new ServerInfo(new AdvertisedSocketAddress("bolt", 7), new MemberId(UUID.randomUUID()), asSet("china-west", "europe"));
    ServerInfo serverH = new ServerInfo(new AdvertisedSocketAddress("bolt", 8), new MemberId(UUID.randomUUID()), asSet("africa"));
    Set<ServerInfo> data = asSet(serverA, serverB, serverC, serverD, serverE, serverF, serverG, serverH);
    // when
    Set<ServerInfo> output = groupFilter.apply(data);
    // then
    Set<Integer> ports = new HashSet<>();
    for (ServerInfo info : output) {
        ports.add(info.boltAddress().getPort());
    }
    assertEquals(asSet(1, 2, 3, 4, 7), ports);
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

AdvertisedSocketAddress (org.neo4j.helpers.AdvertisedSocketAddress)31 Test (org.junit.Test)15 HashMap (java.util.HashMap)7 MemberId (org.neo4j.causalclustering.identity.MemberId)6 BoltConnector (org.neo4j.kernel.configuration.BoltConnector)4 Config (org.neo4j.kernel.configuration.Config)4 BiFunction (java.util.function.BiFunction)3 ClientConnectorAddresses (org.neo4j.causalclustering.discovery.ClientConnectorAddresses)3 ListenSocketAddress (org.neo4j.helpers.ListenSocketAddress)3 MemberAttributeConfig (com.hazelcast.config.MemberAttributeConfig)2 Channel (io.netty.channel.Channel)2 File (java.io.File)2 InetSocketAddress (java.net.InetSocketAddress)2 URI (java.net.URI)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Before (org.junit.Before)2 ConnectorUri (org.neo4j.causalclustering.discovery.ClientConnectorAddresses.ConnectorUri)2 Cluster (org.neo4j.causalclustering.discovery.Cluster)2 CoreServerInfo (org.neo4j.causalclustering.discovery.CoreServerInfo)2