question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CosmosDbConfig.PrimaryKey returns a string that is incompatible with ArmExpression at runtime

See original GitHub issue

I’m trying to add a Cosmos PrimaryKey as a Secret in KeyVault, however the secret deployed to KeyVault is the arm template string, e.g., “[listKeys(resourceId(‘Microsoft.DocumentDB/databaseAccounts’, ‘local-db-server’), providers(‘Microsoft.DocumentDB’,‘databaseAccounts’).apiVersions[0]).primaryMasterKey]”, rather than the primary key value itself.

    test "Generate Cosmos+KeyVault" {
        let db = cosmosDb {
            name "local-db"
        }

        let primaryKeySecret = secret {
            name "pk"
            value (db.PrimaryKey |> ArmExpression) // is the the correct way to consume the property?
        }

        let kv = keyVault {
            name "kv"
            add_secret primaryKeySecret
        }

        let deployment = arm {
            location (Location.UKSouth)
            add_resource kv
        }

        let str =
            deployment.Template
            |> Writer.toJson

        printfn "%O" (db.PrimaryKey |> ArmExpression)
        printfn "%O" (primaryKeySecret.Value)
        printfn "%O" (kv.Secrets.Head.Value)
        printfn "%s" str
    }

Produces the following outputs:

ArmExpression
  "[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', 'local-db-server'), providers('Microsoft.DocumentDB','databaseAccounts').apiVersions[0]).primaryMasterKey]"
ExpressionSecret
  (ArmExpression
     "[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', 'local-db-server'), providers('Microsoft.DocumentDB','databaseAccounts').apiVersions[0]).primaryMasterKey]")
ExpressionSecret
  (ArmExpression
     "[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', 'local-db-server'), providers('Microsoft.DocumentDB','databaseAccounts').apiVersions[0]).primaryMasterKey]")
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "outputs": {},
  "parameters": {},
  "resources": [
    {
      "apiVersion": "2018-02-14",
      "location": "uksouth",
      "name": "kv",
      "properties": {
        "accessPolicies": [],
        "enabledForTemplateDeployment": true,
        "networkAcls": {
          "ipRules": [],
          "virtualNetworkRules": []
        },
        "sku": {
          "family": "A",
          "name": "standard"
        },
        "tenantId": "[subscription().tenantid]"
      },
      "type": "Microsoft.KeyVault/vaults"
    },
    {
      "apiVersion": "2018-02-14",
      "dependsOn": [
        "kv"
      ],
      "location": "uksouth",
      "name": "kv/pk",
      "properties": {
        "attributes": {},
        "value": "[[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', 'local-db-server'), providers('Microsoft.DocumentDB','databaseAccounts').apiVersions[0]).primaryMasterKey]]"
      },
      "type": "Microsoft.KeyVault/vaults/secrets"
    }
  ]
}

Where the kv/pk property value looks like it’s been encoded twice, e.g., [[listKeys…]], first time incorrectly in Cosmos sprintf string, and second time correctly in ArmExpression.eval?

Comparing how Builders.Storage.fs returns it’s key in buildKey against Builders.Cosmos.fs suggests the bug is in Cosmos.PrimaryKey and possibly Cosmos.Endpoint too.

// Storage
let internal buildKey (ResourceName name) =
    sprintf
        "concat('DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=', listKeys('%s', '2017-10-01').keys[0].value)"
            name
            name
    |> ArmExpression

// Cosmos
    member this.PrimaryKey =
        sprintf "[listKeys(resourceId('Microsoft.DocumentDB/databaseAccounts', '%s'), providers('Microsoft.DocumentDB','databaseAccounts').apiVersions[0]).primaryMasterKey]"
            this.AccountName.ResourceName.Value
    member this.Endpoint =
        sprintf "[reference(concat('Microsoft.DocumentDb/databaseAccounts/', '%s')).documentEndpoint]" this.AccountName.ResourceName.Value

I’m happy to try and raise a PR, however I’m new to Farmer in particular and F# secondly and want your feedback first on how Cosmos.PrimaryKey and Endpoint should really look.

Also, is there value in exposing additional cosmos properties such as secondary keys, connection strings, read-only keys, etc?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
isaacabrahamcommented, Jul 2, 2020

This has now been released.

1reaction
isaacabrahamcommented, Jul 2, 2020

I’m also going to put a PR in that checks for double-encoded ARM expressions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found