From 70141def75b1d0ca9b48b302a567acea07e7f7af Mon Sep 17 00:00:00 2001 From: "Meiswinkel, Jan SF/HZA-ZC2S" Date: Fri, 25 Mar 2022 13:57:52 +0100 Subject: [PATCH 1/5] Add CredentialsUseListener --- .../credentials/CredentialsProvider.java | 9 +++ .../credentials/CredentialsUseListener.java | 77 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java diff --git a/src/main/java/com/cloudbees/plugins/credentials/CredentialsProvider.java b/src/main/java/com/cloudbees/plugins/credentials/CredentialsProvider.java index 3db354249..cb8a6db63 100644 --- a/src/main/java/com/cloudbees/plugins/credentials/CredentialsProvider.java +++ b/src/main/java/com/cloudbees/plugins/credentials/CredentialsProvider.java @@ -1462,6 +1462,9 @@ public static List trackAll(@NonNull Run build, @NonN } else { LOGGER.log(Level.FINEST, "TrackAll method (Run variant) called but fingerprints disabled by {0}", FINGERPRINT_ENABLED_NAME); } + for (Credentials c : credentials) { + CredentialsUseListener.fireUse(c, build); + } return credentials; } @@ -1561,6 +1564,9 @@ public static List trackAll(@NonNull Node node, @NonN } else { LOGGER.log(Level.FINEST, "TrackAll method (Node variant) called but fingerprints disabled by {0}", FINGERPRINT_ENABLED_NAME); } + for (Credentials c : credentials) { + CredentialsUseListener.fireUse(c, node); + } return credentials; } @@ -1646,6 +1652,9 @@ public static List trackAll(@NonNull Item item, @NonN } else { LOGGER.log(Level.FINEST, "TrackAll method (Item variant) called but fingerprints disabled by {0}", FINGERPRINT_ENABLED_NAME); } + for (Credentials c : credentials) { + CredentialsUseListener.fireUse(c, item); + } return credentials; } diff --git a/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java b/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java new file mode 100644 index 000000000..727fc1549 --- /dev/null +++ b/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java @@ -0,0 +1,77 @@ +package com.cloudbees.plugins.credentials; + +import hudson.ExtensionList; +import hudson.ExtensionPoint; +import hudson.model.AbstractModelObject; +import hudson.model.Item; +import hudson.model.Run; + +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * A Listener to track {@link Credentials } usage. + */ +public abstract class CredentialsUseListener implements ExtensionPoint { + + /** + * Stop listening to {@link CredentialsUseListener}. + */ + public void unregister() { + all().remove(this); + } + + /** + * Called when {@link Credentials} is used by an object, usually either {@link Run} or {@link hudson.model.Node} + * + * @param c The used Credentials. + * @param obj The object using the credentials. Object is usually either {@link Run} or {@link hudson.model.Node} + */ + public abstract void onUse(Credentials c, AbstractModelObject obj); + + /** + * Called when {@link Credentials} is used by an item . + * + * @param c The used Credentials. + * @param item The object using the credentials. + */ + public abstract void onUse(Credentials c, Item item); + + /** + * Fires the {@link #onUse} event to track the object that uses credentials. + */ + public static void fireUse(Credentials c, AbstractModelObject obj) { + for (CredentialsUseListener listener : all()) { + try { + listener.onUse(c, obj); + } catch (ThreadDeath td) { + throw td; + } catch (Throwable t) { + Logger.getLogger(CredentialsUseListener.class.getName()).log(Level.WARNING, null, t); + } + } + } + + /** + * Fires the {@link #onUse} event to track the item that uses credentials. + */ + public static void fireUse(Credentials c, Item item) { + for (CredentialsUseListener listener : all()) { + try { + listener.onUse(c, item); + } catch (ThreadDeath td) { + throw td; + } catch (Throwable t) { + Logger.getLogger(CredentialsUseListener.class.getName()).log(Level.WARNING, null, t); + } + } + } + + /** + * Returns all the registered {@link CredentialsUseListener} descriptors. + */ + public static ExtensionList all() { + return ExtensionList.lookup(CredentialsUseListener.class); + } + +} From d43b53b1b1a488e3fda4c6a24acc1145f139943e Mon Sep 17 00:00:00 2001 From: "Meiswinkel, Jan SF/HZA-ZC2S" Date: Mon, 28 Mar 2022 11:50:34 +0200 Subject: [PATCH 2/5] Simplify CredentialsUseListener by using latest LTS feature --- pom.xml | 4 +- .../credentials/CredentialsUseListener.java | 66 +++---------------- 2 files changed, 11 insertions(+), 59 deletions(-) diff --git a/pom.xml b/pom.xml index ada06ccd2..225c37d6c 100644 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,7 @@ 999999-SNAPSHOT jenkinsci/${project.artifactId}-plugin - 2.319.1 + 2.332.1 8 4.9.3 @@ -89,7 +89,7 @@ io.jenkins.tools.bom - bom-2.319.x + bom-2.332.x 1210.vcd41f6657f03 import pom diff --git a/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java b/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java index 727fc1549..73f2d01d2 100644 --- a/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java +++ b/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java @@ -1,13 +1,9 @@ package com.cloudbees.plugins.credentials; -import hudson.ExtensionList; import hudson.ExtensionPoint; -import hudson.model.AbstractModelObject; -import hudson.model.Item; -import hudson.model.Run; +import hudson.model.ModelObject; +import jenkins.util.Listeners; -import java.util.logging.Level; -import java.util.logging.Logger; /** * A Listener to track {@link Credentials } usage. @@ -15,63 +11,19 @@ public abstract class CredentialsUseListener implements ExtensionPoint { /** - * Stop listening to {@link CredentialsUseListener}. - */ - public void unregister() { - all().remove(this); - } - - /** - * Called when {@link Credentials} is used by an object, usually either {@link Run} or {@link hudson.model.Node} + * Called when {@link Credentials} is read by an object. * * @param c The used Credentials. - * @param obj The object using the credentials. Object is usually either {@link Run} or {@link hudson.model.Node} + * @param obj The object using the credentials. */ - public abstract void onUse(Credentials c, AbstractModelObject obj); - - /** - * Called when {@link Credentials} is used by an item . - * - * @param c The used Credentials. - * @param item The object using the credentials. - */ - public abstract void onUse(Credentials c, Item item); + public abstract void onUse(Credentials c, ModelObject obj); /** * Fires the {@link #onUse} event to track the object that uses credentials. */ - public static void fireUse(Credentials c, AbstractModelObject obj) { - for (CredentialsUseListener listener : all()) { - try { - listener.onUse(c, obj); - } catch (ThreadDeath td) { - throw td; - } catch (Throwable t) { - Logger.getLogger(CredentialsUseListener.class.getName()).log(Level.WARNING, null, t); - } - } + public static void fireUse(Credentials c, ModelObject obj) { + Listeners.notify(CredentialsUseListener.class, true, listener -> { + listener.onUse(c, obj); + }); } - - /** - * Fires the {@link #onUse} event to track the item that uses credentials. - */ - public static void fireUse(Credentials c, Item item) { - for (CredentialsUseListener listener : all()) { - try { - listener.onUse(c, item); - } catch (ThreadDeath td) { - throw td; - } catch (Throwable t) { - Logger.getLogger(CredentialsUseListener.class.getName()).log(Level.WARNING, null, t); - } - } - } - - /** - * Returns all the registered {@link CredentialsUseListener} descriptors. - */ - public static ExtensionList all() { - return ExtensionList.lookup(CredentialsUseListener.class); - } - } From 735747f77414306d64b3a5e31b2eee88eeba06d8 Mon Sep 17 00:00:00 2001 From: "Meiswinkel, Jan SF/HZA-ZC2S" Date: Tue, 29 Mar 2022 13:00:00 +0200 Subject: [PATCH 3/5] Add @see links --- .../credentials/CredentialsUseListener.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java b/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java index 73f2d01d2..3ec8f416a 100644 --- a/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java +++ b/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java @@ -1,12 +1,20 @@ package com.cloudbees.plugins.credentials; import hudson.ExtensionPoint; +import hudson.model.Item; import hudson.model.ModelObject; +import hudson.model.Node; +import hudson.model.Run; import jenkins.util.Listeners; +import java.util.List; + /** - * A Listener to track {@link Credentials } usage. + * A Listener to track {@link Credentials} usage. + * @see CredentialsProvider#trackAll(Item, List) + * @see CredentialsProvider#trackAll(Run, List) + * @see CredentialsProvider#trackAll(Node, List) */ public abstract class CredentialsUseListener implements ExtensionPoint { @@ -20,10 +28,11 @@ public abstract class CredentialsUseListener implements ExtensionPoint { /** * Fires the {@link #onUse} event to track the object that uses credentials. + * @see CredentialsProvider#trackAll(Item, List) + * @see CredentialsProvider#trackAll(Run, List) + * @see CredentialsProvider#trackAll(Node, List) */ public static void fireUse(Credentials c, ModelObject obj) { - Listeners.notify(CredentialsUseListener.class, true, listener -> { - listener.onUse(c, obj); - }); + Listeners.notify(CredentialsUseListener.class, true, listener -> listener.onUse(c, obj)); } } From b91f8e9cdb07140f6024658de6257f55539cbe53 Mon Sep 17 00:00:00 2001 From: "Meiswinkel, Jan SF/HZA-ZC2S" Date: Tue, 29 Mar 2022 13:42:39 +0200 Subject: [PATCH 4/5] Change CredentialsUseListener to Interface --- .../plugins/credentials/CredentialsUseListener.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java b/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java index 3ec8f416a..a3ecf9595 100644 --- a/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java +++ b/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java @@ -16,7 +16,7 @@ * @see CredentialsProvider#trackAll(Run, List) * @see CredentialsProvider#trackAll(Node, List) */ -public abstract class CredentialsUseListener implements ExtensionPoint { +public interface CredentialsUseListener extends ExtensionPoint { /** * Called when {@link Credentials} is read by an object. @@ -24,7 +24,7 @@ public abstract class CredentialsUseListener implements ExtensionPoint { * @param c The used Credentials. * @param obj The object using the credentials. */ - public abstract void onUse(Credentials c, ModelObject obj); + void onUse(Credentials c, ModelObject obj); /** * Fires the {@link #onUse} event to track the object that uses credentials. @@ -32,7 +32,7 @@ public abstract class CredentialsUseListener implements ExtensionPoint { * @see CredentialsProvider#trackAll(Run, List) * @see CredentialsProvider#trackAll(Node, List) */ - public static void fireUse(Credentials c, ModelObject obj) { + static void fireUse(Credentials c, ModelObject obj) { Listeners.notify(CredentialsUseListener.class, true, listener -> listener.onUse(c, obj)); } } From 342e74259c71c5aff06896cc18e6a0a07818e3b6 Mon Sep 17 00:00:00 2001 From: "Meiswinkel, Jan SF/HZA-ZC2S" Date: Thu, 31 Mar 2022 09:58:39 +0200 Subject: [PATCH 5/5] Make listener more type-specific --- .../credentials/CredentialsUseListener.java | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java b/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java index a3ecf9595..a56ed60b2 100644 --- a/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java +++ b/src/main/java/com/cloudbees/plugins/credentials/CredentialsUseListener.java @@ -19,20 +19,50 @@ public interface CredentialsUseListener extends ExtensionPoint { /** - * Called when {@link Credentials} is read by an object. + * Called when {@link Credentials} is read by a run. * * @param c The used Credentials. - * @param obj The object using the credentials. + * @param run The run using the credentials. */ - void onUse(Credentials c, ModelObject obj); + void onUse(Credentials c, Run run); /** - * Fires the {@link #onUse} event to track the object that uses credentials. - * @see CredentialsProvider#trackAll(Item, List) + * Called when {@link Credentials} is read by a node. + * + * @param c The used Credentials. + * @param node The node using the credentials. + */ + void onUse(Credentials c, Node node); + + /** + * Called when {@link Credentials} is read by an item. + * + * @param c The used Credentials. + * @param item The item using the credentials. + */ + void onUse(Credentials c, Item item); + + /** + * Fires the {@link #onUse} event to track the run that uses credentials. * @see CredentialsProvider#trackAll(Run, List) + */ + static void fireUse(Credentials c, Run run) { + Listeners.notify(CredentialsUseListener.class, true, listener -> listener.onUse(c, run)); + } + + /** + * Fires the {@link #onUse} event to track the node that uses credentials. * @see CredentialsProvider#trackAll(Node, List) */ - static void fireUse(Credentials c, ModelObject obj) { - Listeners.notify(CredentialsUseListener.class, true, listener -> listener.onUse(c, obj)); + static void fireUse(Credentials c, Node node) { + Listeners.notify(CredentialsUseListener.class, true, listener -> listener.onUse(c, node)); + } + + /** + * Fires the {@link #onUse} event to track the item that uses credentials. + * @see CredentialsProvider#trackAll(Item, List) + */ + static void fireUse(Credentials c, Item item) { + Listeners.notify(CredentialsUseListener.class, true, listener -> listener.onUse(c, item)); } }