public KeyPairInfo unmarshall(StaxUnmarshallerContext context) throws Exception {
    KeyPairInfo keyPairInfo = new KeyPairInfo();
    int originalDepth = context.getCurrentDepth();
    int targetDepth = originalDepth + 1;

    if (context.isStartOfDocument()) targetDepth += 1;

    while (true) {
      XMLEvent xmlEvent = context.nextEvent();
      if (xmlEvent.isEndDocument()) return keyPairInfo;

      if (xmlEvent.isAttribute() || xmlEvent.isStartElement()) {
        if (context.testExpression("keyName", targetDepth)) {
          keyPairInfo.setKeyName(StringStaxUnmarshaller.getInstance().unmarshall(context));
          continue;
        }
        if (context.testExpression("keyFingerprint", targetDepth)) {
          keyPairInfo.setKeyFingerprint(StringStaxUnmarshaller.getInstance().unmarshall(context));
          continue;
        }
      } else if (xmlEvent.isEndElement()) {
        if (context.getCurrentDepth() < originalDepth) {
          return keyPairInfo;
        }
      }
    }
  }
Пример #2
0
  private static void createKey(String keyName, AmazonEC2 ec2) {
    try {
      List<KeyPairInfo> keyPairList = ec2.describeKeyPairs().getKeyPairs();
      for (KeyPairInfo keyPair : keyPairList) {
        if (keyName.equalsIgnoreCase(keyPair.getKeyName())) {
          System.out.println("Using key " + keyName);
          return;
        }
      }
      System.out.println("Creating key " + keyName + "in local directory");
      CreateKeyPairRequest newKeyRequest = new CreateKeyPairRequest();
      newKeyRequest.setKeyName(keyName);
      CreateKeyPairResult keyresult = ec2.createKeyPair(newKeyRequest);
      KeyPair keyPair = new KeyPair();
      keyPair = keyresult.getKeyPair();
      String privateKey = keyPair.getKeyMaterial();
      writeKeytoFile(keyName, privateKey);

    } catch (AmazonServiceException ase) {
      System.out.println("Caught Exception: " + ase.getMessage());
      System.out.println("Reponse Status Code: " + ase.getStatusCode());
      System.out.println("Error Code: " + ase.getErrorCode());
      System.out.println("Request ID: " + ase.getRequestId());
    }
  }
Пример #3
0
  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;

    if (obj instanceof KeyPairInfo == false) return false;
    KeyPairInfo other = (KeyPairInfo) obj;

    if (other.getKeyName() == null ^ this.getKeyName() == null) return false;
    if (other.getKeyName() != null && other.getKeyName().equals(this.getKeyName()) == false)
      return false;
    if (other.getKeyFingerprint() == null ^ this.getKeyFingerprint() == null) return false;
    if (other.getKeyFingerprint() != null
        && other.getKeyFingerprint().equals(this.getKeyFingerprint()) == false) return false;
    return true;
  }
Пример #4
0
    public FormValidation doGenerateKey(
        StaplerResponse rsp,
        URL ec2EndpointUrl,
        boolean useInstanceProfileForCredentials,
        String accessId,
        String secretKey)
        throws IOException, ServletException {
      try {
        AWSCredentialsProvider credentialsProvider =
            createCredentialsProvider(useInstanceProfileForCredentials, accessId, secretKey);
        AmazonEC2 ec2 = connect(credentialsProvider, ec2EndpointUrl);
        List<KeyPairInfo> existingKeys = ec2.describeKeyPairs().getKeyPairs();

        int n = 0;
        while (true) {
          boolean found = false;
          for (KeyPairInfo k : existingKeys) {
            if (k.getKeyName().equals("hudson-" + n)) found = true;
          }
          if (!found) break;
          n++;
        }

        CreateKeyPairRequest request = new CreateKeyPairRequest("hudson-" + n);
        KeyPair key = ec2.createKeyPair(request).getKeyPair();

        rsp.addHeader(
            "script",
            "findPreviousFormItem(button,'privateKey').value='"
                + key.getKeyMaterial().replace("\n", "\\n")
                + "'");

        return FormValidation.ok(Messages.EC2Cloud_Success());
      } catch (AmazonClientException e) {
        LOGGER.log(Level.WARNING, "Failed to check EC2 credential", e);
        return FormValidation.error(e.getMessage());
      }
    }