@@ -26,6 +26,7 @@ import (
26
26
"time"
27
27
28
28
"github.com/pkg/errors"
29
+ "golang.org/x/oauth2"
29
30
30
31
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
31
32
)
@@ -42,21 +43,21 @@ const (
42
43
// We support GitLab repositories that use the generic packages feature to publish artifacts and versions.
43
44
// Repositories must use versioned releases.
44
45
type 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
54
55
}
55
56
56
57
var _ Repository = & gitLabRepository {}
57
58
58
59
// 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 ) {
60
61
if configVariablesClient == nil {
61
62
return nil , errors .New ("invalid arguments: configVariablesClient can't be nil" )
62
63
}
@@ -87,20 +88,31 @@ func NewGitLabRepository(providerConfig config.Provider, configVariablesClient c
87
88
componentsPath := urlSplit [8 ]
88
89
89
90
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 )
99
103
}
100
104
101
105
return repo , nil
102
106
}
103
107
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
+
104
116
// Host returns host field of gitLabRepository struct.
105
117
func (g * gitLabRepository ) Host () string {
106
118
return g .host
@@ -154,14 +166,18 @@ func (g *gitLabRepository) GetFile(ctx context.Context, version, path string) ([
154
166
return nil , errors .Wrapf (err , "failed to get file %q with version %q from %q: failed to create request" , path , version , url )
155
167
}
156
168
157
- response , err := g .httpClient .Do (request )
169
+ response , err := g .authenticatingHTTPClient .Do (request )
158
170
if err != nil {
159
171
return nil , errors .Wrapf (err , "failed to get file %q with version %q from %q" , path , version , url )
160
172
}
161
173
162
174
defer response .Body .Close ()
163
175
164
176
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
+ }
165
181
return nil , errors .Errorf ("failed to get file %q with version %q from %q, got %d" , path , version , url , response .StatusCode )
166
182
}
167
183
0 commit comments