diff --git a/drivers/google/auth_util.go b/drivers/google/auth_util.go index 2bbb633437..14122b0a52 100644 --- a/drivers/google/auth_util.go +++ b/drivers/google/auth_util.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "path" + "path/filepath" "strings" "time" @@ -23,13 +24,13 @@ const ( RedirectURI = "urn:ietf:wg:oauth:2.0:oob" ) -func newGCEService(storePath string) (*raw.Service, error) { - client := newOauthClient(storePath) +func newGCEService(storePath, authTokenPath string) (*raw.Service, error) { + client := newOauthClient(storePath, authTokenPath) service, err := raw.New(client) return service, err } -func newOauthClient(storePath string) *http.Client { +func newOauthClient(storePath, authTokenPath string) *http.Client { config := &oauth.Config{ ClientId: ClientId, ClientSecret: ClientSecret, @@ -37,7 +38,8 @@ func newOauthClient(storePath string) *http.Client { AuthURL: AuthURL, TokenURL: TokenURL, } - token := token(storePath, config) + + token := token(storePath, authTokenPath, config) t := oauth.Transport{ Token: token, Config: config, @@ -46,8 +48,13 @@ func newOauthClient(storePath string) *http.Client { return t.Client() } -func token(storePath string, config *oauth.Config) *oauth.Token { - token, err := tokenFromCache(storePath) +func token(storePath, authTokenPath string, config *oauth.Config) *oauth.Token { + tokenPath := authTokenPath + if authTokenPath == "" { + tokenPath = filepath.Join(storePath, "gce_token") + } + log.Debugf("using auth token: %s", tokenPath) + token, err := tokenFromCache(tokenPath) if err != nil { token = tokenFromWeb(config) saveToken(storePath, token) @@ -55,8 +62,7 @@ func token(storePath string, config *oauth.Config) *oauth.Token { return token } -func tokenFromCache(storePath string) (*oauth.Token, error) { - tokenPath := path.Join(storePath, "gce_token") +func tokenFromCache(tokenPath string) (*oauth.Token, error) { f, err := os.Open(tokenPath) if err != nil { return nil, err diff --git a/drivers/google/compute_util.go b/drivers/google/compute_util.go index ea65c988f0..a5a4a7fe23 100644 --- a/drivers/google/compute_util.go +++ b/drivers/google/compute_util.go @@ -14,16 +14,17 @@ import ( // ComputeUtil is used to wrap the raw GCE API code and store common parameters. type ComputeUtil struct { - zone string - instanceName string - userName string - project string - service *raw.Service - zoneURL string - globalURL string - ipAddress string - SwarmMaster bool - SwarmHost string + zone string + instanceName string + userName string + project string + service *raw.Service + zoneURL string + authTokenPath string + globalURL string + ipAddress string + SwarmMaster bool + SwarmHost string } const ( @@ -39,20 +40,21 @@ const ( // NewComputeUtil creates and initializes a ComputeUtil. func newComputeUtil(driver *Driver) (*ComputeUtil, error) { - service, err := newGCEService(driver.storePath) + service, err := newGCEService(driver.storePath, driver.AuthTokenPath) if err != nil { return nil, err } c := ComputeUtil{ - zone: driver.Zone, - instanceName: driver.MachineName, - userName: driver.UserName, - project: driver.Project, - service: service, - zoneURL: apiURL + driver.Project + "/zones/" + driver.Zone, - globalURL: apiURL + driver.Project + "/global", - SwarmMaster: driver.SwarmMaster, - SwarmHost: driver.SwarmHost, + authTokenPath: driver.AuthTokenPath, + zone: driver.Zone, + instanceName: driver.MachineName, + userName: driver.UserName, + project: driver.Project, + service: service, + zoneURL: apiURL + driver.Project + "/zones/" + driver.Zone, + globalURL: apiURL + driver.Project + "/global", + SwarmMaster: driver.SwarmMaster, + SwarmHost: driver.SwarmHost, } return &c, nil } diff --git a/drivers/google/google.go b/drivers/google/google.go index 72389a8318..8a7ed10727 100644 --- a/drivers/google/google.go +++ b/drivers/google/google.go @@ -25,6 +25,7 @@ type Driver struct { MachineType string Scopes string DiskSize int + AuthTokenPath string storePath string UserName string Project string @@ -81,6 +82,11 @@ func GetCreateFlags() []cli.Flag { Usage: "GCE Project", EnvVar: "GOOGLE_PROJECT", }, + cli.StringFlag{ + Name: "google-auth-token", + Usage: "GCE oAuth token", + EnvVar: "GOOGLE_AUTH_TOKEN", + }, cli.StringFlag{ Name: "google-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.DiskSize = flags.Int("google-disk-size") driver.UserName = flags.String("google-username") + driver.AuthTokenPath = flags.String("google-auth-token") driver.Project = flags.String("google-project") driver.Scopes = flags.String("google-scopes") driver.SwarmMaster = flags.Bool("swarm-master")