Skip to content

Commit

Permalink
[ZEPPELIN-2434] Credential feature does work in JDBC interpreter
Browse files Browse the repository at this point in the history
### What is this PR for?
Credential feature does not work in JDBC interpreter.

This PR fixes unittest that does not detect this bug and fix jdbc interpreter to correctly read credential.

### What type of PR is it?
Bug Fix

### Todos
* [x] - Fix unittest
* [x] - Fix condition when use credential, when use property.

### What is the Jira issue?
https://issues.apache.org/jira/browse/ZEPPELIN-2434

### How should this be tested?
Set empty `default.user` property and set id/pw in 'credential' menu. And try use jdbc interpreter.

### Questions:
* Does the licenses files need update? no
* Is there breaking changes for older versions? no
* Does this needs documentation? no

Author: Lee moon soo <moon@apache.org>

Closes #2269 from Leemoonsoo/jdbc_credential and squashes the following commits:

1de7ea6 [Lee moon soo] Use crednetial information instead of property when user is empty string.
  • Loading branch information
Leemoonsoo committed Apr 23, 2017
1 parent 96de730 commit da793f3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
11 changes: 5 additions & 6 deletions jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void open() {
logger.debug("propertyKey: {}", propertyKey);
String[] keyValue = propertyKey.split("\\.", 2);
if (2 == keyValue.length) {
logger.info("key: {}, value: {}", keyValue[0], keyValue[1]);
logger.debug("key: {}, value: {}", keyValue[0], keyValue[1]);

Properties prefixProperties;
if (basePropretiesMap.containsKey(keyValue[0])) {
Expand Down Expand Up @@ -249,6 +249,7 @@ private String getJDBCDriverName(String user, String propertyKey) {

private boolean existAccountInBaseProperty(String propertyKey) {
return basePropretiesMap.get(propertyKey).containsKey(USER_KEY) &&
!isEmpty((String) basePropretiesMap.get(propertyKey).get(USER_KEY)) &&
basePropretiesMap.get(propertyKey).containsKey(PASSWORD_KEY);
}

Expand Down Expand Up @@ -295,7 +296,6 @@ private void setUserProperty(String propertyKey, InterpreterContext interpreterC
}
}
jdbcUserConfigurations.setPropertyMap(propertyKey, basePropretiesMap.get(propertyKey));

if (existAccountInBaseProperty(propertyKey)) {
return;
}
Expand Down Expand Up @@ -576,7 +576,7 @@ private void executePrecode(Connection connection, String propertyKey) throws SQ
String precode = getProperty(String.format(PRECODE_KEY_TEMPLATE, propertyKey));
if (StringUtils.isNotBlank(precode)) {
precode = StringUtils.trim(precode);
logger.info("Run SQL precode '{}'", precode);
logger.debug("Run SQL precode '{}'", precode);
try (Statement statement = connection.createStatement()) {
statement.execute(precode);
if (!connection.getAutoCommit()) {
Expand Down Expand Up @@ -720,16 +720,15 @@ private String replaceReservedChars(String str) {

@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
logger.info("Run SQL command '{}'", cmd);
logger.debug("Run SQL command '{}'", cmd);
String propertyKey = getPropertyKey(cmd);

if (null != propertyKey && !propertyKey.equals(DEFAULT_KEY)) {
cmd = cmd.substring(propertyKey.length() + 2);
}

cmd = cmd.trim();

logger.info("PropertyKey: {}, SQL command: '{}'", propertyKey, cmd);
logger.debug("PropertyKey: {}, SQL command: '{}'", propertyKey, cmd);
return executeSql(propertyKey, cmd, contextInterpreter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,18 +339,18 @@ public void testMultiTenant() throws SQLException, IOException {
* 'jdbc1' interpreter has user('dbuser')/password('dbpassword') property
* 'jdbc2' interpreter doesn't have user/password property
* 'user1' doesn't have Credential information.
* 'user2' has 'jdbc2' Credential information that is same with database account.
* 'user2' has 'jdbc2' Credential information that is 'user2Id' / 'user2Pw' as id and password
*/

JDBCInterpreter jdbc1 = new JDBCInterpreter(getDBProperty("dbuser", "dbpassword"));
JDBCInterpreter jdbc2 = new JDBCInterpreter(getDBProperty(null, null));
JDBCInterpreter jdbc2 = new JDBCInterpreter(getDBProperty("", ""));

AuthenticationInfo user1Credential = getUserAuth("user1", null, null, null);
AuthenticationInfo user2Credential = getUserAuth("user2", "jdbc.jdbc2", "dbuser", "dbpassword");
AuthenticationInfo user2Credential = getUserAuth("user2", "jdbc.jdbc2", "user2Id","user2Pw");

// user1 runs jdbc1
jdbc1.open();
InterpreterContext ctx1 = new InterpreterContext("", "1", "jdbc.jdbc1", "", "", user1Credential,
InterpreterContext ctx1 = new InterpreterContext("", "1", "jdbc1", "", "", user1Credential,
null, null, null, null, null, null);
jdbc1.interpret("", ctx1);

Expand All @@ -361,7 +361,7 @@ public void testMultiTenant() throws SQLException, IOException {

// user1 runs jdbc2
jdbc2.open();
InterpreterContext ctx2 = new InterpreterContext("", "1", "jdbc.jdbc2", "", "", user1Credential,
InterpreterContext ctx2 = new InterpreterContext("", "1", "jdbc2", "", "", user1Credential,
null, null, null, null, null, null);
jdbc2.interpret("", ctx2);

Expand All @@ -372,7 +372,7 @@ public void testMultiTenant() throws SQLException, IOException {

// user2 runs jdbc1
jdbc1.open();
InterpreterContext ctx3 = new InterpreterContext("", "1", "jdbc.jdbc1", "", "", user2Credential,
InterpreterContext ctx3 = new InterpreterContext("", "1", "jdbc1", "", "", user2Credential,
null, null, null, null, null, null);
jdbc1.interpret("", ctx3);

Expand All @@ -383,13 +383,13 @@ public void testMultiTenant() throws SQLException, IOException {

// user2 runs jdbc2
jdbc2.open();
InterpreterContext ctx4 = new InterpreterContext("", "1", "jdbc.jdbc2", "", "", user2Credential,
InterpreterContext ctx4 = new InterpreterContext("", "1", "jdbc2", "", "", user2Credential,
null, null, null, null, null, null);
jdbc2.interpret("", ctx4);

JDBCUserConfigurations user2JDBC2Conf = jdbc2.getJDBCConfiguration("user2");
assertNull(user2JDBC2Conf.getPropertyMap("default").get("user"));
assertNull(user2JDBC2Conf.getPropertyMap("default").get("password"));
assertEquals("user2Id", user2JDBC2Conf.getPropertyMap("default").get("user"));
assertEquals("user2Pw", user2JDBC2Conf.getPropertyMap("default").get("password"));
jdbc2.close();
}

Expand Down

0 comments on commit da793f3

Please sign in to comment.