Java Code Examples for org.jclouds.compute.domain.NodeMetadata#getCredentials()

The following examples show how to use org.jclouds.compute.domain.NodeMetadata#getCredentials() . 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 5 votes vote down vote up
/**
 * Finds a node matching the properties given in config or throws an exception.
 * @param config
 * @return
 */
protected NodeMetadata findNodeOrThrow(ConfigBag config) {
    String user = checkNotNull(getUser(config), "user");
    String rawId = (String) config.getStringKey("id");
    String rawHostname = (String) config.getStringKey("hostname");
    Predicate<ComputeMetadata> predicate = getRebindToMachinePredicate(config);
    LOG.debug("Finding VM {} ({}@{}), in jclouds location for provider {} matching {}", new Object[]{
            rawId != null ? rawId : "<lookup>",
            user,
            rawHostname != null ? rawHostname : "<unspecified>",
            getProvider(),
            predicate
    });
    ComputeService computeService = getComputeService(config);
    Set<? extends NodeMetadata> candidateNodes = computeService.listNodesDetailsMatching(predicate);
    if (candidateNodes.isEmpty()) {
        throw new IllegalArgumentException("Jclouds node not found for rebind with predicate " + predicate);
    } else if (candidateNodes.size() > 1) {
        throw new IllegalArgumentException("Jclouds node for rebind matched multiple with " + predicate + ": " + candidateNodes);
    }
    NodeMetadata node = Iterables.getOnlyElement(candidateNodes);

    OsCredential osCredentials = LocationConfigUtils.getOsCredential(config).checkNoErrors().logAnyWarnings();
    String pkd = osCredentials.getPrivateKeyData();
    String password = osCredentials.getPassword();
    LoginCredentials expectedCredentials = node.getCredentials();
    if (Strings.isNonBlank(pkd)) {
        expectedCredentials = LoginCredentials.fromCredentials(new Credentials(user, pkd));
    } else if (Strings.isNonBlank(password)) {
        expectedCredentials = LoginCredentials.fromCredentials(new Credentials(user, password));
    } else if (expectedCredentials == null) {
        //need some kind of credential object, or will get NPE later
        expectedCredentials = LoginCredentials.fromCredentials(new Credentials(user, null));
    }
    node = NodeMetadataBuilder.fromNodeMetadata(node).credentials(expectedCredentials).build();

    return node;
}
 
Example 2
Source File: JcloudsLocation.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected Map<String,Object> extractSshConfig(ConfigBag setup, NodeMetadata node) {
    ConfigBag nodeConfig = new ConfigBag();
    if (node!=null && node.getCredentials() != null) {
        nodeConfig.putIfNotNull(PASSWORD, node.getCredentials().getOptionalPassword().orNull());
        nodeConfig.putIfNotNull(PRIVATE_KEY_DATA, node.getCredentials().getOptionalPrivateKey().orNull());
    }
    return extractSshConfig(setup, nodeConfig).getAllConfig();
}
 
Example 3
Source File: JcloudsLocation.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected Map<String,Object> extractWinrmConfig(ConfigBag setup, NodeMetadata node) {
    ConfigBag nodeConfig = new ConfigBag();
    if (node!=null && node.getCredentials() != null) {
        nodeConfig.putIfNotNull(PASSWORD, node.getCredentials().getOptionalPassword().orNull());
        nodeConfig.putIfNotNull(PRIVATE_KEY_DATA, node.getCredentials().getOptionalPrivateKey().orNull());
    }
    return extractWinrmConfig(setup, nodeConfig).getAllConfig();
}
 
Example 4
Source File: JcloudsLocation.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected void setHostnameUpdatingCredentials(ConfigBag setup, NodeMetadata metadata) {
    List<String> usersTried = new ArrayList<String>();

    String originalUser = getUser(setup);
    if (groovyTruth(originalUser)) {
        if (setHostname(setup, metadata, false)) return;
        usersTried.add(originalUser);
    }

    LoginCredentials credentials = metadata.getCredentials();
    if (credentials!=null) {
        if (Strings.isNonBlank(credentials.getUser())) setup.put(USER, credentials.getUser());
        if (Strings.isNonBlank(credentials.getOptionalPrivateKey().orNull())) setup.put(PRIVATE_KEY_DATA, credentials.getOptionalPrivateKey().orNull());
        if (setHostname(setup, metadata, false)) {
            if (originalUser!=null && !originalUser.equals(getUser(setup))) {
                LOG.warn("Switching to cloud-specified user at "+metadata+" as "+getUser(setup)+" (failed to connect using: "+usersTried+")");
            }
            return;
        }
        usersTried.add(getUser(setup));
    }

    for (String u: COMMON_USER_NAMES_TO_TRY) {
        setup.put(USER, u);
        if (setHostname(setup, metadata, false)) {
            LOG.warn("Auto-detected user at "+metadata+" as "+getUser(setup)+" (failed to connect using: "+usersTried+")");
            return;
        }
        usersTried.add(getUser(setup));
    }
    // just repeat, so we throw exception
    LOG.warn("Failed to log in to "+metadata+", tried as users "+usersTried+" (throwing original exception)");
    setup.put(USER, originalUser);
    setHostname(setup, metadata, true);
}
 
Example 5
Source File: DefaultConnectivityResolver.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
protected Iterable<LoginCredentials> getCredentialCandidates(
        JcloudsLocation location, NodeMetadata node, ConnectivityResolverOptions options, ConfigBag setup) {
    LoginCredentials userCredentials = null;
    // Figure out which login credentials to use. We only make a connection with
    // initialCredentials when jclouds didn't do any sshing and wait for connectable is true.
    // 0. if jclouds didn't do anything and we should wait for the machine then initial credentials is
    //    whatever waitForSshable determines and then create the user ourselves.
    if (options.skipJcloudsSshing() && options.waitForConnectable()) {
        if (options.isWindows() && options.initialCredentials().isPresent()) {
            return ImmutableList.of(options.initialCredentials().get());
        } else {
            return location.generateCredentials(node.getCredentials(), setup.get(JcloudsLocationConfig.LOGIN_USER));
        }
    }

    // 1. Were they configured by the user?
    LoginCredentials customCredentials = setup.get(JcloudsLocationConfig.CUSTOM_CREDENTIALS);
    if (customCredentials != null) {
        userCredentials = customCredentials;
        //set userName and other data, from these credentials
        Object oldUsername = setup.put(JcloudsLocationConfig.USER, customCredentials.getUser());
        LOG.debug("Using username {}, from custom credentials, on node {}. User was previously {}",
                new Object[]{customCredentials.getUser(), node, oldUsername});
        if (customCredentials.getOptionalPassword().isPresent()) {
            setup.put(JcloudsLocationConfig.PASSWORD, customCredentials.getOptionalPassword().get());
        }
        if (customCredentials.getOptionalPrivateKey().isPresent()) {
            setup.put(JcloudsLocationConfig.PRIVATE_KEY_DATA, customCredentials.getOptionalPrivateKey().get());
        }
    }
    // 2. Can they be extracted from the setup+node?
    if ((userCredentials == null ||
            (!userCredentials.getOptionalPassword().isPresent() && !userCredentials.getOptionalPrivateKey().isPresent())) &&
            options.initialCredentials().isPresent()) {
        // We either don't have any userCredentials, or it is missing both a password/key.
        if (userCredentials != null) {
            LOG.debug("Custom credential from {} is missing both password and private key; " +
                    "extracting them from the VM: {}", JcloudsLocationConfig.CUSTOM_CREDENTIALS.getName(), userCredentials);
        }
        // TODO See waitForSshable, which now handles if the node.getLoginCredentials has both a password+key
        userCredentials = location.extractVmCredentials(setup, node, options.initialCredentials().get());
    }
    if (userCredentials == null) {
        // only happens if something broke above...
        userCredentials = node.getCredentials();
    }

    return userCredentials != null? ImmutableList.of(userCredentials) : ImmutableList.<LoginCredentials>of();
}