-
Notifications
You must be signed in to change notification settings - Fork 24
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
Git Token Based Authentication #192
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ package porch | |
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/go-git/go-git/v5/plumbing/transport" | ||
|
@@ -43,6 +44,9 @@ const ( | |
|
||
//Secret.Data key required for the caBundle | ||
CaBundleDataName = "ca.crt" | ||
|
||
//Secret.Data key substring required for the token | ||
TokenDataName = "token" | ||
) | ||
|
||
func NewCredentialResolver(coreClient client.Reader, resolverChain []Resolver) repository.CredentialResolver { | ||
|
@@ -143,6 +147,50 @@ func (b *BasicAuthCredential) ToAuthMethod() transport.AuthMethod { | |
} | ||
} | ||
|
||
// token based secret verification | ||
// same as basic auth e.g. username:password | ||
// but in token based auth username does not matter and password is the token itself | ||
func NewTokenAuthResolver() Resolver { | ||
return &TokenAuthResolver{} | ||
} | ||
|
||
var _ Resolver = &TokenAuthResolver{} | ||
|
||
type TokenAuthResolver struct{} | ||
|
||
type TokenAuthCredentials struct { | ||
Username string | ||
Password string | ||
} | ||
|
||
func (b *TokenAuthResolver) Resolve(_ context.Context, secret core.Secret) (repository.Credential, bool, error) { | ||
if secret.Data[strings.ToLower(TokenDataName)] == nil && secret.Type == core.SecretTypeOpaque{ | ||
return nil, false, fmt.Errorf("Token secret.Data key must be set as %s", TokenDataName) | ||
} | ||
|
||
return &TokenAuthCredentials{ | ||
Username: string("Username"), | ||
Password: string(secret.Data[TokenDataName]), | ||
}, true, nil | ||
} | ||
|
||
func (b *TokenAuthCredentials) ToString() string { | ||
return b.Password | ||
} | ||
|
||
var _ repository.Credential = &TokenAuthCredentials{} | ||
|
||
func (b *TokenAuthCredentials) Valid() bool { | ||
return true | ||
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. Just checking is this OK? Is there nothing to validate? 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. see i think this might just be a misworded function name because the basic-auth(username & password resolver & ca bundle resolver both return true for this method also) only the gcw resolver returns the following.
which to me means that check if the value is still valid based on a time interval/expiry mechanism which i dont believe apply in the other cases? 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 any checks we can do on validity or expiry of the token would be good. Does the method not have to be called Valid()? I assume this must be an implementation of an interface that is being called by someone? 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. in theory yes but this commit only allows the use of git tokens such as personal access tokens which is what we are using since were interacting with git instances for services such as github, gitlab, gitea etc and i believe these type of tokens do not actually include the expiry date in the token itself (just the key which to use) but rather manage this on the server side of the git instance itself. 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. if lets say we added JWT or perhaps other types of tokens to our list and added ability to process them which could contain other data within their token string such as the expiration date etc then another Resolver would have to be created and that Resolver Valid() method could process that information and return a more accurate validity check instead of just returning true |
||
} | ||
|
||
func (b *TokenAuthCredentials) ToAuthMethod() transport.AuthMethod { | ||
return &http.BasicAuth{ | ||
Username: string(b.Username), | ||
Password: string(b.Password), | ||
} | ||
} | ||
|
||
func NewCaBundleResolver() Resolver { | ||
return &CaBundleResolver{} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.