@@ -2,20 +2,17 @@ package google
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"fmt"
7
6
"log"
8
7
"net/http"
9
- "strings"
10
8
11
9
"github.com/hashicorp/terraform/helper/logging"
12
10
"github.com/hashicorp/terraform/helper/pathorcontents"
13
11
"github.com/hashicorp/terraform/httpclient"
14
12
"github.com/terraform-providers/terraform-provider-google/version"
15
13
16
14
"golang.org/x/oauth2"
17
- "golang.org/x/oauth2/google"
18
- "golang.org/x/oauth2/jwt"
15
+ googleoauth "golang.org/x/oauth2/google"
19
16
appengine "google.golang.org/api/appengine/v1"
20
17
"google.golang.org/api/bigquery/v2"
21
18
"google.golang.org/api/cloudbilling/v1"
@@ -53,6 +50,7 @@ import (
53
50
// provider.
54
51
type Config struct {
55
52
Credentials string
53
+ AccessToken string
56
54
Project string
57
55
Region string
58
56
Zone string
@@ -98,63 +96,20 @@ type Config struct {
98
96
}
99
97
100
98
func (c * Config ) loadAndValidate () error {
101
- var account accountFile
102
99
clientScopes := []string {
103
100
"https://www.googleapis.com/auth/compute" ,
104
101
"https://www.googleapis.com/auth/cloud-platform" ,
105
102
"https://www.googleapis.com/auth/ndev.clouddns.readwrite" ,
106
103
"https://www.googleapis.com/auth/devstorage.full_control" ,
107
104
}
108
105
109
- var client * http.Client
110
- var tokenSource oauth2.TokenSource
111
-
112
- if c .Credentials != "" {
113
- contents , _ , err := pathorcontents .Read (c .Credentials )
114
- if err != nil {
115
- return fmt .Errorf ("Error loading credentials: %s" , err )
116
- }
117
-
118
- // Assume account_file is a JSON string
119
- if err := parseJSON (& account , contents ); err != nil {
120
- return fmt .Errorf ("Error parsing credentials '%s': %s" , contents , err )
121
- }
122
-
123
- // Get the token for use in our requests
124
- log .Printf ("[INFO] Requesting Google token..." )
125
- log .Printf ("[INFO] -- Email: %s" , account .ClientEmail )
126
- log .Printf ("[INFO] -- Scopes: %s" , clientScopes )
127
- log .Printf ("[INFO] -- Private Key Length: %d" , len (account .PrivateKey ))
128
-
129
- conf := jwt.Config {
130
- Email : account .ClientEmail ,
131
- PrivateKey : []byte (account .PrivateKey ),
132
- Scopes : clientScopes ,
133
- TokenURL : "https://accounts.google.com/o/oauth2/token" ,
134
- }
135
-
136
- // Initiate an http.Client. The following GET request will be
137
- // authorized and authenticated on the behalf of
138
- // your service account.
139
- client = conf .Client (context .Background ())
140
-
141
- tokenSource = conf .TokenSource (context .Background ())
142
- } else {
143
- log .Printf ("[INFO] Authenticating using DefaultClient" )
144
- err := error (nil )
145
- client , err = google .DefaultClient (context .Background (), clientScopes ... )
146
- if err != nil {
147
- return err
148
- }
149
-
150
- tokenSource , err = google .DefaultTokenSource (context .Background (), clientScopes ... )
151
- if err != nil {
152
- return err
153
- }
106
+ tokenSource , err := c .getTokenSource (clientScopes )
107
+ if err != nil {
108
+ return err
154
109
}
155
-
156
110
c .tokenSource = tokenSource
157
111
112
+ client := oauth2 .NewClient (context .Background (), tokenSource )
158
113
client .Transport = logging .NewTransport ("Google" , client .Transport )
159
114
160
115
terraformVersion := httpclient .UserAgentString ()
@@ -165,8 +120,6 @@ func (c *Config) loadAndValidate() error {
165
120
c .client = client
166
121
c .userAgent = userAgent
167
122
168
- var err error
169
-
170
123
log .Printf ("[INFO] Instantiating GCE client..." )
171
124
c .clientCompute , err = compute .New (client )
172
125
if err != nil {
@@ -391,17 +344,31 @@ func (c *Config) loadAndValidate() error {
391
344
return nil
392
345
}
393
346
394
- // accountFile represents the structure of the account file JSON file.
395
- type accountFile struct {
396
- PrivateKeyId string `json:"private_key_id"`
397
- PrivateKey string `json:"private_key"`
398
- ClientEmail string `json:"client_email"`
399
- ClientId string `json:"client_id"`
400
- }
347
+ func ( c * Config ) getTokenSource ( clientScopes [] string ) (oauth2. TokenSource , error ) {
348
+ if c . AccessToken != "" {
349
+ log . Printf ( "[INFO] Using configured Google access token (length %d)" , len ( c . AccessToken ))
350
+ log . Printf ( "[INFO] -- Scopes: %s" , clientScopes )
351
+ token := & oauth2. Token { AccessToken : c . AccessToken }
352
+ return oauth2 . StaticTokenSource ( token ), nil
353
+ }
401
354
402
- func parseJSON (result interface {}, contents string ) error {
403
- r := strings .NewReader (contents )
404
- dec := json .NewDecoder (r )
355
+ if c .Credentials != "" {
356
+ contents , _ , err := pathorcontents .Read (c .Credentials )
357
+ if err != nil {
358
+ return nil , fmt .Errorf ("Error loading credentials: %s" , err )
359
+ }
360
+
361
+ creds , err := googleoauth .CredentialsFromJSON (context .Background (), []byte (contents ), clientScopes ... )
362
+ if err != nil {
363
+ return nil , fmt .Errorf ("Unable to parse credentials from '%s': %s" , contents , err )
364
+ }
365
+
366
+ log .Printf ("[INFO] Requesting Google token using Credential File %q..." , c .Credentials )
367
+ log .Printf ("[INFO] -- Scopes: %s" , clientScopes )
368
+ return creds .TokenSource , nil
369
+ }
405
370
406
- return dec .Decode (result )
371
+ log .Printf ("[INFO] Authenticating using DefaultClient" )
372
+ log .Printf ("[INFO] -- Scopes: %s" , clientScopes )
373
+ return googleoauth .DefaultTokenSource (context .Background (), clientScopes ... )
407
374
}
0 commit comments