Cloud Manager Developer Console allows developers to download the entire OSGI Configuration JSON and I am looking for a way to programmatically get the same OSGI Configuration JSON. Is there such an ability ?
We have a requirement to monitor and alert on configuration drift, hence looking for ways to automate the drift check.
Cloud manager API does not seem to provide an API, even though developer console seem to internally use a service to get the entire list. Would this service be available for use if we are open to write an AEM custom job or servlet to provide this information ?
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hi @anjanam
I have created something long back, you can have a look to get the idea.
https://aemlab.blogspot.com/2019/05/aem-osgi-config-dashboard.html
Note : This utility is created as PoC, this is not production ready, please test before you use it.
HI @anjanam ,
To programmatically obtain the same OSGi Configuration JSON available in the Cloud Manager Developer Console, you can leverage AEM's existing APIs and services. While the Cloud Manager API itself may not provide a direct endpoint for this, AEM exposes several endpoints and APIs that you can use to access OSGi configurations. Here's how you can approach this:
AEM provides an OSGi Web Console API that you can use to access configuration details. This API can be accessed via HTTP requests to the AEM instance. The relevant endpoint for fetching configurations is /system/console/configMgr.
You can make a GET request to this endpoint and parse the resulting HTML to extract configuration details. However, parsing HTML is not the most robust solution. Instead, you might want to use the JSON format provided by the Apache Felix Web Management Console.
AEM's OSGi configurations can also be accessed via the JMX MBean Server. Here's a high-level approach to creating a custom servlet to fetch these configurations in JSON format:
Create a Custom Servlet: Write a servlet that registers at a specific path and is accessible via HTTP. This servlet will use the JMX API to fetch OSGi configurations.
Fetch OSGi Configurations via JMX: Use the JMX MBean Server to query the configuration details. The ConfigurationAdmin MBean can be used for this purpose.
Convert Configuration to JSON: Collect the configuration details and convert them into a JSON format.
Here’s an example of how you can implement such a servlet:
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.json.JSONObject;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.cm.ConfigurationAdmin;
import javax.servlet.Servlet;
import java.io.IOException;
import java.util.Dictionary;
import java.util.Enumeration;
@Component
@Service(Servlet.class)
@Properties({
@Property(name = "sling.servlet.paths", value = "/bin/osgi-config")
})
public class OsgiConfigServlet extends SlingAllMethodsServlet {
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
ConfigurationAdmin configAdmin = bundleContext.getService(bundleContext.getServiceReference(ConfigurationAdmin.class));
JSONObject configs = new JSONObject();
try {
for (String pid : configAdmin.getFactoryConfigurationPids()) {
Dictionary<String, Object> config = configAdmin.getConfiguration(pid).getProperties();
JSONObject configJson = new JSONObject();
Enumeration<String> keys = config.keys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
configJson.put(key, config.get(key));
}
configs.put(pid, configJson);
}
} catch (Exception e) {
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().write("Error fetching configurations: " + e.getMessage());
return;
}
response.setContentType("application/json");
response.getWriter().write(configs.toString());
}
}
You can also write a script or use tools like curl to fetch configuration details directly from the /system/console/configMgr endpoint. For example:
curl -u admin:admin http://localhost:4502/system/console/configMgr/.json
Once you have a way to programmatically fetch the OSGi configuration JSON, you can set up a monitoring system to compare the current configuration with a baseline. If any differences are detected, you can trigger alerts.
By creating a custom servlet, you can tailor the functionality to your specific needs and integrate it seamlessly with your existing monitoring and alerting systems.
Thanks ChatGPT, I've been following you, and this is considered spamming, I am reporting you.
Thank you for the insight, going to give this a try.
Hi @anjanam
I have created something long back, you can have a look to get the idea.
https://aemlab.blogspot.com/2019/05/aem-osgi-config-dashboard.html
Note : This utility is created as PoC, this is not production ready, please test before you use it.
Thank you. Appreciate the guidance, looking into it.
Hi,
I think you could look at this from another angle. You must keep in mind that the OSGi configurations will only be updated through a code deployment, meaning that there must be a code change that will alter them. So instead of trying to get the list of configs and compare, you could rely on your source control tool and check if there are changes under a specific path, depending on the source control you use (and I am assuming you have set up your project with your own repository which mirrors Adobe's). You could use out-of-the-box features to trigger "actions" or build a manual check. For example: https://dev.to/omar16100/trigger-bitbucket-pipeline-only-if-certain-files-are-changed-with-google-cl...
Hope this helps