@@ -26,6 +26,7 @@ import (
2626 "time"
2727
2828 "github.com/pkg/errors"
29+ "golang.org/x/oauth2"
2930
3031 "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
3132)
@@ -42,21 +43,21 @@ const (
4243// We support GitLab repositories that use the generic packages feature to publish artifacts and versions.
4344// Repositories must use versioned releases.
4445type gitLabRepository struct {
45- providerConfig config.Provider
46- configVariablesClient config.VariablesClient
47- httpClient * http.Client
48- host string
49- projectSlug string
50- packageName string
51- defaultVersion string
52- rootPath string
53- componentsPath string
46+ providerConfig config.Provider
47+ configVariablesClient config.VariablesClient
48+ authenticatingHTTPClient * http.Client
49+ host string
50+ projectSlug string
51+ packageName string
52+ defaultVersion string
53+ rootPath string
54+ componentsPath string
5455}
5556
5657var _ Repository = & gitLabRepository {}
5758
5859// NewGitLabRepository returns a gitLabRepository implementation.
59- func NewGitLabRepository (providerConfig config.Provider , configVariablesClient config.VariablesClient ) (Repository , error ) {
60+ func NewGitLabRepository (ctx context. Context , providerConfig config.Provider , configVariablesClient config.VariablesClient ) (Repository , error ) {
6061 if configVariablesClient == nil {
6162 return nil , errors .New ("invalid arguments: configVariablesClient can't be nil" )
6263 }
@@ -87,20 +88,31 @@ func NewGitLabRepository(providerConfig config.Provider, configVariablesClient c
8788 componentsPath := urlSplit [8 ]
8889
8990 repo := & gitLabRepository {
90- providerConfig : providerConfig ,
91- configVariablesClient : configVariablesClient ,
92- httpClient : httpClient ,
93- host : host ,
94- projectSlug : projectSlug ,
95- packageName : packageName ,
96- defaultVersion : defaultVersion ,
97- rootPath : rootPath ,
98- componentsPath : componentsPath ,
91+ providerConfig : providerConfig ,
92+ configVariablesClient : configVariablesClient ,
93+ authenticatingHTTPClient : httpClient ,
94+ host : host ,
95+ projectSlug : projectSlug ,
96+ packageName : packageName ,
97+ defaultVersion : defaultVersion ,
98+ rootPath : rootPath ,
99+ componentsPath : componentsPath ,
100+ }
101+ if token , err := configVariablesClient .Get (config .GitLabAccessTokenVariable ); err == nil {
102+ repo .setClientToken (ctx , token )
99103 }
100104
101105 return repo , nil
102106}
103107
108+ // setClientToken sets authenticatingHTTPClient field of gitLabRepository struct.
109+ func (g * gitLabRepository ) setClientToken (ctx context.Context , token string ) {
110+ ts := oauth2 .StaticTokenSource (
111+ & oauth2.Token {AccessToken : token , TokenType : "Bearer" },
112+ )
113+ g .authenticatingHTTPClient = oauth2 .NewClient (ctx , ts )
114+ }
115+
104116// Host returns host field of gitLabRepository struct.
105117func (g * gitLabRepository ) Host () string {
106118 return g .host
@@ -154,14 +166,18 @@ func (g *gitLabRepository) GetFile(ctx context.Context, version, path string) ([
154166 return nil , errors .Wrapf (err , "failed to get file %q with version %q from %q: failed to create request" , path , version , url )
155167 }
156168
157- response , err := g .httpClient .Do (request )
169+ response , err := g .authenticatingHTTPClient .Do (request )
158170 if err != nil {
159171 return nil , errors .Wrapf (err , "failed to get file %q with version %q from %q" , path , version , url )
160172 }
161173
162174 defer response .Body .Close ()
163175
164176 if response .StatusCode != http .StatusOK {
177+ // explicitly check for 401 and return a more specific error
178+ if response .StatusCode == http .StatusUnauthorized {
179+ return nil , errors .Errorf ("failed to get file %q with version %q from %q: unauthorized access, please check your credentials" , path , version , url )
180+ }
165181 return nil , errors .Errorf ("failed to get file %q with version %q from %q, got %d" , path , version , url , response .StatusCode )
166182 }
167183
0 commit comments