@@ -42,6 +42,11 @@ import (
42
42
var missingTargetError = errors .New ("found RemoteSecret does not have a target in the SPIAccessCheck's namespace, this should not happen" )
43
43
var accessTokenNotFoundError = errors .New ("token data is not found in token storage" )
44
44
45
+ const (
46
+ ScmCredentialsSecretLabel = "appstudio.redhat.com/credentials"
47
+ ScmSecretHostnameLabel = "appstudio.redhat.com/scm.host"
48
+ )
49
+
45
50
// GenericLookup implements a token lookup in a generic way such that the users only need to provide a function
46
51
// to provide a service-provider-specific "state" of the token and a "filter" function that uses the token and its
47
52
// state to match it against a binding
@@ -183,6 +188,14 @@ func (l GenericLookup) LookupCredentials(ctx context.Context, cl client.Client,
183
188
if err != nil {
184
189
return nil , err
185
190
}
191
+
192
+ if secret == nil {
193
+ secret , err = l .lookupSCMSecret (ctx , cl , matchable )
194
+ if err != nil {
195
+ return nil , err
196
+ }
197
+ }
198
+
186
199
if secret == nil {
187
200
return nil , nil
188
201
}
@@ -193,6 +206,28 @@ func (l GenericLookup) LookupCredentials(ctx context.Context, cl client.Client,
193
206
}, nil
194
207
}
195
208
209
+ func (l GenericLookup ) lookupSCMSecret (ctx context.Context , cl client.Client , matchable Matchable ) (* v1.Secret , error ) {
210
+ lg := log .FromContext (ctx )
211
+ repoUrl , err := l .RepoUrlParser (matchable .RepoUrl ())
212
+ if err != nil {
213
+ return nil , fmt .Errorf ("error parsing the repo URL %s: %w" , matchable .RepoUrl (), err )
214
+ }
215
+ secretList := & v1.SecretList {}
216
+ opts := client .ListOption (& client.MatchingLabels {
217
+ ScmCredentialsSecretLabel : "scm" ,
218
+ ScmSecretHostnameLabel : repoUrl .Host ,
219
+ })
220
+
221
+ if err := cl .List (ctx , secretList , client .InNamespace (matchable .ObjNamespace ()), opts ); err != nil {
222
+ return nil , fmt .Errorf ("failed to list SCM secrets in %s namespace: %w" , matchable .ObjNamespace (), err )
223
+ }
224
+ if len (secretList .Items ) > 0 {
225
+ lg .V (logs .DebugLevel ).Info ("found SCM secret" , "name" , secretList .Items [0 ].Name )
226
+ return & secretList .Items [0 ], nil
227
+ }
228
+ return nil , nil
229
+ }
230
+
196
231
// lookupRemoteSecrets searches for RemoteSecrets with RSServiceProviderHostLabel in the same namespaces matchable and
197
232
// filters them using GenericLookup's RemoteSecretFilter.
198
233
func (l GenericLookup ) lookupRemoteSecrets (ctx context.Context , cl client.Client , matchable Matchable ) ([]v1beta1.RemoteSecret , error ) {
0 commit comments