...

Source file src/github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/util/gcloudutil.go

Documentation: github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/util

     1  // Copyright 2018 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package util
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"encoding/json"
    21  	"fmt"
    22  	"runtime"
    23  	"time"
    24  
    25  	"github.com/GoogleCloudPlatform/cloudsql-proxy/logging"
    26  	"golang.org/x/oauth2"
    27  	exec "golang.org/x/sys/execabs"
    28  )
    29  
    30  // GcloudConfigData represents the data returned by `gcloud config config-helper`.
    31  type GcloudConfigData struct {
    32  	Configuration struct {
    33  		Properties struct {
    34  			Core struct {
    35  				Project string
    36  				Account string
    37  			}
    38  		}
    39  	}
    40  	Credential struct {
    41  		AccessToken string    `json:"access_token"`
    42  		TokenExpiry time.Time `json:"token_expiry"`
    43  	}
    44  }
    45  
    46  func (cfg *GcloudConfigData) oauthToken() *oauth2.Token {
    47  	return &oauth2.Token{
    48  		AccessToken: cfg.Credential.AccessToken,
    49  		Expiry:      cfg.Credential.TokenExpiry,
    50  	}
    51  }
    52  
    53  type GcloudStatusCode int
    54  
    55  const (
    56  	GcloudOk GcloudStatusCode = iota
    57  	GcloudNotFound
    58  	// generic execution failure error not specified above.
    59  	GcloudExecErr
    60  )
    61  
    62  type GcloudError struct {
    63  	GcloudError error
    64  	Status      GcloudStatusCode
    65  }
    66  
    67  func (e *GcloudError) Error() string {
    68  	return e.GcloudError.Error()
    69  }
    70  
    71  // GcloudConfig returns a GcloudConfigData object or an error of type *GcloudError.
    72  func GcloudConfig() (*GcloudConfigData, error) {
    73  	gcloudCmd := "gcloud"
    74  	if runtime.GOOS == "windows" {
    75  		gcloudCmd = gcloudCmd + ".cmd"
    76  	}
    77  
    78  	if _, err := exec.LookPath(gcloudCmd); err != nil {
    79  		return nil, &GcloudError{err, GcloudNotFound}
    80  	}
    81  
    82  	buf, errbuf := new(bytes.Buffer), new(bytes.Buffer)
    83  	cmd := exec.Command(gcloudCmd, "--format", "json", "config", "config-helper", "--min-expiry", "1h")
    84  	cmd.Stdout = buf
    85  	cmd.Stderr = errbuf
    86  
    87  	if err := cmd.Run(); err != nil {
    88  		err = fmt.Errorf("error reading config: %v; stderr was:\n%v", err, errbuf)
    89  		logging.Errorf("GcloudConfig: %v", err)
    90  		return nil, &GcloudError{err, GcloudExecErr}
    91  	}
    92  
    93  	data := &GcloudConfigData{}
    94  	if err := json.Unmarshal(buf.Bytes(), data); err != nil {
    95  		logging.Errorf("Failed to unmarshal bytes from gcloud: %v", err)
    96  		logging.Errorf("   gcloud returned:\n%s", buf)
    97  		return nil, &GcloudError{err, GcloudExecErr}
    98  	}
    99  
   100  	return data, nil
   101  }
   102  
   103  // gcloudTokenSource implements oauth2.TokenSource via the `gcloud config config-helper` command.
   104  type gcloudTokenSource struct {
   105  }
   106  
   107  // Token helps gcloudTokenSource implement oauth2.TokenSource.
   108  func (src *gcloudTokenSource) Token() (*oauth2.Token, error) {
   109  	cfg, err := GcloudConfig()
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  	return cfg.oauthToken(), nil
   114  }
   115  
   116  func GcloudTokenSource(ctx context.Context) (oauth2.TokenSource, error) {
   117  	src := &gcloudTokenSource{}
   118  	tok, err := src.Token()
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  	return oauth2.ReuseTokenSource(tok, src), nil
   123  }
   124  

View as plain text