Esempio n. 1
0
  @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;
  }
Esempio n. 2
0
  @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;
  }
Esempio n. 3
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;
  }
Esempio n. 4
0
  /** creates composite terminals if there are errors or authentication issues. */
  @Action(
      value = "/admin/createTerms",
      results = {@Result(name = "success", location = "/admin/secure_shell.jsp")})
  public String createTerms() {

    Long userId = AuthUtil.getUserId(servletRequest.getSession());
    Long sessionId = AuthUtil.getSessionId(servletRequest.getSession());

    if (pendingSystemStatus != null && pendingSystemStatus.getId() != null) {

      // get status
      currentSystemStatus = SystemStatusDB.getSystemStatus(pendingSystemStatus.getId(), userId);
      // if initial status run script
      if (currentSystemStatus != null
          && (HostSystem.INITIAL_STATUS.equals(currentSystemStatus.getStatusCd())
              || HostSystem.AUTH_FAIL_STATUS.equals(currentSystemStatus.getStatusCd())
              || HostSystem.PUBLIC_KEY_FAIL_STATUS.equals(currentSystemStatus.getStatusCd()))) {

        // set current servletRequest.getSession()
        currentSystemStatus =
            SSHUtil.openSSHTermOnSystem(
                passphrase, password, userId, sessionId, currentSystemStatus, userSchSessionMap);
      }
      if (currentSystemStatus != null
          && (HostSystem.AUTH_FAIL_STATUS.equals(currentSystemStatus.getStatusCd())
              || HostSystem.PUBLIC_KEY_FAIL_STATUS.equals(currentSystemStatus.getStatusCd()))) {

        pendingSystemStatus = currentSystemStatus;

      } else {

        pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId);
        // if success loop through systems until finished or need password
        while (pendingSystemStatus != null
            && currentSystemStatus != null
            && HostSystem.SUCCESS_STATUS.equals(currentSystemStatus.getStatusCd())) {
          currentSystemStatus =
              SSHUtil.openSSHTermOnSystem(
                  passphrase, password, userId, sessionId, pendingSystemStatus, userSchSessionMap);
          pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId);
        }
      }
    }
    if (SystemStatusDB.getNextPendingSystem(userId) == null) {
      // check user map
      if (userSchSessionMap != null && !userSchSessionMap.isEmpty()) {

        // get user servletRequest.getSession()s
        Map<Long, SchSession> schSessionMap = userSchSessionMap.get(userId).getSchSessionMap();

        for (SchSession schSession : schSessionMap.values()) {
          // add to host system list
          systemList.add(schSession.getHostSystem());
          // run script it exists
          if (script != null && script.getId() != null && script.getId() > 0) {
            script = ScriptDB.getScript(script.getId(), userId);
            BufferedReader reader = new BufferedReader(new StringReader(script.getScript()));
            String line;
            try {
              while ((line = reader.readLine()) != null) {
                schSession.getCommander().println(line);
              }
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        }
      }
    }

    return SUCCESS;
  }