Skip to content

Commit 64f1c03

Browse files
author
Moritz Schmitz von Hülst
authored
Feature Request: Add GOOGLE_OAUTH_ACCESS_TOKEN support for modules hosted on GCS (#302)
* add oauth token support * add tests
1 parent 817ea41 commit 64f1c03

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

get_gcs.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package getter
33
import (
44
"context"
55
"fmt"
6+
"golang.org/x/oauth2"
7+
"google.golang.org/api/option"
68
"net/url"
79
"os"
810
"path/filepath"
@@ -28,7 +30,7 @@ func (g *GCSGetter) ClientMode(u *url.URL) (ClientMode, error) {
2830
return 0, err
2931
}
3032

31-
client, err := storage.NewClient(ctx)
33+
client, err := g.getClient(ctx)
3234
if err != nil {
3335
return 0, err
3436
}
@@ -82,7 +84,7 @@ func (g *GCSGetter) Get(dst string, u *url.URL) error {
8284
return err
8385
}
8486

85-
client, err := storage.NewClient(ctx)
87+
client, err := g.getClient(ctx)
8688
if err != nil {
8789
return err
8890
}
@@ -124,7 +126,7 @@ func (g *GCSGetter) GetFile(dst string, u *url.URL) error {
124126
return err
125127
}
126128

127-
client, err := storage.NewClient(ctx)
129+
client, err := g.getClient(ctx)
128130
if err != nil {
129131
return err
130132
}
@@ -175,3 +177,20 @@ func (g *GCSGetter) parseURL(u *url.URL) (bucket, path, fragment string, err err
175177
}
176178
return
177179
}
180+
181+
func (g *GCSGetter) getClient(ctx context.Context) (client *storage.Client, err error) {
182+
var opts []option.ClientOption
183+
184+
if v, ok := os.LookupEnv("GOOGLE_OAUTH_ACCESS_TOKEN"); ok {
185+
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{
186+
AccessToken: v,
187+
})
188+
opts = append(opts, option.WithTokenSource(tokenSource))
189+
}
190+
191+
newClient, err := storage.NewClient(ctx, opts...)
192+
if err != nil {
193+
return nil, err
194+
}
195+
return newClient, nil
196+
}

get_gcs_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,22 @@ func TestGCSGetter_Url(t *testing.T) {
237237
})
238238
}
239239
}
240+
241+
func TestGCSGetter_GetFile_OAuthAccessToken(t *testing.T) {
242+
g := new(GCSGetter)
243+
dst := tempTestFile(t)
244+
defer os.RemoveAll(filepath.Dir(dst))
245+
246+
// Download
247+
err := g.GetFile(
248+
dst, testURL("https://www.googleapis.com/storage/v1/go-getter-test/go-getter/folder/main.tf"))
249+
if err != nil {
250+
t.Fatalf("err: %s", err)
251+
}
252+
253+
// Verify the main file exists
254+
if _, err := os.Stat(dst); err != nil {
255+
t.Fatalf("err: %s", err)
256+
}
257+
assertContents(t, dst, "# Main\n")
258+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/pmezard/go-difflib v1.0.0 // indirect
2020
github.com/stretchr/testify v1.2.2 // indirect
2121
github.com/ulikunitz/xz v0.5.8
22+
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
2223
google.golang.org/api v0.9.0
2324
gopkg.in/cheggaaa/pb.v1 v1.0.27 // indirect
2425
)

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU
2525
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
2626
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
2727
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
28-
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
2928
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
3029
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
3130
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
@@ -102,7 +101,6 @@ golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4Iltr
102101
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
103102
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
104103
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
105-
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
106104
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
107105
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
108106
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -116,9 +114,7 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w
116114
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
117115
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 h1:HyfiK1WMnHj5FXFXatD+Qs1A/xC2Run6RzeW1SyHxpc=
118116
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
119-
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
120117
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
121-
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2 h1:z99zHgr7hKfrUcX/KsoJk5FJfjTceCKIp96+biqP4To=
122118
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
123119
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
124120
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

0 commit comments

Comments
 (0)