Example #1
0
  /** returns keypairs as a json string */
  @Action(value = "/manage/getKeyPairJSON")
  public String getKeyPairJSON() {

    AWSCred awsCred = AWSCredDB.getAWSCred();

    // set  AWS credentials for service
    BasicAWSCredentials awsCredentials =
        new BasicAWSCredentials(awsCred.getAccessKey(), awsCred.getSecretKey());
    AmazonEC2 service = new AmazonEC2Client(awsCredentials);

    service.setEndpoint(ec2Key.getEc2Region());

    DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();

    DescribeKeyPairsResult describeKeyPairsResult =
        service.describeKeyPairs(describeKeyPairsRequest);

    List<KeyPairInfo> keyPairInfoList = describeKeyPairsResult.getKeyPairs();
    String json = new Gson().toJson(keyPairInfoList);
    try {
      servletResponse.getOutputStream().write(json.getBytes());
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }
Example #2
0
  @Action(
      value = "/manage/importEC2Key",
      results = {
        @Result(name = "input", location = "/manage/view_ec2_keys.jsp"),
        @Result(name = "success", location = "/manage/viewEC2Keys.action", type = "redirect")
      })
  public String importEC2Key() {

    String retVal = SUCCESS;

    try {
      // get AWS credentials from DB
      AWSCred awsCred = AWSCredDB.getAWSCred();

      // set  AWS credentials for service
      BasicAWSCredentials awsCredentials =
          new BasicAWSCredentials(awsCred.getAccessKey(), awsCred.getSecretKey());

      // create service
      AmazonEC2 service = new AmazonEC2Client(awsCredentials);
      service.setEndpoint(ec2Key.getEc2Region());

      // describe key pair request
      DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
      describeKeyPairsRequest.setKeyNames(Arrays.asList(ec2Key.getKeyNm()));

      // call service
      DescribeKeyPairsResult describeKeyPairsResult =
          service.describeKeyPairs(describeKeyPairsRequest);

      if (describeKeyPairsResult != null && describeKeyPairsResult.getKeyPairs().size() > 0) {
        // add to db
        Long keyId = EC2KeyDB.saveEC2Key(ec2Key);
        SSHUtil.storePrivateKey(keyId.toString(), ec2Key.getPrivateKey().trim());
      } else {
        addActionError("Imported key does not exist on AWS");
        retVal = INPUT;
      }

    } catch (AmazonServiceException ex) {
      addActionError(ex.getMessage());
      retVal = INPUT;
    }

    return retVal;
  }