Beispiel #1
0
  @Action(
      value = "/admin/exitTerms",
      results = {@Result(name = "success", location = "/admin/menu.action", type = "redirect")})
  public String exitTerms() {

    Long userId = AuthUtil.getUserId(servletRequest.getSession());
    // check user map
    if (userSchSessionMap != null && !userSchSessionMap.isEmpty()) {

      // get user servletRequest.getSession()s
      for (Long userKey : userSchSessionMap.keySet()) {
        UserSchSessions userSchSessions = userSchSessionMap.get(userKey);

        // get current time and subtract number of hours set to determine expire time
        Calendar expireTime = Calendar.getInstance();
        expireTime.add(
            Calendar.HOUR,
            (-1
                * Integer.parseInt(
                    AppConfigLkup.getProperty(
                        "timeoutSshAfter")))); // subtract hours to get expire time

        // if current user or session has timed out remove ssh session
        if (userId.equals(userKey) || userSchSessions.getStartTime().before(expireTime.getTime())) {
          Map<Long, SchSession> schSessionMap = userSchSessionMap.get(userKey).getSchSessionMap();

          for (Long sessionKey : schSessionMap.keySet()) {

            SchSession schSession = schSessionMap.get(sessionKey);

            // disconnect ssh session
            schSession.getChannel().disconnect();
            schSession.getSession().disconnect();
            schSession.setChannel(null);
            schSession.setSession(null);
            schSession.setInputToChannel(null);
            schSession.setCommander(null);
            schSession.setOutFromChannel(null);
            schSession = null;
            // remove from map
            schSessionMap.remove(sessionKey);
          }

          // clear and remove session map for user
          schSessionMap.clear();
          userSchSessionMap.remove(userKey);
          SessionOutputUtil.removeUserSession(userKey);
        }
      }
    }

    return SUCCESS;
  }
Beispiel #2
0
/** Action to import private key for EC2 instances */
public class EC2KeyAction extends ActionSupport implements ServletResponseAware {

  EC2Key ec2Key;
  SortedSet sortedSet = new SortedSet();
  HttpServletResponse servletResponse;
  static Map<String, String> ec2RegionMap = AppConfigLkup.getMapProperties("ec2Regions");

  @Action(
      value = "/manage/viewEC2Keys",
      results = {@Result(name = "success", location = "/manage/view_ec2_keys.jsp")})
  public String viewEC2Keys() {

    AWSCred awsCred = AWSCredDB.getAWSCred();
    // check to see if aws creds have been set
    if (awsCred != null) {
      sortedSet = EC2KeyDB.getEC2KeySet(sortedSet);
    } else {
      addActionMessage(
          "EC2 Keys not available. Set AWS credentials <a href=\"setAWSCred.action\">here</a>");
    }

    return SUCCESS;
  }

  /** 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;
  }

  @Action(
      value = "/manage/submitEC2Key",
      results = {
        @Result(name = "input", location = "/manage/view_ec2_keys.jsp"),
        @Result(name = "success", location = "/manage/viewEC2Keys.action", type = "redirect")
      })
  public String submitEC2Key() {

    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());

      // create key pair request
      CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest();
      createKeyPairRequest.withKeyName(ec2Key.getKeyNm());

      // call service
      CreateKeyPairResult createKeyPairResult = service.createKeyPair(createKeyPairRequest);
      // get key pair result
      KeyPair keyPair = createKeyPairResult.getKeyPair();

      // set private key
      String privateKey = keyPair.getKeyMaterial();
      ec2Key.setPrivateKey(privateKey);

      // add to db
      Long keyId = EC2KeyDB.saveEC2Key(ec2Key);

      // store private key
      SSHUtil.storePrivateKey(keyId.toString(), ec2Key.getPrivateKey().trim());
    } catch (AmazonServiceException ex) {
      addActionError(ex.getMessage());
      retVal = INPUT;
    }

    return retVal;
  }

  @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;
  }

  @Action(
      value = "/manage/deleteEC2Key",
      results = {
        @Result(name = "success", location = "/manage/viewEC2Keys.action", type = "redirect")
      })
  public String deleteEC2Key() {

    EC2KeyDB.deleteEC2Key(ec2Key.getId());
    SSHUtil.deletePrivateKey(ec2Key.getId().toString());

    return SUCCESS;
  }

  /** Validates fields for importing an ec2 key */
  public void validateImportEC2Key() {

    if (ec2Key.getEc2Region() == null || ec2Key.getEc2Region().trim().equals("")) {
      addFieldError("ec2Key.ec2Region", "Required");
    }
    if (ec2Key.getKeyNm() == null || ec2Key.getKeyNm().trim().equals("")) {
      addFieldError("ec2Key.keyNm", "Required");
    }
    if (ec2Key.getPrivateKey() == null || ec2Key.getPrivateKey().trim().equals("")) {
      addFieldError("ec2Key.privateKey", "Required");
    }
    if (hasErrors()) {

      sortedSet = EC2KeyDB.getEC2KeySet(sortedSet);
    }
  }

  /** Validates fields for credential submit */
  public void validateSubmitEC2Key() {
    if (ec2Key.getEc2Region() == null || ec2Key.getEc2Region().trim().equals("")) {
      addFieldError("ec2Key.ec2Region", "Required");
    }
    if (ec2Key.getKeyNm() == null || ec2Key.getKeyNm().trim().equals("")) {
      addFieldError("ec2Key.keyNm", "Required");
    }
    if (hasErrors()) {

      sortedSet = EC2KeyDB.getEC2KeySet(sortedSet);
    }
  }

  public SortedSet getSortedSet() {
    return sortedSet;
  }

  public void setSortedSet(SortedSet sortedSet) {
    this.sortedSet = sortedSet;
  }

  public Map getEc2RegionMap() {
    return ec2RegionMap;
  }

  public void setEc2RegionMap(Map<String, String> ec2RegionMap) {
    this.ec2RegionMap = ec2RegionMap;
  }

  public EC2Key getEc2Key() {
    return ec2Key;
  }

  public void setEc2Key(EC2Key ec2Key) {
    this.ec2Key = ec2Key;
  }

  public HttpServletResponse getServletResponse() {
    return servletResponse;
  }

  public void setServletResponse(HttpServletResponse servletResponse) {
    this.servletResponse = servletResponse;
  }
}