Search in sources :

Example 1 with AuthDynamicFeature

use of io.dropwizard.auth.AuthDynamicFeature in project irontest by zheng-wang.

the class IronTestApplication method createSystemResources.

private void createSystemResources(IronTestConfiguration configuration, Environment environment) {
    final DBIFactory dbiFactory = new DBIFactory();
    final DBI jdbi = dbiFactory.build(environment, configuration.getSystemDatabase(), "systemDatabase");
    // create DAO objects
    final FolderDAO folderDAO = jdbi.onDemand(FolderDAO.class);
    final EnvironmentDAO environmentDAO = jdbi.onDemand(EnvironmentDAO.class);
    final EndpointDAO endpointDAO = jdbi.onDemand(EndpointDAO.class);
    final TestcaseDAO testcaseDAO = jdbi.onDemand(TestcaseDAO.class);
    final TeststepDAO teststepDAO = jdbi.onDemand(TeststepDAO.class);
    final AssertionDAO assertionDAO = jdbi.onDemand(AssertionDAO.class);
    final UtilsDAO utilsDAO = jdbi.onDemand(UtilsDAO.class);
    final FolderTreeNodeDAO folderTreeNodeDAO = jdbi.onDemand(FolderTreeNodeDAO.class);
    final UserDefinedPropertyDAO udpDAO = jdbi.onDemand(UserDefinedPropertyDAO.class);
    final DataTableColumnDAO dataTableColumnDAO = jdbi.onDemand(DataTableColumnDAO.class);
    final DataTableCellDAO dataTableCellDAO = jdbi.onDemand(DataTableCellDAO.class);
    final TestcaseRunDAO testcaseRunDAO = jdbi.onDemand(TestcaseRunDAO.class);
    final TestcaseIndividualRunDAO testcaseIndividualRunDAO = jdbi.onDemand(TestcaseIndividualRunDAO.class);
    final TeststepRunDAO teststepRunDAO = jdbi.onDemand(TeststepRunDAO.class);
    UserDAO userDAO = null;
    if (isInTeamMode(configuration)) {
        userDAO = jdbi.onDemand(UserDAO.class);
    }
    AppInfo appInfo = new AppInfo();
    if (isInTeamMode(configuration)) {
        appInfo.setAppMode(AppMode.TEAM);
        // ignore bindHost
        DefaultServerFactory server = (DefaultServerFactory) configuration.getServerFactory();
        List<ConnectorFactory> applicationConnectors = server.getApplicationConnectors();
        HttpConnectorFactory httpConnectorFactory = (HttpConnectorFactory) applicationConnectors.get(0);
        httpConnectorFactory.setBindHost(null);
        // turn on user authentication and authorization
        environment.jersey().register(new AuthDynamicFeature(new BasicCredentialAuthFilter.Builder<SimplePrincipal>().setAuthenticator(new ResourceAuthenticator(userDAO)).setAuthorizer(new ResourceAuthorizer()).buildAuthFilter()));
        environment.jersey().register(RolesAllowedDynamicFeature.class);
        environment.jersey().register(new AuthResponseFilter());
    }
    // create database tables
    // order is important!!! (there are foreign keys linking them)
    folderDAO.createSequenceIfNotExists();
    folderDAO.createTableIfNotExists();
    folderDAO.insertARootNodeIfNotExists();
    environmentDAO.createSequenceIfNotExists();
    environmentDAO.createTableIfNotExists();
    endpointDAO.createSequenceIfNotExists();
    endpointDAO.createTableIfNotExists();
    testcaseDAO.createSequenceIfNotExists();
    testcaseDAO.createTableIfNotExists();
    teststepDAO.createSequenceIfNotExists();
    teststepDAO.createTableIfNotExists();
    assertionDAO.createSequenceIfNotExists();
    assertionDAO.createTableIfNotExists();
    udpDAO.createSequenceIfNotExists();
    udpDAO.createTableIfNotExists();
    dataTableColumnDAO.createSequenceIfNotExists();
    dataTableColumnDAO.createTableIfNotExists();
    dataTableCellDAO.createSequenceIfNotExists();
    dataTableCellDAO.createTableIfNotExists();
    testcaseRunDAO.createSequenceIfNotExists();
    testcaseRunDAO.createTableIfNotExists();
    testcaseIndividualRunDAO.createSequenceIfNotExists();
    testcaseIndividualRunDAO.createTableIfNotExists();
    teststepRunDAO.createSequenceIfNotExists();
    teststepRunDAO.createTableIfNotExists();
    if (isInTeamMode(configuration)) {
        userDAO.createSequenceIfNotExists();
        userDAO.createTableIfNotExists();
        userDAO.insertBuiltinAdminUserIfNotExists();
    }
    // register APIs
    environment.jersey().register(new SystemResource(appInfo));
    environment.jersey().register(new ManagedEndpointResource(appInfo, endpointDAO));
    environment.jersey().register(new TestcaseResource(testcaseDAO, teststepDAO));
    environment.jersey().register(new FolderResource(folderDAO));
    environment.jersey().register(new FolderTreeNodeResource(folderTreeNodeDAO));
    environment.jersey().register(new TeststepResource(appInfo, teststepDAO, udpDAO, utilsDAO));
    environment.jersey().register(new WSDLResource());
    environment.jersey().register(new EnvironmentResource(environmentDAO));
    environment.jersey().register(new TestcaseRunResource(testcaseDAO, udpDAO, teststepDAO, utilsDAO, testcaseRunDAO, teststepRunDAO));
    environment.jersey().register(new AssertionResource(udpDAO, teststepDAO, utilsDAO));
    environment.jersey().register(new UDPResource(udpDAO));
    environment.jersey().register(new DataTableResource(utilsDAO));
    if (isInTeamMode(configuration)) {
        environment.jersey().register(new UserResource(userDAO));
    }
    // if turned on in config.yml, register jersey LoggingFilter (used for logging Iron Test resource oriented HTTP API requests and responses)
    DefaultLoggingFactory defaultLoggingFactory = (DefaultLoggingFactory) configuration.getLoggingFactory();
    if (defaultLoggingFactory.getLoggers().containsKey(LoggingFilter.class.getName())) {
        environment.jersey().register(new LoggingFilter(Logger.getLogger(LoggingFilter.class.getName()), true));
    }
    // register exception mappers
    environment.jersey().register(new IronTestLoggingExceptionMapper());
}
Also used : ResourceAuthenticator(io.irontest.auth.ResourceAuthenticator) LoggingFilter(org.glassfish.jersey.filter.LoggingFilter) DefaultLoggingFactory(io.dropwizard.logging.DefaultLoggingFactory) DBI(org.skife.jdbi.v2.DBI) HttpConnectorFactory(io.dropwizard.jetty.HttpConnectorFactory) ConnectorFactory(io.dropwizard.jetty.ConnectorFactory) BasicCredentialAuthFilter(io.dropwizard.auth.basic.BasicCredentialAuthFilter) AuthDynamicFeature(io.dropwizard.auth.AuthDynamicFeature) ResourceAuthorizer(io.irontest.auth.ResourceAuthorizer) AuthResponseFilter(io.irontest.auth.AuthResponseFilter) AppInfo(io.irontest.models.AppInfo) HttpConnectorFactory(io.dropwizard.jetty.HttpConnectorFactory) SimplePrincipal(io.irontest.auth.SimplePrincipal) DBIFactory(io.dropwizard.jdbi.DBIFactory) DefaultServerFactory(io.dropwizard.server.DefaultServerFactory)

Example 2 with AuthDynamicFeature

use of io.dropwizard.auth.AuthDynamicFeature in project ratelimitj by mokies.

the class RateLimitApplication method run.

@Override
public void run(Configuration configuration, Environment environment) {
    environment.jersey().register(new LoginResource());
    environment.jersey().register(new UserResource());
    environment.jersey().register(new TrekResource());
    environment.jersey().register(new AuthDynamicFeature(new OAuthCredentialAuthFilter.Builder<PrincipalImpl>().setAuthenticator(new TestOAuthAuthenticator()).setPrefix("Bearer").buildAuthFilter()));
    environment.jersey().register(RolesAllowedDynamicFeature.class);
    environment.jersey().register(new AuthValueFactoryProvider.Binder<>(PrincipalImpl.class));
    // TODO move this cleanup into the tests
    environment.lifecycle().manage(new Managed() {

        @Override
        public void start() {
        }

        @Override
        public void stop() {
            flushRedis();
        }

        private void flushRedis() {
            try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
                connection.sync().flushdb();
            }
            redisClient.shutdownAsync();
        }
    });
}
Also used : UserResource(es.moki.ratelimij.dropwizard.component.app.api.UserResource) TestOAuthAuthenticator(es.moki.ratelimij.dropwizard.component.app.auth.TestOAuthAuthenticator) AuthValueFactoryProvider(io.dropwizard.auth.AuthValueFactoryProvider) LoginResource(es.moki.ratelimij.dropwizard.component.app.api.LoginResource) TrekResource(es.moki.ratelimij.dropwizard.component.app.api.TrekResource) StatefulRedisConnection(io.lettuce.core.api.StatefulRedisConnection) OAuthCredentialAuthFilter(io.dropwizard.auth.oauth.OAuthCredentialAuthFilter) AuthDynamicFeature(io.dropwizard.auth.AuthDynamicFeature) PrincipalImpl(io.dropwizard.auth.PrincipalImpl) Managed(io.dropwizard.lifecycle.Managed)

Example 3 with AuthDynamicFeature

use of io.dropwizard.auth.AuthDynamicFeature in project irontest by zheng-wang.

the class IronTestApplication method createSystemResources.

private void createSystemResources(IronTestConfiguration configuration, Environment environment, Jdbi systemDBJdbi, WireMockServer wireMockServer) {
    systemDBJdbi.registerArgument(new PropertiesArgumentFactory());
    systemDBJdbi.registerArgument(new EndpointPropertiesArgumentFactory());
    // create DAO objects
    final VersionDAO versionDAO = systemDBJdbi.onDemand(VersionDAO.class);
    final FolderDAO folderDAO = systemDBJdbi.onDemand(FolderDAO.class);
    final EnvironmentDAO environmentDAO = systemDBJdbi.onDemand(EnvironmentDAO.class);
    final EndpointDAO endpointDAO = systemDBJdbi.onDemand(EndpointDAO.class);
    final TestcaseDAO testcaseDAO = systemDBJdbi.onDemand(TestcaseDAO.class);
    final TeststepDAO teststepDAO = systemDBJdbi.onDemand(TeststepDAO.class);
    final AssertionDAO assertionDAO = systemDBJdbi.onDemand(AssertionDAO.class);
    final PropertyExtractorDAO propertyExtractorDAO = systemDBJdbi.onDemand(PropertyExtractorDAO.class);
    final UtilsDAO utilsDAO = systemDBJdbi.onDemand(UtilsDAO.class);
    final FolderTreeNodeDAO folderTreeNodeDAO = systemDBJdbi.onDemand(FolderTreeNodeDAO.class);
    final UserDefinedPropertyDAO udpDAO = systemDBJdbi.onDemand(UserDefinedPropertyDAO.class);
    final DataTableDAO dataTableDAO = systemDBJdbi.onDemand(DataTableDAO.class);
    final DataTableColumnDAO dataTableColumnDAO = systemDBJdbi.onDemand(DataTableColumnDAO.class);
    final DataTableCellDAO dataTableCellDAO = systemDBJdbi.onDemand(DataTableCellDAO.class);
    final TestcaseRunDAO testcaseRunDAO = systemDBJdbi.onDemand(TestcaseRunDAO.class);
    final TestcaseIndividualRunDAO testcaseIndividualRunDAO = systemDBJdbi.onDemand(TestcaseIndividualRunDAO.class);
    final TeststepRunDAO teststepRunDAO = systemDBJdbi.onDemand(TeststepRunDAO.class);
    final HTTPStubMappingDAO httpStubMappingDAO = systemDBJdbi.onDemand(HTTPStubMappingDAO.class);
    UserDAO userDAO = null;
    if (isInTeamMode(configuration)) {
        userDAO = systemDBJdbi.onDemand(UserDAO.class);
    }
    AppInfo appInfo = new AppInfo();
    if (isInTeamMode(configuration)) {
        appInfo.setAppMode(AppMode.TEAM);
        // ignore bindHost
        DefaultServerFactory server = (DefaultServerFactory) configuration.getServerFactory();
        List<ConnectorFactory> applicationConnectors = server.getApplicationConnectors();
        HttpConnectorFactory httpConnectorFactory = (HttpConnectorFactory) applicationConnectors.get(0);
        httpConnectorFactory.setBindHost(null);
        // turn on user authentication and authorization
        environment.jersey().register(new AuthDynamicFeature(new BasicCredentialAuthFilter.Builder<SimplePrincipal>().setAuthenticator(new ResourceAuthenticator(userDAO)).setAuthorizer(new ResourceAuthorizer()).buildAuthFilter()));
        environment.jersey().register(RolesAllowedDynamicFeature.class);
        environment.jersey().register(new AuthResponseFilter());
    }
    // create database tables
    // keep the order!!! (there are foreign keys linking some of them)
    versionDAO.createTableIfNotExists();
    versionDAO.insertVersionIfNotExists();
    folderDAO.createSequenceIfNotExists();
    folderDAO.createTableIfNotExists();
    folderDAO.insertARootNodeIfNotExists();
    environmentDAO.createSequenceIfNotExists();
    environmentDAO.createTableIfNotExists();
    endpointDAO.createSequenceIfNotExists();
    endpointDAO.createTableIfNotExists();
    testcaseDAO.createSequenceIfNotExists();
    testcaseDAO.createTableIfNotExists();
    teststepDAO.createSequenceIfNotExists();
    teststepDAO.createTableIfNotExists();
    assertionDAO.createSequenceIfNotExists();
    assertionDAO.createTableIfNotExists();
    propertyExtractorDAO.createSequenceIfNotExists();
    propertyExtractorDAO.createTableIfNotExists();
    udpDAO.createSequenceIfNotExists();
    udpDAO.createTableIfNotExists();
    dataTableColumnDAO.createSequenceIfNotExists();
    dataTableColumnDAO.createTableIfNotExists();
    dataTableColumnDAO.insertCaptionColumnForTestcasesWithoutDataTableColumn();
    dataTableCellDAO.createSequenceIfNotExists();
    dataTableCellDAO.createTableIfNotExists();
    testcaseRunDAO.createSequenceIfNotExists();
    testcaseRunDAO.createTableIfNotExists();
    testcaseIndividualRunDAO.createSequenceIfNotExists();
    testcaseIndividualRunDAO.createTableIfNotExists();
    teststepRunDAO.createSequenceIfNotExists();
    teststepRunDAO.createTableIfNotExists();
    httpStubMappingDAO.createSequenceIfNotExists();
    httpStubMappingDAO.createTableIfNotExists();
    if (isInTeamMode(configuration)) {
        userDAO.createSequenceIfNotExists();
        userDAO.createTableIfNotExists();
        userDAO.insertBuiltinAdminUserIfNotExists();
    }
    // register APIs
    environment.jersey().register(new SystemResource(appInfo));
    environment.jersey().register(new ManagedEndpointResource(appInfo, endpointDAO));
    environment.jersey().register(new TestcaseResource(testcaseDAO, teststepDAO));
    environment.jersey().register(new FolderResource(folderDAO, testcaseDAO));
    environment.jersey().register(new FolderTreeNodeResource(folderTreeNodeDAO));
    environment.jersey().register(new TeststepResource(appInfo, teststepDAO, udpDAO, utilsDAO, dataTableDAO, assertionDAO));
    environment.jersey().register(new WSDLResource());
    environment.jersey().register(new EnvironmentResource(environmentDAO));
    environment.jersey().register(new TestcaseRunResource(testcaseDAO, utilsDAO, testcaseRunDAO, teststepRunDAO, wireMockServer));
    environment.jersey().register(new AssertionResource(udpDAO, teststepDAO, dataTableDAO, assertionDAO));
    environment.jersey().register(new UDPResource(udpDAO));
    environment.jersey().register(new DataTableResource(dataTableDAO, dataTableColumnDAO, dataTableCellDAO));
    environment.jersey().register(new HTTPStubResource(httpStubMappingDAO, wireMockServer, udpDAO, dataTableDAO));
    environment.jersey().register(new MockServerResource(wireMockServer));
    environment.jersey().register(new PropertyExtractorResource(udpDAO, dataTableDAO, propertyExtractorDAO));
    if (isInTeamMode(configuration)) {
        environment.jersey().register(new UserResource(userDAO));
    }
    // if turned on in config.yml, register jersey LoggingFilter (used for logging Iron Test resource oriented HTTP API requests and responses)
    DefaultLoggingFactory defaultLoggingFactory = (DefaultLoggingFactory) configuration.getLoggingFactory();
    if (defaultLoggingFactory.getLoggers().containsKey(LoggingFilter.class.getName())) {
        environment.jersey().register(new LoggingFilter(Logger.getLogger(LoggingFilter.class.getName()), true));
    }
    // register exception mappers
    environment.jersey().register(new IronTestLoggingExceptionMapper());
}
Also used : ResourceAuthenticator(io.irontest.auth.ResourceAuthenticator) DefaultLoggingFactory(io.dropwizard.logging.DefaultLoggingFactory) BasicCredentialAuthFilter(io.dropwizard.auth.basic.BasicCredentialAuthFilter) AuthDynamicFeature(io.dropwizard.auth.AuthDynamicFeature) ResourceAuthorizer(io.irontest.auth.ResourceAuthorizer) AuthResponseFilter(io.irontest.auth.AuthResponseFilter) AppInfo(io.irontest.models.AppInfo) HttpConnectorFactory(io.dropwizard.jetty.HttpConnectorFactory) DefaultServerFactory(io.dropwizard.server.DefaultServerFactory) LoggingFilter(org.glassfish.jersey.filter.LoggingFilter) HttpConnectorFactory(io.dropwizard.jetty.HttpConnectorFactory) ConnectorFactory(io.dropwizard.jetty.ConnectorFactory) SimplePrincipal(io.irontest.auth.SimplePrincipal)

Example 4 with AuthDynamicFeature

use of io.dropwizard.auth.AuthDynamicFeature in project thunder by RohanNagar.

the class ThunderApplication method run.

@Override
public void run(ThunderConfiguration config, Environment env) {
    ThunderComponent component = DaggerThunderComponent.builder().daoModule(new DaoModule()).dynamoDbModule(new DynamoDbModule(config.getDynamoConfiguration())).emailModule(new EmailModule(config.getEmailConfiguration())).thunderModule(new ThunderModule(env.metrics(), config)).build();
    // Authentication
    env.jersey().register(new AuthDynamicFeature(new BasicCredentialAuthFilter.Builder<Key>().setAuthenticator(component.getThunderAuthenticator()).setRealm("THUNDER - AUTHENTICATION").buildAuthFilter()));
    env.jersey().register(new AuthValueFactoryProvider.Binder<>(Key.class));
    // HealthChecks
    env.healthChecks().register("DynamoDB", component.getDynamoDbHealthCheck());
    // Resources
    env.jersey().register(component.getUserResource());
    env.jersey().register(component.getVerificationResource());
}
Also used : BasicCredentialAuthFilter(io.dropwizard.auth.basic.BasicCredentialAuthFilter) AuthValueFactoryProvider(io.dropwizard.auth.AuthValueFactoryProvider) DynamoDbModule(com.sanction.thunder.dynamodb.DynamoDbModule) EmailModule(com.sanction.thunder.email.EmailModule) AuthDynamicFeature(io.dropwizard.auth.AuthDynamicFeature) DaoModule(com.sanction.thunder.dao.DaoModule) Key(com.sanction.thunder.authentication.Key)

Example 5 with AuthDynamicFeature

use of io.dropwizard.auth.AuthDynamicFeature in project dropwizard by dropwizard.

the class HelloWorldApplication method run.

@Override
public void run(HelloWorldConfiguration configuration, Environment environment) {
    final PersonDAO dao = new PersonDAO(hibernateBundle.getSessionFactory());
    final Template template = configuration.buildTemplate();
    environment.healthChecks().register("template", new TemplateHealthCheck(template));
    environment.admin().addTask(new EchoTask());
    environment.jersey().register(DateRequiredFeature.class);
    environment.jersey().register(new AuthDynamicFeature(new BasicCredentialAuthFilter.Builder<User>().setAuthenticator(new ExampleAuthenticator()).setAuthorizer(new ExampleAuthorizer()).setRealm("SUPER SECRET STUFF").buildAuthFilter()));
    environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));
    environment.jersey().register(RolesAllowedDynamicFeature.class);
    environment.jersey().register(new HelloWorldResource(template));
    environment.jersey().register(new ViewResource());
    environment.jersey().register(new ProtectedResource());
    environment.jersey().register(new PeopleResource(dao));
    environment.jersey().register(new PersonResource(dao));
    environment.jersey().register(new FilteredResource());
}
Also used : User(com.example.helloworld.core.User) AuthValueFactoryProvider(io.dropwizard.auth.AuthValueFactoryProvider) PeopleResource(com.example.helloworld.resources.PeopleResource) ExampleAuthorizer(com.example.helloworld.auth.ExampleAuthorizer) ExampleAuthenticator(com.example.helloworld.auth.ExampleAuthenticator) Template(com.example.helloworld.core.Template) PersonDAO(com.example.helloworld.db.PersonDAO) ProtectedResource(com.example.helloworld.resources.ProtectedResource) ViewResource(com.example.helloworld.resources.ViewResource) TemplateHealthCheck(com.example.helloworld.health.TemplateHealthCheck) AuthDynamicFeature(io.dropwizard.auth.AuthDynamicFeature) HelloWorldResource(com.example.helloworld.resources.HelloWorldResource) PersonResource(com.example.helloworld.resources.PersonResource) EchoTask(com.example.helloworld.tasks.EchoTask) FilteredResource(com.example.helloworld.resources.FilteredResource)

Aggregations

AuthDynamicFeature (io.dropwizard.auth.AuthDynamicFeature)5 AuthValueFactoryProvider (io.dropwizard.auth.AuthValueFactoryProvider)3 BasicCredentialAuthFilter (io.dropwizard.auth.basic.BasicCredentialAuthFilter)3 ConnectorFactory (io.dropwizard.jetty.ConnectorFactory)2 HttpConnectorFactory (io.dropwizard.jetty.HttpConnectorFactory)2 DefaultLoggingFactory (io.dropwizard.logging.DefaultLoggingFactory)2 DefaultServerFactory (io.dropwizard.server.DefaultServerFactory)2 AuthResponseFilter (io.irontest.auth.AuthResponseFilter)2 ResourceAuthenticator (io.irontest.auth.ResourceAuthenticator)2 ResourceAuthorizer (io.irontest.auth.ResourceAuthorizer)2 SimplePrincipal (io.irontest.auth.SimplePrincipal)2 AppInfo (io.irontest.models.AppInfo)2 LoggingFilter (org.glassfish.jersey.filter.LoggingFilter)2 ExampleAuthenticator (com.example.helloworld.auth.ExampleAuthenticator)1 ExampleAuthorizer (com.example.helloworld.auth.ExampleAuthorizer)1 Template (com.example.helloworld.core.Template)1 User (com.example.helloworld.core.User)1 PersonDAO (com.example.helloworld.db.PersonDAO)1 TemplateHealthCheck (com.example.helloworld.health.TemplateHealthCheck)1 FilteredResource (com.example.helloworld.resources.FilteredResource)1