-
Notifications
You must be signed in to change notification settings - Fork 50
Improve Credentials logging by using CredentialUseListener #68
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
PierreBtz
merged 13 commits into
jenkinsci:master
from
meiswjn:feature/add-credentials-listener
May 13, 2022
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ff22459
Add CredentialUseListener
meiswjn 6e23b70
Generalize types
meiswjn 65d73b8
Add tests
meiswjn eff45dd
Add maven.config to use incrementals
meiswjn b83ec7b
Remove local snapshot version of credentials plugin
meiswjn d5d9de7
Add translation for new option; Fix translation for other option
meiswjn e60fa01
Add type-specific implementations; improve string formatting and docs…
meiswjn 0e8f8e3
Complete existing test cases
meiswjn 3080aa8
Bump versions to meet creds plugin requirements
meiswjn 8cfc731
Merge remote-tracking branch 'upstream/HEAD' into feature/add-credent…
PierreBtz 8d3a274
Add documentation
PierreBtz 06770ea
Fix casc test
PierreBtz 6a8d1fb
Add a logger for debug purposes
PierreBtz 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
116 changes: 116 additions & 0 deletions
116
src/main/java/hudson/plugins/audit_trail/CredentialUsageListener.java
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 |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package hudson.plugins.audit_trail; | ||
|
|
||
| import com.cloudbees.plugins.credentials.Credentials; | ||
| import com.cloudbees.plugins.credentials.CredentialsProvider; | ||
| import com.cloudbees.plugins.credentials.CredentialsUseListener; | ||
| import com.cloudbees.plugins.credentials.common.IdCredentials; | ||
| import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials; | ||
| import hudson.Extension; | ||
| import hudson.model.Item; | ||
| import hudson.model.Node; | ||
| import hudson.model.Run; | ||
|
|
||
| import javax.inject.Inject; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
|
|
||
| /** | ||
| * Log when credentials are used. Only works if the job decides to access the credentials via the | ||
| * {@link com.cloudbees.plugins.credentials.CredentialsProvider}. Credential-types that do not extend | ||
| * {@link com.cloudbees.plugins.credentials.Credentials} | ||
| * | ||
| * @author Jan Meiswinkel | ||
| */ | ||
| @Extension | ||
| public class CredentialUsageListener implements CredentialsUseListener { | ||
| @Inject | ||
| AuditTrailPlugin configuration; | ||
|
|
||
| /** | ||
| * Triggered when the {@link com.cloudbees.plugins.credentials.CredentialsProvider} accesses | ||
| * {@link com.cloudbees.plugins.credentials.Credentials}. | ||
| * | ||
| * @see CredentialsProvider#trackAll(Run, java.util.List) | ||
| * | ||
| * @param c The used Credentials. | ||
| * @param run The object using the credentials. | ||
| */ | ||
| @Override | ||
| public void onUse(Credentials c, Run run) { | ||
| if (!configuration.shouldLogCredentialsUsage()) | ||
| return; | ||
|
|
||
| StringBuilder builder = new StringBuilder(100); | ||
|
|
||
| String runName = run.getExternalizableId(); | ||
| String runType = run.getClass().toString(); | ||
| builder.append(String.format("'%s' (%s) ", runName, runType)); | ||
| auditLog(c, builder); | ||
| } | ||
|
|
||
| /** | ||
| * Triggered when the {@link com.cloudbees.plugins.credentials.CredentialsProvider} accesses | ||
| * {@link com.cloudbees.plugins.credentials.Credentials}. | ||
| * | ||
| * @see CredentialsProvider#trackAll(Node, java.util.List) | ||
|
|
||
| * @param c The used Credentials. | ||
| * @param node The object using the credentials. | ||
| */ | ||
| @Override | ||
| public void onUse(Credentials c, Node node) { | ||
| if (!configuration.shouldLogCredentialsUsage()) | ||
| return; | ||
|
|
||
| StringBuilder builder = new StringBuilder(100); | ||
|
|
||
| String nodeName = node.getNodeName(); | ||
| String nodeType = node.getClass().toString(); | ||
| builder.append(String.format("'%s' (%s) ", nodeName, nodeType)); | ||
| auditLog(c, builder); | ||
| } | ||
|
|
||
| /** | ||
| * Triggered when the {@link com.cloudbees.plugins.credentials.CredentialsProvider} accesses | ||
| * {@link com.cloudbees.plugins.credentials.Credentials}. | ||
| * | ||
| * @see CredentialsProvider#trackAll(Item, java.util.List) | ||
| * | ||
| * @param c The used Credentials. | ||
| * @param item The object using the credentials. | ||
| */ | ||
| @Override | ||
| public void onUse(Credentials c, Item item) { | ||
| if (!configuration.shouldLogCredentialsUsage()) | ||
| return; | ||
|
|
||
| StringBuilder builder = new StringBuilder(100); | ||
|
|
||
| String runName = item.getFullName(); | ||
| String itemType = item.getClass().toString(); | ||
| builder.append(String.format("'%s' (%s) ", runName, itemType)); | ||
| auditLog(c, builder); | ||
| } | ||
|
|
||
| private void auditLog(Credentials c, StringBuilder builder) { | ||
| String credsType = c.getClass().toString(); | ||
| if (c instanceof BaseStandardCredentials) { | ||
| String credsId = ((BaseStandardCredentials) c).getId(); | ||
| builder.append(String.format("used credentials '%s' (%s).", credsId, credsType)); | ||
| } else if (c instanceof IdCredentials) { | ||
| String credsId = ((IdCredentials) c).getId(); | ||
| builder.append(String.format("used credentials '%s' (%s).", credsId, credsType)); | ||
| } else { | ||
| String noIdAvailableWarning = builder + ("used an unsupported credentials type (" + credsType + | ||
| ") whose ID cannot be audit-logged. Consider opening an issue."); | ||
| Logger.getLogger(CredentialUsageListener.class.getName()).log(Level.WARNING, null, noIdAvailableWarning); | ||
|
|
||
| builder.append("used credentials of type " + credsType + " (Note: Used fallback method for log as " + | ||
| "credentials type is not supported. See INFO log for more information)."); | ||
| } | ||
|
|
||
| for (AuditLogger logger : configuration.getLoggers()) { | ||
| logger.log(builder.toString()); | ||
| } | ||
| } | ||
| } |
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
96 changes: 96 additions & 0 deletions
96
src/test/java/hudson/plugins/audit_trail/CredentialUsageListenerTest.java
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 |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package hudson.plugins.audit_trail; | ||
|
|
||
| import com.cloudbees.plugins.credentials.Credentials; | ||
| import com.cloudbees.plugins.credentials.CredentialsProvider; | ||
| import com.cloudbees.plugins.credentials.CredentialsScope; | ||
| import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; | ||
| import hudson.Util; | ||
| import hudson.model.FreeStyleProject; | ||
| import hudson.model.Item; | ||
| import hudson.slaves.DumbSlave; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TemporaryFolder; | ||
| import org.jvnet.hudson.test.JenkinsRule; | ||
|
|
||
| import java.io.File; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| public class CredentialUsageListenerTest { | ||
| @Rule | ||
| public JenkinsRule r = new JenkinsRule(); | ||
| @Rule | ||
| public TemporaryFolder tmpDir = new TemporaryFolder(); | ||
|
|
||
| @Test | ||
| public void jobCredentialUsageIsLogged() throws Exception { | ||
| String logFileName = "jobCredentialUsageIsProperlyLogged.log"; | ||
| File logFile = new File(tmpDir.getRoot(), logFileName); | ||
| JenkinsRule.WebClient wc = r.createWebClient(); | ||
| new SimpleAuditTrailPluginConfiguratorHelper(logFile).sendConfiguration(r, wc); | ||
|
|
||
| FreeStyleProject job = r.createFreeStyleProject("test-job"); | ||
| String id = "id"; | ||
| Credentials creds = new UsernamePasswordCredentialsImpl( | ||
| CredentialsScope.GLOBAL, id, "description", "username", "password"); | ||
| CredentialsProvider.track(job, creds); | ||
|
|
||
| String log = Util.loadFile(new File(tmpDir.getRoot(), logFileName + ".0"), StandardCharsets.UTF_8); | ||
| assertTrue("logged actions: " + log, Pattern.compile(".*test-job.*used credentials '" + id + "'.*", Pattern.DOTALL).matcher(log).matches()); | ||
| } | ||
|
|
||
| @Test | ||
| public void nodeCredentialUsageIsLogged() throws Exception { | ||
| String logFileName = "nodeCredentialUsageIsProperlyLogged.log"; | ||
| File logFile = new File(tmpDir.getRoot(), logFileName); | ||
| JenkinsRule.WebClient wc = r.createWebClient(); | ||
| new SimpleAuditTrailPluginConfiguratorHelper(logFile).sendConfiguration(r, wc); | ||
|
|
||
| DumbSlave dummyAgent = r.createSlave(); | ||
| dummyAgent.setNodeName("test-agent"); | ||
| String id = "id"; | ||
| Credentials creds = new UsernamePasswordCredentialsImpl( | ||
| CredentialsScope.GLOBAL, id, "description", "username", "password"); | ||
| CredentialsProvider.track(dummyAgent, creds); | ||
|
|
||
| String log = Util.loadFile(new File(tmpDir.getRoot(), logFileName + ".0"), StandardCharsets.UTF_8); | ||
| assertTrue("logged actions: " + log, Pattern.compile(".*test-agent.*used credentials '" + id + "'.*", Pattern.DOTALL).matcher(log).matches()); | ||
| } | ||
|
|
||
| @Test | ||
| public void itemCredentialUsageIsLogged() throws Exception { | ||
| String logFileName = "itemCredentialUsageIsProperlyLogged.log"; | ||
| File logFile = new File(tmpDir.getRoot(), logFileName); | ||
| JenkinsRule.WebClient wc = r.createWebClient(); | ||
| new SimpleAuditTrailPluginConfiguratorHelper(logFile).sendConfiguration(r, wc); | ||
| // 'Folder' because it is a non-traditional item to access credentials. | ||
| Item item = r.createFolder("test-item"); | ||
|
|
||
| String id = "id"; | ||
| Credentials creds = new UsernamePasswordCredentialsImpl( | ||
| CredentialsScope.GLOBAL, id, "description", "username", "password"); | ||
| CredentialsProvider.track(item, creds); | ||
| String log = Util.loadFile(new File(tmpDir.getRoot(), logFileName + ".0"), StandardCharsets.UTF_8); | ||
| assertTrue("logged actions: " + log, Pattern.compile(".*test-item.*used credentials '" + id + "'.*", Pattern.DOTALL).matcher(log).matches()); | ||
| } | ||
|
|
||
| @Test | ||
| public void disabledLoggingOptionIsRespected() throws Exception { | ||
| String logFileName = "disabledCredentialUsageIsRespected.log"; | ||
| File logFile = new File(tmpDir.getRoot(), logFileName); | ||
| JenkinsRule.WebClient wc = r.createWebClient(); | ||
| new SimpleAuditTrailPluginConfiguratorHelper(logFile).withLogCredentialsUsage(false).sendConfiguration(r, wc); | ||
|
|
||
| FreeStyleProject job = r.createFreeStyleProject("test-job"); | ||
| String id = "id"; | ||
| Credentials creds = new UsernamePasswordCredentialsImpl( | ||
| CredentialsScope.GLOBAL, id, "description", "username", "password"); | ||
| CredentialsProvider.track(job, creds); | ||
|
|
||
| String log = Util.loadFile(new File(tmpDir.getRoot(), logFileName + ".0"), StandardCharsets.UTF_8); | ||
| assertTrue(log.isEmpty()); | ||
| } | ||
| } |
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
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.