Search in sources :

Example 1 with FatalException

use of org.jboss.pnc.bacon.common.exception.FatalException in project bacon by project-ncl.

the class App method run.

public int run(String[] args) {
    CommandLine commandLine = new CommandLine(this);
    commandLine.setExecutionExceptionHandler(new ExceptionMessageHandler());
    commandLine.setUsageHelpAutoWidth(true);
    if (args.length == 0) {
        // From https://github.com/remkop/picocli/wiki/JLine-3-Examples and
        // https://github.com/remkop/picocli/tree/master/picocli-shell-jline3
        AnsiConsole.systemInstall();
        // set up JLine built-in commands
        Builtins builtins = new Builtins(App::workDir, null, null);
        builtins.rename(Builtins.Command.TTOP, "top");
        builtins.alias("zle", "widget");
        builtins.alias("bindkey", "keymap");
        PicocliCommands picocliCommands = new PicocliCommands(commandLine);
        init();
        Parser parser = new DefaultParser();
        try (Terminal terminal = TerminalBuilder.builder().build()) {
            SystemRegistry systemRegistry = new SystemRegistryImpl(parser, terminal, App::workDir, null);
            systemRegistry.setCommandRegistries(builtins, picocliCommands);
            LineReader reader = LineReaderBuilder.builder().terminal(terminal).completer(systemRegistry.completer()).parser(parser).variable(LineReader.HISTORY_FILE, Constant.HISTORY).variable(LineReader.HISTORY_FILE_SIZE, 100).variable(LineReader.LIST_MAX, // max tab completion candidates
            50).build();
            builtins.setLineReader(reader);
            TailTipWidgets tailTipWidgets = new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TailTipWidgets.TipType.COMPLETER);
            tailTipWidgets.enable();
            KeyMap<Binding> keyMap = reader.getKeyMaps().get("main");
            keyMap.bind(new Reference("tailtip-toggle"), KeyMap.alt("s"));
            log.info("Tooltips enabled ; press Alt-s to disable");
            String prompt = "prompt> ";
            // start the shell and process input until the user quits with Ctrl-D
            String line;
            while (true) {
                try {
                    systemRegistry.cleanUp();
                    line = reader.readLine(prompt, null, (MaskingCallback) null, null);
                    systemRegistry.execute(line);
                } catch (UserInterruptException e) {
                // Ignore
                } catch (EndOfFileException e) {
                    AnsiConsole.systemUninstall();
                    return 0;
                } catch (Exception e) {
                    systemRegistry.trace(e);
                }
            }
        } catch (IOException e) {
            throw new FatalException("Unable to construct terminal console", e);
        }
    } else {
        return commandLine.setExecutionStrategy(parseResult -> executionStrategy(commandLine, parseResult)).execute(args);
    }
}
Also used : Binding(org.jline.reader.Binding) ConsoleAppender(ch.qos.logback.core.ConsoleAppender) VersionProvider(org.jboss.pnc.bacon.common.cli.VersionProvider) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LineReaderBuilder(org.jline.reader.LineReaderBuilder) DefaultParser(org.jline.reader.impl.DefaultParser) Pig(org.jboss.pnc.bacon.pig.Pig) MaskingCallback(org.jline.reader.MaskingCallback) LoggerFactory(org.slf4j.LoggerFactory) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) SystemRegistryImpl(org.jline.console.impl.SystemRegistryImpl) LoggerContext(ch.qos.logback.classic.LoggerContext) PicocliCommands(picocli.shell.jline3.PicocliCommands) SystemRegistry(org.jline.console.SystemRegistry) Binding(org.jline.reader.Binding) TailTipWidgets(org.jline.widget.TailTipWidgets) Command(picocli.CommandLine.Command) Path(java.nio.file.Path) Reference(org.jline.reader.Reference) CommandLine(picocli.CommandLine) Terminal(org.jline.terminal.Terminal) KeyMap(org.jline.keymap.KeyMap) LineReader(org.jline.reader.LineReader) FatalException(org.jboss.pnc.bacon.common.exception.FatalException) Pnc(org.jboss.pnc.bacon.pnc.Pnc) IOException(java.io.IOException) TerminalBuilder(org.jline.terminal.TerminalBuilder) Level(ch.qos.logback.classic.Level) Slf4j(lombok.extern.slf4j.Slf4j) Option(picocli.CommandLine.Option) AnsiConsole(org.fusesource.jansi.AnsiConsole) EndOfFileException(org.jline.reader.EndOfFileException) Parser(org.jline.reader.Parser) Paths(java.nio.file.Paths) Config(org.jboss.pnc.bacon.config.Config) Builtins(org.jline.console.impl.Builtins) Da(org.jboss.bacon.da.Da) UserInterruptException(org.jline.reader.UserInterruptException) Constant(org.jboss.pnc.bacon.common.Constant) IExecutionExceptionHandler(picocli.CommandLine.IExecutionExceptionHandler) INHERIT(picocli.CommandLine.ScopeType.INHERIT) ObjectHelper(org.jboss.pnc.bacon.common.ObjectHelper) MaskingCallback(org.jline.reader.MaskingCallback) EndOfFileException(org.jline.reader.EndOfFileException) FatalException(org.jboss.pnc.bacon.common.exception.FatalException) Reference(org.jline.reader.Reference) PicocliCommands(picocli.shell.jline3.PicocliCommands) TailTipWidgets(org.jline.widget.TailTipWidgets) Builtins(org.jline.console.impl.Builtins) IOException(java.io.IOException) UserInterruptException(org.jline.reader.UserInterruptException) Terminal(org.jline.terminal.Terminal) SystemRegistryImpl(org.jline.console.impl.SystemRegistryImpl) FatalException(org.jboss.pnc.bacon.common.exception.FatalException) IOException(java.io.IOException) EndOfFileException(org.jline.reader.EndOfFileException) UserInterruptException(org.jline.reader.UserInterruptException) DefaultParser(org.jline.reader.impl.DefaultParser) Parser(org.jline.reader.Parser) CommandLine(picocli.CommandLine) SystemRegistry(org.jline.console.SystemRegistry) LineReader(org.jline.reader.LineReader) DefaultParser(org.jline.reader.impl.DefaultParser)

Example 2 with FatalException

use of org.jboss.pnc.bacon.common.exception.FatalException in project bacon by project-ncl.

the class PncClientHelper method setup.

public static Configuration setup(boolean authenticationNeeded) {
    Config config = Config.instance();
    KeycloakConfig keycloakConfig = config.getActiveProfile().getKeycloak();
    String bearerToken = "";
    if (authenticationNeeded) {
        if (keycloakConfig == null) {
            throw new FatalException("Keycloak section is needed in the configuration file!");
        }
        keycloakConfig.validate();
        bearerToken = getBearerToken(keycloakConfig);
        if (bearerToken == null || bearerToken.isEmpty()) {
            throw new FatalException("Credentials don't seem to be valid");
        }
    }
    config.getActiveProfile().getPnc().validate();
    String url = config.getActiveProfile().getPnc().getUrl();
    try {
        URI uri = new URI(url);
        Integer port = null;
        if (uri.getPort() != -1) {
            port = uri.getPort();
        }
        Configuration configuration = Configuration.builder().protocol(uri.getScheme()).port(port).host(uri.getHost()).bearerToken(bearerToken).pageSize(50).build();
        printBannerIfNecessary(configuration);
        return configuration;
    } catch (URISyntaxException e) {
        throw new FatalException("URI syntax issue", e);
    }
}
Also used : Configuration(org.jboss.pnc.client.Configuration) FatalException(org.jboss.pnc.bacon.common.exception.FatalException) KeycloakConfig(org.jboss.pnc.bacon.config.KeycloakConfig) Config(org.jboss.pnc.bacon.config.Config) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) KeycloakConfig(org.jboss.pnc.bacon.config.KeycloakConfig)

Example 3 with FatalException

use of org.jboss.pnc.bacon.common.exception.FatalException in project bacon by project-ncl.

the class PncClientHelper method getBearerToken.

/**
 * Return null if it couldn't get the authentication token. This generally means that the credentials are not valid
 *
 * @param keycloakConfig the keycloak config
 * @return the token, or null if we couldn't get it
 */
private static String getBearerToken(KeycloakConfig keycloakConfig) {
    log.debug("Authenticating to keycloak");
    try {
        KeycloakClient client = new DirectKeycloakClientImpl();
        Credential credential;
        if (keycloakConfig.isServiceAccount()) {
            credential = client.getCredentialServiceAccount(keycloakConfig.getUrl(), keycloakConfig.getRealm(), keycloakConfig.getUsername(), keycloakConfig.getClientSecret());
        } else {
            credential = client.getCredential(keycloakConfig.getUrl(), keycloakConfig.getRealm(), keycloakConfig.getClientId(), keycloakConfig.getUsername());
        }
        return credential.getAccessToken();
    } catch (KeycloakClientException e) {
        throw new FatalException("Keycloak authentication failed!", e);
    }
}
Also used : KeycloakClientException(org.jboss.pnc.bacon.auth.KeycloakClientException) Credential(org.jboss.pnc.bacon.auth.model.Credential) FatalException(org.jboss.pnc.bacon.common.exception.FatalException) KeycloakClient(org.jboss.pnc.bacon.auth.spi.KeycloakClient) DirectKeycloakClientImpl(org.jboss.pnc.bacon.auth.DirectKeycloakClientImpl)

Example 4 with FatalException

use of org.jboss.pnc.bacon.common.exception.FatalException in project bacon by project-ncl.

the class PigFunctionalTest method init.

static String init(Path configDir, boolean clean, Optional<String> releaseStorageUrl, Path targetDirectory) {
    String suffix = prepareSuffix();
    // todo release storage url mocking
    if (configDir == null) {
        throw new FatalException("You need to specify the configuration directory!");
    }
    // validate the PiG config
    PigConfig pig = Config.instance().getActiveProfile().getPig();
    if (pig == null) {
        throw new Validate.ConfigMissingException("Pig configuration missing");
    }
    pig.validate();
    PigContext.init(clean, configDir, targetDirectory.toAbsolutePath().toString(), releaseStorageUrl, Collections.emptyMap());
    return suffix;
}
Also used : PigConfig(org.jboss.pnc.bacon.config.PigConfig) FatalException(org.jboss.pnc.bacon.common.exception.FatalException)

Example 5 with FatalException

use of org.jboss.pnc.bacon.common.exception.FatalException in project bacon by project-ncl.

the class DirectKeycloakClientImpl method askForPassword.

private static String askForPassword() {
    Console console = System.console();
    if (console == null) {
        throw new FatalException("Couldn't get console instance");
    }
    char[] passwordArray = console.readPassword("Enter your password: ");
    return new String(passwordArray);
}
Also used : FatalException(org.jboss.pnc.bacon.common.exception.FatalException) Console(java.io.Console)

Aggregations

FatalException (org.jboss.pnc.bacon.common.exception.FatalException)7 Config (org.jboss.pnc.bacon.config.Config)2 Level (ch.qos.logback.classic.Level)1 LoggerContext (ch.qos.logback.classic.LoggerContext)1 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)1 Console (java.io.Console)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1