Exemplo n.º 1
0
 @Override
 public String toString() {
   return Objects.toStringHelper("")
       .add("hostAndPort", hostAndPort)
       .add("loginUser", loginCredentials.getUser())
       .add("session", session != null ? session.hashCode() : null)
       .add("connectTimeout", connectTimeout)
       .add("sessionTimeout", sessionTimeout)
       .toString();
 }
Exemplo n.º 2
0
 public JschSshClient(
     ProxyConfig proxyConfig,
     BackoffLimitedRetryHandler backoffLimitedRetryHandler,
     HostAndPort socket,
     LoginCredentials loginCredentials,
     int timeout) {
   this.user = checkNotNull(loginCredentials, "loginCredentials").getUser();
   this.host = checkNotNull(socket, "socket").getHostText();
   checkArgument(socket.getPort() > 0, "ssh port must be greater then zero" + socket.getPort());
   checkArgument(
       loginCredentials.getPassword() != null || loginCredentials.getPrivateKey() != null,
       "you must specify a password or a key");
   this.backoffLimitedRetryHandler =
       checkNotNull(backoffLimitedRetryHandler, "backoffLimitedRetryHandler");
   if (loginCredentials.getPrivateKey() == null) {
     this.toString =
         String.format(
             "%s:pw[%s]@%s:%d",
             loginCredentials.getUser(),
             base16()
                 .lowerCase()
                 .encode(md5().hashString(loginCredentials.getPassword(), UTF_8).asBytes()),
             host,
             socket.getPort());
   } else {
     String fingerPrint = fingerprintPrivateKey(loginCredentials.getPrivateKey());
     String sha1 = sha1PrivateKey(loginCredentials.getPrivateKey());
     this.toString =
         String.format(
             "%s:rsa[fingerprint(%s),sha1(%s)]@%s:%d",
             loginCredentials.getUser(), fingerPrint, sha1, host, socket.getPort());
   }
   sessionConnection =
       SessionConnection.builder()
           .hostAndPort(HostAndPort.fromParts(host, socket.getPort()))
           .loginCredentials(loginCredentials)
           .proxy(checkNotNull(proxyConfig, "proxyConfig"))
           .connectTimeout(timeout)
           .sessionTimeout(timeout)
           .build();
 }
  @Override
  public NodeAndInitialCredentials<Instance> createNodeWithGroupEncodedIntoName(
      String group, String name, Template template) {
    GoogleComputeEngineTemplateOptions options =
        GoogleComputeEngineTemplateOptions.class.cast(template.getOptions());

    checkNotNull(options.getNetworks(), "template options must specify a network");
    checkNotNull(template.getHardware().getUri(), "hardware must have a URI");
    checkNotNull(template.getImage().getUri(), "image URI is null");

    List<AttachDisk> disks = Lists.newArrayList();
    disks.add(AttachDisk.newBootDisk(template.getImage().getUri()));

    Iterator<String> networks = options.getNetworks().iterator();

    URI network = URI.create(networks.next());
    assert !networks.hasNext() : "Error: Options should specify only one network";

    // Add tags from template
    ArrayList<String> tags = new ArrayList<String>(options.getTags());

    // Add tags for firewalls
    FirewallTagNamingConvention naming = firewallTagNamingConvention.get(group);
    List<String> ports = simplifyPorts(options.getInboundPorts());
    if (ports != null) {
      tags.add(naming.name(ports));
    }

    NewInstance newInstance =
        new NewInstance.Builder(
                name,
                template.getHardware().getUri(), // machineType
                network,
                disks)
            .description(group)
            .tags(Tags.create(null, ImmutableList.copyOf(tags)))
            .serviceAccounts(options.serviceAccounts())
            .build();

    // Add metadata from template and for ssh key and image id
    newInstance.metadata().putAll(options.getUserMetadata());

    LoginCredentials credentials = resolveNodeCredentials(template);
    if (options.getPublicKey() != null) {
      newInstance
          .metadata()
          .put(
              "sshKeys",
              format(
                  "%s:%s %s@localhost",
                  credentials.getUser(), options.getPublicKey(), credentials.getUser()));
    }

    String zone = template.getLocation().getId();
    InstanceApi instanceApi = api.instancesInZone(zone);
    Operation create = instanceApi.create(newInstance);

    // We need to see the created instance so that we can access the newly created disk.
    AtomicReference<Instance> instance =
        Atomics.newReference(
            Instance.create( //
                "0000000000000000000", // id can't be null, but isn't available until provisioning
                                       // is done.
                null, // creationTimestamp
                create.targetLink(), // selfLink
                newInstance.name(), // name
                newInstance.description(), // description
                newInstance.tags(), // tags
                newInstance.machineType(), // machineType
                Instance.Status.PROVISIONING, // status
                null, // statusMessage
                create.zone(), // zone
                null, // canIpForward
                null, // networkInterfaces
                null, // disks
                newInstance.metadata(), // metadata
                newInstance.serviceAccounts(), // serviceAccounts
                Scheduling.create(OnHostMaintenance.MIGRATE, true) // scheduling
                ));
    checkState(instanceVisible.apply(instance), "instance %s is not api visible!", instance.get());

    // Add lookup for InstanceToNodeMetadata
    diskToSourceImage.put(instance.get().disks().get(0).source(), template.getImage().getUri());

    return new NodeAndInitialCredentials<Instance>(
        instance.get(), instance.get().selfLink().toString(), credentials);
  }
Exemplo n.º 4
-1
 @Override
 public Session create() throws Exception {
   JSch jsch = new JSch();
   session =
       jsch.getSession(
           loginCredentials.getUser(),
           hostAndPort.getHostText(),
           hostAndPort.getPortOrDefault(22));
   if (sessionTimeout != 0) session.setTimeout(sessionTimeout);
   if (loginCredentials.getPrivateKey() == null) {
     session.setPassword(loginCredentials.getPassword());
   } else {
     byte[] privateKey = loginCredentials.getPrivateKey().getBytes();
     if (CredentialUtils.isPrivateKeyEncrypted(privateKey)) {
       throw new IllegalArgumentException(
           "JschSshClientModule does not support private keys that require a passphrase");
     }
     jsch.addIdentity(
         loginCredentials.getUser(),
         Arrays.copyOf(privateKey, privateKey.length),
         null,
         emptyPassPhrase);
   }
   java.util.Properties config = new java.util.Properties();
   config.put("StrictHostKeyChecking", "no");
   session.setConfig(config);
   session.connect(connectTimeout);
   return session;
 }