|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2026, CloudBees, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package jenkins.telemetry.impl; |
| 26 | + |
| 27 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 28 | +import hudson.Extension; |
| 29 | +import java.time.LocalDate; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.concurrent.ConcurrentHashMap; |
| 32 | +import java.util.concurrent.atomic.AtomicLong; |
| 33 | +import jenkins.telemetry.Telemetry; |
| 34 | +import net.sf.json.JSONArray; |
| 35 | +import net.sf.json.JSONObject; |
| 36 | +import org.kohsuke.accmod.Restricted; |
| 37 | +import org.kohsuke.accmod.restrictions.NoExternalUse; |
| 38 | + |
| 39 | +/** |
| 40 | + * Telemetry implementation gathering information about password field masking. |
| 41 | + */ |
| 42 | +@Extension |
| 43 | +@Restricted(NoExternalUse.class) |
| 44 | +public class PasswordMasking extends Telemetry { |
| 45 | + |
| 46 | + private static final Map<String, AtomicLong> maskingCounts = new ConcurrentHashMap<>(); |
| 47 | + |
| 48 | + /** |
| 49 | + * Records when password masking occurs. |
| 50 | + * |
| 51 | + * @param className the class name of the object |
| 52 | + * @param closestAncestor the closest ancestor class name |
| 53 | + * @param jellyView the Jelly view where masking occurred |
| 54 | + */ |
| 55 | + public static void recordMasking(String className, String closestAncestor, String jellyView) { |
| 56 | + if (Telemetry.isDisabled()) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + String key = className + "|" + closestAncestor + "|" + jellyView; |
| 61 | + maskingCounts.computeIfAbsent(key, k -> new AtomicLong(0)).incrementAndGet(); |
| 62 | + } |
| 63 | + |
| 64 | + @NonNull |
| 65 | + @Override |
| 66 | + public String getDisplayName() { |
| 67 | + return "Password field masking"; |
| 68 | + } |
| 69 | + |
| 70 | + @NonNull |
| 71 | + @Override |
| 72 | + public LocalDate getStart() { |
| 73 | + return LocalDate.of(2026, 1, 26); |
| 74 | + } |
| 75 | + |
| 76 | + @NonNull |
| 77 | + @Override |
| 78 | + public LocalDate getEnd() { |
| 79 | + return LocalDate.of(2026, 7, 26); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public JSONObject createContent() { |
| 84 | + JSONArray events = new JSONArray(); |
| 85 | + for (Map.Entry<String, AtomicLong> entry : maskingCounts.entrySet()) { |
| 86 | + String[] parts = entry.getKey().split("\\|", 3); |
| 87 | + if (parts.length == 3) { |
| 88 | + String className = parts[0]; |
| 89 | + String closestAncestor = parts[1]; |
| 90 | + String jellyView = parts[2]; |
| 91 | + long count = entry.getValue().longValue(); |
| 92 | + |
| 93 | + JSONObject event = new JSONObject(); |
| 94 | + event.put("className", className); |
| 95 | + event.put("closestAncestor", closestAncestor); |
| 96 | + event.put("jellyView", jellyView); |
| 97 | + event.put("count", count); |
| 98 | + events.add(event); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + maskingCounts.clear(); |
| 103 | + |
| 104 | + JSONObject payload = new JSONObject(); |
| 105 | + payload.put("components", buildComponentInformation()); |
| 106 | + payload.put("masking", events); |
| 107 | + |
| 108 | + return payload; |
| 109 | + } |
| 110 | +} |
0 commit comments