How to encrypt and decrypt OSGI config values

First, we have to make a service configuration by using the code given below. In this code I am showing you the normal way to show osgi config value.

@Component(service = TestService.class, immediate = true)

@Designate(ocd = TestService.TestConfig.class)

public class TestService {

   @ObjectClassDefinition(name = “TestConfig”, description = “This is the test config”)

   public @interface TestConfig {

       @AttributeDefinition(name = “Title”, description = “Enter the title”)

       String myTitle();

       @AttributeDefinition(name = “Description”, description = “Enter the description”)

       String myDesc();

   }

   private String serviceTitle;

   private String serviceDesc;

   @Activate @Modified

   private void init(TestConfig testConfig) {

       serviceTitle = testConfig.myTitle();

       serviceDesc = testConfig.myDesc();

   }

   public String getServiceTitle() {

       return serviceTitle;

   }

   public String getServiceDesc() {

       return serviceDesc;

   }

}

By using the above code we will get our service configuration.

Now we will give the value of the title and description from the .cfg.json file, so that we can make it encrypted. So we will make a file under ui.config in the codebase. 

The name of the file should be packageName.ServiceName.cfg.json as shown below:

com.demo.core.services.TestService.cfg.json

{

 “myTitle”:”This is the title author”,

 “myDesc”: “This is the desc author”

}

How we can encrypt our values?
Go to http://localhost:4502/system/console/crypto path we can protect our config values as shown below:

Here are the protected values of the config file.

com.demo.core.services.TestService.cfg.json

{

“myTitle”:”{694b41a028d9dd6b7e54ca2a9229a8b15d3489b2efd0f44d98eeb76347afecc7ba84b70167add1270da337c0ef59a375}”,

 “myDesc”: “{8f7fc13b8d4fb9f22984c7b462ca336e343d503d47b0cfe2baaf6891c972b8c28d1505da17dcb85fb4975538deeba15e}”

}

How we can decrypt our values in code?
By using CryptoSupport we can unprotect the values of the config file. For that we will use cryptoSupport service in our class to unprotect the values.

@Reference

private CryptoSupport cryptoSupport;

@Activate @Modified

private void init(TestConfig testConfig) throws CryptoException {

   serviceTitle = testConfig.myTitle();

   serviceDesc = testConfig.myDesc();

   String title = PropertiesUtil.toString(serviceTitle, StringUtils.EMPTY);

   if(cryptoSupport.isProtected(title)){

       serviceTitle=cryptoSupport.unprotect(serviceTitle);

   } else {

       serviceTitle=title;

   }

   log.debug(“This is title unprotected value :- {}”,serviceTitle);

   String desc = PropertiesUtil.toString(serviceDesc, StringUtils.EMPTY);

   if (cryptoSupport.isProtected(desc)){

       cryptoSupport.unprotect(serviceDesc);

   } else {

       serviceDesc = desc;

   }

   log.debug(“This is desc unprotected value :- {}”,serviceDesc);

}

Here we got the unprotected value of the config file in the log file.

By Avni Rajput