-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for gcloud auth (#1166)
- Loading branch information
Showing
7 changed files
with
280 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package gcloud | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"runtime" | ||
"time" | ||
|
||
"golang.org/x/oauth2" | ||
exec "golang.org/x/sys/execabs" | ||
) | ||
|
||
// config represents the credentials returned by `gcloud config config-helper`. | ||
type config struct { | ||
Credential struct { | ||
AccessToken string `json:"access_token"` | ||
TokenExpiry time.Time `json:"token_expiry"` | ||
} | ||
} | ||
|
||
func (c *config) Token() *oauth2.Token { | ||
return &oauth2.Token{ | ||
AccessToken: c.Credential.AccessToken, | ||
Expiry: c.Credential.TokenExpiry, | ||
} | ||
} | ||
|
||
// Path returns the absolute path to the gcloud command. If the command is not | ||
// found it returns an error. | ||
func Path() (string, error) { | ||
g := "gcloud" | ||
if runtime.GOOS == "windows" { | ||
g = g + ".cmd" | ||
} | ||
return exec.LookPath(g) | ||
} | ||
|
||
// configHelper implements oauth2.TokenSource via the `gcloud config config-helper` command. | ||
type configHelper struct{} | ||
|
||
// Token helps gcloudTokenSource implement oauth2.TokenSource. | ||
func (configHelper) Token() (*oauth2.Token, error) { | ||
gcloudCmd, err := Path() | ||
if err != nil { | ||
return nil, err | ||
} | ||
buf, errbuf := new(bytes.Buffer), new(bytes.Buffer) | ||
cmd := exec.Command(gcloudCmd, "--format", "json", "config", "config-helper", "--min-expiry", "1h") | ||
cmd.Stdout = buf | ||
cmd.Stderr = errbuf | ||
|
||
if err := cmd.Run(); err != nil { | ||
err = fmt.Errorf("error reading config: %v; stderr was:\n%v", err, errbuf) | ||
return nil, err | ||
} | ||
|
||
c := &config{} | ||
if err := json.Unmarshal(buf.Bytes(), c); err != nil { | ||
return nil, err | ||
} | ||
return c.Token(), nil | ||
} | ||
|
||
// TokenSource returns an oauth2.TokenSource backed by the gcloud CLI. | ||
func TokenSource() (oauth2.TokenSource, error) { | ||
h := configHelper{} | ||
tok, err := h.Token() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return oauth2.ReuseTokenSource(tok, h), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package gcloud_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/GoogleCloudPlatform/cloudsql-proxy/v2/internal/gcloud" | ||
"github.com/GoogleCloudPlatform/cloudsql-proxy/v2/internal/testutil" | ||
) | ||
|
||
func TestGcloud(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping gcloud integration tests") | ||
} | ||
|
||
cleanup := testutil.ConfigureGcloud(t) | ||
defer cleanup() | ||
|
||
// gcloud is now configured. Try to obtain a token from gcloud config | ||
// helper. | ||
ts, err := gcloud.TokenSource() | ||
if err != nil { | ||
t.Fatalf("failed to get token source: %v", err) | ||
} | ||
|
||
_, err = ts.Token() | ||
if err != nil { | ||
t.Fatalf("failed to get token: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package testutil | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/GoogleCloudPlatform/cloudsql-proxy/v2/internal/gcloud" | ||
) | ||
|
||
// ConfigureGcloud configures gcloud using only GOOGLE_APPLICATION_CREDENTIALS | ||
// and stores the resulting configuration in a temporary directory as set by | ||
// CLOUDSDK_CONFIG, which changes the gcloud config directory from the | ||
// default. We use a temporary directory to avoid trampling on any existing | ||
// gcloud config. | ||
func ConfigureGcloud(t *testing.T) func() { | ||
dir, err := ioutil.TempDir("", "cloudsdk*") | ||
if err != nil { | ||
t.Fatalf("failed to create temp dir: %v", err) | ||
} | ||
os.Setenv("CLOUDSDK_CONFIG", dir) | ||
|
||
gcloudCmd, err := gcloud.Path() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
keyFile, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS") | ||
if !ok { | ||
t.Fatal("GOOGLE_APPLICATION_CREDENTIALS is not set in the environment") | ||
} | ||
os.Unsetenv("GOOGLE_APPLICATION_CREDENTIALS") | ||
|
||
buf := &bytes.Buffer{} | ||
cmd := exec.Command(gcloudCmd, "auth", "activate-service-account", "--key-file", keyFile) | ||
cmd.Stdout = buf | ||
|
||
if err := cmd.Run(); err != nil { | ||
t.Fatalf("failed to active service account. err = %v, message = %v", err, buf.String()) | ||
} | ||
|
||
return func() { | ||
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", keyFile) | ||
os.Unsetenv("CLOUDSDK_CONFIG") | ||
} | ||
|
||
} |
Oops, something went wrong.