-
Notifications
You must be signed in to change notification settings - Fork 358
Implement credentials tracking #834
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
Changes from all commits
e91398a
55ea43a
cc028d4
d1a1fe1
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 |
---|---|---|
|
@@ -158,7 +158,7 @@ public BitbucketSCMSource scmSource() { | |
@NonNull | ||
public BitbucketGitSCMBuilder withCredentials(String credentialsId, BitbucketRepositoryProtocol protocol) { | ||
if (StringUtils.isNotBlank(credentialsId)) { | ||
StandardCredentials credentials = BitbucketCredentials.lookupCredentials( | ||
StandardCredentials credentials = BitbucketCredentials.lookupCredentialsAndTrackUsage( | ||
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. this is already tracked by the git plugin on checkout. If your pipeline does not have a |
||
scmSource.getServerUrl(), | ||
scmSource.getOwner(), | ||
DescriptorImpl.SAME.equals(scmSource.getCheckoutCredentialsId()) ? credentialsId : scmSource.getCheckoutCredentialsId(), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ | |
import hudson.model.Action; | ||
import hudson.model.Actionable; | ||
import hudson.model.Item; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import hudson.plugins.git.GitSCM; | ||
import hudson.scm.SCM; | ||
|
@@ -1072,7 +1073,7 @@ public DescriptorImpl getDescriptor() { | |
|
||
@CheckForNull | ||
/* package */ StandardCredentials credentials() { | ||
return BitbucketCredentials.lookupCredentials( | ||
return BitbucketCredentials.lookupCredentialsAndTrackUsage( | ||
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. it's too earlier, in this way you track same credentials multiple time on webhook event. It should be tracked only at the beginning of retrieve method |
||
getServerUrl(), | ||
getOwner(), | ||
getCredentialsId(), | ||
|
@@ -1225,6 +1226,11 @@ public static void setEventDelaySeconds(int eventDelaySeconds) { | |
BitbucketSCMSource.eventDelaySeconds = Math.min(300, Math.max(0, eventDelaySeconds)); | ||
} | ||
|
||
public static BitbucketSCMSource findForRun(Run<?, ?> run) { | ||
SCMSource s = SCMSource.SourceByItem.findSource(run.getParent()); | ||
return s instanceof BitbucketSCMSource ? (BitbucketSCMSource) s : null; | ||
} | ||
|
||
private void initCloneLinks() { | ||
if (primaryCloneLinks == null) { | ||
BitbucketApi bitbucket = buildBitbucketClient(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.cloudbees.jenkins.plugins.bitbucket; | ||
|
||
import com.cloudbees.plugins.credentials.CredentialsProvider; | ||
import hudson.Extension; | ||
import hudson.model.Run; | ||
import hudson.model.listeners.RunListener; | ||
|
||
/** | ||
* Tracks the usage of credentials | ||
*/ | ||
@Extension | ||
public class CredentialTrackingRunListener extends RunListener<Run<?, ?>> { | ||
Comment on lines
+11
to
+12
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'm surprised that this is needed. Is this how other plugins track the credentials of runs? 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. To be honest I have no idea. With my limited knowledge though it kind of makes sense as at the time of the actual job invocation any generic code wouldn't know about the configured credentials anymore as they would have been transfored already into whatever format is actually required to perform the task (eg an environment variable, or a request header, etc.), and even if you would hook exactly into that transformation you most likely wouldn't have access to the actual Please just let me know on how you want to proceed here. 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. Other plugins may be using CredentialsProvider.findCredentialById, which usually calls CredentialsProvider.track. However, a search for 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. So how should we procceed here? 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. @KalleOlaviNiemitalo any feedback on this? |
||
@Override | ||
public void onInitialize(Run<?, ?> run) { | ||
final BitbucketSCMSource source = BitbucketSCMSource.findForRun(run); | ||
|
||
if (source == null) { | ||
return; | ||
} | ||
|
||
final boolean usesSshCheckout = source.getTraits().stream().anyMatch(scmSourceTrait -> scmSourceTrait instanceof SSHCheckoutTrait); | ||
|
||
if (!usesSshCheckout) { | ||
CredentialsProvider.track(run, source.credentials()); | ||
} | ||
} | ||
} |
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.
Why tracks credentials when populate the select of mirrors?