-
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 6 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| -Pconsume-incrementals | ||
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
65 changes: 65 additions & 0 deletions
65
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,65 @@ | ||
| package hudson.plugins.audit_trail; | ||
|
|
||
| import com.cloudbees.plugins.credentials.Credentials; | ||
| import com.cloudbees.plugins.credentials.CredentialsUseListener; | ||
| import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials; | ||
| import hudson.Extension; | ||
| import hudson.model.ModelObject; | ||
|
|
||
| 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 extends CredentialsUseListener { | ||
| @Inject | ||
| AuditTrailPlugin configuration; | ||
|
|
||
| /** | ||
| * Triggered when the {@link com.cloudbees.plugins.credentials.CredentialsProvider} accesses | ||
| * {@link com.cloudbees.plugins.credentials.Credentials} | ||
| * | ||
| * @param c The used Credentials. | ||
| * @param obj The object using the credentials. | ||
| */ | ||
| @Override | ||
| public void onUse(Credentials c, ModelObject obj) { | ||
| if (!configuration.shouldLogCredentialsUsage()) { | ||
| return; | ||
| } | ||
|
|
||
| StringBuilder builder = new StringBuilder(100); | ||
|
|
||
| String objName = obj.toString(); | ||
| String objType = obj.getClass().toString(); | ||
meiswjn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| builder.append("'" + objName | ||
| + "' (" + objType + ") "); | ||
meiswjn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| String credsType = c.getClass().toString(); | ||
|
|
||
| if (c instanceof BaseStandardCredentials) { | ||
| String credsId = ((BaseStandardCredentials) c).getId(); | ||
| builder.append("used credentials '" + credsId + "' (" + credsType + ")."); | ||
| } else { | ||
| String nonDefaultWarning = builder + ("used an unsupported credentials type (" + credsType + | ||
| ") that may potentially not be audit-logged correctly."); | ||
| Logger.getLogger(CredentialUsageListener.class.getName()).log(Level.INFO, null, nonDefaultWarning); | ||
|
|
||
| builder.append("used credentials '" + c + "' (" + credsType + ") (Note: Used fallback method for log as " + | ||
meiswjn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "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
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.