google: enable specifying an auth token

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
Evan Hazlett 2015-02-26 12:38:10 -05:00
parent e0bda2075d
commit 0b4d58d554
3 changed files with 43 additions and 28 deletions

View File

@ -7,6 +7,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path" "path"
"path/filepath"
"strings" "strings"
"time" "time"
@ -23,13 +24,13 @@ const (
RedirectURI = "urn:ietf:wg:oauth:2.0:oob" RedirectURI = "urn:ietf:wg:oauth:2.0:oob"
) )
func newGCEService(storePath string) (*raw.Service, error) { func newGCEService(storePath, authTokenPath string) (*raw.Service, error) {
client := newOauthClient(storePath) client := newOauthClient(storePath, authTokenPath)
service, err := raw.New(client) service, err := raw.New(client)
return service, err return service, err
} }
func newOauthClient(storePath string) *http.Client { func newOauthClient(storePath, authTokenPath string) *http.Client {
config := &oauth.Config{ config := &oauth.Config{
ClientId: ClientId, ClientId: ClientId,
ClientSecret: ClientSecret, ClientSecret: ClientSecret,
@ -37,7 +38,8 @@ func newOauthClient(storePath string) *http.Client {
AuthURL: AuthURL, AuthURL: AuthURL,
TokenURL: TokenURL, TokenURL: TokenURL,
} }
token := token(storePath, config)
token := token(storePath, authTokenPath, config)
t := oauth.Transport{ t := oauth.Transport{
Token: token, Token: token,
Config: config, Config: config,
@ -46,8 +48,13 @@ func newOauthClient(storePath string) *http.Client {
return t.Client() return t.Client()
} }
func token(storePath string, config *oauth.Config) *oauth.Token { func token(storePath, authTokenPath string, config *oauth.Config) *oauth.Token {
token, err := tokenFromCache(storePath) tokenPath := authTokenPath
if authTokenPath == "" {
tokenPath = filepath.Join(storePath, "gce_token")
}
log.Debugf("using auth token: %s", tokenPath)
token, err := tokenFromCache(tokenPath)
if err != nil { if err != nil {
token = tokenFromWeb(config) token = tokenFromWeb(config)
saveToken(storePath, token) saveToken(storePath, token)
@ -55,8 +62,7 @@ func token(storePath string, config *oauth.Config) *oauth.Token {
return token return token
} }
func tokenFromCache(storePath string) (*oauth.Token, error) { func tokenFromCache(tokenPath string) (*oauth.Token, error) {
tokenPath := path.Join(storePath, "gce_token")
f, err := os.Open(tokenPath) f, err := os.Open(tokenPath)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -14,16 +14,17 @@ import (
// ComputeUtil is used to wrap the raw GCE API code and store common parameters. // ComputeUtil is used to wrap the raw GCE API code and store common parameters.
type ComputeUtil struct { type ComputeUtil struct {
zone string zone string
instanceName string instanceName string
userName string userName string
project string project string
service *raw.Service service *raw.Service
zoneURL string zoneURL string
globalURL string authTokenPath string
ipAddress string globalURL string
SwarmMaster bool ipAddress string
SwarmHost string SwarmMaster bool
SwarmHost string
} }
const ( const (
@ -39,20 +40,21 @@ const (
// NewComputeUtil creates and initializes a ComputeUtil. // NewComputeUtil creates and initializes a ComputeUtil.
func newComputeUtil(driver *Driver) (*ComputeUtil, error) { func newComputeUtil(driver *Driver) (*ComputeUtil, error) {
service, err := newGCEService(driver.storePath) service, err := newGCEService(driver.storePath, driver.AuthTokenPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
c := ComputeUtil{ c := ComputeUtil{
zone: driver.Zone, authTokenPath: driver.AuthTokenPath,
instanceName: driver.MachineName, zone: driver.Zone,
userName: driver.UserName, instanceName: driver.MachineName,
project: driver.Project, userName: driver.UserName,
service: service, project: driver.Project,
zoneURL: apiURL + driver.Project + "/zones/" + driver.Zone, service: service,
globalURL: apiURL + driver.Project + "/global", zoneURL: apiURL + driver.Project + "/zones/" + driver.Zone,
SwarmMaster: driver.SwarmMaster, globalURL: apiURL + driver.Project + "/global",
SwarmHost: driver.SwarmHost, SwarmMaster: driver.SwarmMaster,
SwarmHost: driver.SwarmHost,
} }
return &c, nil return &c, nil
} }

View File

@ -25,6 +25,7 @@ type Driver struct {
MachineType string MachineType string
Scopes string Scopes string
DiskSize int DiskSize int
AuthTokenPath string
storePath string storePath string
UserName string UserName string
Project string Project string
@ -81,6 +82,11 @@ func GetCreateFlags() []cli.Flag {
Usage: "GCE Project", Usage: "GCE Project",
EnvVar: "GOOGLE_PROJECT", EnvVar: "GOOGLE_PROJECT",
}, },
cli.StringFlag{
Name: "google-auth-token",
Usage: "GCE oAuth token",
EnvVar: "GOOGLE_AUTH_TOKEN",
},
cli.StringFlag{ cli.StringFlag{
Name: "google-scopes", Name: "google-scopes",
Usage: "GCE Scopes (comma-separated if multiple scopes)", Usage: "GCE Scopes (comma-separated if multiple scopes)",
@ -119,6 +125,7 @@ func (driver *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
driver.MachineType = flags.String("google-machine-type") driver.MachineType = flags.String("google-machine-type")
driver.DiskSize = flags.Int("google-disk-size") driver.DiskSize = flags.Int("google-disk-size")
driver.UserName = flags.String("google-username") driver.UserName = flags.String("google-username")
driver.AuthTokenPath = flags.String("google-auth-token")
driver.Project = flags.String("google-project") driver.Project = flags.String("google-project")
driver.Scopes = flags.String("google-scopes") driver.Scopes = flags.String("google-scopes")
driver.SwarmMaster = flags.Bool("swarm-master") driver.SwarmMaster = flags.Bool("swarm-master")