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);
}
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);
}
}
}
}
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;
}
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)));
}
}
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;
}
Aggregations