CosmosDbConfig.PrimaryKey returns a string that is incompatible with ArmExpression at runtime
See original GitHub issueI’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:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
This has now been released.
I’m also going to put a PR in that checks for double-encoded ARM expressions.