Search in sources :

Example 1 with Group

use of org.apache.qpid.server.model.Group in project qpid-broker-j by apache.

the class FileBasedGroupProviderImplTest method testRemoveGroupAndMember.

public void testRemoveGroupAndMember() throws Exception {
    Map<String, Set<String>> input = new HashMap<>();
    input.put("supers", Sets.newHashSet("root"));
    input.put("operators", Sets.newHashSet("operator", "root"));
    _groupFile = createTemporaryGroupFile(input);
    Map<String, Object> providerAttrs = new HashMap<>();
    String groupsFile = _groupFile.getAbsolutePath();
    providerAttrs.put(FileBasedGroupProvider.TYPE, GROUP_FILE_PROVIDER_TYPE);
    providerAttrs.put(FileBasedGroupProvider.PATH, groupsFile);
    providerAttrs.put(FileBasedGroupProvider.NAME, getTestName());
    @SuppressWarnings("unchecked") GroupProvider<?> provider = _objectFactory.create(GroupProvider.class, providerAttrs, _broker);
    assertThat(provider.getChildren(Group.class).size(), is(equalTo(2)));
    Group operators = provider.getChildByName(Group.class, "operators");
    GroupMember rootMember = (GroupMember) operators.getChildByName(GroupMember.class, "root");
    rootMember.delete();
    assertThat(operators.getChildren(GroupMember.class).size(), is(equalTo(1)));
    Group supers = provider.getChildByName(Group.class, "supers");
    assertThat(supers.getChildren(GroupMember.class).size(), is(equalTo(1)));
    operators.delete();
    assertThat(provider.getChildren(Group.class).size(), is(equalTo(1)));
}
Also used : Group(org.apache.qpid.server.model.Group) GroupMember(org.apache.qpid.server.model.GroupMember) Set(java.util.Set) HashMap(java.util.HashMap)

Example 2 with Group

use of org.apache.qpid.server.model.Group in project qpid-broker-j by apache.

the class FileBasedGroupProviderImpl method onOpen.

@Override
protected void onOpen() {
    super.onOpen();
    FileGroupDatabase groupDatabase = new FileGroupDatabase();
    try {
        groupDatabase.setGroupFile(getPath());
    } catch (IOException | RuntimeException e) {
        if (e instanceof IllegalConfigurationException) {
            throw (IllegalConfigurationException) e;
        }
        throw new IllegalConfigurationException(String.format("Cannot load groups from '%s'", getPath()), e);
    }
    _groupDatabase = groupDatabase;
    Set<Principal> groups = getGroupPrincipals();
    Collection<Group> principals = new ArrayList<>(groups.size());
    for (Principal group : groups) {
        Map<String, Object> attrMap = new HashMap<String, Object>();
        UUID id = UUID.randomUUID();
        attrMap.put(ConfiguredObject.ID, id);
        attrMap.put(ConfiguredObject.NAME, group.getName());
        GroupAdapter groupAdapter = new GroupAdapter(attrMap);
        principals.add(groupAdapter);
        groupAdapter.registerWithParents();
        // TODO - we know this is safe, but the sync method shouldn't really be called from the management thread
        groupAdapter.openAsync();
    }
}
Also used : Group(org.apache.qpid.server.model.Group) HashMap(java.util.HashMap) FileGroupDatabase(org.apache.qpid.server.security.group.FileGroupDatabase) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) UUID(java.util.UUID) GroupPrincipal(org.apache.qpid.server.security.group.GroupPrincipal) Principal(java.security.Principal)

Example 3 with Group

use of org.apache.qpid.server.model.Group in project qpid-broker-j by apache.

the class FileBasedGroupProviderImplTest method testAddGroupAndMember.

public void testAddGroupAndMember() throws Exception {
    _groupFile = createTemporaryGroupFile(Collections.emptyMap());
    Map<String, Object> providerAttrs = new HashMap<>();
    String groupsFile = _groupFile.getAbsolutePath();
    providerAttrs.put(FileBasedGroupProvider.TYPE, GROUP_FILE_PROVIDER_TYPE);
    providerAttrs.put(FileBasedGroupProvider.PATH, groupsFile);
    providerAttrs.put(FileBasedGroupProvider.NAME, getTestName());
    @SuppressWarnings("unchecked") GroupProvider<?> provider = _objectFactory.create(GroupProvider.class, providerAttrs, _broker);
    assertThat(provider.getChildren(Group.class).size(), is(equalTo(0)));
    final Map<String, Object> groupAttrs = Collections.singletonMap(Group.NAME, "supers");
    Group superGroup = provider.createChild(Group.class, groupAttrs);
    assertThat(superGroup.getName(), is(equalTo("supers")));
    final Map<String, Object> memberAttrs = Collections.singletonMap(GroupMember.NAME, "root");
    GroupMember rootMember = (GroupMember) superGroup.createChild(GroupMember.class, memberAttrs);
    assertThat(rootMember.getName(), is(equalTo("root")));
}
Also used : Group(org.apache.qpid.server.model.Group) GroupMember(org.apache.qpid.server.model.GroupMember) HashMap(java.util.HashMap)

Example 4 with Group

use of org.apache.qpid.server.model.Group in project qpid-broker-j by apache.

the class FileBasedGroupProviderImplTest method testExistingGroupFile.

public void testExistingGroupFile() throws Exception {
    Map<String, Set<String>> input = new HashMap<>();
    input.put("super", Sets.newHashSet("root"));
    _groupFile = createTemporaryGroupFile(input);
    Map<String, Object> providerAttrs = new HashMap<>();
    String groupsFile = _groupFile.getAbsolutePath();
    providerAttrs.put(FileBasedGroupProvider.TYPE, GROUP_FILE_PROVIDER_TYPE);
    providerAttrs.put(FileBasedGroupProvider.PATH, groupsFile);
    providerAttrs.put(FileBasedGroupProvider.NAME, getTestName());
    @SuppressWarnings("unchecked") GroupProvider<?> provider = _objectFactory.create(GroupProvider.class, providerAttrs, _broker);
    Set<Principal> adminGroups = provider.getGroupPrincipalsForUser(() -> "root");
    assertThat("root has unexpected group membership", adminGroups.stream().map(Principal::getName).collect(Collectors.toSet()), containsInAnyOrder("super"));
    Collection<Group> groups = provider.getChildren(Group.class);
    assertThat(groups.size(), is(equalTo(1)));
    Group<?> superGroup = groups.iterator().next();
    assertThat(superGroup.getName(), is(equalTo("super")));
    Collection<GroupMember> members = superGroup.getChildren(GroupMember.class);
    assertThat(members.size(), is(equalTo(1)));
    GroupMember rootMember = members.iterator().next();
    assertThat(rootMember.getName(), is(equalTo("root")));
}
Also used : Group(org.apache.qpid.server.model.Group) GroupMember(org.apache.qpid.server.model.GroupMember) Set(java.util.Set) HashMap(java.util.HashMap) Principal(java.security.Principal)

Example 5 with Group

use of org.apache.qpid.server.model.Group in project qpid-broker-j by apache.

the class FileBasedGroupProviderImplTest method createTemporaryGroupFile.

private File createTemporaryGroupFile(Map<String, Set<String>> groups) throws Exception {
    File groupFile = File.createTempFile("group", "grp");
    groupFile.deleteOnExit();
    Properties props = new Properties();
    Map<String, String> m = groups.entrySet().stream().collect(Collectors.toMap(e -> e.getKey() + ".users", e -> e.getValue().stream().collect(Collectors.joining(","))));
    props.putAll(m);
    try (final FileOutputStream out = new FileOutputStream(groupFile)) {
        props.store(out, "test group file");
    }
    return groupFile;
}
Also used : GROUP_FILE_PROVIDER_TYPE(org.apache.qpid.server.model.adapter.FileBasedGroupProviderImpl.GROUP_FILE_PROVIDER_TYPE) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Group(org.apache.qpid.server.model.Group) HashMap(java.util.HashMap) GroupProvider(org.apache.qpid.server.model.GroupProvider) Assert.assertThat(org.junit.Assert.assertThat) ConfiguredObjectFactory(org.apache.qpid.server.model.ConfiguredObjectFactory) Map(java.util.Map) GroupMember(org.apache.qpid.server.model.GroupMember) Properties(java.util.Properties) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) TestFileUtils(org.apache.qpid.test.utils.TestFileUtils) Collection(java.util.Collection) Broker(org.apache.qpid.server.model.Broker) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) File(java.io.File) Sets(com.google.common.collect.Sets) QpidTestCase(org.apache.qpid.test.utils.QpidTestCase) Principal(java.security.Principal) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) BrokerTestHelper(org.apache.qpid.server.model.BrokerTestHelper) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Collections(java.util.Collections) FileOutputStream(java.io.FileOutputStream) Properties(java.util.Properties) File(java.io.File)

Aggregations

HashMap (java.util.HashMap)5 Group (org.apache.qpid.server.model.Group)5 GroupMember (org.apache.qpid.server.model.GroupMember)4 Principal (java.security.Principal)3 Set (java.util.Set)3 IllegalConfigurationException (org.apache.qpid.server.configuration.IllegalConfigurationException)2 Sets (com.google.common.collect.Sets)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Properties (java.util.Properties)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 AbstractConfiguredObject (org.apache.qpid.server.model.AbstractConfiguredObject)1 Broker (org.apache.qpid.server.model.Broker)1 BrokerTestHelper (org.apache.qpid.server.model.BrokerTestHelper)1