Habitat Templating Capability for Simple Math

I am in process of converting a cookbook template over to a habitat template and have the need to generate some of the content based upon some simple math counters. We start with a static number of lines that are numbered, for example with 1-9 and then the user can provide an array of additional items that need to generated the next X number of entries.

Here is a snippet of the chef cookbook template (erb) where we do this:

security.provider.1=com.ibm.jsse2.IBMJSSEProvider2
security.provider.2=com.ibm.crypto.provider.IBMJCE
security.provider.3=com.ibm.security.jgss.IBMJGSSProvider
security.provider.4=com.ibm.security.cert.IBMCertPath
security.provider.5=com.ibm.security.sasl.IBMSASL
security.provider.6=com.ibm.xml.crypto.IBMXMLCryptoProvider
security.provider.7=com.ibm.xml.enc.IBMXMLEncProvider
security.provider.8=com.ibm.security.jgss.mech.spnego.IBMSPNEGO
security.provider.9=sun.security.provider.Sun
<% counter = 9 %>
<% idx = -1 %>
<% if @config['serverConfiguration']['additionalSecurityProviders'] %>
  <% @config['serverConfiguration']['additionalSecurityProviders'].each do |provider| %>
    <% counter += 1 %>
security.provider.<%= counter %>=<%= provider %>
  <% end %>
<% end %>
<% if @config['hardwareCrypto'] %>
  <% @config['hardwareCrypto'].each do |token_label| %>
    <% counter += 1 %>
    <% idx += 1 %>
security.provider.<%= counter %>=com.ibm.crypto.pkcs11impl.provider.IBMPKCS11Impl <%= "#{@dirs['security']}/pkcs11.#{idx}.cfg" %>
  <% end %>
<% end %>

securerandom.source=file:/dev/urandom

And this is my conversion to a habitat template, but not sure how we can do the math to increment the counters (counter & idx which are ruby variables).

security.provider.1=com.ibm.jsse2.IBMJSSEProvider2
security.provider.2=com.ibm.crypto.provider.IBMJCE
security.provider.3=com.ibm.security.jgss.IBMJGSSProvider
security.provider.4=com.ibm.security.cert.IBMCertPath
security.provider.5=com.ibm.security.sasl.IBMSASL
security.provider.6=com.ibm.xml.crypto.IBMXMLCryptoProvider
security.provider.7=com.ibm.xml.enc.IBMXMLEncProvider
security.provider.8=com.ibm.security.jgss.mech.spnego.IBMSPNEGO
security.provider.9=sun.security.provider.Sun

#TODO convert from erb to habitat
<% counter = 9 %>
<% idx = -1 %>
{{#if cfg.server.additionalSecurityProviders ~}}
  {{#each cfg.server.additionalSecurityProviders as |provider| ~}}
    <% counter += 1 %>
security.provider.<%= counter %>={{provider}}
  {{/each ~}}
{{/if ~}}
{{#if cfg.server.hardwareCrypto ~}}
  {{#each cfg.server.hardwareCrypto as |token_label| ~}}
    <% counter += 1 %>
    <% idx += 1 %>
security.provider.<%= counter %>=com.ibm.crypto.pkcs11impl.provider.IBMPKCS11Impl {{pkg.svc_var_path}}/security/pkcs11.#{idx}.cfg
  {{/each ~}}
{{/if ~}}

securerandom.source=file:/dev/urandom

login.configuration.provider=com.ibm.security.auth.login.ConfigFile

Habitat uses handlebars templating, which are very basic. Advanced stuff can only be done with helpers, of which a few come with handlebars and a few are added by Habitat: https://www.habitat.sh/docs/reference/#helpers

It is unfortunately not possible to supply additional helpers at the plan level

There are two proposed paths toward allowing some dynamism in config that I’m tracking personally:

Yes I’ve also seen the helpers as well and they are a bit limiting. I’m curious to know as well if there is any way we can write our own custom helpers? There isn’t any documentation on this that i could find.

1 Like