@@ -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 }
0 commit comments