IMPALA-6862: Privilege.java needs to support Sentry 1.5.1 and 2.0.0 BitFieldAction is an asbract class in Sentry 1.5.1, but it is no longer an abstract class in Sentry 2.0.0. This patch fixes the issue by making Privilege.java support both versions of Sentry.
Testing: - Ran all front-end tests with profile=2 and profile=3 Cherry-picks: not for 2.x Change-Id: Ibbf4eeab3927ff93ed51066b164b8874d5f76e23 Reviewed-on: http://gerrit.cloudera.org:8080/10082 Reviewed-by: Alex Behm <[email protected]> Tested-by: Impala Public Jenkins <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/impala/repo Commit: http://git-wip-us.apache.org/repos/asf/impala/commit/249e5abd Tree: http://git-wip-us.apache.org/repos/asf/impala/tree/249e5abd Diff: http://git-wip-us.apache.org/repos/asf/impala/diff/249e5abd Branch: refs/heads/master Commit: 249e5abda520dab9aea5b2462469ad43bf508020 Parents: 3ebf30a Author: Fredy wijaya <[email protected]> Authored: Mon Apr 16 16:05:17 2018 -0700 Committer: Impala Public Jenkins <[email protected]> Committed: Wed Apr 18 08:05:30 2018 +0000 ---------------------------------------------------------------------- fe/pom.xml | 12 ++ .../authorization/ImpalaActionFactoryTest.java | 132 +++++++++++++++++++ .../apache/impala/authorization/Privilege.java | 11 +- .../authorization/ImpalaActionFactoryTest.java | 132 ------------------- 4 files changed, 154 insertions(+), 133 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/impala/blob/249e5abd/fe/pom.xml ---------------------------------------------------------------------- diff --git a/fe/pom.xml b/fe/pom.xml index 67ea81c..0abd088 100644 --- a/fe/pom.xml +++ b/fe/pom.xml @@ -434,6 +434,18 @@ under the License. </sources> </configuration> </execution> + <execution> + <id>add-test-source</id> + <phase>generate-test-sources</phase> + <goals> + <goal>add-test-source</goal> + </goals> + <configuration> + <sources> + <source>${project.basedir}/src/compat-minicluster-profile-${env.IMPALA_MINICLUSTER_PROFILE}/test/java</source> + </sources> + </configuration> + </execution> </executions> </plugin> http://git-wip-us.apache.org/repos/asf/impala/blob/249e5abd/fe/src/compat-minicluster-profile-3/test/java/org/apache/impala/authorization/ImpalaActionFactoryTest.java ---------------------------------------------------------------------- diff --git a/fe/src/compat-minicluster-profile-3/test/java/org/apache/impala/authorization/ImpalaActionFactoryTest.java b/fe/src/compat-minicluster-profile-3/test/java/org/apache/impala/authorization/ImpalaActionFactoryTest.java new file mode 100644 index 0000000..bd39839 --- /dev/null +++ b/fe/src/compat-minicluster-profile-3/test/java/org/apache/impala/authorization/ImpalaActionFactoryTest.java @@ -0,0 +1,132 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.impala.authorization; + +import com.google.common.collect.Lists; +import org.apache.impala.authorization.Privilege.ImpalaAction; +import org.apache.sentry.core.common.BitFieldAction; +import org.junit.Test; + +import java.util.List; +import java.util.Random; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +public class ImpalaActionFactoryTest { + @Test + public void testGetActionsByCode() { + ImpalaActionFactory factory = new ImpalaActionFactory(); + + List<? extends BitFieldAction> actual = factory.getActionsByCode( + ImpalaAction.SELECT.getCode() | + ImpalaAction.INSERT.getCode() | + ImpalaAction.CREATE.getCode()); + List<ImpalaAction> expected = Lists.newArrayList( + ImpalaAction.SELECT, + ImpalaAction.INSERT, + ImpalaAction.CREATE); + assertBitFieldActions(expected, actual); + + actual = factory.getActionsByCode( + ImpalaAction.SELECT.getCode() | + ImpalaAction.INSERT.getCode() | + ImpalaAction.ALTER.getCode() | + ImpalaAction.CREATE.getCode() | + ImpalaAction.DROP.getCode() | + ImpalaAction.REFRESH.getCode()); + expected = Lists.newArrayList( + ImpalaAction.SELECT, + ImpalaAction.INSERT, + ImpalaAction.ALTER, + ImpalaAction.CREATE, + ImpalaAction.DROP, + ImpalaAction.REFRESH, + ImpalaAction.ALL); + assertBitFieldActions(expected, actual); + + actual = factory.getActionsByCode(ImpalaAction.ALL.getCode()); + expected = Lists.newArrayList( + ImpalaAction.SELECT, + ImpalaAction.INSERT, + ImpalaAction.ALTER, + ImpalaAction.CREATE, + ImpalaAction.DROP, + ImpalaAction.REFRESH, + ImpalaAction.ALL); + assertBitFieldActions(expected, actual); + + try { + factory.getActionsByCode(Integer.MAX_VALUE); + fail("IllegalArgumentException should be thrown."); + } catch (IllegalArgumentException e) { + assertEquals(String.format("Action code must between 1 and %d.", + ImpalaAction.ALL.getCode()), e.getMessage()); + } + + try { + factory.getActionsByCode(Integer.MIN_VALUE); + fail("IllegalArgumentException should be thrown."); + } catch (IllegalArgumentException e) { + assertEquals(String.format("Action code must between 1 and %d.", + ImpalaAction.ALL.getCode()), e.getMessage()); + } + } + + private static void assertBitFieldActions(List<ImpalaAction> expected, + List<? extends BitFieldAction> actual) { + assertEquals(expected.size(), actual.size()); + for (int i = 0; i < actual.size(); i++) { + assertEquals(expected.get(i).getValue(), actual.get(i).getValue()); + assertEquals(expected.get(i).getCode(), actual.get(i).getActionCode()); + } + } + + @Test + public void testGetActionByName() { + ImpalaActionFactory impala = new ImpalaActionFactory(); + + for (ImpalaAction action : ImpalaAction.values()) { + testGetActionByName(impala, action, action.getValue()); + } + assertNull(impala.getActionByName("foo")); + } + + private static void testGetActionByName(ImpalaActionFactory impala, + ImpalaAction expected, String name) { + assertEquals(toBitFieldAction(expected), + impala.getActionByName(name.toUpperCase())); + assertEquals(toBitFieldAction(expected), + impala.getActionByName(name.toLowerCase())); + assertEquals(toBitFieldAction(expected), + impala.getActionByName(randomizeCaseSensitivity(name))); + } + + private static String randomizeCaseSensitivity(String str) { + char[] chars = str.toCharArray(); + Random random = new Random(System.currentTimeMillis()); + for (int i = 0; i < chars.length; i++) { + chars[i] = (random.nextBoolean()) ? Character.toUpperCase(chars[i]) : chars[i]; + } + return new String(chars); + } + + private static BitFieldAction toBitFieldAction(ImpalaAction action) { + return new BitFieldAction(action.getValue(), action.getCode()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/impala/blob/249e5abd/fe/src/main/java/org/apache/impala/authorization/Privilege.java ---------------------------------------------------------------------- diff --git a/fe/src/main/java/org/apache/impala/authorization/Privilege.java b/fe/src/main/java/org/apache/impala/authorization/Privilege.java index 7afe127..0b1c2f8 100644 --- a/fe/src/main/java/org/apache/impala/authorization/Privilege.java +++ b/fe/src/main/java/org/apache/impala/authorization/Privilege.java @@ -68,8 +68,17 @@ public enum Privilege { private final BitFieldAction bitFieldAction_; + // In Sentry 1.5.1, BitFieldAction is an abstract class. In Sentry 2.0.0, + // BitFieldAction is no longer an absract class. To support both versions, + // we can extend BitFieldAction. + private static class ImpalaBitFieldAction extends BitFieldAction { + public ImpalaBitFieldAction(String name, int code) { + super(name, code); + } + } + ImpalaAction(String value, int code) { - bitFieldAction_ = new BitFieldAction(value, code); + bitFieldAction_ = new ImpalaBitFieldAction(value, code); } @Override http://git-wip-us.apache.org/repos/asf/impala/blob/249e5abd/fe/src/test/java/org/apache/impala/authorization/ImpalaActionFactoryTest.java ---------------------------------------------------------------------- diff --git a/fe/src/test/java/org/apache/impala/authorization/ImpalaActionFactoryTest.java b/fe/src/test/java/org/apache/impala/authorization/ImpalaActionFactoryTest.java deleted file mode 100644 index bd39839..0000000 --- a/fe/src/test/java/org/apache/impala/authorization/ImpalaActionFactoryTest.java +++ /dev/null @@ -1,132 +0,0 @@ -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -package org.apache.impala.authorization; - -import com.google.common.collect.Lists; -import org.apache.impala.authorization.Privilege.ImpalaAction; -import org.apache.sentry.core.common.BitFieldAction; -import org.junit.Test; - -import java.util.List; -import java.util.Random; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; - -public class ImpalaActionFactoryTest { - @Test - public void testGetActionsByCode() { - ImpalaActionFactory factory = new ImpalaActionFactory(); - - List<? extends BitFieldAction> actual = factory.getActionsByCode( - ImpalaAction.SELECT.getCode() | - ImpalaAction.INSERT.getCode() | - ImpalaAction.CREATE.getCode()); - List<ImpalaAction> expected = Lists.newArrayList( - ImpalaAction.SELECT, - ImpalaAction.INSERT, - ImpalaAction.CREATE); - assertBitFieldActions(expected, actual); - - actual = factory.getActionsByCode( - ImpalaAction.SELECT.getCode() | - ImpalaAction.INSERT.getCode() | - ImpalaAction.ALTER.getCode() | - ImpalaAction.CREATE.getCode() | - ImpalaAction.DROP.getCode() | - ImpalaAction.REFRESH.getCode()); - expected = Lists.newArrayList( - ImpalaAction.SELECT, - ImpalaAction.INSERT, - ImpalaAction.ALTER, - ImpalaAction.CREATE, - ImpalaAction.DROP, - ImpalaAction.REFRESH, - ImpalaAction.ALL); - assertBitFieldActions(expected, actual); - - actual = factory.getActionsByCode(ImpalaAction.ALL.getCode()); - expected = Lists.newArrayList( - ImpalaAction.SELECT, - ImpalaAction.INSERT, - ImpalaAction.ALTER, - ImpalaAction.CREATE, - ImpalaAction.DROP, - ImpalaAction.REFRESH, - ImpalaAction.ALL); - assertBitFieldActions(expected, actual); - - try { - factory.getActionsByCode(Integer.MAX_VALUE); - fail("IllegalArgumentException should be thrown."); - } catch (IllegalArgumentException e) { - assertEquals(String.format("Action code must between 1 and %d.", - ImpalaAction.ALL.getCode()), e.getMessage()); - } - - try { - factory.getActionsByCode(Integer.MIN_VALUE); - fail("IllegalArgumentException should be thrown."); - } catch (IllegalArgumentException e) { - assertEquals(String.format("Action code must between 1 and %d.", - ImpalaAction.ALL.getCode()), e.getMessage()); - } - } - - private static void assertBitFieldActions(List<ImpalaAction> expected, - List<? extends BitFieldAction> actual) { - assertEquals(expected.size(), actual.size()); - for (int i = 0; i < actual.size(); i++) { - assertEquals(expected.get(i).getValue(), actual.get(i).getValue()); - assertEquals(expected.get(i).getCode(), actual.get(i).getActionCode()); - } - } - - @Test - public void testGetActionByName() { - ImpalaActionFactory impala = new ImpalaActionFactory(); - - for (ImpalaAction action : ImpalaAction.values()) { - testGetActionByName(impala, action, action.getValue()); - } - assertNull(impala.getActionByName("foo")); - } - - private static void testGetActionByName(ImpalaActionFactory impala, - ImpalaAction expected, String name) { - assertEquals(toBitFieldAction(expected), - impala.getActionByName(name.toUpperCase())); - assertEquals(toBitFieldAction(expected), - impala.getActionByName(name.toLowerCase())); - assertEquals(toBitFieldAction(expected), - impala.getActionByName(randomizeCaseSensitivity(name))); - } - - private static String randomizeCaseSensitivity(String str) { - char[] chars = str.toCharArray(); - Random random = new Random(System.currentTimeMillis()); - for (int i = 0; i < chars.length; i++) { - chars[i] = (random.nextBoolean()) ? Character.toUpperCase(chars[i]) : chars[i]; - } - return new String(chars); - } - - private static BitFieldAction toBitFieldAction(ImpalaAction action) { - return new BitFieldAction(action.getValue(), action.getCode()); - } -} \ No newline at end of file
