How to split array into string for Service Fabric cluster ARM template

In order to use a certificates for Service Fabric cluster issues by custom, non publicly trusted CA you’d need to supply a list of thumbprints of intermediate CAs. In a form of a comma-separated string. What means if you have an array of thumbprints (what’s natural as it’s an easier way to maintain the list) like this:

"clusterCertIssuers": [
  "417E225037FBFAA4F95761D5AE729E1AEA7E3A42", // Microsoft IT TLS CA 1
  "54D9D20239080C32316ED9FF980A48988F4ADF2D", // Microsoft IT TLS CA 2
  "8A38755D0996823FE8FA3116A277CE446EAC4E99", // Microsoft IT TLS CA 4
  "AD898AC73DF333EB60AC1F5FC6C4B2219DDB79B7", // Microsoft IT TLS CA 5
]

Then you would need to convert them to a string like this:

"certificateCommonNames": {
  "commonNames": [
    {
      "certificateCommonName": "[variables('clusterCertSubjectName')]",
      "certificateIssuerThumbprint": "417E225037FBFAA4F95761D5AE729E1AEA7E3A42,54D9D20239080C32316ED9FF980A48988F4ADF2D,8A38755D0996823FE8FA3116A277CE446EAC4E99,AD898AC73DF333EB60AC1F5FC6C4B2219DDB79B7"
    }
  ],
  "x509StoreName": "[variables('certificateStoreName')]"
}

What obviously nobody wants to do. If only ARM supported a join(string, array)function! Here’s a UserVoice post dated back to May 2017. As of date of writing, there is no such string function whatsoever.

Here’s a solution, hopefully temporal, until ARM comes up with a proper one:

"clusterCertIssuersArr": {
  "copy": [
    {
      "name": "certsLoop",
      "count": "[length(variables('clusterCertIssuers'))]",
      "input": "[concat(variables('clusterCertIssuers')[copyIndex('certsLoop')], ',')]"
    }
  ]
},
"clusterCertIssuersStr": "[replace(replace(replace(string(variables('clusterCertIssuersArr').certsLoop), '[\"', ''), '\"]', ''), '\",\"', '')]",
"clusterCertIssuersSubstr": "[substring(variables('clusterCertIssuersStr'), 0, sub(length(variables('clusterCertIssuersStr')), 1))]",

It does 3 things:

  1. Creates a temporal array where each element of the initial array is appended by comma
  2. Converts the temporal array into a string and replaces all JSON array artifacts with empty string (effectively removes them), means ["a","b"] becomes just a,b
  3. Drop the last comma

That’s it, folks. Happy deployments!

This entry was posted in Infrastructure and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.