@@ -2,7 +2,9 @@ package vault
22
33import (
44 "fmt"
5+ "io/ioutil"
56 "log"
7+ "os"
68 "strings"
79
810 "github.com/hashicorp/terraform/helper/schema"
@@ -22,7 +24,7 @@ func Provider() terraform.ResourceProvider {
2224 "token" : & schema.Schema {
2325 Type : schema .TypeString ,
2426 Required : true ,
25- DefaultFunc : schema .EnvDefaultFunc ("VAULT_TOKEN" , nil ),
27+ DefaultFunc : schema .EnvDefaultFunc ("VAULT_TOKEN" , "" ),
2628 Description : "Token to use to authenticate to Vault." ,
2729 },
2830 "ca_cert_file" : & schema.Schema {
@@ -122,6 +124,18 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
122124 return nil , fmt .Errorf ("failed to configure Vault API: %s" , err )
123125 }
124126
127+ token := d .Get ("token" ).(string )
128+ if token == "" {
129+ // Use the vault CLI's token, if present.
130+ tokenFile := fmt .Sprintf ("%s/.vault-token" , os .Getenv ("HOME" ))
131+ tokenBytes , err := ioutil .ReadFile (tokenFile )
132+ if err != nil {
133+ return nil , fmt .Errorf ("No vault token found: %s" , err )
134+ }
135+
136+ token = string (tokenBytes )
137+ }
138+
125139 // In order to enforce our relatively-short lease TTL, we derive a
126140 // temporary child token that inherits all of the policies of the
127141 // token we were given but expires after max_lease_ttl_seconds.
@@ -135,7 +149,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
135149 // can explicitly be revoked, and this limited scope won't apply to
136150 // any secrets that are *written* by Terraform to Vault.
137151
138- client .SetToken (d . Get ( " token" ).( string ) )
152+ client .SetToken (token )
139153 renewable := false
140154 childTokenLease , err := client .Auth ().Token ().Create (& api.TokenCreateRequest {
141155 DisplayName : "terraform" ,
0 commit comments