Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AnalyzeAuthStmtsTest.java
Go to the documentation of this file.
1 // Copyright (c) 2014 Cloudera, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.analysis;
16 
17 import java.util.HashSet;
18 
19 import org.junit.Test;
20 
26 import com.cloudera.impala.thrift.TQueryCtx;
27 
28 public class AnalyzeAuthStmtsTest extends AnalyzerTest {
31  analyzer_.getCatalog().getAuthPolicy().addRole(
32  new Role("myRole", new HashSet<String>()));
33  }
34 
35  @Override
36  protected Analyzer createAnalyzer(String defaultDb) {
37  TQueryCtx queryCtx =
38  TestUtils.createQueryContext(defaultDb, System.getProperty("user.name"));
39  return new Analyzer(catalog_, queryCtx,
40  AuthorizationConfig.createHadoopGroupAuthConfig("server1", null, null));
41  }
42 
43  private Analyzer createAuthDisabledAnalyzer(String defaultDb) {
44  TQueryCtx queryCtx =
45  TestUtils.createQueryContext(defaultDb, System.getProperty("user.name"));
46  return new Analyzer(catalog_, queryCtx,
48  }
49 
50  @Test
51  public void AnalyzeShowRoles() {
52  AnalyzesOk("SHOW ROLES");
53  AnalyzesOk("SHOW ROLE GRANT GROUP myGroup");
54  AnalyzesOk("SHOW CURRENT ROLES");
55 
56  Analyzer authDisabledAnalyzer = createAuthDisabledAnalyzer(Catalog.DEFAULT_DB);
57  AnalysisError("SHOW ROLES", authDisabledAnalyzer,
58  "Authorization is not enabled.");
59  AnalysisError("SHOW ROLE GRANT GROUP myGroup", authDisabledAnalyzer,
60  "Authorization is not enabled.");
61  AnalysisError("SHOW CURRENT ROLES", authDisabledAnalyzer,
62  "Authorization is not enabled.");
63  }
64 
65  @Test
66  public void AnalyzeShowGrantRole() {
67  AnalyzesOk("SHOW GRANT ROLE myRole");
68  AnalyzesOk("SHOW GRANT ROLE myRole ON SERVER");
69  AnalyzesOk("SHOW GRANT ROLE myRole ON DATABASE functional");
70  AnalyzesOk("SHOW GRANT ROLE myRole ON TABLE foo");
71  AnalyzesOk("SHOW GRANT ROLE myRole ON TABLE functional.alltypes");
72  AnalyzesOk("SHOW GRANT ROLE myRole ON URI 'hdfs:////test-warehouse//foo'");
73  AnalysisError("SHOW GRANT ROLE does_not_exist",
74  "Role 'does_not_exist' does not exist.");
75  AnalysisError("SHOW GRANT ROLE does_not_exist ON SERVER",
76  "Role 'does_not_exist' does not exist.");
77 
78  Analyzer authDisabledAnalyzer = createAuthDisabledAnalyzer(Catalog.DEFAULT_DB);
79  AnalysisError("SHOW GRANT ROLE myRole", authDisabledAnalyzer,
80  "Authorization is not enabled.");
81  AnalysisError("SHOW GRANT ROLE myRole ON SERVER", authDisabledAnalyzer,
82  "Authorization is not enabled.");
83  }
84 
85  @Test
87  AnalyzesOk("DROP ROLE myRole");
88  AnalyzesOk("CREATE ROLE doesNotExist");
89 
90  AnalysisError("DROP ROLE doesNotExist", "Role 'doesNotExist' does not exist.");
91  AnalysisError("CREATE ROLE myRole", "Role 'myRole' already exists.");
92 
93  // Role names are case-insensitive
94  AnalyzesOk("DROP ROLE MYrole");
95  AnalysisError("CREATE ROLE MYrole", "Role 'MYrole' already exists.");
96 
97  Analyzer authDisabledAnalyzer = createAuthDisabledAnalyzer(Catalog.DEFAULT_DB);
98  AnalysisError("DROP ROLE myRole", authDisabledAnalyzer,
99  "Authorization is not enabled.");
100  AnalysisError("CREATE ROLE doesNotExist", authDisabledAnalyzer,
101  "Authorization is not enabled.");
102  }
103 
104  @Test
106  AnalyzesOk("GRANT ROLE myrole TO GROUP abc");
107  AnalyzesOk("REVOKE ROLE myrole FROM GROUP abc");
108  AnalysisError("GRANT ROLE doesNotExist TO GROUP abc",
109  "Role 'doesNotExist' does not exist.");
110  AnalysisError("REVOKE ROLE doesNotExist FROM GROUP abc",
111  "Role 'doesNotExist' does not exist.");
112 
113  Analyzer authDisabledAnalyzer = createAuthDisabledAnalyzer(Catalog.DEFAULT_DB);
114  AnalysisError("GRANT ROLE myrole TO GROUP abc", authDisabledAnalyzer,
115  "Authorization is not enabled.");
116  AnalysisError("REVOKE ROLE myrole FROM GROUP abc", authDisabledAnalyzer,
117  "Authorization is not enabled.");
118  }
119 
120  @Test
122  boolean[] isGrantVals = {true, false};
123  for (boolean isGrant: isGrantVals) {
124  Object[] formatArgs = new String[] {"REVOKE", "FROM"};
125  if (isGrant) formatArgs = new String[] {"GRANT", "TO"};
126  // ALL privileges
127  AnalyzesOk(String.format("%s ALL ON TABLE foo %s myrole", formatArgs));
128  AnalyzesOk(String.format("%s ALL ON TABLE bar.foo %s myrole", formatArgs));
129  AnalyzesOk(String.format("%s ALL ON DATABASE foo %s myrole", formatArgs));
130  AnalyzesOk(String.format("%s ALL ON SERVER %s myrole", formatArgs));
131  AnalyzesOk(String.format("%s ALL ON URI 'hdfs:////abc//123' %s myrole",
132  formatArgs));
133  AnalysisError(String.format("%s ALL ON URI 'xxxx:////abc//123' %s myrole",
134  formatArgs), "No FileSystem for scheme: xxxx");
135 
136  // INSERT privilege
137  AnalyzesOk(String.format("%s INSERT ON TABLE foo %s myrole", formatArgs));
138  AnalyzesOk(String.format("%s INSERT ON TABLE bar.foo %s myrole", formatArgs));
139  AnalyzesOk(String.format("%s INSERT ON DATABASE foo %s myrole", formatArgs));
140  AnalysisError(String.format("%s INSERT ON SERVER %s myrole", formatArgs),
141  "Only 'ALL' privilege may be applied at SERVER scope in privilege spec.");
142  AnalysisError(String.format("%s INSERT ON URI 'hdfs:////abc//123' %s myrole",
143  formatArgs), "Only 'ALL' privilege may be applied at URI scope in " +
144  "privilege spec.");
145 
146  AnalyzesOk(String.format("%s SELECT ON TABLE foo %s myrole", formatArgs));
147  AnalyzesOk(String.format("%s SELECT ON TABLE bar.foo %s myrole", formatArgs));
148  AnalyzesOk(String.format("%s SELECT ON DATABASE foo %s myrole", formatArgs));
149  AnalyzesOk(String.format("%s SELECT ON TABLE foo %s myrole", formatArgs));
150  AnalyzesOk(String.format("%s SELECT ON TABLE bar.foo %s myrole", formatArgs));
151  AnalysisError(String.format("%s SELECT ON SERVER %s myrole", formatArgs),
152  "Only 'ALL' privilege may be applied at SERVER scope in privilege spec.");
153  AnalysisError(String.format("%s SELECT ON URI 'hdfs:////abc//123' %s myrole",
154  formatArgs), "Only 'ALL' privilege may be applied at URI scope in " +
155  "privilege spec.");
156  }
157 
158  Analyzer authDisabledAnalyzer = createAuthDisabledAnalyzer(Catalog.DEFAULT_DB);
159  AnalysisError("GRANT ALL ON SERVER TO myRole", authDisabledAnalyzer,
160  "Authorization is not enabled.");
161  AnalysisError("REVOKE ALL ON SERVER FROM myRole", authDisabledAnalyzer,
162  "Authorization is not enabled.");
163 
164  TQueryCtx queryCtxNoUsername = TestUtils.createQueryContext("default", "");
165  Analyzer noUsernameAnalyzer = new Analyzer(catalog_, queryCtxNoUsername,
166  AuthorizationConfig.createHadoopGroupAuthConfig("server1", null, null));
167  AnalysisError("GRANT ALL ON SERVER TO myRole", noUsernameAnalyzer,
168  "Cannot execute authorization statement with an empty username.");
169  }
170 }
static AuthorizationConfig createHadoopGroupAuthConfig(String serverName, String policyFile, String sentryConfigFile)
uint64_t Test(T *ht, const ProbeTuple *input, uint64_t num_tuples)
static final String DEFAULT_DB
Definition: Catalog.java:58