public static void main()

in ambari-server/src/main/java/org/apache/ambari/server/security/encryption/CredentialProvider.java [119:207]


  public static void main(String args[]) {
    if (args != null && args.length > 0) {
      String action = args[0];
      String alias = null;
      String masterKey = null;
      CredentialProvider credentialProvider = null;
      Configuration configuration = new Configuration();
      if (args.length > 1 && !args[1].isEmpty()) {
        alias = args[1];
      } else {
        LOG.error("No valid arguments provided.");
        System.exit(1);
      }
      // None - To avoid incorrectly assuming redirection as argument
      if (args.length > 3 && !args[3].isEmpty() && !args[3].equalsIgnoreCase
        ("None")) {
        masterKey = args[3];
        LOG.debug("Master key provided as an argument.");
      }
      try {
        credentialProvider = new CredentialProvider(masterKey,
          configuration.getMasterKeyLocation(),
          configuration.isMasterKeyPersisted());
      } catch (Exception ex) {
        ex.printStackTrace();
        System.exit(1);
      }
      LOG.info("action => " + action + ", alias => " + alias);
      if (action.equalsIgnoreCase("PUT")) {
        String password = null;
        if (args.length > 2 && !args[2].isEmpty()) {
          password = args[2];
        }
        if (alias != null && !alias.isEmpty()
          && password != null && !password.isEmpty()) {
          try {
            credentialProvider.addAliasToCredentialStore(alias, password);
          } catch (AmbariException e) {
            e.printStackTrace();
          }
        } else {
          LOG.error("Alias and password are required arguments.");
          System.exit(1);
        }
      } else if (action.equalsIgnoreCase("GET")) {
        String writeFilePath = null;
        if (args.length > 2 && !args[2].isEmpty()) {
          writeFilePath = args[2];
        }
        if (alias != null && !alias.isEmpty() && writeFilePath != null &&
          !writeFilePath.isEmpty()) {
          String passwd = "";
          try {
            char[] retPasswd = credentialProvider.getPasswordForAlias(alias);
            if (retPasswd != null) {
              passwd = new String(retPasswd);
            }
          } catch (AmbariException e) {
            LOG.error("Error retrieving password for alias.");
            e.printStackTrace();
          }
          FileOutputStream fo = null;
          try {
            fo = new FileOutputStream(writeFilePath);
            fo.write(passwd.getBytes());
          } catch (FileNotFoundException fe) {
            fe.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          } finally {
            if (fo != null) {
              try {
                fo.close();
              } catch (IOException e) {
              }
            }
          }
        } else {
          LOG.error("Alias and file path are required arguments.");
        }
      } else if (action.equalsIgnoreCase("RESET")) {

      }
    } else {
      LOG.error("No arguments provided to " + "CredentialProvider");
      System.exit(1);
    }
    System.exit(0);
  }