Skip to content

Commit 625f440

Browse files
committed
Merge pull request hashicorp#900 from sparkprime/oauth2
Port to oauth2, fix hashicorp#606
2 parents 408f838 + 4d280f0 commit 625f440

3 files changed

Lines changed: 51 additions & 34 deletions

File tree

builtin/providers/google/config.go

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
"net/http"
88
"os"
99

10-
"code.google.com/p/goauth2/oauth"
11-
"code.google.com/p/goauth2/oauth/jwt"
1210
"code.google.com/p/google-api-go-client/compute/v1"
11+
12+
"golang.org/x/oauth2"
13+
"golang.org/x/oauth2/google"
14+
"golang.org/x/oauth2/jwt"
1315
)
1416

15-
const clientScopes string = "https://www.googleapis.com/auth/compute"
1617

1718
// Config is the configuration structure used to instantiate the Google
1819
// provider.
@@ -38,38 +39,52 @@ func (c *Config) loadAndValidate() error {
3839
c.Region = os.Getenv("GOOGLE_REGION")
3940
}
4041

41-
if err := loadJSON(&account, c.AccountFile); err != nil {
42-
return fmt.Errorf(
43-
"Error loading account file '%s': %s",
44-
c.AccountFile,
45-
err)
46-
}
42+
var client *http.Client
4743

48-
// Get the token for use in our requests
49-
log.Printf("[INFO] Requesting Google token...")
50-
log.Printf("[INFO] -- Email: %s", account.ClientEmail)
51-
log.Printf("[INFO] -- Scopes: %s", clientScopes)
52-
log.Printf("[INFO] -- Private Key Length: %d", len(account.PrivateKey))
53-
jwtTok := jwt.NewToken(
54-
account.ClientEmail,
55-
clientScopes,
56-
[]byte(account.PrivateKey))
57-
token, err := jwtTok.Assert(new(http.Client))
58-
if err != nil {
59-
return fmt.Errorf("Error retrieving auth token: %s", err)
60-
}
44+
if c.AccountFile != "" {
45+
if err := loadJSON(&account, c.AccountFile); err != nil {
46+
return fmt.Errorf(
47+
"Error loading account file '%s': %s",
48+
c.AccountFile,
49+
err)
50+
}
51+
52+
clientScopes := []string{"https://www.googleapis.com/auth/compute"}
53+
54+
// Get the token for use in our requests
55+
log.Printf("[INFO] Requesting Google token...")
56+
log.Printf("[INFO] -- Email: %s", account.ClientEmail)
57+
log.Printf("[INFO] -- Scopes: %s", clientScopes)
58+
log.Printf("[INFO] -- Private Key Length: %d", len(account.PrivateKey))
59+
60+
conf := jwt.Config{
61+
Email: account.ClientEmail,
62+
PrivateKey: []byte(account.PrivateKey),
63+
Scopes: clientScopes,
64+
TokenURL: "https://accounts.google.com/o/oauth2/token",
65+
}
66+
67+
// Initiate an http.Client. The following GET request will be
68+
// authorized and authenticated on the behalf of
69+
// your service account.
70+
client = conf.Client(oauth2.NoContext)
71+
72+
} else {
73+
log.Printf("[INFO] Requesting Google token via GCE Service Role...")
74+
client = &http.Client{
75+
Transport: &oauth2.Transport{
76+
// Fetch from Google Compute Engine's metadata server to retrieve
77+
// an access token for the provided account.
78+
// If no account is specified, "default" is used.
79+
Source: google.ComputeTokenSource(""),
80+
},
81+
}
6182

62-
// Instantiate the transport to communicate to Google
63-
transport := &oauth.Transport{
64-
Config: &oauth.Config{
65-
ClientId: account.ClientId,
66-
Scope: clientScopes,
67-
},
68-
Token: token,
6983
}
7084

7185
log.Printf("[INFO] Instantiating GCE client...")
72-
c.clientCompute, err = compute.New(transport.Client())
86+
var err error
87+
c.clientCompute, err = compute.New(client)
7388
if err != nil {
7489
return err
7590
}

builtin/providers/google/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func Provider() terraform.ResourceProvider {
1111
Schema: map[string]*schema.Schema{
1212
"account_file": &schema.Schema{
1313
Type: schema.TypeString,
14-
Required: true,
14+
Optional: true,
1515
DefaultFunc: schema.EnvDefaultFunc("GOOGLE_ACCOUNT_FILE", nil),
1616
},
1717

website/source/docs/providers/google/index.html.markdown

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ resource "google_compute_instance" "default" {
3434

3535
The following keys can be used to configure the provider.
3636

37-
* `account_file` - (Required) Path to the JSON file used to describe
38-
your account credentials, downloaded from Google Cloud Console. More
39-
details on retrieving this file are below.
37+
* `account_file` - (Required) Path to the JSON file used to describe your
38+
account credentials, downloaded from Google Cloud Console. More details on
39+
retrieving this file are below. The _account file_ can be "" if you
40+
are running terraform from a GCE instance with a properly-configured [Compute
41+
Engine Service Account](https://cloud.google.com/compute/docs/authentication).
4042

4143
* `project` - (Required) The name of the project to apply any resources to.
4244

0 commit comments

Comments
 (0)