Search in sources :

Example 96 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.

the class KarafMBeanServerGuardTest method testGetAttributes2.

public void testGetAttributes2() throws Throwable {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    MBeanAttributeInfo attr = new MBeanAttributeInfo("Toast", "boolean", "", true, false, true);
    MBeanAttributeInfo attr2 = new MBeanAttributeInfo("TestSomething", "boolean", "", true, false, true);
    MBeanAttributeInfo attr3 = new MBeanAttributeInfo("Butter", "boolean", "", true, true, true);
    MBeanInfo mbeanInfo = EasyMock.createMock(MBeanInfo.class);
    EasyMock.expect(mbeanInfo.getAttributes()).andReturn(new MBeanAttributeInfo[] { attr, attr2, attr3 }).anyTimes();
    EasyMock.replay(mbeanInfo);
    final MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.expect(mbs.getMBeanInfo(on)).andReturn(mbeanInfo).anyTimes();
    EasyMock.replay(mbs);
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("isT*", "editor");
    configuration.put("getToast", "admin");
    configuration.put("getButter", "editor");
    configuration.put("getTest*", "admin");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    Subject subject = loginWithTestRoles("editor", "admin");
    Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
        try {
            Method im = MBeanServer.class.getMethod("getAttributes", ObjectName.class, String[].class);
            guard.invoke(mbs, im, new Object[] { on, new String[] { "Toast" } });
            guard.invoke(mbs, im, new Object[] { on, new String[] { "TestSomething", "Toast" } });
            try {
                guard.invoke(mbs, im, new Object[] { on, new String[] { "Butter", "Toast" } });
                fail("Should not have allowed the invocation");
            } catch (SecurityException se) {
            }
            return null;
        } catch (Throwable ex) {
            throw new RuntimeException(ex);
        }
    });
}
Also used : Method(java.lang.reflect.Method) Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 97 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.

the class KarafMBeanServerGuardTest method testRequiredRolesMethodNameWildcard2.

public void testRequiredRolesMethodNameWildcard2() throws Exception {
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("ge", "janitor");
    configuration.put("get", "admin");
    configuration.put("get*", "viewer");
    configuration.put("*", "manager");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    assertEquals(Collections.singletonList("viewer"), guard.getRequiredRoles(on, "getFoo", new Object[] {}, new String[] {}));
    assertEquals(Collections.singletonList("admin"), guard.getRequiredRoles(on, "get", new Object[] {}, new String[] {}));
    assertEquals(Collections.singletonList("janitor"), guard.getRequiredRoles(on, "ge", new Object[] {}, new String[] {}));
}
Also used : ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 98 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project karaf by apache.

the class KarafMBeanServerGuardTest method testGetAttributeIs.

public void testGetAttributeIs() throws Throwable {
    final ObjectName on = ObjectName.getInstance("foo.bar:type=Test");
    MBeanAttributeInfo attr = new MBeanAttributeInfo("Toast", "boolean", "", true, false, true);
    MBeanAttributeInfo attr2 = new MBeanAttributeInfo("TestAttr", "java.lang.String", "", true, false, false);
    MBeanAttributeInfo attr3 = new MBeanAttributeInfo("Butter", "int", "", true, true, false);
    MBeanInfo mbeanInfo = EasyMock.createMock(MBeanInfo.class);
    EasyMock.expect(mbeanInfo.getAttributes()).andReturn(new MBeanAttributeInfo[] { attr, attr2, attr3 }).anyTimes();
    EasyMock.replay(mbeanInfo);
    final MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.expect(mbs.getMBeanInfo(on)).andReturn(mbeanInfo).anyTimes();
    EasyMock.replay(mbs);
    Dictionary<String, Object> configuration = new Hashtable<>();
    configuration.put("getToast", "admin");
    configuration.put("isToast", "editor");
    configuration.put("getTest*", "admin");
    ConfigurationAdmin ca = getMockConfigAdmin(configuration);
    final KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(ca);
    Subject subject = loginWithTestRoles("editor", "admin");
    Subject.doAs(subject, (PrivilegedAction<Void>) () -> {
        try {
            Method im = MBeanServer.class.getMethod("getAttribute", ObjectName.class, String.class);
            guard.invoke(mbs, im, new Object[] { on, "Toast" });
            guard.invoke(mbs, im, new Object[] { on, "TestAttr" });
            try {
                guard.invoke(mbs, im, new Object[] { on, "Butter" });
                fail("Should not have allowed the invocation");
            } catch (SecurityException se) {
            }
            return null;
        } catch (Throwable ex) {
            throw new RuntimeException(ex);
        }
    });
}
Also used : Method(java.lang.reflect.Method) Subject(javax.security.auth.Subject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 99 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project sling by apache.

the class ServicesListener method notifyChange.

public synchronized void notifyChange() {
    // check if all services are available
    final ResourceChangeListener listener = (ResourceChangeListener) this.changeHandlerListener.getService();
    final ConfigurationAdmin configAdmin = (ConfigurationAdmin) this.configAdminListener.getService();
    if (configAdmin != null && listener != null) {
        if (configTaskCreator == null) {
            final Hashtable<String, String> props = new Hashtable<String, String>();
            props.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Configuration Install Task Factory");
            props.put(Constants.SERVICE_VENDOR, VENDOR);
            props.put(InstallTaskFactory.NAME, "org.osgi.service.cm");
            props.put(ResourceTransformer.NAME, "org.osgi.service.cm");
            this.configTaskCreator = new ConfigTaskCreator(listener, configAdmin);
            // start and register osgi installer service
            final String[] serviceInterfaces = { InstallTaskFactory.class.getName(), ConfigurationListener.class.getName(), ResourceTransformer.class.getName() };
            configTaskCreatorRegistration = this.bundleContext.registerService(serviceInterfaces, configTaskCreator, props);
        }
    } else {
        this.stop();
    }
}
Also used : ConfigurationListener(org.osgi.service.cm.ConfigurationListener) InstallTaskFactory(org.apache.sling.installer.api.tasks.InstallTaskFactory) Hashtable(java.util.Hashtable) ResourceChangeListener(org.apache.sling.installer.api.ResourceChangeListener) ResourceTransformer(org.apache.sling.installer.api.tasks.ResourceTransformer) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 100 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin in project sling by apache.

the class MDCStateServlet method createTestConfig.

private void createTestConfig() throws IOException {
    ConfigurationAdmin ca = (ConfigurationAdmin) configAdminTracker.getService();
    Configuration cfg = ca.getConfiguration("org.apache.sling.extensions.mdc.internal.MDCInsertingFilter", null);
    Dictionary<String, Object> dict = new Hashtable<String, Object>();
    dict.put("headers", new String[] { "mdc-test-header" });
    dict.put("parameters", new String[] { "mdc-test-param" });
    dict.put("cookies", new String[] { "mdc-test-cookie" });
    cfg.update(dict);
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) JsonObject(javax.json.JsonObject) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Aggregations

ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)108 Configuration (org.osgi.service.cm.Configuration)42 Subject (javax.security.auth.Subject)24 Test (org.junit.Test)19 Hashtable (java.util.Hashtable)14 BundleContext (org.osgi.framework.BundleContext)14 ServiceReference (org.osgi.framework.ServiceReference)13 IOException (java.io.IOException)9 Dictionary (java.util.Dictionary)8 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)7 Method (java.lang.reflect.Method)6 Bundle (org.osgi.framework.Bundle)6 File (java.io.File)4 Properties (java.util.Properties)4 MBeanServer (javax.management.MBeanServer)4 ObjectName (javax.management.ObjectName)4 KarafMBeanServerGuard (org.apache.karaf.management.KarafMBeanServerGuard)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 CompositeData (javax.management.openmbean.CompositeData)3