blob: be3d07883ac50577b309717d2e1b748594b3c1dd [file] [log] [blame]
/**
* 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.sentry.provider.db.service.persistent;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;
import java.io.File;
import java.util.Collections;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.sentry.provider.db.service.model.MSentryPrivilege;
import org.apache.sentry.provider.db.service.model.MSentryRole;
import org.apache.sentry.provider.db.service.thrift.SentryPolicyStoreProcessor;
import org.apache.sentry.provider.db.service.thrift.TSentryGroup;
import org.apache.sentry.provider.db.service.thrift.TSentryPrivilege;
import org.apache.sentry.provider.db.service.thrift.TSentryRole;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.common.io.Files;
public class TestSentryStore {
private static File dataDir;
private static SentryStore sentryStore;
@BeforeClass
public static void setup() throws Exception {
dataDir = new File(Files.createTempDir(), SentryStore.DEFAULT_DATA_DIR);
sentryStore = new SentryStore(dataDir.getPath());
}
@AfterClass
public static void teardown() {
if (sentryStore != null) {
sentryStore.stop();
}
if (dataDir != null) {
FileUtils.deleteQuietly(dataDir);
}
}
private static CommitContext createRole(String r, String g) throws Exception {
TSentryRole role = new TSentryRole();
role.setGrantorPrincipal(g);
role.setRoleName(r);
return sentryStore.createSentryRole(role);
}
@Test
public void testCreateDuplicateRole() throws Exception {
String roleName = "test-dup-role";
String grantor = "g1";
createRole(roleName, grantor);
try {
createRole(roleName, grantor);
fail("Expected SentryAlreadyExistsException");
} catch(SentryAlreadyExistsException e) {
// expected
}
}
@Test
public void testCreateDropRole() throws Exception {
String roleName = "test-drop-role";
String grantor = "g1";
long seqId = createRole(roleName, grantor).getSequenceId();
assertEquals(seqId + 1, sentryStore.dropSentryRole(roleName).getSequenceId());
}
@Test(expected = SentryNoSuchObjectException.class)
public void testAddDeleteGroupsNonExistantRole()
throws Exception {
String roleName = "non-existant-role";
String grantor = "g1";
Set<TSentryGroup> groups = Sets.newHashSet();
sentryStore.alterSentryRoleAddGroups(grantor, roleName, groups);
}
@Test
public void testAddDeleteGroups() throws Exception {
String roleName = "test-groups";
String grantor = "g1";
long seqId = createRole(roleName, grantor).getSequenceId();
Set<TSentryGroup> groups = Sets.newHashSet();
TSentryGroup group = new TSentryGroup();
group.setGroupName("test-groups-g1");
groups.add(group);
group = new TSentryGroup();
group.setGroupName("test-groups-g2");
groups.add(group);
assertEquals(seqId + 1, sentryStore.alterSentryRoleAddGroups(grantor,
roleName, groups).getSequenceId());
assertEquals(seqId + 2, sentryStore.alterSentryRoleDeleteGroups(roleName, groups)
.getSequenceId());
MSentryRole role = sentryStore.getMSentryRoleByName(roleName);
assertEquals(Collections.emptySet(), role.getGroups());
}
@Test
public void testGrantRevokePrivilege() throws Exception {
String roleName = "test-privilege";
String grantor = "g1";
long seqId = createRole(roleName, grantor).getSequenceId();
TSentryPrivilege privilege = new TSentryPrivilege();
privilege.setPrivilegeScope("TABLE");
privilege.setServerName("server1");
privilege.setDbName("db1");
privilege.setTableName("tbl1");
privilege.setAction("SELECT");
privilege.setGrantorPrincipal(grantor);
privilege.setCreateTime(System.currentTimeMillis());
privilege.setPrivilegeName(SentryPolicyStoreProcessor.constructPrivilegeName(privilege));
assertEquals(seqId + 1, sentryStore.alterSentryRoleGrantPrivilege(roleName, privilege)
.getSequenceId());
MSentryRole role = sentryStore.getMSentryRoleByName(roleName);
Set<MSentryPrivilege> privileges = role.getPrivileges();
assertEquals(privileges.toString(), 1, privileges.size());
assertEquals(privilege.getPrivilegeName(), Iterables.get(privileges, 0).getPrivilegeName());
assertEquals(seqId + 2, sentryStore.alterSentryRoleRevokePrivilege(roleName, privilege.getPrivilegeName())
.getSequenceId());
}
}