Java Code Examples for org.jclouds.domain.LoginCredentials#getUser()

The following examples show how to use org.jclouds.domain.LoginCredentials#getUser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JcloudsLocation.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** @return An Iterable of credentials based on nodeCreds containing different parameters. */
Iterable<LoginCredentials> generateCredentials(LoginCredentials nodeCreds, @Nullable String loginUserOverride) {
    String nodeUser = nodeCreds.getUser();
    Set<String> users = MutableSet.of();
    if (Strings.isNonBlank(nodeUser)) {
        users.add(nodeUser);
    }
    if (Strings.isNonBlank(loginUserOverride)) {
        users.add(loginUserOverride);
    }
    List<LoginCredentials> credentialsToTry = new ArrayList<>();
    for (String user : users) {
        if (nodeCreds.getOptionalPassword().isPresent() && nodeCreds.getOptionalPrivateKey().isPresent()) {
            credentialsToTry.add(LoginCredentials.builder(nodeCreds).noPassword().user(user).build());
            credentialsToTry.add(LoginCredentials.builder(nodeCreds).noPrivateKey().user(user).build());
        } else {
            credentialsToTry.add(LoginCredentials.builder(nodeCreds).user(user).build());
        }
    }
    return credentialsToTry;
}
 
Example 2
Source File: JcloudsLocation.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a temporary ssh machine location (i.e. will not be persisted), which uses the given credentials.
 * It ignores any credentials (e.g. password, key-phrase, etc) that are supplied in the config.
 */
protected SshMachineLocation createTemporarySshMachineLocation(HostAndPort hostAndPort, LoginCredentials creds, ConfigBag config) {
    String initialUser = creds.getUser();
    Optional<String> initialPassword = creds.getOptionalPassword();
    Optional<String> initialPrivateKey = creds.getOptionalPrivateKey();

    Map<String,Object> sshProps = Maps.newLinkedHashMap(config.getAllConfig());
    sshProps.put("user", initialUser);
    sshProps.put("address", hostAndPort.getHostText());
    sshProps.put("port", hostAndPort.getPort());
    sshProps.put(AbstractLocation.TEMPORARY_LOCATION.getName(), true);
    sshProps.put(LocalLocationManager.CREATE_UNMANAGED.getName(), true);

    sshProps.remove("id");
    sshProps.remove("password");
    sshProps.remove("privateKeyData");
    sshProps.remove("privateKeyFile");
    sshProps.remove("privateKeyPassphrase");

    if (initialPassword.isPresent()) sshProps.put("password", initialPassword.get());
    if (initialPrivateKey.isPresent()) sshProps.put("privateKeyData", initialPrivateKey.get());

    if (isManaged()) {
        return getManagementContext().getLocationManager().createLocation(sshProps, SshMachineLocation.class);
    } else {
        return new SshMachineLocation(sshProps);
    }
}
 
Example 3
Source File: JcloudsLocation.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a temporary WinRM machine location (i.e. will not be persisted), which uses the given credentials.
 * It ignores any credentials (e.g. password, key-phrase, etc) that are supplied in the config.
 */
protected WinRmMachineLocation createTemporaryWinRmMachineLocation(HostAndPort hostAndPort, LoginCredentials creds, ConfigBag config) {
    String initialUser = creds.getUser();
    Optional<String> initialPassword = creds.getOptionalPassword();
    Optional<String> initialPrivateKey = creds.getOptionalPrivateKey();

    Map<String,Object> winrmProps = Maps.newLinkedHashMap(config.getAllConfig());
    winrmProps.put("user", initialUser);
    winrmProps.put("address", hostAndPort.getHostText());
    winrmProps.put("port", hostAndPort.getPort());
    winrmProps.put(AbstractLocation.TEMPORARY_LOCATION.getName(), true);
    winrmProps.put(LocalLocationManager.CREATE_UNMANAGED.getName(), true);
    winrmProps.remove("password");
    winrmProps.remove("privateKeyData");
    winrmProps.remove("privateKeyFile");
    winrmProps.remove("privateKeyPassphrase");
    String winrmClass = config().get(WinRmMachineLocation.WINRM_TOOL_CLASS);
    if (Strings.isNonBlank(winrmClass)) {
        winrmProps.put(WinRmMachineLocation.WINRM_TOOL_CLASS.getName(), winrmClass);
    }

    if (initialPassword.isPresent()) winrmProps.put("password", initialPassword.get());
    if (initialPrivateKey.isPresent()) winrmProps.put("privateKeyData", initialPrivateKey.get());

    if (isManaged()) {
        return getManagementContext().getLocationManager().createLocation(winrmProps, WinRmMachineLocation.class);
    } else {
        throw new UnsupportedOperationException("Cannot create temporary WinRmMachineLocation because " + this + " is not managed");
    }
}
 
Example 4
Source File: JcloudsLocation.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
/**
 * Create the user immediately - executing ssh commands as required.
 */
protected LoginCredentials createUser(
        ComputeService computeService, NodeMetadata node, HostAndPort managementHostAndPort,
        LoginCredentials initialCredentials, ConfigBag config) {
    Image image = (node.getImageId() != null) ? computeService.getImage(node.getImageId()) : null;
    CreateUserStatements userCreation = createUserStatements(image, config);

    if (!userCreation.statements().isEmpty()) {
        // If unsure of OS family, default to unix for rendering statements.
        org.jclouds.scriptbuilder.domain.OsFamily scriptOsFamily;
        if (isWindows(node, config)) {
            scriptOsFamily = org.jclouds.scriptbuilder.domain.OsFamily.WINDOWS;
        } else {
            scriptOsFamily = org.jclouds.scriptbuilder.domain.OsFamily.UNIX;
        }

        boolean windows = isWindows(node, config);

        if (windows) {
            LOG.warn("Unable to execute statements on WinRM in JcloudsLocation; skipping for "+node+": "+userCreation.statements());
        } else {
            List<String> commands = Lists.newArrayList();
            for (Statement statement : userCreation.statements()) {
                InitAdminAccess initAdminAccess = new InitAdminAccess(new AdminAccessConfiguration.Default());
                initAdminAccess.visit(statement);
                commands.add(statement.render(scriptOsFamily));
            }

            String initialUser = initialCredentials.getUser();
            boolean authSudo = initialCredentials.shouldAuthenticateSudo();
            Optional<String> password = initialCredentials.getOptionalPassword();
            
            // TODO Retrying lots of times as workaround for vcloud-director. There the guest customizations
            // can cause the VM to reboot shortly after it was ssh'able.
            Map<String,Object> execProps = MutableMap.<String, Object>builder()
                    .put(ShellTool.PROP_RUN_AS_ROOT.getName(), true)
                    .put(SshTool.PROP_AUTH_SUDO.getName(), authSudo)
                    .put(SshTool.PROP_ALLOCATE_PTY.getName(), true)
                    .putIfNotNull(SshTool.PROP_PASSWORD.getName(), authSudo ? password.orNull() : null)
                    .put(SshTool.PROP_SSH_TRIES.getName(), 50)
            .put(SshTool.PROP_SSH_TRIES_TIMEOUT.getName(), 10*60*1000)
            .build();

            if (LOG.isDebugEnabled()) {
                LOG.debug("VM {}: executing user creation/setup via {}@{}; commands: {}", new Object[] {
                        getCreationString(config), initialUser, managementHostAndPort, commands});
            }

            SshMachineLocation sshLoc = createTemporarySshMachineLocation(managementHostAndPort, initialCredentials, config);
            try {
                // BROOKLYN-188: for SUSE, need to specify the path (for groupadd, useradd, etc)
                Map<String, ?> env = ImmutableMap.of("PATH", sbinPath());

                int exitcode = sshLoc.execScript(execProps, "create-user", commands, env);

                if (exitcode != 0) {
                    LOG.warn("exit code {} when creating user for {}; usage may subsequently fail", exitcode, node);
                }
            } finally {
                if (getManagementContext().getLocationManager().isManaged(sshLoc)) {
                    getManagementContext().getLocationManager().unmanage(sshLoc);
                }
                Streams.closeQuietly(sshLoc);
            }
        }
    }

    return userCreation.credentials();
}
 
Example 5
Source File: JcloudsLocation.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
protected void waitForReachable(Callable<Boolean> checker, String hostAndPort, Iterable<LoginCredentials> credentialsToLog, ConfigBag setup, Duration timeout) {
    if (LOG.isDebugEnabled()) {
        List<String> credsToString = Lists.newArrayList();
        for (LoginCredentials creds : credentialsToLog) {
            String user = creds.getUser();
            String password;
            String key;
            if (Boolean.TRUE.equals(setup.get(LOG_CREDENTIALS))) {
                password = creds.getOptionalPassword().or("<absent>");
                key = creds.getOptionalPrivateKey().or("<absent>");
            } else {
                password = creds.getOptionalPassword().isPresent() ? "******" : "<absent>";
                key = creds.getOptionalPrivateKey().isPresent() ? "******" : "<absent>";
            }
            credsToString.add("user="+user+", password="+password+", key="+key);
        }

        LOG.debug("VM {}: reported online, now waiting {} for it to be contactable on {}; trying {} credential{}: {}",
                new Object[] {
                        getCreationString(setup), timeout,
                        hostAndPort,
                        Iterables.size(credentialsToLog),
                        Strings.s(Iterables.size(credentialsToLog)),
                        (credsToString.size() == 1) ? credsToString.get(0) : "(multiple!):" + Joiner.on("\n\t").join(credsToString)
                });
    }

    Stopwatch stopwatch = Stopwatch.createStarted();

    ReferenceWithError<Boolean> reachable = new Repeater("reachable repeater ")
            .backoff(Duration.ONE_SECOND, 2, Duration.TEN_SECONDS) // exponential backoff, to 10 seconds
            .until(checker)
            .limitTimeTo(timeout)
            .runKeepingError();

    if (!reachable.getWithoutError()) {
        throw new IllegalStateException("Connection failed for "
                +hostAndPort+" ("+getCreationString(setup)+") after waiting "
                +Time.makeTimeStringRounded(timeout), reachable.getError());
    }

    LOG.debug("VM {}: connection succeeded after {} on {}",new Object[] {
            getCreationString(setup), Time.makeTimeStringRounded(stopwatch),
            hostAndPort});
}