@@ -20,7 +20,6 @@ import (
20
20
"encoding/json"
21
21
"fmt"
22
22
"os"
23
- "regexp"
24
23
"strings"
25
24
"time"
26
25
@@ -75,13 +74,13 @@ type WIAuthProvider struct {
75
74
registryHostGetter RegistryHostGetter
76
75
getAADAccessToken AADAccessTokenGetter
77
76
reportMetrics MetricsReporter
78
- hostPredicates [] * regexp. Regexp
77
+ endpoints [] string
79
78
}
80
79
81
80
type azureWIAuthProviderConf struct {
82
81
Name string `json:"name"`
83
82
ClientID string `json:"clientID,omitempty"`
84
- HostScope []string `json:"hostScope ,omitempty"`
83
+ Endpoints []string `json:"endpoints ,omitempty"`
85
84
}
86
85
87
86
const (
@@ -118,9 +117,12 @@ func (s *AzureWIProviderFactory) Create(authProviderConfig provider.AuthProvider
118
117
}
119
118
}
120
119
121
- hostPredicates , err := parseHostScopeToPredicates (conf .HostScope )
122
- if err != nil {
123
- return nil , re .ErrorCodeConfigInvalid .WithError (err )
120
+ if len (conf .Endpoints ) == 0 {
121
+ conf .Endpoints = []string {defaultACREndpoint }
122
+ } else {
123
+ if err := validateHostScope (conf .Endpoints ); err != nil {
124
+ return nil , re .ErrorCodeConfigInvalid .WithError (err )
125
+ }
124
126
}
125
127
126
128
// retrieve an AAD Access token
@@ -137,7 +139,7 @@ func (s *AzureWIProviderFactory) Create(authProviderConfig provider.AuthProvider
137
139
registryHostGetter : & defaultRegistryHostGetterImpl {}, // Concrete implementation
138
140
getAADAccessToken : & defaultAADAccessTokenGetterImpl {}, // Concrete implementation
139
141
reportMetrics : & defaultMetricsReporterImpl {},
140
- hostPredicates : hostPredicates ,
142
+ endpoints : conf . Endpoints ,
141
143
}, nil
142
144
}
143
145
@@ -168,7 +170,7 @@ func (d *WIAuthProvider) Provide(ctx context.Context, artifact string) (provider
168
170
return provider.AuthConfig {}, re .ErrorCodeHostNameInvalid .WithComponentType (re .AuthProvider )
169
171
}
170
172
171
- if err := validateHost (artifactHostName , d .hostPredicates ); err != nil {
173
+ if err := validateHost (artifactHostName , d .endpoints ); err != nil {
172
174
return provider.AuthConfig {}, re .ErrorCodeHostNameInvalid .WithError (err )
173
175
}
174
176
@@ -220,30 +222,6 @@ func (d *WIAuthProvider) Provide(ctx context.Context, artifact string) (provider
220
222
return authConfig , nil
221
223
}
222
224
223
- func parseHostScopeToPredicates (hostScope []string ) ([]* regexp.Regexp , error ) {
224
- if err := validateHostScope (hostScope ); err != nil {
225
- return nil , err
226
- }
227
-
228
- var predicates []* regexp.Regexp
229
- if len (hostScope ) == 0 {
230
- re , err := regexp .Compile ("^" + defaultHostScope + "$" )
231
- if err != nil {
232
- return nil , fmt .Errorf ("failed to compile default host scope regex: %w" , err )
233
- }
234
- predicates = append (predicates , re )
235
- } else {
236
- for _ , scope := range hostScope {
237
- re , err := regexp .Compile ("^" + strings .ReplaceAll (scope , "*" , ".*" ) + "$" )
238
- if err != nil {
239
- return nil , fmt .Errorf ("failed to compile host scope regex: %w" , err )
240
- }
241
- predicates = append (predicates , re )
242
- }
243
- }
244
- return predicates , nil
245
- }
246
-
247
225
// validateHostScope checks if the host scope is valid for auth provider.
248
226
// A valid host is either a fully qualified domain name or a wildcard domain
249
227
// name folloiwing RFC 1034.
@@ -256,20 +234,38 @@ func parseHostScopeToPredicates(hostScope []string) ([]*regexp.Regexp, error) {
256
234
// - example.*
257
235
// - *example.com
258
236
func validateHostScope (hostScope []string ) error {
259
- pattern := regexp .MustCompile (`^(\*\.)?([^*]+\.)*[^*.]+$` )
260
237
for _ , scope := range hostScope {
261
- if ! pattern .MatchString (scope ) {
262
- return fmt .Errorf ("invalid host scope %s" , scope )
238
+ switch strings .Count (scope , "*" ) {
239
+ case 0 :
240
+ continue
241
+ case 1 :
242
+ if ! strings .HasPrefix (scope , "*." ) {
243
+ return fmt .Errorf ("invalid wildcard domain name: %s, it must start with '*.'" , scope )
244
+ }
245
+ if len (scope ) < 3 {
246
+ return fmt .Errorf ("invalid wildcard domain name: %s, it must have at least one character after '*.'" , scope )
247
+ }
248
+ default :
249
+ return fmt .Errorf ("invalid wildcard domain name: %s, it must have at most one wildcard character" , scope )
263
250
}
264
251
}
265
252
return nil
266
253
}
267
254
268
255
// validateHost checks if the host is in the scope of the store auth provider.
269
- func validateHost (host string , predicates []* regexp.Regexp ) error {
270
- for _ , scope := range predicates {
271
- if scope .MatchString (host ) {
272
- return nil
256
+ func validateHost (host string , endpoints []string ) error {
257
+ for _ , endpoint := range endpoints {
258
+ switch strings .Count (endpoint , "*" ) {
259
+ case 0 :
260
+ if host == endpoint {
261
+ return nil
262
+ }
263
+ case 1 :
264
+ if strings .HasSuffix (host , strings .TrimPrefix (endpoint , "*" )) {
265
+ return nil
266
+ }
267
+ default :
268
+ continue
273
269
}
274
270
}
275
271
return fmt .Errorf ("the artifact host %s is not in the scope of the store auth provider" , host )
0 commit comments