15 package com.cloudera.impala.catalog;
17 import java.util.List;
21 import org.apache.commons.net.ntp.TimeStamp;
22 import org.apache.log4j.Logger;
23 import org.apache.sentry.core.common.ActiveRoleSet;
24 import org.apache.sentry.provider.cache.PrivilegeCache;
26 import com.cloudera.impala.thrift.TColumn;
27 import com.cloudera.impala.thrift.TPrivilege;
28 import com.cloudera.impala.thrift.TResultRow;
29 import com.cloudera.impala.thrift.TResultSet;
30 import com.cloudera.impala.thrift.TResultSetMetadata;
32 import com.google.common.base.Strings;
33 import com.google.common.collect.Lists;
34 import com.google.common.collect.Maps;
35 import com.google.common.collect.Sets;
56 private static final Logger
LOG = Logger.getLogger(AuthorizationPolicy.class);
59 private final CatalogObjectCache<Role>
roleCache_ =
new CatalogObjectCache<Role>();
62 Map<Integer, String>
roleIds_ = Maps.newHashMap();
76 Role existingRole = roleCache_.get(role.getName());
79 if (existingRole != null &&
83 if (existingRole != null) {
86 if (existingRole.
getId() == role.getId()) {
97 Set<String> grantedRoles = groupsToRoles_.get(groupName);
98 if (grantedRoles == null) {
99 grantedRoles = Sets.newHashSet();
100 groupsToRoles_.put(groupName, grantedRoles);
102 grantedRoles.add(role.getName().toLowerCase());
106 roleIds_.put(role.getId(), role.
getName());
116 LOG.trace(
"Adding privilege: " + privilege.getName() +
117 " role ID: " + privilege.getRoleId());
120 throw new CatalogException(String.format(
"Error adding privilege: %s. Role ID " +
121 "'%d' does not exist.", privilege.getName(), privilege.getRoleId()));
123 LOG.trace(
"Adding privilege: " + privilege.getName() +
" to role: " +
124 role.getName() +
"ID: " + role.getId());
125 role.addPrivilege(privilege);
138 throw new CatalogException(String.format(
"Error removing privilege: %s. Role ID " +
139 "'%d' does not exist.", privilege.getName(), privilege.getRoleId()));
141 LOG.trace(
"Removing privilege: '" + privilege.getName() +
"' from Role ID: " +
142 privilege.getRoleId() +
" Role Name: " + role.getName());
143 return role.removePrivilege(privilege.getName());
150 return roleCache_.getValues();
157 return Sets.newHashSet(roleCache_.keySet());
164 return roleCache_.get(roleName);
171 String roleName = roleIds_.get(roleId);
172 if (roleName == null)
return null;
173 return roleCache_.get(roleName);
181 String roleName = roleIds_.get(roleId);
182 if (roleName == null)
return null;
183 Role role = roleCache_.get(roleName);
184 return role.getPrivilege(privilegeName);
191 List<Role> grantedRoles = Lists.newArrayList();
192 Set<String> roleNames = groupsToRoles_.get(groupName);
193 if (roleNames != null) {
194 for (String roleName: roleNames) {
196 Role role = roleCache_.get(roleName);
197 if (role != null) grantedRoles.add(roleCache_.get(roleName));
208 Role removedRole = roleCache_.remove(roleName);
209 if (removedRole == null)
return null;
213 Set<String> roles = groupsToRoles_.get(grantGroup);
214 if (roles != null) roles.remove(roleName.toLowerCase());
217 roleIds_.remove(removedRole.getId());
228 Role role = roleCache_.get(roleName);
229 if (role == null)
throw new CatalogException(
"Role does not exist: " + roleName);
230 role.addGrantGroup(groupName);
231 Set<String> grantedRoles = groupsToRoles_.get(groupName);
232 if (grantedRoles == null) {
233 grantedRoles = Sets.newHashSet();
234 groupsToRoles_.put(groupName, grantedRoles);
236 grantedRoles.add(roleName.toLowerCase());
247 Role role = roleCache_.get(roleName);
248 if (role == null)
throw new CatalogException(
"Role does not exist: " + roleName);
249 role.removeGrantGroup(groupName);
250 Set<String> grantedRoles = groupsToRoles_.get(groupName);
251 if (grantedRoles != null) {
252 grantedRoles.remove(roleName.toLowerCase());
261 public synchronized Set<String>
263 Set<String> privileges = Sets.newHashSet();
264 if (roleSet != ActiveRoleSet.ALL) {
265 throw new UnsupportedOperationException(
"Impala does not support role subsets.");
269 for (String groupName: groups) {
271 for (
Role role: grantedRoles) {
273 String authorizeable = privilege.getName();
274 if (authorizeable == null) {
275 LOG.trace(
"Ignoring invalid privilege: " + privilege.getName());
278 privileges.add(authorizeable);
296 TResultSet result =
new TResultSet();
297 result.setSchema(
new TResultSetMetadata());
303 result.getSchema().addToColumns(
306 result.setRows(Lists.<TResultRow>newArrayList());
309 if (role == null)
return result;
311 TPrivilege privilege = p.toThrift();
312 if (filter != null) {
314 filter.setPrivilege_level(privilege.getPrivilege_level());
315 String privName = RolePrivilege.buildRolePrivilegeName(filter);
316 if (!privName.equalsIgnoreCase(privilege.getPrivilege_name()))
continue;
319 rowBuilder.add(privilege.getScope().toString());
320 rowBuilder.add(Strings.nullToEmpty(privilege.getDb_name()));
321 rowBuilder.add(Strings.nullToEmpty(privilege.getTable_name()));
322 rowBuilder.add(Strings.nullToEmpty(privilege.getUri()));
323 rowBuilder.add(privilege.getPrivilege_level().toString());
324 rowBuilder.add(Boolean.toString(privilege.isHas_grant_opt()));
325 if (privilege.getCreate_time_ms() == -1) {
326 rowBuilder.add(null);
329 TimeStamp.getNtpTime(privilege.getCreate_time_ms()).toDateString());
331 result.addToRows(rowBuilder.get());
synchronized void addPrivilege(RolePrivilege privilege)
List< RolePrivilege > getPrivileges()
synchronized TResultSet getRolePrivileges(String roleName, TPrivilege filter)
Map< Integer, String > roleIds_
static final ScalarType STRING
synchronized RolePrivilege removePrivilege(RolePrivilege privilege)
synchronized Role removeGrantGroup(String roleName, String groupName)
synchronized Role getRole(int roleId)
static final ScalarType BOOLEAN
synchronized long getCatalogVersion()
Map< String, Set< String > > groupsToRoles_
synchronized List< Role > getAllRoles()
final CatalogObjectCache< Role > roleCache_
synchronized Role getRole(String roleName)
synchronized Role addGrantGroup(String roleName, String groupName)
synchronized List< Role > getGrantedRoles(String groupName)
Set< String > getGrantGroups()
synchronized RolePrivilege getPrivilege(int roleId, String privilegeName)
void toThrift(TColumnType container)
synchronized void addRole(Role role)
synchronized Set< String > getAllRoleNames()
synchronized Role removeRole(String roleName)
synchronized Set< String > listPrivileges(Set< String > groups, ActiveRoleSet roleSet)