Here is a simple way to use a custom property on the Apprenda platform to authenticate an application on the platform against a proprietary application management system. The first thing to do, of course is to create a custom property via the SOC. I’ve named my custom property ‘appid’ and you’ll see how it gets used below. If the administrator checks the ‘Required for Deployment’ option when creating the custom property, then every application will be required to assign a value for that property in order for the app to be moved out of the definition stage. Requiring developers to assign a value is a good start but, in this case, we're also asserting that it’s not sufficient to have just any value for the 'appid' property. Let’s write a small bit of code to validate it.
I simply modified the extensions example project. In this case, we are going to acquire the custom property value in the hook for OnPromotingVersion (ReadOnlyVersionDTO version,ApplicationVersionStageDTO proposedStage). This enables us to check for the custom property value against a proprietary application management system before the application is published.
//Check if this is going into the Published stage and if so, validate against known keys
if (proposedStage.Equals(ApplicationVersionStageDTO.Published))
{
var applicationManager = new ApplicationManager(version.ApplicationAlias);
IEnumerable<string> appIdValues = null;
foreach (var custProp in applicationManager.GetApplicationCustomProperties())
{
switch (custProp.Name)
{
case "appid":
{
//Remember custom properties is potentially a list of values
appIdValues = custProp.Values;
//Do something interesting with custom property value here –
// e.g. check it against a database or run it through a custom hash
// algorithm to validate it’s authenticity
}
break;
...
}
}
}
In order to get the correct version.ApplicationAlias in the code above, the entire block above should be run inside the correct TenantContext. To do this, I wrap the block above with the using statement:
using (EstablishProperContext()) { … }
Where the EstablishProperContext() method is simply a conditional statement which acquires the proper context. We do this because the extension is executed by the platform TenantContext and we want the applications TenantContext - which has our custom property. To learn more about Contexts and how they are used in the apprenda platform, visit the docs pages.
if (TenantContext.Current.History.Skip(1).Any())
{
return TenantContext.NewTenantContext(TenantContext.Current.History.Skip(1).First().TenantId);
}
A more detailed description for creating custom properties can be found in the support article Leveraging Custom Properties to Track Custom Application Metadata. Accessing custom properties from an extension is simple as long as you have the correct TenantContext. For more information on using Extensions and other hooks available in the Apprenda platform, visit the Apprenda docs.
Comments