Merge pull request #583 from thockin/v3_cred_helper_cache

v3: Change from "store" to "cache" for credentials
This commit is contained in:
Kubernetes Prow Robot 2022-07-14 10:10:59 -07:00 committed by GitHub
commit d01fc42dce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 15 deletions

View File

@ -405,9 +405,6 @@ func main() {
} }
*flPassword = string(passwordFileBytes) *flPassword = string(passwordFileBytes)
} }
if err := storeGitCredentials(ctx, *flUsername, *flPassword, *flRepo); err != nil {
handleError(false, "ERROR: can't create .netrc file: %v", err)
}
} }
if *flSSH { if *flSSH {
@ -504,13 +501,33 @@ func main() {
go exechookRunner.Run(context.Background()) go exechookRunner.Run(context.Background())
} }
// Craft a function that can be called to refresh credentials when needed.
refreshCreds := func(ctx context.Context) error {
// These should all be mutually-exclusive configs.
if *flUsername != "" {
if err := storeGitCredentials(ctx, *flUsername, *flPassword, *flRepo); err != nil {
return err
}
}
if *flAskPassURL != "" {
// When using an auth URL, the credentials can be dynamic, it needs to be
// re-fetched each time.
if err := callGitAskPassURL(ctx, *flAskPassURL); err != nil {
askpassCount.WithLabelValues(metricKeyError).Inc()
return err
}
askpassCount.WithLabelValues(metricKeySuccess).Inc()
}
return nil
}
initialSync := true initialSync := true
failCount := 0 failCount := 0
for { for {
log.V(1).Info("syncing repo") log.V(1).Info("syncing repo")
start := time.Now() start := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(*flSyncTimeout)) ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(*flSyncTimeout))
if changed, hash, err := syncRepo(ctx, *flRepo, *flBranch, *flRev, *flDepth, *flRoot, *flDest, *flAskPassURL, *flSubmodules); err != nil { if changed, hash, err := syncRepo(ctx, *flRepo, *flBranch, *flRev, *flDepth, *flRoot, *flDest, refreshCreds, *flSubmodules); err != nil {
failCount++ failCount++
updateSyncMetrics(metricKeyError, start) updateSyncMetrics(metricKeyError, start)
if *flMaxSyncFailures != -1 && failCount > *flMaxSyncFailures { if *flMaxSyncFailures != -1 && failCount > *flMaxSyncFailures {
@ -1022,16 +1039,8 @@ func revIsHash(ctx context.Context, rev, gitRoot string) (bool, error) {
// syncRepo syncs the branch of a given repository to the destination at the given rev. // syncRepo syncs the branch of a given repository to the destination at the given rev.
// returns (1) whether a change occured, (2) the new hash, and (3) an error if one happened // returns (1) whether a change occured, (2) the new hash, and (3) an error if one happened
func syncRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot, dest string, authURL string, submoduleMode string) (bool, string, error) { func syncRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot, dest string, refreshCreds func(context.Context) error, submoduleMode string) (bool, string, error) {
if authURL != "" { refreshCreds(ctx)
// When using an auth URL, the credentials can be dynamic, it needs to be
// re-fetched each time.
if err := callGitAskPassURL(ctx, authURL); err != nil {
askpassCount.WithLabelValues(metricKeyError).Inc()
return false, "", fmt.Errorf("failed to get credentials from auth URL: %v", err)
}
askpassCount.WithLabelValues(metricKeySuccess).Inc()
}
currentWorktreeGit := filepath.Join(dest, ".git") currentWorktreeGit := filepath.Join(dest, ".git")
var hash string var hash string
@ -1226,7 +1235,7 @@ func setupDefaultGitConfigs(ctx context.Context) error {
}, { }, {
// How to manage credentials (for those modes that need it). // How to manage credentials (for those modes that need it).
key: "credential.helper", key: "credential.helper",
val: "store", val: "cache --timeout 3600",
}} }}
for _, kv := range configs { for _, kv := range configs {
if _, err := cmdRunner.Run(ctx, "", nil, *flGitCmd, "config", "--global", kv.key, kv.val); err != nil { if _, err := cmdRunner.Run(ctx, "", nil, *flGitCmd, "config", "--global", kv.key, kv.val); err != nil {