From 6989acccac68802dd0f1be3606d7c323f3197264 Mon Sep 17 00:00:00 2001 From: "jaehwan.choi" Date: Wed, 4 Oct 2023 14:33:51 +0900 Subject: [PATCH] Fix Class cast exception occurs because VarMaskRegexEntry data is not bound. --- .../MaskPasswordsBuildWrapper.java | 4 ++-- .../maskpasswords/MaskPasswordsConfig.java | 20 +++++++++---------- .../MaskPasswordsConsoleLogFilter.java | 2 +- .../MaskPasswordsWorkflowTest.java | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsBuildWrapper.java b/src/main/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsBuildWrapper.java index 2071629..8ead2d6 100644 --- a/src/main/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsBuildWrapper.java +++ b/src/main/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsBuildWrapper.java @@ -108,7 +108,7 @@ public ConsoleLogFilter createLoggerDecorator(Run build) { // global regexes List globalVarMaskRegexes = config.getGlobalVarMaskRegexesU(); for(MaskPasswordsConfig.VarMaskRegexEntry globalVarMaskRegex: globalVarMaskRegexes) { - allRegexes.add(globalVarMaskRegex.getValue().getRegex()); + allRegexes.add(globalVarMaskRegex.getValue()); } // job's passwords @@ -531,7 +531,7 @@ public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContex continue; } writer.startNode(VAR_MASK_REGEX_NODE); - writer.addAttribute(REGEX_NAME, varMaskRegex.getName()); + writer.addAttribute(REGEX_NAME, varMaskRegex.getKey()); writer.addAttribute(REGEX_ATT, varMaskRegex.getRegexString()); writer.endNode(); } diff --git a/src/main/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsConfig.java b/src/main/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsConfig.java index 0ad5195..b7c7bfd 100644 --- a/src/main/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsConfig.java +++ b/src/main/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsConfig.java @@ -655,18 +655,18 @@ public void setRegex(VarMaskRegex regex) { } public String getKey() { - return this.getName(); + return this.key; } public void setKey(String key) { this.key = key; } - public VarMaskRegex getValue() { - return this.getRegex(); + public String getValue() { + return this.getRegex().getRegex(); } - public void setValue(VarMaskRegex regex) { - this.setRegex(regex); + public void setValue(String regex) { + this.setRegex(new VarMaskRegex(regex)); } public String getRegexString() { @@ -684,7 +684,7 @@ public String toString() { @SuppressFBWarnings(value = "CN_IDIOM_NO_SUPER_CALL", justification = "We do not expect anybody to use this class." + "If they do, they must override clone() as well") public Object clone() { - return new VarMaskRegexEntry(this.getName(), this.getRegex()); + return new VarMaskRegexEntry(this.getKey(), this.getRegex()); } @Override @@ -702,7 +702,7 @@ public boolean equals(Object other) { return false; } else { VarMaskRegexEntry otherE = (VarMaskRegexEntry) other; - return (this.getName().equals(otherE.getName())) && this.getRegex().equals(otherE.getRegex()); + return (this.getKey().equals(otherE.getKey())) && this.getRegex().equals(otherE.getRegex()); } } @@ -717,7 +717,7 @@ public String getDisplayName() { public UninstantiatedDescribable customUninstantiate(@NonNull UninstantiatedDescribable step) { Map arguments = step.getArguments(); Map newMap1 = new HashMap<>(); - newMap1.put("name", arguments.get("name")); + newMap1.put("key", arguments.get("key")); newMap1.put("value", arguments.get("value")); return step.withArguments(newMap1); } @@ -726,8 +726,8 @@ public UninstantiatedDescribable customUninstantiate(@NonNull UninstantiatedDesc @Override public Map customInstantiate(@NonNull Map arguments) { Map newMap = new HashMap<>(); - newMap.put("name", arguments.get("name")); - newMap.put("value", new VarMaskRegex((String)arguments.get("value"))); + newMap.put("key", arguments.get("key")); + newMap.put("value", String.valueOf(arguments.get("value"))); return newMap; } } diff --git a/src/main/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsConsoleLogFilter.java b/src/main/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsConsoleLogFilter.java index 6795143..8cf67cb 100644 --- a/src/main/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsConsoleLogFilter.java +++ b/src/main/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsConsoleLogFilter.java @@ -77,7 +77,7 @@ public OutputStream decorateLogger(Run _ignore, OutputStream logger) throws IOEx // global regexes List globalVarMaskRegexes = config.getGlobalVarMaskRegexesU(); for(MaskPasswordsConfig.VarMaskRegexEntry globalVarMaskRegex: globalVarMaskRegexes) { - regexes.add(globalVarMaskRegex.getValue().getRegex()); + regexes.add(globalVarMaskRegex.getValue()); } return new MaskPasswordsOutputStream(logger, passwords, regexes); } diff --git a/src/test/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsWorkflowTest.java b/src/test/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsWorkflowTest.java index 82623bc..bb6ec99 100644 --- a/src/test/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsWorkflowTest.java +++ b/src/test/java/com/michelin/cio/hudson/plugins/maskpasswords/MaskPasswordsWorkflowTest.java @@ -89,7 +89,7 @@ public void regexConfigRoundTrip() throws Exception { story.then(step -> { MaskPasswordsBuildWrapper bw1 = new MaskPasswordsBuildWrapper( null, - Collections.singletonList(new MaskPasswordsConfig.VarMaskRegexEntry("test", new MaskPasswordsBuildWrapper.VarMaskRegex("foobar"))) + Collections.singletonList(new MaskPasswordsConfig.VarMaskRegexEntry("test", "foobar")) ); CoreWrapperStep step1 = new CoreWrapperStep(bw1); CoreWrapperStep step2 = new StepConfigTester(step).configRoundTrip(step1); @@ -98,7 +98,7 @@ public void regexConfigRoundTrip() throws Exception { assertEquals(1, regexes.size()); MaskPasswordsConfig.VarMaskRegexEntry regex = regexes.get(0); assertEquals("foobar", regex.getRegexString()); - assertEquals("test", regex.getName()); + assertEquals("test", regex.getKey()); }); }