Search in sources :

Example 1 with ConfigurationPermission

use of org.osgi.service.cm.ConfigurationPermission in project felix by apache.

the class LocationTest method testRegionBinding.

@Test
public void testRegionBinding() throws Exception {
    try {
        new ConfigurationPermission(REGION, ConfigurationPermission.TARGET);
    } catch (IllegalArgumentException e) {
        // not an R5 CA
        return;
    }
    final String pid = COMPONENT_NAME;
    deleteConfig(pid);
    checkConfigurationCount(pid, 0, -1);
    TestCase.assertNull(SimpleComponent.INSTANCE);
    Configuration config = configure(pid, REGION);
    delay();
    findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertNotNull(SimpleComponent.INSTANCE);
    TestCase.assertEquals(PROP_NAME, SimpleComponent.INSTANCE.getProperty(PROP_NAME));
    Bundle b2 = installBundle(descriptorFile, COMPONENT_PACKAGE, "simplecomponent2", "0.0.11", null);
    b2.start();
    checkConfigurationCount(b2, pid, 1, ComponentConfigurationDTO.ACTIVE);
}
Also used : Configuration(org.osgi.service.cm.Configuration) Bundle(org.osgi.framework.Bundle) ConfigurationPermission(org.osgi.service.cm.ConfigurationPermission) Test(org.junit.Test)

Example 2 with ConfigurationPermission

use of org.osgi.service.cm.ConfigurationPermission in project felix by apache.

the class RegionConfigurationSupport method checkBundleLocation.

private boolean checkBundleLocation(String configBundleLocation, Bundle bundle) {
    boolean result;
    if (configBundleLocation == null) {
        result = true;
    } else if (configBundleLocation.startsWith("?")) {
        // multilocation
        result = bundle.hasPermission(new ConfigurationPermission(configBundleLocation, ConfigurationPermission.TARGET));
    } else {
        result = configBundleLocation.equals(bundle.getLocation());
    }
    logger.log(LogService.LOG_DEBUG, "checkBundleLocation: location {0}, returning {1}", new Object[] { configBundleLocation, result }, null);
    return result;
}
Also used : ConfigurationPermission(org.osgi.service.cm.ConfigurationPermission)

Example 3 with ConfigurationPermission

use of org.osgi.service.cm.ConfigurationPermission in project felix by apache.

the class TargetedPIDTest method testTargetedPID.

@Test
public void testTargetedPID() throws Exception {
    try {
        new ConfigurationPermission(REGION, ConfigurationPermission.TARGET);
    } catch (IllegalArgumentException e) {
        // not an R5 CA
        return;
    }
    String pid = COMPONENT_NAME;
    theConfig.put(TARGETED_PID, pid);
    Configuration config = configure(pid);
    config.setBundleLocation(REGION);
    String pidSN = pid + "|simplecomponent2";
    theConfig.put(TARGETED_PID, pidSN);
    Configuration configSN = configure(pidSN);
    configSN.setBundleLocation(REGION);
    String pidSNV = pidSN + "|0.0.12";
    theConfig.put(TARGETED_PID, pidSNV);
    Configuration configSNV = configure(pidSNV);
    configSNV.setBundleLocation(REGION);
    String pidSNVL = pidSNV + "|bundleLocation";
    theConfig.put(TARGETED_PID, pidSNVL);
    Configuration configSNVL = configure(pidSNVL);
    configSNVL.setBundleLocation(REGION);
    delay();
    // Add more and more specific components to check that they pick up the appropriate configuration
    Set<ComponentConfigurationDTO> known = new HashSet<ComponentConfigurationDTO>();
    final ComponentConfigurationDTO component = findComponentConfigurationByName(COMPONENT_NAME, ComponentConfigurationDTO.ACTIVE);
    known.add(component);
    TestCase.assertNotNull(SimpleComponent.INSTANCE);
    SimpleComponent sc = SimpleComponent.INSTANCE;
    TestCase.assertEquals(pid, sc.getProperty(TARGETED_PID));
    Bundle bSN = installBundle(descriptorFile, COMPONENT_PACKAGE, "simplecomponent2", "0.0.11", null);
    bSN.start();
    findComponentConfigurationByName(bSN, pid, ComponentConfigurationDTO.ACTIVE);
    SimpleComponent scSN = SimpleComponent.INSTANCE;
    TestCase.assertEquals(pidSN, scSN.getProperty(TARGETED_PID));
    Bundle bSNV = installBundle(descriptorFile, COMPONENT_PACKAGE, "simplecomponent2", "0.0.12", null);
    bSNV.start();
    findComponentConfigurationByName(bSNV, pid, ComponentConfigurationDTO.ACTIVE);
    SimpleComponent scSNV = SimpleComponent.INSTANCE;
    TestCase.assertEquals(pidSNV, scSNV.getProperty(TARGETED_PID));
    Bundle bSNVL = installBundle(descriptorFile, COMPONENT_PACKAGE, "simplecomponent2", "0.0.12", "bundleLocation");
    bSNVL.start();
    findComponentConfigurationsByName(bSNVL, pid, ComponentConfigurationDTO.ACTIVE);
    SimpleComponent scSNVL = SimpleComponent.INSTANCE;
    TestCase.assertEquals(pidSNVL, scSNVL.getProperty(TARGETED_PID));
    // remove configurations to check that the components now use the less specific configurations.
    configSNVL.delete();
    delay();
    findComponentConfigurationsByName(bSNVL, pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(pidSNV, scSNVL.getProperty(TARGETED_PID));
    configSNV.delete();
    delay();
    findComponentConfigurationsByName(bSNVL, pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(pidSN, scSNVL.getProperty(TARGETED_PID));
    findComponentConfigurationByName(bSNV, pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(pidSN, scSNV.getProperty(TARGETED_PID));
    configSN.delete();
    delay();
    findComponentConfigurationsByName(bSNVL, pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(pid, scSNVL.getProperty(TARGETED_PID));
    findComponentConfigurationByName(bSNV, pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(pid, scSNV.getProperty(TARGETED_PID));
    findComponentConfigurationByName(bSN, pid, ComponentConfigurationDTO.ACTIVE);
    TestCase.assertEquals(pid, scSN.getProperty(TARGETED_PID));
}
Also used : Configuration(org.osgi.service.cm.Configuration) Bundle(org.osgi.framework.Bundle) SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) ComponentConfigurationDTO(org.osgi.service.component.runtime.dto.ComponentConfigurationDTO) ConfigurationPermission(org.osgi.service.cm.ConfigurationPermission) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with ConfigurationPermission

use of org.osgi.service.cm.ConfigurationPermission in project felix by apache.

the class ConfigurationAdminImpl method checkPermission.

/**
 * Checks whether the current access control context (call stack) has
 * the given permission for the given bundle location and throws a
 * <code>SecurityException</code> if this is not the case.
 *
 * @param name The bundle location to check for permission. If this
 *      is <code>null</code> permission is always granted.
 * @param action The action to check.
 * @param checkOwn If {@code false} permission is always granted if
 *      {@code name} is the same as the using bundle's location.
 *
 * @throws SecurityException if the access control context does not
 *      have the appropriate permission
 */
void checkPermission(final ConfigurationManager configurationManager, String name, String action, boolean checkOwn) {
    // the caller's permission must be checked
    final SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        // CM 1.4 / 104.11.1 Implicit permission
        if (name != null && (checkOwn || !name.equals(getBundle().getLocation()))) {
            try {
                sm.checkPermission(new ConfigurationPermission(name, action));
                Log.logger.log(LogService.LOG_DEBUG, "Explicit Permission; grant {0} permission on configuration bound to {1} to bundle {2}", new Object[] { action, name, getBundle().getLocation() });
            } catch (SecurityException se) {
                Log.logger.log(LogService.LOG_DEBUG, "No Permission; denied {0} permission on configuration bound to {1} to bundle {2}; reason: {3}", new Object[] { action, name, getBundle().getLocation(), se.getMessage() });
                throw se;
            }
        } else if (Log.logger.isLogEnabled(LogService.LOG_DEBUG)) {
            Log.logger.log(LogService.LOG_DEBUG, "Implicit Permission; grant {0} permission on configuration bound to {1} to bundle {2}", new Object[] { action, name, getBundle().getLocation() });
        }
    } else if (Log.logger.isLogEnabled(LogService.LOG_DEBUG)) {
        Log.logger.log(LogService.LOG_DEBUG, "No SecurityManager installed; grant {0} permission on configuration bound to {1} to bundle {2}", new Object[] { action, name, getBundle().getLocation() });
    }
}
Also used : ConfigurationPermission(org.osgi.service.cm.ConfigurationPermission)

Aggregations

ConfigurationPermission (org.osgi.service.cm.ConfigurationPermission)4 Test (org.junit.Test)2 Bundle (org.osgi.framework.Bundle)2 Configuration (org.osgi.service.cm.Configuration)2 HashSet (java.util.HashSet)1 SimpleComponent (org.apache.felix.scr.integration.components.SimpleComponent)1 ComponentConfigurationDTO (org.osgi.service.component.runtime.dto.ComponentConfigurationDTO)1