Search in sources :

Example 11 with Preference

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

the class BrokerImplTest method testPurgeUser.

@Test
public void testPurgeUser() throws Exception {
    final String testUsername = "testUser";
    final String testPassword = "testPassword";
    // setup broker
    Map<String, Object> brokerAttributes = new HashMap<>();
    brokerAttributes.put("name", "Broker");
    brokerAttributes.put(Broker.MODEL_VERSION, BrokerModel.MODEL_VERSION);
    brokerAttributes.put(Broker.DURABLE, true);
    _brokerImpl = new BrokerImpl(brokerAttributes, _systemConfig);
    _brokerImpl.open();
    // setup auth provider with testuser
    final Map<String, Object> authProviderAttributes = new HashMap<>();
    authProviderAttributes.put(ConfiguredObject.NAME, "testAuthProvider");
    authProviderAttributes.put(ConfiguredObject.TYPE, "Simple");
    SimpleAuthenticationManager authenticationProvider = new SimpleAuthenticationManager(authProviderAttributes, _brokerImpl);
    authenticationProvider.create();
    authenticationProvider.addUser(testUsername, testPassword);
    // setup preference owned by testuser
    final Map<String, Object> preferenceAttributes = new HashMap<>();
    UUID preferenceId = UUID.randomUUID();
    preferenceAttributes.put(Preference.ID_ATTRIBUTE, preferenceId);
    preferenceAttributes.put(Preference.NAME_ATTRIBUTE, "testPref");
    preferenceAttributes.put(Preference.TYPE_ATTRIBUTE, "X-testPrefType");
    preferenceAttributes.put(Preference.VALUE_ATTRIBUTE, Collections.EMPTY_MAP);
    Subject testUserSubject = new Subject();
    testUserSubject.getPrincipals().add(new AuthenticatedPrincipal(new UsernamePrincipal(testUsername, authenticationProvider)));
    testUserSubject.setReadOnly();
    final Collection<Preference> preferences = Collections.singleton(PreferenceFactory.fromAttributes(_brokerImpl, preferenceAttributes));
    Subject.doAs(testUserSubject, new PrivilegedAction<Void>() {

        @Override
        public Void run() {
            try {
                _brokerImpl.getUserPreferences().updateOrAppend(preferences).get(10, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                e.printStackTrace();
                fail("Failed to put preference:");
            }
            return null;
        }
    });
    // test pre-conditions
    Collection<Preference> preferencesBeforePurge = getPreferencesAs(testUserSubject);
    assertEquals("Unexpected number of preferences before userPurge", (long) 1, (long) preferencesBeforePurge.size());
    assertEquals("Unexpected preference before userPurge", preferenceId, preferencesBeforePurge.iterator().next().getId());
    assertTrue("User was not valid before userPurge", authenticationProvider.getUsers().containsKey(testUsername));
    _brokerImpl.purgeUser(authenticationProvider, testUsername);
    // test post-conditions
    Collection<Preference> preferencesAfterPurge = getPreferencesAs(testUserSubject);
    assertEquals("Preferences were not deleted during userPurge", Collections.EMPTY_SET, preferencesAfterPurge);
    assertEquals("User was not deleted from authentication Provider", Collections.EMPTY_MAP, authenticationProvider.getUsers());
    verify(_preferenceStore).replace(Collections.singleton(preferenceId), Collections.EMPTY_SET);
}
Also used : HashMap(java.util.HashMap) Subject(javax.security.auth.Subject) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal) UsernamePrincipal(org.apache.qpid.server.security.auth.UsernamePrincipal) BrokerImpl(org.apache.qpid.server.model.BrokerImpl) Preference(org.apache.qpid.server.model.preferences.Preference) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) SimpleAuthenticationManager(org.apache.qpid.server.security.auth.manager.SimpleAuthenticationManager) UUID(java.util.UUID) Test(org.junit.Test)

Example 12 with Preference

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

the class PreferencesRecoverer method setUserPreferences.

private void setUserPreferences(ConfiguredObject<?> associatedObject, Map<UUID, Collection<PreferenceRecord>> objectToRecordMap, PreferenceStore preferenceStore, final Set<UUID> corruptedRecords) {
    final Collection<PreferenceRecord> preferenceRecords = objectToRecordMap.remove(associatedObject.getId());
    Collection<Preference> recoveredPreferences = new ArrayList<>();
    if (preferenceRecords != null) {
        for (PreferenceRecord preferenceRecord : preferenceRecords) {
            Map<String, Object> attributes = preferenceRecord.getAttributes();
            try {
                Preference recoveredPreference = PreferenceFactory.fromAttributes(associatedObject, attributes);
                validateRecoveredPreference(recoveredPreference);
                recoveredPreferences.add(recoveredPreference);
            } catch (IllegalArgumentException e) {
                LOGGER.info(String.format("Cannot recover preference '%s/%s'", preferenceRecord.getId(), attributes.get(Preference.NAME_ATTRIBUTE)), e);
                corruptedRecords.add(preferenceRecord.getId());
            }
        }
    }
    associatedObject.setUserPreferences(new UserPreferencesImpl(_executor, associatedObject, preferenceStore, recoveredPreferences));
    if (!(associatedObject instanceof PreferencesRoot)) {
        Collection<Class<? extends ConfiguredObject>> childrenCategories = associatedObject.getModel().getChildTypes(associatedObject.getCategoryClass());
        for (Class<? extends ConfiguredObject> childCategory : childrenCategories) {
            Collection<? extends ConfiguredObject> children = associatedObject.getChildren(childCategory);
            for (ConfiguredObject<?> child : children) {
                setUserPreferences(child, objectToRecordMap, preferenceStore, corruptedRecords);
            }
        }
    }
}
Also used : UserPreferencesImpl(org.apache.qpid.server.model.preferences.UserPreferencesImpl) ArrayList(java.util.ArrayList) Preference(org.apache.qpid.server.model.preferences.Preference) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject)

Example 13 with Preference

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

the class LatestManagementControllerTest method createPreferences.

private UUID createPreferences(final Subject testSubject, final QueueManagingVirtualHost<?> virtualHost, final String preferenceType, final String preferenceName, final Map<String, Object> preferenceValue) throws Exception {
    UUID uuid = UUID.randomUUID();
    final Preference preference = new PreferenceImpl(virtualHost, uuid, preferenceName, preferenceType, "Some preference", null, new Date(), new Date(), null, new GenericPreferenceValueFactory().createInstance(preferenceValue));
    final List<Preference> preferenceList = Collections.singletonList(preference);
    final Future<Void> result = Subject.doAs(testSubject, (PrivilegedAction<Future<Void>>) () -> virtualHost.getUserPreferences().updateOrAppend(preferenceList));
    result.get(2000L, TimeUnit.MILLISECONDS);
    return uuid;
}
Also used : GenericPreferenceValueFactory(org.apache.qpid.server.model.preferences.GenericPreferenceValueFactory) Preference(org.apache.qpid.server.model.preferences.Preference) Future(java.util.concurrent.Future) UUID(java.util.UUID) PreferenceImpl(org.apache.qpid.server.model.preferences.PreferenceImpl) Date(java.util.Date)

Example 14 with Preference

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

the class RestUserPreferenceHandler method handlePUT.

public void handlePUT(ConfiguredObject<?> target, RequestInfo requestInfo, Object providedObject) {
    UserPreferences userPreferences = target.getUserPreferences();
    if (userPreferences == null) {
        throw new NotFoundException("User preferences are not available");
    }
    final List<String> preferencesParts = requestInfo.getPreferencesParts();
    if (preferencesParts.size() == 2) {
        if (!(providedObject instanceof Map)) {
            throw new IllegalArgumentException("expected object");
        }
        Map<String, Object> providedAttributes = (Map<String, Object>) providedObject;
        String type = preferencesParts.get(0);
        String name = preferencesParts.get(1);
        ensureAttributeMatches(providedAttributes, "name", name);
        ensureAttributeMatches(providedAttributes, "type", type);
        Preference preference = PreferenceFactory.fromAttributes(target, providedAttributes);
        awaitFuture(userPreferences.replaceByTypeAndName(type, name, preference));
    } else if (preferencesParts.size() == 1) {
        String type = preferencesParts.get(0);
        if (!(providedObject instanceof List)) {
            throw new IllegalArgumentException("expected a list of objects");
        }
        List<Preference> replacementPreferences = validateAndConvert(target, type, (List<Object>) providedObject);
        awaitFuture(userPreferences.replaceByType(type, replacementPreferences));
    } else if (preferencesParts.size() == 0) {
        if (!(providedObject instanceof Map)) {
            throw new IllegalArgumentException("expected object");
        }
        List<Preference> replacementPreferences = validateAndConvert(target, (Map<String, Object>) providedObject);
        awaitFuture(userPreferences.replace(replacementPreferences));
    } else {
        throw new IllegalArgumentException(String.format("unexpected path '%s'", Joiner.on("/").join(preferencesParts)));
    }
}
Also used : UserPreferences(org.apache.qpid.server.model.preferences.UserPreferences) Preference(org.apache.qpid.server.model.preferences.Preference) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 15 with Preference

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

the class RestUserPreferenceHandler method validateAndConvert.

private List<Preference> validateAndConvert(final ConfiguredObject<?> target, final Map<String, Object> providedObjectMap) {
    List<Preference> replacementPreferences = new ArrayList<>();
    for (String type : providedObjectMap.keySet()) {
        if (!(providedObjectMap.get(type) instanceof List)) {
            final String errorMessage = String.format("expected a list of objects for attribute '%s'", type);
            throw new IllegalArgumentException(errorMessage);
        }
        for (Object preferenceObject : (List<Object>) providedObjectMap.get(type)) {
            if (!(preferenceObject instanceof Map)) {
                final String errorMessage = String.format("encountered non preference object in list of type '%s'", type);
                throw new IllegalArgumentException(errorMessage);
            }
            Map<String, Object> preferenceAttributes = (Map<String, Object>) preferenceObject;
            ensureAttributeMatches(preferenceAttributes, "type", type);
            Preference preference = PreferenceFactory.fromAttributes(target, preferenceAttributes);
            replacementPreferences.add(preference);
        }
    }
    return replacementPreferences;
}
Also used : Preference(org.apache.qpid.server.model.preferences.Preference) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Preference (org.apache.qpid.server.model.preferences.Preference)40 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)39 Test (org.junit.Test)31 Subject (javax.security.auth.Subject)16 HashMap (java.util.HashMap)11 Map (java.util.Map)9 Set (java.util.Set)9 HashSet (java.util.HashSet)8 ArrayList (java.util.ArrayList)6 List (java.util.List)6 AuthenticatedPrincipal (org.apache.qpid.server.security.auth.AuthenticatedPrincipal)5 Date (java.util.Date)4 GroupPrincipal (org.apache.qpid.server.security.group.GroupPrincipal)4 Principal (java.security.Principal)3 UUID (java.util.UUID)3 GenericPrincipal (org.apache.qpid.server.model.preferences.GenericPrincipal)3 UserPreferencesImpl (org.apache.qpid.server.model.preferences.UserPreferencesImpl)3 LinkedHashSet (java.util.LinkedHashSet)2 CurrentThreadTaskExecutor (org.apache.qpid.server.configuration.updater.CurrentThreadTaskExecutor)2 UserPreferences (org.apache.qpid.server.model.preferences.UserPreferences)2