Search in sources :

Example 6 with ConfigContext

use of com.nexblocks.authguard.config.ConfigContext in project AuthGuard by AuthGuard.

the class ExchangesBinderTest method unsupportedExchange.

@Test
void unsupportedExchange() {
    final ConfigContext rootContext = Mockito.mock(ConfigContext.class);
    final ConfigContext exchangeContext = Mockito.mock(ConfigContext.class);
    final ExchangesBinder.ExchangeConfig allowedExchange = new ExchangesBinder.ExchangeConfig();
    allowedExchange.setFrom("unsupported");
    allowedExchange.setTo("random");
    Mockito.when(rootContext.getSubContext("exchange")).thenReturn(exchangeContext);
    Mockito.when(exchangeContext.getAsCollection("allowed", ExchangesBinder.ExchangeConfig.class)).thenReturn(Collections.singletonList(allowedExchange));
    final Collection<String> searchPackages = Collections.singletonList("com.nexblocks.authguard.bindings");
    assertThatThrownBy(() -> Guice.createInjector(new ExchangesBinder(rootContext, searchPackages))).isInstanceOf(CreationException.class).extracting("cause").isInstanceOf(ConfigurationException.class);
}
Also used : ConfigContext(com.nexblocks.authguard.config.ConfigContext) Test(org.junit.jupiter.api.Test)

Example 7 with ConfigContext

use of com.nexblocks.authguard.config.ConfigContext in project AuthGuard by AuthGuard.

the class MessageBusTest method setup.

@BeforeAll
void setup() {
    final ObjectNode configNode = new ObjectNode(JsonNodeFactory.instance);
    configNode.set("channels", new ArrayNode(JsonNodeFactory.instance).add("accounts").add("auth"));
    final ConfigContext configContext = new JacksonConfigContext(configNode);
    messageBus = new MessageBus(new RxPublisherFactory(), configContext);
}
Also used : JacksonConfigContext(com.nexblocks.authguard.config.JacksonConfigContext) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RxPublisherFactory(com.nexblocks.authguard.emb.rxjava.RxPublisherFactory) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JacksonConfigContext(com.nexblocks.authguard.config.JacksonConfigContext) ConfigContext(com.nexblocks.authguard.config.ConfigContext) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 8 with ConfigContext

use of com.nexblocks.authguard.config.ConfigContext in project AuthGuard by AuthGuard.

the class VerificationSubscriberTest method setup.

@BeforeEach
void setup() {
    emailProvider = Mockito.mock(EmailProvider.class);
    accountTokensRepository = Mockito.mock(AccountTokensRepository.class);
    final ConfigContext configContext = Mockito.mock(ConfigContext.class);
    final ImmutableVerificationConfig verificationConfig = ImmutableVerificationConfig.builder().emailVerificationLife("1d").build();
    Mockito.when(configContext.asConfigBean(ImmutableVerificationConfig.class)).thenReturn(verificationConfig);
    verificationSubscriber = new VerificationSubscriber(emailProvider, accountTokensRepository, configContext);
}
Also used : EmailProvider(com.nexblocks.authguard.external.email.EmailProvider) AccountTokensRepository(com.nexblocks.authguard.dal.cache.AccountTokensRepository) ConfigContext(com.nexblocks.authguard.config.ConfigContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 9 with ConfigContext

use of com.nexblocks.authguard.config.ConfigContext in project AuthGuard by AuthGuard.

the class AuthGuardCli method execute.

public int execute(final String[] args) {
    final Options options = cliOption();
    final CommandLine cmd = parseCommandLineOptions(options, args);
    if (cmd == null) {
        return 1;
    }
    // help
    if (cmd.hasOption("help")) {
        new HelpFormatter().printHelp("authguard", options);
        return 0;
    }
    // config
    final ConfigContext configContext;
    if (cmd.hasOption("config")) {
        configContext = configurationLoader.loadFromFile(cmd.getOptionValue("config"));
    } else {
        configContext = configurationLoader.loadFromResources();
    }
    log.info("Initialized configuration context");
    log.debug("Loaded configuration: {}", configContext);
    // run the server
    try {
        serverRunner.run(configContext);
    } catch (final ProvisionException | CreationException e) {
        if (e.getCause() != null) {
            if (e.getCause().getMessage() != null) {
                log.error("Failed to initialize the server. Error: {}", e.getCause().getMessage());
            } else {
                log.error("Failed to initialize the server. Error: ", e.getCause());
            }
        } else {
            log.error("Failed to initialize the server. Error: ", e);
        }
        return 1;
    } catch (final ConfigurationException e) {
        log.error("Configuration error: {}", e.getMessage());
        return 2;
    } catch (final InitializationException e) {
        log.error("Failed to initialize the server. Error: {}", e.getMessage());
        return 3;
    }
    return 0;
}
Also used : ProvisionException(com.google.inject.ProvisionException) ConfigurationException(com.nexblocks.authguard.service.exceptions.ConfigurationException) CreationException(com.google.inject.CreationException) InitializationException(com.nexblocks.authguard.rest.exceptions.InitializationException) ConfigContext(com.nexblocks.authguard.config.ConfigContext)

Example 10 with ConfigContext

use of com.nexblocks.authguard.config.ConfigContext in project AuthGuard by AuthGuard.

the class ServerRunner method run.

public void run(final ConfigContext configContext) {
    // class search
    final Collection<String> searchPackages = configContext.getSubContext("injection").getAsCollection("packages", String.class);
    final ClassSearch classSearch = new ClassSearch(searchPackages);
    // injectors
    final Injector injector = Guice.createInjector(new MappersBinder(), new ConfigBinder(configContext), new ExchangesBinder(configContext, searchPackages), new ApiKeysExchangeBinder(configContext, searchPackages), new ApiRoutesBinder(searchPackages, configContext), new ServicesBinder(configContext), new JwtBinder(configContext), new DalBinder(configContext, searchPackages), new EmbBinder(searchPackages), new ExternalProvidersBinder(configContext, searchPackages));
    log.info("Initialed injection binders");
    // run bootstraps
    new BootstrapRunner(classSearch, injector).runAll();
    log.info("Completed bootstrap");
    // run the server
    final ImmutableServerConfig serverConfig = Optional.ofNullable(configContext.getAsConfigBean("server", ImmutableServerConfig.class)).orElseGet(() -> ImmutableServerConfig.builder().port(3000).build());
    new AuthGuardServer(injector).start(Javalin.create(config -> {
        config.enforceSsl = serverConfig.enforceSsl();
        config.server(() -> new JettyServerProvider(serverConfig).get());
        config.accessManager(new RolesAccessManager());
    }));
}
Also used : com.nexblocks.authguard.bindings(com.nexblocks.authguard.bindings) Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) BootstrapRunner(com.nexblocks.authguard.bootstrap.BootstrapRunner) JettyServerProvider(com.nexblocks.authguard.rest.server.JettyServerProvider) MappersBinder(com.nexblocks.authguard.rest.bindings.MappersBinder) Javalin(io.javalin.Javalin) AuthGuardServer(com.nexblocks.authguard.rest.server.AuthGuardServer) ClassSearch(com.nexblocks.authguard.injection.ClassSearch) Injector(com.google.inject.Injector) RolesAccessManager(com.nexblocks.authguard.rest.access.RolesAccessManager) Guice(com.google.inject.Guice) Optional(java.util.Optional) ConfigContext(com.nexblocks.authguard.config.ConfigContext) ImmutableServerConfig(com.nexblocks.authguard.rest.config.ImmutableServerConfig) AuthGuardServer(com.nexblocks.authguard.rest.server.AuthGuardServer) MappersBinder(com.nexblocks.authguard.rest.bindings.MappersBinder) RolesAccessManager(com.nexblocks.authguard.rest.access.RolesAccessManager) JettyServerProvider(com.nexblocks.authguard.rest.server.JettyServerProvider) Injector(com.google.inject.Injector) ImmutableServerConfig(com.nexblocks.authguard.rest.config.ImmutableServerConfig) ClassSearch(com.nexblocks.authguard.injection.ClassSearch) BootstrapRunner(com.nexblocks.authguard.bootstrap.BootstrapRunner)

Aggregations

ConfigContext (com.nexblocks.authguard.config.ConfigContext)16 Test (org.junit.jupiter.api.Test)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 JacksonConfigContext (com.nexblocks.authguard.config.JacksonConfigContext)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 ServiceMapperImpl (com.nexblocks.authguard.service.mappers.ServiceMapperImpl)3 Injector (com.google.inject.Injector)2 OtpRepository (com.nexblocks.authguard.dal.cache.OtpRepository)2 MessageBus (com.nexblocks.authguard.emb.MessageBus)2 EmailProvider (com.nexblocks.authguard.external.email.EmailProvider)2 ExchangeService (com.nexblocks.authguard.service.ExchangeService)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 CreationException (com.google.inject.CreationException)1 Guice (com.google.inject.Guice)1 ProvisionException (com.google.inject.ProvisionException)1 Named (com.google.inject.name.Named)1 com.nexblocks.authguard.bindings (com.nexblocks.authguard.bindings)1 BootstrapRunner (com.nexblocks.authguard.bootstrap.BootstrapRunner)1 EmptyConfigContext (com.nexblocks.authguard.config.EmptyConfigContext)1