Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit 34ae088

Browse files
authored
Add scm secrets into credentials lookup mechnism & fix gitlab client creation (#939)
--------- Signed-off-by: Max Shaposhnyk <[email protected]>
1 parent a07e830 commit 34ae088

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

Diff for: pkg/serviceprovider/gitlab/downloadfilecapability.go

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import (
2020
"errors"
2121
"fmt"
2222
"net/http"
23+
"strings"
24+
25+
"github.com/redhat-appstudio/remote-secret/pkg/logs"
2326

2427
api "github.com/redhat-appstudio/service-provider-integration-operator/api/v1beta1"
2528
"github.com/redhat-appstudio/service-provider-integration-operator/pkg/serviceprovider"
@@ -71,6 +74,8 @@ func (f downloadFileCapability) DownloadFile(ctx context.Context, request api.SP
7174
refOption = gitlab.GetFileOptions{Ref: gitlab.Ptr("HEAD")}
7275
}
7376

77+
lg.V(logs.DebugLevel).Info("Downloading file", "owner", owner, "project", project, "filePath", request.FilePath, "ref", refOption.Ref)
78+
request.FilePath = strings.TrimPrefix(request.FilePath, "/")
7479
file, resp, err := glClient.RepositoryFiles.GetFile(owner+"/"+project, request.FilePath, &refOption)
7580
if err != nil {
7681
// unfortunately, GitLab library closes the response body, so it is cannot be read

Diff for: pkg/serviceprovider/gitlab/gitlabclientbuilder.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ type gitlabClientBuilder struct {
3434
var _ serviceprovider.AuthenticatedClientBuilder[gitlab.Client] = (*gitlabClientBuilder)(nil)
3535

3636
func (builder gitlabClientBuilder) CreateAuthenticatedClient(ctx context.Context, credentials serviceprovider.Credentials) (*gitlab.Client, error) {
37-
client, err := gitlab.NewOAuthClient(credentials.Token, gitlab.WithHTTPClient(builder.httpClient), gitlab.WithBaseURL(builder.gitlabBaseUrl))
37+
var client *gitlab.Client
38+
var err error
39+
if credentials.Username == "" {
40+
client, err = gitlab.NewClient(credentials.Token, gitlab.WithHTTPClient(builder.httpClient), gitlab.WithBaseURL(builder.gitlabBaseUrl))
41+
} else {
42+
client, err = gitlab.NewBasicAuthClient(credentials.Username, credentials.Token, gitlab.WithHTTPClient(builder.httpClient), gitlab.WithBaseURL(builder.gitlabBaseUrl))
43+
}
3844
if err != nil {
3945
return nil, fmt.Errorf("failed to created new authenticated GitLab client: %w", err)
4046
}

Diff for: pkg/serviceprovider/lookup.go

+35
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ import (
4242
var missingTargetError = errors.New("found RemoteSecret does not have a target in the SPIAccessCheck's namespace, this should not happen")
4343
var accessTokenNotFoundError = errors.New("token data is not found in token storage")
4444

45+
const (
46+
ScmCredentialsSecretLabel = "appstudio.redhat.com/credentials"
47+
ScmSecretHostnameLabel = "appstudio.redhat.com/scm.host"
48+
)
49+
4550
// GenericLookup implements a token lookup in a generic way such that the users only need to provide a function
4651
// to provide a service-provider-specific "state" of the token and a "filter" function that uses the token and its
4752
// state to match it against a binding
@@ -183,6 +188,14 @@ func (l GenericLookup) LookupCredentials(ctx context.Context, cl client.Client,
183188
if err != nil {
184189
return nil, err
185190
}
191+
192+
if secret == nil {
193+
secret, err = l.lookupSCMSecret(ctx, cl, matchable)
194+
if err != nil {
195+
return nil, err
196+
}
197+
}
198+
186199
if secret == nil {
187200
return nil, nil
188201
}
@@ -193,6 +206,28 @@ func (l GenericLookup) LookupCredentials(ctx context.Context, cl client.Client,
193206
}, nil
194207
}
195208

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+
196231
// lookupRemoteSecrets searches for RemoteSecrets with RSServiceProviderHostLabel in the same namespaces matchable and
197232
// filters them using GenericLookup's RemoteSecretFilter.
198233
func (l GenericLookup) lookupRemoteSecrets(ctx context.Context, cl client.Client, matchable Matchable) ([]v1beta1.RemoteSecret, error) {

0 commit comments

Comments
 (0)