Merge pull request #2052 from sparkprime/useragent

Use new Google API and OAuth libs, add UserAgent string
This commit is contained in:
Eric Johnson 2015-04-17 12:20:35 -07:00
commit 419f3271f1
1 changed files with 38 additions and 15 deletions

View File

@ -4,16 +4,15 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"runtime"
"time" "time"
"code.google.com/p/google-api-go-client/compute/v1"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
// oauth2 "github.com/rasa/oauth2-fork-b3f9a68" "golang.org/x/oauth2"
"github.com/rasa/oauth2-fork-b3f9a68" "golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
// oauth2 "github.com/rasa/oauth2-fork-b3f9a68/google" "google.golang.org/api/compute/v1"
"github.com/rasa/oauth2-fork-b3f9a68/google"
) )
// driverGCE is a Driver implementation that actually talks to GCE. // driverGCE is a Driver implementation that actually talks to GCE.
@ -27,9 +26,10 @@ type driverGCE struct {
var DriverScopes = []string{"https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/devstorage.full_control"} var DriverScopes = []string{"https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/devstorage.full_control"}
func NewDriverGCE(ui packer.Ui, p string, a *accountFile) (Driver, error) { func NewDriverGCE(ui packer.Ui, p string, a *accountFile) (Driver, error) {
var f *oauth2.Options
var err error var err error
var client *http.Client
// Auth with AccountFile first if provided // Auth with AccountFile first if provided
if a.PrivateKey != "" { if a.PrivateKey != "" {
log.Printf("[INFO] Requesting Google token via AccountFile...") log.Printf("[INFO] Requesting Google token via AccountFile...")
@ -37,22 +37,45 @@ func NewDriverGCE(ui packer.Ui, p string, a *accountFile) (Driver, error) {
log.Printf("[INFO] -- Scopes: %s", DriverScopes) log.Printf("[INFO] -- Scopes: %s", DriverScopes)
log.Printf("[INFO] -- Private Key Length: %d", len(a.PrivateKey)) log.Printf("[INFO] -- Private Key Length: %d", len(a.PrivateKey))
f, err = oauth2.New( conf := jwt.Config{
oauth2.JWTClient(a.ClientEmail, []byte(a.PrivateKey)), Email: a.ClientEmail,
oauth2.Scope(DriverScopes...), PrivateKey: []byte(a.PrivateKey),
google.JWTEndpoint()) Scopes: DriverScopes,
TokenURL: "https://accounts.google.com/o/oauth2/token",
}
// Initiate an http.Client. The following GET request will be
// authorized and authenticated on the behalf of
// your service account.
client = conf.Client(oauth2.NoContext)
} else { } else {
log.Printf("[INFO] Requesting Google token via GCE Service Role...") log.Printf("[INFO] Requesting Google token via GCE Service Role...")
client = &http.Client{
f, err = oauth2.New(google.ComputeEngineAccount("")) Transport: &oauth2.Transport{
// Fetch from Google Compute Engine's metadata server to retrieve
// an access token for the provided account.
// If no account is specified, "default" is used.
Source: google.ComputeTokenSource(""),
},
}
} }
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Printf("[INFO] Instantiating GCE client using...") log.Printf("[INFO] Instantiating GCE client...")
service, err := compute.New(&http.Client{Transport: f.NewTransport()}) service, err := compute.New(client)
// Set UserAgent
versionString := "0.0.0"
// TODO(dcunnin): Use Packer's version code from version.go
// versionString := main.Version
// if main.VersionPrerelease != "" {
// versionString = fmt.Sprintf("%s-%s", versionString, main.VersionPrerelease)
// }
service.UserAgent = fmt.Sprintf(
"(%s %s) Packer/%s", runtime.GOOS, runtime.GOARCH, versionString)
if err != nil { if err != nil {
return nil, err return nil, err
} }