Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify auto-generated pac-gitauth secret URL #1311

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/content/docs/guide/privaterepo.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ This secret contains a [Git Config](https://git-scm.com/docs/git-config) file:
file: .git-credentials, which includes the https URL using the token obtained
from the GitHub application or secret attached to the repo CR.

{{< hint info >}} For compatibility, the [Git
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think we need multiple hints which is close to the other one and the info is a bit confusing for the end user... but no worries let's merge this and i'll rephrase that thereafter.

image

Config](https://git-scm.com/docs/git-config) file uses the detected repository's
base URL instead of the full URL. For more information, see [this
issue](https://github.com/openshift-pipelines/pipelines-as-code/issues/1307) {{<
/hint >}}

The secret includes a key referencing the token as a key to let you easily use it in your task for
other provider operations. See the documentation with example on how to use it
[here](../authoringprs/#using-the-temporary-github-app-token-for-github-api-operations)
Expand Down
3 changes: 2 additions & 1 deletion pkg/secrets/basic_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ func MakeBasicAuthSecret(runevent *info.Event, secretName string) (*corev1.Secre
// in the *** to do it in shell.
token := url.QueryEscape(runevent.Provider.Token)

baseCloneURL := fmt.Sprintf("%s://%s", repoURL.Scheme, repoURL.Host)
urlWithToken := fmt.Sprintf("%s://%s:%s@%s%s", repoURL.Scheme, gitUser, token, repoURL.Host, repoURL.Path)
secretData := map[string]string{
".gitconfig": fmt.Sprintf(basicAuthGitConfigData, cloneURL),
".gitconfig": fmt.Sprintf(basicAuthGitConfigData, baseCloneURL),
".git-credentials": urlWithToken,
// With the GitHub APP method the token is available for 8h if you have
// the user to server token expiration. the token is scoped to the
Expand Down
15 changes: 15 additions & 0 deletions pkg/secrets/basic_auth_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package secrets

import (
"fmt"
"regexp"
"strings"
"testing"

Expand All @@ -25,6 +27,7 @@ func TestCreateBasicAuthSecret(t *testing.T) {
name string
targetNS string
event info.Event
expectedGitConfigURL string
expectedGitCredentials string
expectedStartSecretName string
expectedError bool
Expand All @@ -34,6 +37,7 @@ func TestCreateBasicAuthSecret(t *testing.T) {
name: "Target secret not there",
targetNS: nsNotThere,
event: event,
expectedGitConfigURL: "https://forge",
expectedGitCredentials: "https://git:verysecrete@forge/owner/repo",
expectedStartSecretName: "pac-gitauth-owner-repo",
expectedLabels: map[string]string{
Expand All @@ -50,6 +54,7 @@ func TestCreateBasicAuthSecret(t *testing.T) {
Repository: "yoyo",
URL: "https://forge/owner/yoyo/foo/bar/linux/kernel",
},
expectedGitConfigURL: "https://forge",
expectedGitCredentials: "https://git:verysecrete@forge/owner/yoyo/foo/bar/linux/kernel",
expectedStartSecretName: "pac-gitauth-owner-repo",
expectedLabels: map[string]string{
Expand All @@ -62,13 +67,15 @@ func TestCreateBasicAuthSecret(t *testing.T) {
name: "Use clone URL",
targetNS: nsNotThere,
event: event,
expectedGitConfigURL: "https://forge",
expectedGitCredentials: "https://git:verysecrete@forge/owner/repo",
expectedStartSecretName: "pac-gitauth-owner-repo",
},
{
name: "Target secret already there",
targetNS: nsthere,
event: event,
expectedGitConfigURL: "https://forge",
expectedGitCredentials: "https://git:verysecrete@forge/owner/repo",
expectedStartSecretName: "pac-gitauth-owner-repo",
},
Expand All @@ -80,6 +87,7 @@ func TestCreateBasicAuthSecret(t *testing.T) {
Repository: "CASE",
URL: "https://forge/UPPER/CASE",
},
expectedGitConfigURL: "https://forge",
expectedGitCredentials: "https://git:verysecrete@forge/UPPER/CASE",
expectedStartSecretName: "pac-gitauth-upper-case",
},
Expand All @@ -92,6 +100,7 @@ func TestCreateBasicAuthSecret(t *testing.T) {
URL: "https://forge/hello/moto",
CloneURL: "https://forge/miss/robinson",
},
expectedGitConfigURL: "https://forge",
expectedGitCredentials: "https://git:verysecrete@forge/miss/robinson",
expectedStartSecretName: "pac-gitauth-upper-case",
},
Expand All @@ -107,6 +116,7 @@ func TestCreateBasicAuthSecret(t *testing.T) {
Token: "supersecrete",
},
},
expectedGitConfigURL: "https://forge",
expectedGitCredentials: "https://superman:supersecrete@forge/bat/cave",
expectedStartSecretName: "pac-gitauth-upper-case",
},
Expand All @@ -126,6 +136,11 @@ func TestCreateBasicAuthSecret(t *testing.T) {
}
}
assert.Assert(t, strings.HasPrefix(secret.GetName(), tt.expectedStartSecretName))
gitConfig := secret.StringData[".gitconfig"]
regPattern := fmt.Sprintf("\\[credential\\s+\\\"%s\\\"\\]", tt.expectedGitConfigURL)
match, err := regexp.MatchString(regPattern, gitConfig)
assert.NilError(t, err)
assert.Assert(t, match, ".gitconfig URL should not have path component: %s", gitConfig)
assert.Equal(t, secret.StringData[".git-credentials"], tt.expectedGitCredentials)
})
}
Expand Down
Loading