-
Notifications
You must be signed in to change notification settings - Fork 209
git: refactor authentication, checkout and verification #462
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0cf0d4e
git: refactor AuthStrategy into AuthOptions
hiddeco 80b9807
gogit: add CheckoutStrategy tests
hiddeco 5a1fcc2
git: standardise commit and (PGP) verification
hiddeco b7376ce
gogit: allow checkout of commit without branch
hiddeco 4a23126
libgit2: make RemoteCallbacks helper public
hiddeco 942c310
pkg/git: AuthOptions.Validate() test improvements
darkowlzz 8c581dd
Add git.CheckoutStrategy auth tests
darkowlzz c814e0f
git: remove ', error:' from returned error
hiddeco 7a5d8b1
git: styling nitpicks
hiddeco 99428f5
libgit2: change credentialsCallback logic
hiddeco 562af6d
Add git.CheckoutStrategy SemVer checkout tests
darkowlzz 5bd08a6
Add Host field check in AuthOptions.Validate()
darkowlzz f9a3404
Update gittestserver
darkowlzz a7f2e87
transportAuth(): Add checks for invalid transports
darkowlzz d0ca107
docs: GitRepository commit without branch example
hiddeco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,43 +17,82 @@ limitations under the License. | |
package git | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/go-git/go-git/v5/plumbing/transport" | ||
git2go "github.com/libgit2/git2go/v31" | ||
corev1 "k8s.io/api/core/v1" | ||
"github.com/ProtonMail/go-crypto/openpgp" | ||
) | ||
|
||
const ( | ||
DefaultOrigin = "origin" | ||
DefaultBranch = "master" | ||
DefaultPublicKeyAuthUser = "git" | ||
CAFile = "caFile" | ||
) | ||
type Implementation string | ||
|
||
type Hash []byte | ||
|
||
type Commit interface { | ||
Verify(secret corev1.Secret) error | ||
Hash() string | ||
// String returns the SHA1 Hash as a string. | ||
func (h Hash) String() string { | ||
return string(h) | ||
} | ||
|
||
type CheckoutStrategy interface { | ||
Checkout(ctx context.Context, path, url string, auth *Auth) (Commit, string, error) | ||
type Signature struct { | ||
Name string | ||
Email string | ||
When time.Time | ||
} | ||
|
||
type CheckoutOptions struct { | ||
GitImplementation string | ||
RecurseSubmodules bool | ||
type Commit struct { | ||
// Hash is the SHA1 hash of the commit. | ||
Hash Hash | ||
// Reference is the original reference of the commit, for example: | ||
// 'refs/tags/foo'. | ||
Reference string | ||
// Author is the original author of the commit. | ||
Author Signature | ||
// Committer is the one performing the commit, might be different from | ||
// Author. | ||
Committer Signature | ||
// Signature is the PGP signature of the commit. | ||
Signature string | ||
// Encoded is the encoded commit, without any signature. | ||
Encoded []byte | ||
// Message is the commit message, contains arbitrary text. | ||
Message string | ||
} | ||
|
||
// TODO(hidde): candidate for refactoring, so that we do not directly | ||
// depend on implementation specifics here. | ||
type Auth struct { | ||
AuthMethod transport.AuthMethod | ||
CABundle []byte | ||
CredCallback git2go.CredentialsCallback | ||
CertCallback git2go.CertificateCheckCallback | ||
// String returns a string representation of the Commit, composed | ||
// out the last part of the Reference element, and/or Hash. | ||
// For example: 'tag-1/a0c14dc8580a23f79bc654faa79c4f62b46c2c22', | ||
// for a "tag-1" tag. | ||
func (c *Commit) String() string { | ||
if short := strings.SplitAfterN(c.Reference, "/", 3); len(short) == 3 { | ||
return fmt.Sprintf("%s/%s", short[2], c.Hash) | ||
} | ||
return fmt.Sprintf("HEAD/%s", c.Hash) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to document the revision format in |
||
} | ||
|
||
type AuthSecretStrategy interface { | ||
Method(secret corev1.Secret) (*Auth, error) | ||
// Verify the Signature of the commit with the given key rings. | ||
// It returns the fingerprint of the key the signature was verified | ||
// with, or an error. | ||
func (c *Commit) Verify(keyRing ...string) (string, error) { | ||
if c.Signature == "" { | ||
return "", fmt.Errorf("commit does not have a PGP signature") | ||
} | ||
|
||
for _, r := range keyRing { | ||
reader := strings.NewReader(r) | ||
keyring, err := openpgp.ReadArmoredKeyRing(reader) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to read armored key ring: %w", err) | ||
} | ||
signer, err := openpgp.CheckArmoredDetachedSignature(keyring, bytes.NewBuffer(c.Encoded), bytes.NewBufferString(c.Signature), nil) | ||
if err == nil { | ||
return fmt.Sprintf("%X", signer.PrimaryKey.Fingerprint[12:20]), nil | ||
} | ||
} | ||
return "", fmt.Errorf("failed to verify commit with any of the given key rings") | ||
} | ||
|
||
type CheckoutStrategy interface { | ||
Checkout(ctx context.Context, path, url string, config *AuthOptions) (*Commit, error) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.