Search in sources :

Example 26 with Configuration

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);
            }
        }
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) TinyBundle(org.ops4j.pax.tinybundles.core.TinyBundle) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Test(org.junit.Test)

Example 27 with Configuration

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);
    }
}
Also used : Dictionary(java.util.Dictionary) Enumeration(java.util.Enumeration) Configuration(org.osgi.service.cm.Configuration)

Example 28 with Configuration

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;
}
Also used : AccessControlContext(java.security.AccessControlContext) Configuration(org.osgi.service.cm.Configuration) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Subject(javax.security.auth.Subject)

Example 29 with Configuration

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"));
}
Also used : Configuration(org.osgi.service.cm.Configuration) KarafMBeanServerGuard(org.apache.karaf.management.KarafMBeanServerGuard) CompositeData(javax.management.openmbean.CompositeData) ObjectName(javax.management.ObjectName) TabularData(javax.management.openmbean.TabularData) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) MBeanServer(javax.management.MBeanServer)

Example 30 with Configuration

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;
    }
}
Also used : Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) IOException(java.io.IOException) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal)

Aggregations

Configuration (org.osgi.service.cm.Configuration)226 Test (org.junit.Test)85 Hashtable (java.util.Hashtable)75 IOException (java.io.IOException)55 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)49 Dictionary (java.util.Dictionary)36 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)19 ServiceReference (org.osgi.framework.ServiceReference)19 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)18 Matchers.anyString (org.mockito.Matchers.anyString)16 BundleContext (org.osgi.framework.BundleContext)15 RegistrySourceConfiguration (org.codice.ddf.registry.federationadmin.service.internal.RegistrySourceConfiguration)11 Map (java.util.Map)10 Bundle (org.osgi.framework.Bundle)10 File (java.io.File)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 Mockito.anyString (org.mockito.Mockito.anyString)9 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)8 SkipUnstableTest (org.codice.ddf.itests.common.annotations.SkipUnstableTest)7