diff --git a/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java b/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaS tore.java index 8909e74c2b..2c569bc4e2 100644 --- a/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java +++ b/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java @@ -1331,10 +1331,16 @@ private void create_database_core(RawStore ms, final Database db) } else { try { - // Since this may be done as random user (if doAs=true) he may not have access - // to the managed directory. We run this as an admin user - madeManagedDir = UserGroupInformation.getLoginUser().doAs(new PrivilegedExceptionAction() { - @Override public Boolean run() throws MetaException { + // Since this may be done as random user (if doAs=true) end user may not have access + // to the default managed directory. We run this as an admin user + // if user explicitly specifies location within create statement, then we run this as end user. + UserGroupInformation ugi; + if(wh.getDefaultDatabasePath(db.getName()).toString().equals(dbPath)) + ugi = UserGroupInformation.getLoginUser(); + else + ugi = UserGroupInformation.getCurrentUser(); + madeManagedDir = ugi.doAs(new PrivilegedExceptionAction() { + @Override public Boolean run() throws MetaException { if (!wh.isDir(dbPath)) { LOG.info("Creating database path in managed directory " + dbPath); if (!wh.mkdirs(dbPath)) { @@ -1766,7 +1772,13 @@ private void drop_database_core(RawStore ms, String catName, } else { try { final Database dbFinal = db; - Boolean deleted = UserGroupInformation.getLoginUser().doAs(new PrivilegedExceptionAction() { + UserGroupInformation ugi; + // We run this as end-user if db location is not in default metastore warehouse directory + if(wh.getDefaultDatabasePath(db.getName()).toString().equals(db.getLocationUri())) + ugi = UserGroupInformation.getLoginUser(); + else + ugi = UserGroupInformation.getCurrentUser(); + Boolean deleted = ugi.doAs(new PrivilegedExceptionAction() { @Override public Boolean run() throws MetaException { return wh.deleteDir(new Path(dbFinal.getLocationUri()), true, dbFinal); }