Search in sources :

Example 1 with HttpDriver

use of org.neo4j.ogm.drivers.http.driver.HttpDriver in project LinkAgent by shulieTech.

the class Neo4jSessionConstructorInterceptor method afterLast.

@Override
public void afterLast(Advice advice) {
    Object[] args = advice.getParameterArray();
    Object target = advice.getTarget();
    /**
     * 压测状态判断
     * 影子session回调
     */
    if (!PradarSwitcher.isClusterTestEnabled() || target instanceof Neo4JSessionExt) {
        return;
    }
    try {
        // 业务库session
        Neo4jSession sourceSession = (Neo4jSession) target;
        DriverConfiguration driverConfiguration = ((HttpDriver) args[1]).getConfiguration();
        String s = driverConfiguration.getURI();
        Credentials credentials = driverConfiguration.getCredentials();
        String username = null;
        if (credentials instanceof AuthTokenCredentials) {
            username = ((AuthTokenCredentials) credentials).credentials();
        } else if (credentials instanceof UsernamePasswordCredentials) {
            username = ((UsernamePasswordCredentials) credentials).getUsername();
        }
        DataSourceMeta<Neo4jSession> neo4jSessionDataSourceMeta = new DataSourceMeta<Neo4jSession>(s, username, sourceSession);
        if (DataSourceWrapUtil.pressureDataSources.containsKey(neo4jSessionDataSourceMeta) && DataSourceWrapUtil.pressureDataSources.get(neo4jSessionDataSourceMeta) != null) {
            // 该业务库数据源已经初始化过影子库
            return;
        }
        if (isPerformanceDataSource(driverConfiguration)) {
            // 业务库配置是已初始化的影子库
            return;
        }
        // 从应用的影子库配置中获取
        String key = DbUrlUtils.getKey(neo4jSessionDataSourceMeta.getUrl(), neo4jSessionDataSourceMeta.getUsername());
        if (!GlobalConfig.getInstance().containsShadowDatabaseConfig(key)) {
            ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("业务库配置:::url: " + s + "; 中间件类型:other").closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
            return;
        }
        MetaData metaData = (MetaData) args[0];
        DataSourceWrapUtil.metaDataMap.put(sourceSession, metaData);
        DataSourceWrapUtil.wrap(neo4jSessionDataSourceMeta);
    } catch (Throwable e) {
        ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0003").setMessage("影子库配置异常,无法由配置正确生成影子库!").setDetail("url: " + ((HttpDriver) args[1]).getConfiguration().getURI() + Throwables.getStackTraceAsString(e)).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
        throw new PressureMeasureError("Neo4J-002:影子库初始化失败:", e);
    }
}
Also used : Neo4JSessionExt(com.pamirs.attach.plugin.neo4j.config.Neo4JSessionExt) AuthTokenCredentials(org.neo4j.ogm.authentication.AuthTokenCredentials) DriverConfiguration(org.neo4j.ogm.config.DriverConfiguration) DataSourceMeta(com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta) UsernamePasswordCredentials(org.neo4j.ogm.authentication.UsernamePasswordCredentials) MetaData(org.neo4j.ogm.MetaData) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) HttpDriver(org.neo4j.ogm.drivers.http.driver.HttpDriver) AuthTokenCredentials(org.neo4j.ogm.authentication.AuthTokenCredentials) Credentials(org.neo4j.ogm.authentication.Credentials) UsernamePasswordCredentials(org.neo4j.ogm.authentication.UsernamePasswordCredentials)

Example 2 with HttpDriver

use of org.neo4j.ogm.drivers.http.driver.HttpDriver in project neo4j-ogm by neo4j.

the class DriverServiceTest method shouldDisableCertificateValidationIfIgnoreSSLHandshake.

/**
 * This test is marked @Ignore by default because it requires a locally running
 * Neo4j server to be installed, authenticating with 'neo4j:password'.
 * Note: The mechanism to ignore SSL handshaking installs a trust-everybody trust manager into
 * any HttpDriver that is created with the 'ACCEPT_UNSIGNED' trust strategy baseConfiguration setting.
 * It does not contaminate the behaviour of other any HttpDrivers that may be running.
 */
@Test
@Ignore
public void shouldDisableCertificateValidationIfIgnoreSSLHandshake() {
    Duration.ofDays(2);
    Period.ofDays(2);
    HttpPost request = new HttpPost("https://neo4j:password@localhost:7473/db/data/transaction/commit");
    request.setEntity(new StringEntity("{\n" + "  \"statements\" : [ {\n" + "    \"statement\" : \"MATCH (n) RETURN id(n)\"\n" + "  } ]\n" + "}", "UTF-8"));
    // note that the default driver class is set from the URI if a driver class has not yet been configured
    // now set the config to ignore SSL handshaking and try again;
    Configuration configuration = new Configuration.Builder().uri("https://neo4j:password@localhost:7473").trustStrategy("ACCEPT_UNSIGNED").build();
    SessionFactory sf = new SessionFactory(configuration, "org.neo4j.ogm.domain.social.User");
    try (HttpDriver driver = sf.unwrap(HttpDriver.class)) {
        driver.configure(configuration);
        driver.executeHttpRequest(request);
        Assert.fail("Should have thrown security exception");
    } catch (Exception e) {
    // expected
    }
    sf.close();
    sf = new SessionFactory(configuration, "org.neo4j.ogm.domain.social.User");
    try (HttpDriver driver = sf.unwrap(HttpDriver.class)) {
        driver.configure(configuration);
        driver.executeHttpRequest(request);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Should NOT have thrown security exception");
    }
    sf.close();
}
Also used : SessionFactory(org.neo4j.ogm.session.SessionFactory) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpDriver(org.neo4j.ogm.drivers.http.driver.HttpDriver) IOException(java.io.IOException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

HttpDriver (org.neo4j.ogm.drivers.http.driver.HttpDriver)2 Neo4JSessionExt (com.pamirs.attach.plugin.neo4j.config.Neo4JSessionExt)1 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)1 DataSourceMeta (com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta)1 IOException (java.io.IOException)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 MetaData (org.neo4j.ogm.MetaData)1 AuthTokenCredentials (org.neo4j.ogm.authentication.AuthTokenCredentials)1 Credentials (org.neo4j.ogm.authentication.Credentials)1 UsernamePasswordCredentials (org.neo4j.ogm.authentication.UsernamePasswordCredentials)1 DriverConfiguration (org.neo4j.ogm.config.DriverConfiguration)1 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)1 SessionFactory (org.neo4j.ogm.session.SessionFactory)1