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/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

View File

@ -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
}

View File

@ -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")