Search in sources :

Example 71 with ConfigurationAdmin

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

the class LogDumpProvider method createDump.

/**
     * Attach log entries from directory.
     */
public void createDump(DumpDestination destination) throws Exception {
    // get the ConfigAdmin service
    ServiceReference ref = bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
    if (ref == null) {
        return;
    }
    // get the PAX Logging configuration
    ConfigurationAdmin configurationAdmin = (ConfigurationAdmin) bundleContext.getService(ref);
    try {
        Configuration configuration = configurationAdmin.getConfiguration("org.ops4j.pax.logging", null);
        // get the ".file" Pax Logging properties
        Dictionary dictionary = configuration.getProperties();
        for (Enumeration e = dictionary.keys(); e.hasMoreElements(); ) {
            String property = (String) e.nextElement();
            if (property.endsWith(".file")) {
                // it's a file appender, get the file location
                String location = (String) dictionary.get(property);
                File file = new File(location);
                if (file.exists()) {
                    FileInputStream inputStream = new FileInputStream(file);
                    OutputStream outputStream = destination.add("log/" + file.getName());
                    StreamUtils.copy(inputStream, outputStream);
                }
            }
        }
    } catch (Exception e) {
        throw e;
    } finally {
        bundleContext.ungetService(ref);
    }
}
Also used : Dictionary(java.util.Dictionary) Enumeration(java.util.Enumeration) Configuration(org.osgi.service.cm.Configuration) OutputStream(java.io.OutputStream) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) File(java.io.File) FileInputStream(java.io.FileInputStream) ServiceReference(org.osgi.framework.ServiceReference)

Example 72 with ConfigurationAdmin

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

the class Activator method doStart.

protected void doStart() throws Exception {
    BundleContext systemBundleContext = bundleContext.getBundle(0).getBundleContext();
    ConfigurationAdmin configurationAdmin = getTrackedService(ConfigurationAdmin.class);
    Resolver resolver = new ResolverImpl(new Slf4jResolverLog(LoggerFactory.getLogger(ResolverImpl.class)));
    URLStreamHandlerService mvnUrlHandler = getTrackedService(URLStreamHandlerService.class);
    if (configurationAdmin == null || mvnUrlHandler == null) {
        return;
    }
    StandardRegionDigraph dg = DigraphHelper.loadDigraph(bundleContext);
    registerRegionDiGraph(dg);
    boolean configCfgStore = getBoolean("configCfgStore", FeaturesService.DEFAULT_CONFIG_CFG_STORE);
    FeatureConfigInstaller configInstaller = configurationAdmin != null ? new FeatureConfigInstaller(configurationAdmin, configCfgStore) : null;
    installSupport = new BundleInstallSupportImpl(bundleContext.getBundle(), bundleContext, systemBundleContext, configInstaller, dg);
    register(RegionDigraphPersistence.class, () -> installSupport.saveState());
    FeatureRepoFinder featureFinder = new FeatureRepoFinder();
    register(ManagedService.class, featureFinder, FeatureRepoFinder.getServiceProperties());
    Repository globalRepository = getGlobalRepository();
    FeaturesServiceConfig cfg = getConfig();
    StateStorage stateStorage = createStateStorage();
    featuresService = new FeaturesServiceImpl(stateStorage, featureFinder, configurationAdmin, resolver, installSupport, globalRepository, cfg);
    try {
        EventAdminListener eventAdminListener = new EventAdminListener(bundleContext);
        featuresService.registerListener(eventAdminListener);
    } catch (Throwable t) {
    // No EventAdmin support in this case 
    }
    register(FeaturesService.class, featuresService);
    featuresListenerTracker = createFeatureListenerTracker();
    featuresListenerTracker.open();
    FeaturesServiceMBeanImpl featuresServiceMBean = new FeaturesServiceMBeanImpl();
    featuresServiceMBean.setBundleContext(bundleContext);
    featuresServiceMBean.setFeaturesService(featuresService);
    registerMBean(featuresServiceMBean, "type=feature");
    String[] featuresRepositories = getStringArray("featuresRepositories", "");
    String featuresBoot = getString("featuresBoot", "");
    boolean featuresBootAsynchronous = getBoolean("featuresBootAsynchronous", false);
    BootFeaturesInstaller bootFeaturesInstaller = new BootFeaturesInstaller(bundleContext, featuresService, featuresRepositories, featuresBoot, featuresBootAsynchronous);
    bootFeaturesInstaller.start();
}
Also used : FeaturesServiceImpl(org.apache.karaf.features.internal.service.FeaturesServiceImpl) BootFeaturesInstaller(org.apache.karaf.features.internal.service.BootFeaturesInstaller) Resolver(org.osgi.service.resolver.Resolver) URLStreamHandlerService(org.osgi.service.url.URLStreamHandlerService) StateStorage(org.apache.karaf.features.internal.service.StateStorage) BundleInstallSupportImpl(org.apache.karaf.features.internal.service.BundleInstallSupportImpl) ResolverImpl(org.apache.felix.resolver.ResolverImpl) FeatureRepoFinder(org.apache.karaf.features.internal.service.FeatureRepoFinder) FeaturesServiceMBeanImpl(org.apache.karaf.features.internal.management.FeaturesServiceMBeanImpl) StandardRegionDigraph(org.eclipse.equinox.internal.region.StandardRegionDigraph) EventAdminListener(org.apache.karaf.features.internal.service.EventAdminListener) FeatureConfigInstaller(org.apache.karaf.features.internal.service.FeatureConfigInstaller) XmlRepository(org.apache.karaf.features.internal.repository.XmlRepository) Repository(org.osgi.service.repository.Repository) AggregateRepository(org.apache.karaf.features.internal.repository.AggregateRepository) JsonRepository(org.apache.karaf.features.internal.repository.JsonRepository) FeaturesServiceConfig(org.apache.karaf.features.internal.service.FeaturesServiceConfig) Slf4jResolverLog(org.apache.karaf.features.internal.resolver.Slf4jResolverLog) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) BundleContext(org.osgi.framework.BundleContext)

Example 73 with ConfigurationAdmin

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

the class Activator method doStart.

@Override
protected void doStart() throws Exception {
    ConfigurationAdmin configurationAdmin = getTrackedService(ConfigurationAdmin.class);
    if (configurationAdmin == null) {
        return;
    }
    final BundleServiceImpl bundleService = new BundleServiceImpl(bundleContext);
    register(BundleService.class, bundleService);
    bundleStateServicesTracker = new ServiceTracker<>(bundleContext, BundleStateService.class, new ServiceTrackerCustomizer<BundleStateService, BundleStateService>() {

        @Override
        public BundleStateService addingService(ServiceReference<BundleStateService> reference) {
            BundleStateService service = bundleContext.getService(reference);
            bundleService.registerBundleStateService(service);
            return service;
        }

        @Override
        public void modifiedService(ServiceReference<BundleStateService> reference, BundleStateService service) {
        }

        @Override
        public void removedService(ServiceReference<BundleStateService> reference, BundleStateService service) {
            bundleService.unregisterBundleStateService(service);
            bundleContext.ungetService(reference);
        }
    });
    bundleStateServicesTracker.open();
    bundleWatcher = new BundleWatcherImpl(bundleContext, new MavenConfigService(configurationAdmin), bundleService);
    bundleWatcher.start();
    register(BundleWatcher.class, bundleWatcher);
    BundlesMBeanImpl bundlesMBeanImpl = new BundlesMBeanImpl(bundleContext, bundleService);
    registerMBean(bundlesMBeanImpl, "type=bundle");
}
Also used : MavenConfigService(org.apache.karaf.bundle.core.internal.MavenConfigService) BundleStateService(org.apache.karaf.bundle.core.BundleStateService) BundleWatcherImpl(org.apache.karaf.bundle.core.internal.BundleWatcherImpl) BundleServiceImpl(org.apache.karaf.bundle.core.internal.BundleServiceImpl) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) BundlesMBeanImpl(org.apache.karaf.bundle.core.internal.BundlesMBeanImpl) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceReference(org.osgi.framework.ServiceReference)

Example 74 with ConfigurationAdmin

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

the class JMXSecurityTest method testJMXSecurityAsViewer.

@Test
public void testJMXSecurityAsViewer() throws Exception {
    String suffix = "_" + counter.incrementAndGet();
    String managerUser = "managerUser" + System.currentTimeMillis() + suffix;
    String managerGroup = "managerGroup" + System.currentTimeMillis() + suffix;
    String viewerUser = "viewerUser" + System.currentTimeMillis() + suffix;
    System.out.println(executeCommand("jaas:realm-manage --realm karaf" + ";jaas:user-add " + managerUser + " " + managerUser + ";jaas:group-add " + managerUser + " " + managerGroup + ";jaas:group-role-add " + managerGroup + " viewer" + ";jaas:group-role-add " + managerGroup + " manager" + ";jaas:user-add " + viewerUser + " " + viewerUser + ";jaas:role-add " + viewerUser + " viewer" + ";jaas:update" + ";jaas:realm-manage --realm karaf" + ";jaas:user-list", new RolePrincipal("admin")));
    ConfigurationAdmin configurationAdmin = getOsgiService(ConfigurationAdmin.class, 30000);
    org.osgi.service.cm.Configuration configuration = configurationAdmin.getConfiguration("org.apache.karaf.management", null);
    if (configuration == null) {
    }
    JMXConnector connector = getJMXConnector(viewerUser, viewerUser);
    MBeanServerConnection connection = connector.getMBeanServerConnection();
    ObjectName systemMBean = new ObjectName("org.apache.karaf:type=system,name=root");
    assertEquals(100, connection.getAttribute(systemMBean, "StartLevel"));
    assertSetAttributeSecEx(connection, systemMBean, new Attribute("StartLevel", 101));
    assertEquals("Changing the start level should have no effect for a viewer", 100, connection.getAttribute(systemMBean, "StartLevel"));
    assertInvokeSecEx(connection, systemMBean, "halt");
    ObjectName memoryMBean = new ObjectName("java.lang:type=Memory");
    assertEquals(false, connection.getAttribute(memoryMBean, "Verbose"));
    assertSetAttributeSecEx(connection, memoryMBean, new Attribute("Verbose", true));
    assertEquals("Changing the verbosity should have no effect for a viewer", false, connection.getAttribute(memoryMBean, "Verbose"));
    assertInvokeSecEx(connection, memoryMBean, "gc");
    testJMXSecurityMBean(connection, false, false);
    testKarafConfigAdminMBean(connection, false, false);
    testOSGiConfigAdminMBean(connection, false, false);
}
Also used : Attribute(javax.management.Attribute) JMXConnector(javax.management.remote.JMXConnector) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 75 with ConfigurationAdmin

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

the class JMXSecurityMBeanImplTestCase method testCanInvokeBulkCacheConfigAdmin.

public void testCanInvokeBulkCacheConfigAdmin() throws Exception {
    MBeanServer mbs = EasyMock.createMock(MBeanServer.class);
    EasyMock.replay(mbs);
    Configuration fooWildcardTesting = EasyMock.createMock(Configuration.class);
    EasyMock.expect(fooWildcardTesting.getPid()).andReturn("jmx.acl.foo._.testing").once();
    EasyMock.replay(fooWildcardTesting);
    Dictionary<String, Object> fooBarProperties = new Hashtable<>();
    // using '*' frees us from mocking JAAS
    fooBarProperties.put("testMethod(java.lang.String)", "*");
    fooBarProperties.put("testMethod(long)", "*");
    Configuration fooBarTesting = EasyMock.createMock(Configuration.class);
    EasyMock.expect(fooBarTesting.getPid()).andReturn("jmx.acl.foo.bar.testing").once();
    EasyMock.expect(fooBarTesting.getProperties()).andReturn(fooBarProperties).once();
    EasyMock.replay(fooBarTesting);
    ConfigurationAdmin testConfigAdmin = EasyMock.createMock(ConfigurationAdmin.class);
    EasyMock.expect(testConfigAdmin.listConfigurations(EasyMock.eq("(service.pid=jmx.acl*)"))).andReturn(new Configuration[] { fooWildcardTesting, fooBarTesting }).once();
    EasyMock.expect(testConfigAdmin.listConfigurations(EasyMock.eq("(service.pid=jmx.acl.whitelist)"))).andReturn(new Configuration[0]).once();
    EasyMock.expect(testConfigAdmin.getConfiguration(EasyMock.eq("jmx.acl.foo.bar.testing"), EasyMock.isNull(String.class))).andReturn(fooBarTesting).once();
    EasyMock.replay(testConfigAdmin);
    KarafMBeanServerGuard guard = new KarafMBeanServerGuard();
    guard.setConfigAdmin(testConfigAdmin);
    String objectName = "foo.bar.testing:type=SomeMBean";
    String objectName2 = "foo.bar.testing:type=SomeOtherMBean";
    JMXSecurityMBeanImpl mb = new JMXSecurityMBeanImpl();
    mb.setMBeanServer(mbs);
    mb.setGuard(guard);
    Map<String, List<String>> query = new HashMap<>();
    query.put(objectName, Collections.singletonList("testMethod(java.lang.String)"));
    query.put(objectName2, Collections.singletonList("testMethod(long)"));
    TabularData result = mb.canInvoke(query);
    assertEquals(2, result.size());
    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(true, cd2.get("CanInvoke"));
    CompositeData cd4 = result.get(new Object[] { objectName2, "testMethod(long)" });
    assertEquals(objectName2, cd4.get("ObjectName"));
    assertEquals("testMethod(long)", cd4.get("Method"));
    assertEquals(true, cd4.get("CanInvoke"));
    EasyMock.verify(testConfigAdmin, fooWildcardTesting, fooBarTesting);
}
Also used : Configuration(org.osgi.service.cm.Configuration) KarafMBeanServerGuard(org.apache.karaf.management.KarafMBeanServerGuard) CompositeData(javax.management.openmbean.CompositeData) TabularData(javax.management.openmbean.TabularData) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) MBeanServer(javax.management.MBeanServer)

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