Github user interma commented on a diff in the pull request:

    https://github.com/apache/incubator-hawq/pull/1253#discussion_r121583805
  
    --- Diff: 
ranger-plugin/admin-plugin/src/main/java/org/apache/hawq/ranger/service/HawqClient.java
 ---
    @@ -91,125 +88,42 @@
         }
     
         public HawqClient(String serviceName, Map<String, String> 
connectionProperties) throws Exception {
    -        super(serviceName,connectionProperties);
    +        super(serviceName, connectionProperties);
             this.connectionProperties = connectionProperties;
    -        initHawq();
         }
    -    
    -    public void initHawq() throws Exception {
    -           if(connectionProperties.containsKey(AUTHENTICATION)) {
    -                   isKerberosAuth = 
connectionProperties.get(AUTHENTICATION).equals(KERBEROS);
    -           }
    -           if (isKerberosAuth) {
    -                   LOG.info("Secured Mode: JDBC Connection done with 
preAuthenticated Subject");
    -                   
    -                   // do kinit in hawqclient by principal name and password
    -                   final String userName = getConfigHolder().getUserName();
    -                   final String password = getConfigHolder().getPassword();
    -                   
    -                   String[] kinitcmd ={
    -                           "/bin/sh",
    -                           "-c",
    -                           "echo '"+password+"' | kinit " + userName
    -                   };
    -                   java.lang.Runtime rt = java.lang.Runtime.getRuntime();
    -                   if (LOG.isDebugEnabled()) {
    -                           LOG.debug("kinit command: "+"echo 
'"+password+"' | kinit " + userName);
    -                   }
    -                   java.lang.Process p = rt.exec(kinitcmd);
    -                   
    -                   Subject.doAs(getLoginSubject(), new 
PrivilegedExceptionAction<Void>(){
    -                           public Void run() throws Exception {
    -                                   final String lookupPricipalName = 
getConfigHolder().getUserName();
    -                                   final String serverprincipal = 
connectionProperties.get("principal");
    -                                   initConnectionKerberos(serverprincipal, 
lookupPricipalName);
    -                                   return null;
    -                   }});
    -           }
    -           else {
    -                   LOG.info("Trying to use UnSecure client with username 
"+ getConfigHolder().getUserName() +" and password");
    -                   final String userName = getConfigHolder().getUserName();
    -                   final String password = getConfigHolder().getPassword();
    -                   initConnection(userName, password);
    -           }
    -   }
    -
    -    private void initConnectionKerberos(String serverPricipal, String 
userPrincipal) throws SQLException{
    -       try {
    -                   String url = 
String.format("jdbc:postgresql://%s:%s/%s?kerberosServerName=%s&jaasApplicationName=pgjdbc&user=%s",
 
    -                                   connectionProperties.get("hostname"), 
    -                                   connectionProperties.get("port"), 
DEFAULT_DATABASE, 
    -                                   serverPricipal, userPrincipal
    -                                   );
    -                   if (LOG.isDebugEnabled()) {
    -                           LOG.debug("InitConnectionKerberos "+ url);
    -                   }
    -                   con = DriverManager.getConnection(url); 
    -                   jdbc_url_template = 
String.format("jdbc:postgresql://%s:%s/%s?kerberosServerName=%s&jaasApplicationName=pgjdbc&user=%s",
 
    -                                   connectionProperties.get("hostname"), 
    -                                   connectionProperties.get("port"), 
DEFAULT_DATABASE_TEMPLATE, 
    -                                   serverPricipal, userPrincipal
    -                                   );
    -       } catch (SQLException e) {
    -         e.printStackTrace();
    -          LOG.error("Unable to Connect to Hawq", e);
    -          throw e;
    -       } catch (SecurityException se) {
    -                   se.printStackTrace();
    -           }
    -   }
    -
    -   
    -   private void initConnection(String userName, String password) throws 
SQLException  {
    -           try {
    -                   String url = 
String.format("jdbc:postgresql://%s:%s/%s", 
connectionProperties.get("hostname"), connectionProperties.get("port"), 
DEFAULT_DATABASE);
    -                   if (LOG.isDebugEnabled()) {
    -                           LOG.debug("InitConnectionKerberos "+ url);
    -                   }
    -                   con = DriverManager.getConnection(url, userName, 
password);
    -                   jdbc_url_template = 
String.format("jdbc:postgresql://%s:%s/%s", 
connectionProperties.get("hostname"), connectionProperties.get("port"), 
DEFAULT_DATABASE_TEMPLATE);
    -           } catch (SQLException e) {
    -                     e.printStackTrace();
    -             LOG.error("Unable to Connect to Hawq", e);
    -             throw e;
    -           } catch (SecurityException se) {
    -                   se.printStackTrace();
    -           }
    -   }
    -
    -   public void setConnection(Connection conn) {
    -           con = conn;
    -   }
    -   
    -   public void resetConnection(String db) throws SQLException{
    -           try {
    -                   if(db == null) {
    -                           return;
    -                   }
    -                   String newdb = db;
    -                   LOG.debug("resetconnectionbefore "+ jdbc_url_template + 
newdb);
    -                   String url = 
jdbc_url_template.replace(DEFAULT_DATABASE_TEMPLATE, newdb);
    -                   if (LOG.isDebugEnabled()) {
    -                           LOG.debug("resetconnection "+ 
jdbc_url_template+ url);
    -                   }
    -                   if(con !=null && !con.isClosed()){
    -                           con.close();
    -                   }
    -                   if (isKerberosAuth) {
    -                           con = DriverManager.getConnection(url); 
    -                   } else {
    -                           final String userName = 
getConfigHolder().getUserName();
    -                           final String password = 
getConfigHolder().getPassword();
    -                           con = DriverManager.getConnection(url, 
userName, password);
    -                   } 
    -           } catch (SQLException e) {
    -                   e.printStackTrace();
    -                   LOG.error("Unable to Connect to Hawq", e);
    -                   throw e;
    -           } catch (SecurityException se) {
    -                   se.printStackTrace();
    -           }
    -   }
    +
    +
    +    private Connection getConnection(Map<String, String> 
connectionProperties) throws SQLException {
    +        return getConnection(connectionProperties, null);
    +    }
    +
    +    private Connection getConnection(Map<String, String> 
connectionProperties, String database) throws SQLException {
    +
    +        String db = database != null ? database : DEFAULT_DATABASE;
    +        Properties props = new Properties();
    +
    +        if (LOG.isDebugEnabled()) {
    +            LOG.debug("<== HawqClient.checkConnection configuration" + 
connectionProperties );
    +        }
    +
    +        if (connectionProperties.containsKey(AUTHENTICATION) && 
connectionProperties.get(AUTHENTICATION).equals(KERBEROS)) {
    +            //kerberos mode
    +            props.setProperty("kerberosServerName", 
connectionProperties.get("principal"));
    +            props.setProperty("jaasApplicationName", "pgjdbc");
    +        }
    --- End diff --
    
    just need add the two properties in kerberos mode in jdbc.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to