Port to oauth2, fix #606

This commit is contained in:
Dave Cunningham 2015-01-30 14:53:09 -05:00
parent 6947ba2518
commit aa009516b7
2 changed files with 33 additions and 28 deletions

View File

@ -7,9 +7,12 @@ import (
"net/http" "net/http"
"os" "os"
"code.google.com/p/goauth2/oauth"
"code.google.com/p/goauth2/oauth/jwt"
"code.google.com/p/google-api-go-client/compute/v1" "code.google.com/p/google-api-go-client/compute/v1"
// oauth2 "github.com/rasa/oauth2-fork-b3f9a68"
"github.com/rasa/oauth2-fork-b3f9a68"
// oauth2 "github.com/rasa/oauth2-fork-b3f9a68/google"
"github.com/rasa/oauth2-fork-b3f9a68/google"
) )
const clientScopes string = "https://www.googleapis.com/auth/compute" const clientScopes string = "https://www.googleapis.com/auth/compute"
@ -38,6 +41,10 @@ func (c *Config) loadAndValidate() error {
c.Region = os.Getenv("GOOGLE_REGION") c.Region = os.Getenv("GOOGLE_REGION")
} }
var f *oauth2.Options
var err error
if c.AccountFile != "" {
if err := loadJSON(&account, c.AccountFile); err != nil { if err := loadJSON(&account, c.AccountFile); err != nil {
return fmt.Errorf( return fmt.Errorf(
"Error loading account file '%s': %s", "Error loading account file '%s': %s",
@ -50,26 +57,24 @@ func (c *Config) loadAndValidate() error {
log.Printf("[INFO] -- Email: %s", account.ClientEmail) log.Printf("[INFO] -- Email: %s", account.ClientEmail)
log.Printf("[INFO] -- Scopes: %s", clientScopes) log.Printf("[INFO] -- Scopes: %s", clientScopes)
log.Printf("[INFO] -- Private Key Length: %d", len(account.PrivateKey)) log.Printf("[INFO] -- Private Key Length: %d", len(account.PrivateKey))
jwtTok := jwt.NewToken(
account.ClientEmail, f, err = oauth2.New(
clientScopes, oauth2.JWTClient(account.ClientEmail, []byte(account.PrivateKey)),
[]byte(account.PrivateKey)) oauth2.Scope(clientScopes),
token, err := jwtTok.Assert(new(http.Client)) google.JWTEndpoint())
} else {
log.Printf("[INFO] Requesting Google token via GCE Service Role...")
f, err = oauth2.New(google.ComputeEngineAccount(""))
}
if err != nil { if err != nil {
return fmt.Errorf("Error retrieving auth token: %s", err) return fmt.Errorf("Error retrieving auth token: %s", err)
} }
// Instantiate the transport to communicate to Google
transport := &oauth.Transport{
Config: &oauth.Config{
ClientId: account.ClientId,
Scope: clientScopes,
},
Token: token,
}
log.Printf("[INFO] Instantiating GCE client...") log.Printf("[INFO] Instantiating GCE client...")
c.clientCompute, err = compute.New(transport.Client()) c.clientCompute, err = compute.New(&http.Client{Transport: f.NewTransport()})
if err != nil { if err != nil {
return err return err
} }

View File

@ -11,7 +11,7 @@ func Provider() terraform.ResourceProvider {
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"account_file": &schema.Schema{ "account_file": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Optional: true,
DefaultFunc: schema.EnvDefaultFunc("GOOGLE_ACCOUNT_FILE", nil), DefaultFunc: schema.EnvDefaultFunc("GOOGLE_ACCOUNT_FILE", nil),
}, },