Skip to content

Commit

Permalink
Feature Request: Add GOOGLE_OAUTH_ACCESS_TOKEN support for modules ho…
Browse files Browse the repository at this point in the history
…sted on GCS (#302)

* add oauth token support
* add tests
  • Loading branch information
Moritz Schmitz von Hülst authored Dec 3, 2021
1 parent 817ea41 commit 64f1c03
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
25 changes: 22 additions & 3 deletions get_gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package getter
import (
"context"
"fmt"
"golang.org/x/oauth2"
"google.golang.org/api/option"
"net/url"
"os"
"path/filepath"
Expand All @@ -28,7 +30,7 @@ func (g *GCSGetter) ClientMode(u *url.URL) (ClientMode, error) {
return 0, err
}

client, err := storage.NewClient(ctx)

This comment has been minimized.

Copy link
@Boobie84

Boobie84 Jan 3, 2022

client, err := storage.NewClient(ctx)
client, err := g.getClient(ctx)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -82,7 +84,7 @@ func (g *GCSGetter) Get(dst string, u *url.URL) error {
return err
}

client, err := storage.NewClient(ctx)
client, err := g.getClient(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -124,7 +126,7 @@ func (g *GCSGetter) GetFile(dst string, u *url.URL) error {
return err
}

client, err := storage.NewClient(ctx)
client, err := g.getClient(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -175,3 +177,20 @@ func (g *GCSGetter) parseURL(u *url.URL) (bucket, path, fragment string, err err
}
return
}

func (g *GCSGetter) getClient(ctx context.Context) (client *storage.Client, err error) {
var opts []option.ClientOption

if v, ok := os.LookupEnv("GOOGLE_OAUTH_ACCESS_TOKEN"); ok {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: v,
})
opts = append(opts, option.WithTokenSource(tokenSource))
}

newClient, err := storage.NewClient(ctx, opts...)
if err != nil {
return nil, err
}
return newClient, nil
}
19 changes: 19 additions & 0 deletions get_gcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,22 @@ func TestGCSGetter_Url(t *testing.T) {
})
}
}

func TestGCSGetter_GetFile_OAuthAccessToken(t *testing.T) {
g := new(GCSGetter)
dst := tempTestFile(t)
defer os.RemoveAll(filepath.Dir(dst))

// Download
err := g.GetFile(
dst, testURL("https://www.googleapis.com/storage/v1/go-getter-test/go-getter/folder/main.tf"))
if err != nil {
t.Fatalf("err: %s", err)
}

// Verify the main file exists
if _, err := os.Stat(dst); err != nil {
t.Fatalf("err: %s", err)
}
assertContents(t, dst, "# Main\n")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2 // indirect
github.com/ulikunitz/xz v0.5.8
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
google.golang.org/api v0.9.0
gopkg.in/cheggaaa/pb.v1 v1.0.27 // indirect
)
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
Expand Down Expand Up @@ -102,7 +101,6 @@ golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4Iltr
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -116,9 +114,7 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 h1:HyfiK1WMnHj5FXFXatD+Qs1A/xC2Run6RzeW1SyHxpc=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2 h1:z99zHgr7hKfrUcX/KsoJk5FJfjTceCKIp96+biqP4To=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand Down

0 comments on commit 64f1c03

Please sign in to comment.