{"action":"created","issue":{"url":"https://api.github.com/repos/hashicorp/consul-template/issues/759","repository_url":"https://api.github.com/repos/hashicorp/consul-template","labels_url":"https://api.github.com/repos/hashicorp/consul-template/issues/759/labels{/name}","comments_url":"https://api.github.com/repos/hashicorp/consul-template/issues/759/comments","events_url":"https://api.github.com/repos/hashicorp/consul-template/issues/759/events","html_url":"https://github.com/hashicorp/consul-template/issues/759","id":184487765,"number":759,"title":"JSON Value possible typing issue","user":{"login":"mross462","id":1168471,"avatar_url":"https://avatars.githubusercontent.com/u/1168471?v=3","gravatar_id":"","url":"https://api.github.com/users/mross462","html_url":"https://github.com/mross462","followers_url":"https://api.github.com/users/mross462/followers","following_url":"https://api.github.com/users/mross462/following{/other_user}","gists_url":"https://api.github.com/users/mross462/gists{/gist_id}","starred_url":"https://api.github.com/users/mross462/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mross462/subscriptions","organizations_url":"https://api.github.com/users/mross462/orgs","repos_url":"https://api.github.com/users/mross462/repos","events_url":"https://api.github.com/users/mross462/events{/privacy}","received_events_url":"https://api.github.com/users/mross462/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-10-21T13:41:42Z","updated_at":"2016-10-21T15:29:29Z","closed_at":"2016-10-21T15:29:29Z","body":"Hi all,\r\n\r\nHaving an issue with consul template and vault:\r\n\r\n```\r\n~$ consul --version\r\nConsul v0.7.0\r\n~$ vault --version\r\nVault v0.6.1\r\n```\r\n\r\nI have the following stored in vault:\r\n\r\n```\r\n~$ vault read secret/staging/constants/v2/service/thing/elasticsearch\r\nKey             \tValue\r\n---             \t-----\r\nrefresh_interval\t720h0m0s\r\nJSON            \t{\"base_url\":\"a_url\",\"external_base_url\":\"another_url\"}\r\n```\r\n\r\nWith the following consul-template:\r\n\r\n```\r\n---\r\n{{key \"services/common/environment\"}}:{{with $secret := vault \"secret/staging/constants/v2/service/thing/elasticsearch\" }}\r\n  base_uri: {{$secret.Data}}\r\n{{end}}\r\n```\r\n\r\nI'm getting:\r\n\r\n```\r\n---\r\nstaging:\r\n  base_uri: map[JSON:{\"base_url\":\"a_url\",\"external_base_url\":\"another_url\"}]\r\n```\r\n\r\nBut when using the dot notation in the template to access base_url:\r\n\r\n```\r\n---\r\n{{key \"services/common/environment\"}}:{{with $secret := vault \"secret/staging/constants/v2/service/thing/elasticsearch\" }}\r\n  base_uri: {{$secret.Data.base_url}}\r\n{{end}}\r\n```\r\n\r\nI'm getting:\r\n\r\n```\r\n---\r\nstaging:\r\n  base_uri: <no value>\r\n```\r\n  \r\nMy concern is that the typing of what is being read into consul is preventing this from occurring.\r\n\r\nAny help or ideas would be appreciated."},"comment":{"url":"https://api.github.com/repos/hashicorp/consul-template/issues/comments/255408561","html_url":"https://github.com/hashicorp/consul-template/issues/759#issuecomment-255408561","issue_url":"https://api.github.com/repos/hashicorp/consul-template/issues/759","id":255408561,"user":{"login":"sethvargo","id":408570,"avatar_url":"https://avatars.githubusercontent.com/u/408570?v=3","gravatar_id":"","url":"https://api.github.com/users/sethvargo","html_url":"https://github.com/sethvargo","followers_url":"https://api.github.com/users/sethvargo/followers","following_url":"https://api.github.com/users/sethvargo/following{/other_user}","gists_url":"https://api.github.com/users/sethvargo/gists{/gist_id}","starred_url":"https://api.github.com/users/sethvargo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sethvargo/subscriptions","organizations_url":"https://api.github.com/users/sethvargo/orgs","repos_url":"https://api.github.com/users/sethvargo/repos","events_url":"https://api.github.com/users/sethvargo/events{/privacy}","received_events_url":"https://api.github.com/users/sethvargo/received_events","type":"User","site_admin":false},"created_at":"2016-10-21T15:29:29Z","updated_at":"2016-10-21T15:29:29Z","body":"Hi @mross462 \r\n\r\nThank you for opening an issue. This is behaving as expected. You inserted a key-value pair into Vault named JSON. The type of that value is a string, whose contents happen to be JSON. They are stored as a string though.\r\n\r\nWhen you are reading in Consul Template, the response from Vault looks like this:\r\n\r\n```json\r\n{\r\n  \"Data\": {\r\n    \"JSON\": \"{\\\"base_url\\\":\\\"a_ur\\l\",\\\"external_base_ur\\l\":\\\"another_url\\\"}\"\r\n  }\r\n}\r\n```\r\n\r\nNotice that is a JSON payload which contains a JSON _string_ at `.Data.JSON`.\r\n\r\nTo parse this value out, you need to use `parseJSON` on `$secret.Data.JSON`:\r\n\r\n```\r\n---\r\n{{key \"services/common/environment\"}}:{{with $secret := vault \"secret/staging/constants/v2/service/thing/elasticsearch\" }}\r\n{{with $parsed := $secret.Data.JSON | parseJSON}}\r\n  base_uri: {{$parsed.base_url}}\r\n{{ end }\r\n{{end}}\r\n```\r\n\r\nBut this is really a hack in itself. I would discourage storing JSON blobs as data in the secret backend, since you're doubling the work. Instead of having a JSON key, you could just write the raw key-value pairs like this:\r\n\r\n```\r\n$ vault write secret/staging/constants/v2/service/thing/elasticsearch base_url=a_url external_base_url=other\r\n```\r\n\r\nThen your template is much simpler:\r\n\r\n```\r\n{{key \"services/common/environment\"}}:{{with $secret := vault \"secret/staging/constants/v2/service/thing/elasticsearch\" }}\r\n  base_uri: {{$secret.Data.base_url}}\r\n{{end}}\r\n```"}}
{"actor":{"display_login":"sethvargo"}}