use of org.osgi.service.cm.Configuration in project karaf by apache.
the class EncryptableConfigAdminPropertyPlaceholderTest method testEncryptConfigProperty.
@Test
public void testEncryptConfigProperty() throws Exception {
for (Bundle bundle : bundleContext.getBundles()) {
System.out.println(bundle.getSymbolicName() + " / " + bundle.getVersion());
}
configAdmin = getOsgiService(ConfigurationAdmin.class);
assertNotNull(configAdmin);
Configuration config = configAdmin.createFactoryConfiguration("encrypt.config", null);
Dictionary props = new Properties();
// Encrypt a key/value
// bar is encrypted and link to foo key
encryptedValue = enc.encrypt("bar");
props.put("foo", encryptedValue);
config.update(props);
Configuration[] configs = configAdmin.listConfigurations(null);
for (Configuration conf : configs) {
String pid = conf.getPid();
// System.out.println(">> ConfigImpl pid : " + pid);
Dictionary<String, ?> dict = conf.getProperties();
for (Enumeration e = dict.keys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
Object value = dict.get(key);
if (key.equals("foo")) {
String val = (String) value;
// Verify encrypted value
assertEquals(encryptedValue, val);
// Decrypt and check value
String decrypt = enc.decrypt(val);
assertEquals("bar", decrypt);
}
}
}
}
use of org.osgi.service.cm.Configuration in project karaf by apache.
the class ProfileEdit method importPidFromLocalConfigAdmin.
/**
* Imports the pid to the target Map.
*/
private void importPidFromLocalConfigAdmin(String pid, Map<String, Object> target) {
try {
Configuration[] configuration = configurationAdmin.listConfigurations("(service.pid=" + pid + ")");
if (configuration != null && configuration.length > 0) {
Dictionary dictionary = configuration[0].getProperties();
Enumeration keyEnumeration = dictionary.keys();
while (keyEnumeration.hasMoreElements()) {
String key = String.valueOf(keyEnumeration.nextElement());
//file.install.filename needs to be skipped as it specific to the current container.
if (!key.equals(FILE_INSTALL_FILENAME_PROPERTY)) {
String value = String.valueOf(dictionary.get(key));
target.put(key, value);
}
}
}
} catch (Exception e) {
LOGGER.warn("Error while importing configuration {} to profile.", pid);
}
}
use of org.osgi.service.cm.Configuration in project karaf by apache.
the class BulkRequestContext method newContext.
public static BulkRequestContext newContext(ConfigurationAdmin configAdmin) throws IOException {
BulkRequestContext context = new BulkRequestContext();
context.configAdmin = configAdmin;
try {
// check JAAS subject here
AccessControlContext acc = AccessController.getContext();
if (acc == null) {
context.anonymous = true;
} else {
Subject subject = Subject.getSubject(acc);
if (subject == null) {
context.anonymous = true;
} else {
context.principals.addAll(subject.getPrincipals());
}
}
// list available ACL configs - valid for this instance only
for (Configuration config : configAdmin.listConfigurations("(service.pid=jmx.acl*)")) {
context.allPids.add(config.getPid());
}
// list available ACT whitelist configs
Configuration[] configs = configAdmin.listConfigurations("(service.pid=jmx.acl.whitelist)");
if (configs != null) {
for (Configuration config : configs) {
context.whiteListProperties.add(config.getProperties());
}
}
} catch (InvalidSyntaxException ise) {
throw new RuntimeException(ise);
}
return context;
}
use of org.osgi.service.cm.Configuration in project karaf by apache.
the class JMXSecurityMBeanImplTestCase method testCanInvokeBulk.
public void testCanInvokeBulk() throws Exception {
MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
EasyMock.replay(mbs);
ConfigurationAdmin testConfigAdmin = EasyMock.createMock(ConfigurationAdmin.class);
EasyMock.expect(testConfigAdmin.listConfigurations(EasyMock.eq("(service.pid=jmx.acl*)"))).andReturn(new Configuration[0]).anyTimes();
EasyMock.expect(testConfigAdmin.listConfigurations(EasyMock.eq("(service.pid=jmx.acl.whitelist)"))).andReturn(new Configuration[0]).once();
EasyMock.replay(testConfigAdmin);
KarafMBeanServerGuard testGuard = EasyMock.createMock(KarafMBeanServerGuard.class);
String objectName = "foo.bar.testing:type=SomeMBean";
final String[] la = new String[] { "long" };
final String[] sa = new String[] { "java.lang.String" };
EasyMock.expect(testGuard.getConfigAdmin()).andReturn(testConfigAdmin).anyTimes();
EasyMock.expect(testGuard.canInvoke(EasyMock.anyObject(BulkRequestContext.class), EasyMock.eq(mbs), EasyMock.eq(new ObjectName(objectName)), EasyMock.eq("testMethod"), EasyMock.aryEq(la))).andReturn(true).anyTimes();
EasyMock.expect(testGuard.canInvoke(EasyMock.anyObject(BulkRequestContext.class), EasyMock.eq(mbs), EasyMock.eq(new ObjectName(objectName)), EasyMock.eq("testMethod"), EasyMock.aryEq(sa))).andReturn(false).anyTimes();
EasyMock.expect(testGuard.canInvoke(EasyMock.anyObject(BulkRequestContext.class), EasyMock.eq(mbs), EasyMock.eq(new ObjectName(objectName)), EasyMock.eq("otherMethod"))).andReturn(true).anyTimes();
String objectName2 = "foo.bar.testing:type=SomeOtherMBean";
EasyMock.expect(testGuard.canInvoke(EasyMock.anyObject(BulkRequestContext.class), EasyMock.eq(mbs), EasyMock.eq(new ObjectName(objectName2)))).andReturn(true).anyTimes();
String objectName3 = "foo.bar.foo.testing:type=SomeOtherMBean";
EasyMock.expect(testGuard.canInvoke(EasyMock.anyObject(BulkRequestContext.class), EasyMock.eq(mbs), EasyMock.eq(new ObjectName(objectName3)))).andReturn(false).anyTimes();
EasyMock.replay(testGuard);
JMXSecurityMBeanImpl mb = new JMXSecurityMBeanImpl();
mb.setMBeanServer(mbs);
mb.setGuard(testGuard);
Map<String, List<String>> query = new HashMap<>();
query.put(objectName, Arrays.asList("otherMethod", "testMethod(long)", "testMethod(java.lang.String)"));
query.put(objectName2, Collections.emptyList());
query.put(objectName3, Collections.emptyList());
TabularData result = mb.canInvoke(query);
assertEquals(5, result.size());
CompositeData cd = result.get(new Object[] { objectName, "testMethod(long)" });
assertEquals(objectName, cd.get("ObjectName"));
assertEquals("testMethod(long)", cd.get("Method"));
assertEquals(true, cd.get("CanInvoke"));
CompositeData cd2 = result.get(new Object[] { objectName, "testMethod(java.lang.String)" });
assertEquals(objectName, cd2.get("ObjectName"));
assertEquals("testMethod(java.lang.String)", cd2.get("Method"));
assertEquals(false, cd2.get("CanInvoke"));
CompositeData cd3 = result.get(new Object[] { objectName, "otherMethod" });
assertEquals(objectName, cd3.get("ObjectName"));
assertEquals("otherMethod", cd3.get("Method"));
assertEquals(true, cd3.get("CanInvoke"));
CompositeData cd4 = result.get(new Object[] { objectName2, "" });
assertEquals(objectName2, cd4.get("ObjectName"));
assertEquals("", cd4.get("Method"));
assertEquals(true, cd4.get("CanInvoke"));
CompositeData cd5 = result.get(new Object[] { objectName3, "" });
assertEquals(objectName3, cd5.get("ObjectName"));
assertEquals("", cd5.get("Method"));
assertEquals(false, cd5.get("CanInvoke"));
}
use of org.osgi.service.cm.Configuration in project karaf by apache.
the class OsgiConfigLoginModule method login.
public boolean login() throws LoginException {
try {
String pid = (String) options.get(PID);
Configuration config = ConfigAdminHolder.getService().getConfiguration(pid, null);
Dictionary properties = config.getProperties();
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("Username: ");
callbacks[1] = new PasswordCallback("Password: ", false);
try {
callbackHandler.handle(callbacks);
} catch (IOException ioe) {
throw new LoginException(ioe.getMessage());
} catch (UnsupportedCallbackException uce) {
throw new LoginException(uce.getMessage() + " not available to obtain information from user");
}
String user = ((NameCallback) callbacks[0]).getName();
String password = new String(((PasswordCallback) callbacks[1]).getPassword());
String userInfos = (String) properties.get(USER_PREFIX + user);
if (userInfos == null) {
if (!this.detailedLoginExcepion) {
throw new FailedLoginException("login failed");
} else {
throw new FailedLoginException("User does not exist");
}
}
String[] infos = userInfos.split(",");
String storedPassword = infos[0];
// check the provided password
if (!checkPassword(password, storedPassword)) {
if (!this.detailedLoginExcepion) {
throw new FailedLoginException("login failed");
} else {
throw new FailedLoginException("Password for " + user + " does not match");
}
}
principals = new HashSet<>();
principals.add(new UserPrincipal(user));
for (int i = 1; i < infos.length; i++) {
principals.add(new RolePrincipal(infos[i]));
}
return true;
} catch (LoginException e) {
throw e;
} catch (Exception e) {
throw (LoginException) new LoginException("Unable to authenticate user").initCause(e);
} finally {
callbackHandler = null;
options = null;
}
}
Aggregations