Ejemplo n.º 1
0
 public FormValidation doCheckAccessId(@QueryParameter String value)
     throws IOException, ServletException {
   if (value.trim().length() != 20) {
     return FormValidation.error(Messages.EC2Cloud_InvalidAccessId());
   }
   return FormValidation.validateBase64(
       value, false, false, Messages.EC2Cloud_InvalidAccessId());
 }
Ejemplo n.º 2
0
    protected FormValidation doTestConnection(
        URL ec2endpoint,
        boolean useInstanceProfileForCredentials,
        String accessId,
        String secretKey,
        String privateKey)
        throws IOException, ServletException {
      try {
        AWSCredentialsProvider credentialsProvider =
            createCredentialsProvider(useInstanceProfileForCredentials, accessId, secretKey);
        AmazonEC2 ec2 = connect(credentialsProvider, ec2endpoint);
        ec2.describeInstances();

        if (privateKey == null)
          return FormValidation.error(
              "Private key is not specified. Click 'Generate Key' to generate one.");

        if (privateKey.trim().length() > 0) {
          // check if this key exists
          EC2PrivateKey pk = new EC2PrivateKey(privateKey);
          if (pk.find(ec2) == null)
            return FormValidation.error(
                "The EC2 key pair private key isn't registered to this EC2 region (fingerprint is "
                    + pk.getFingerprint()
                    + ")");
        }

        return FormValidation.ok(Messages.EC2Cloud_Success());
      } catch (AmazonClientException e) {
        LOGGER.log(Level.WARNING, "Failed to check EC2 credential", e);
        return FormValidation.error(e.getMessage());
      }
    }
Ejemplo n.º 3
0
    public FormValidation doCheckUseInstanceProfileForCredentials(@QueryParameter boolean value) {
      if (value) {
        try {
          new InstanceProfileCredentialsProvider().getCredentials();
        } catch (AmazonClientException e) {
          return FormValidation.error(
              Messages.EC2Cloud_FailedToObtainCredentailsFromEC2(), e.getMessage());
        }
      }

      return FormValidation.ok();
    }
Ejemplo n.º 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());
      }
    }
Ejemplo n.º 5
0
 public FormValidation doCheckSecretKey(@QueryParameter String value)
     throws IOException, ServletException {
   return FormValidation.validateBase64(
       value, false, false, Messages.EC2Cloud_InvalidSecretKey());
 }