Skip to content

Commit 8186ee4

Browse files
committed
- fixed the jenkins comments by added SuppressWarnings annotation.
1 parent d2bb0da commit 8186ee4

File tree

7 files changed

+76
-25
lines changed

7 files changed

+76
-25
lines changed

src/main/java/io/jenkins/plugins/vigilnz/build/SecurityCheckBuilder.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
import hudson.tasks.Builder;
1313
import hudson.util.FormValidation;
1414
import hudson.util.ListBoxModel;
15-
import hudson.util.Secret;
15+
import com.fasterxml.jackson.core.JsonProcessingException;
16+
import com.fasterxml.jackson.databind.ObjectMapper;
1617
import io.jenkins.cli.shaded.org.apache.commons.lang.StringUtils;
1718
import io.jenkins.plugins.vigilnz.api.ApiService;
1819
import io.jenkins.plugins.vigilnz.credentials.TokenCredentials;
20+
import io.jenkins.plugins.vigilnz.models.ApiResponse;
1921
import io.jenkins.plugins.vigilnz.ui.ScanResultAction;
2022
import jenkins.model.Jenkins;
2123
import org.kohsuke.stapler.AncestorInPath;
@@ -31,7 +33,6 @@
3133
// This file for Jenkins FreeStyle Job Method
3234
public class SecurityCheckBuilder extends Builder {
3335

34-
/** Credential ID (identifier to look up the actual credential, not sensitive) */
3536
private final String credentialsId;
3637
private String targetFile; // Optional parameter
3738
private boolean cveScan;
@@ -104,12 +105,14 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
104105
// Validate at least one scan type is selected
105106
if (scanTypes.isEmpty()) {
106107
listener.error("Error: At least one scan type must be selected.");
108+
attachResult(build, buildErrorResponse("At least one scan type must be selected."));
107109
return false;
108110
}
109111

110112
// Validate credentials ID is provided
111113
if (credentialsId == null || credentialsId.trim().isEmpty()) {
112114
listener.error("Error: Credentials ID is required. Please select a credential in the build step configuration.");
115+
attachResult(build, buildErrorResponse("Credentials ID is required."));
113116
return false;
114117
}
115118

@@ -122,6 +125,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
122125

123126
if (creds == null) {
124127
listener.error("Error: Vigilnz Token credential not found with ID: " + credentialsId);
128+
attachResult(build, buildErrorResponse("Vigilnz Token credential not found with ID: " + credentialsId));
125129
return false;
126130
}
127131
// Get the actual token value from the credential
@@ -143,6 +147,8 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
143147
build.addAction(new ScanResultAction(result));
144148
} catch (Exception e) {
145149
listener.error("Scan failed");
150+
attachResult(build, buildErrorResponse("Scan failed: " + e.getMessage()));
151+
build.addAction(new ScanResultAction(new ApiResponse().toString()));
146152
return false;
147153
}
148154

@@ -223,4 +229,21 @@ public FormValidation doCheckScanType(@AncestorInPath Item project, @QueryParame
223229

224230
}
225231

232+
private void attachResult(AbstractBuild build, String json) {
233+
try {
234+
build.addAction(new ScanResultAction(json));
235+
} catch (Exception ignored) {
236+
// Swallow to avoid masking original error
237+
}
238+
}
239+
240+
private String buildErrorResponse(String message) {
241+
ApiResponse resp = new ApiResponse();
242+
resp.setMessage(message);
243+
try {
244+
return new ObjectMapper().writeValueAsString(resp);
245+
} catch (JsonProcessingException e) {
246+
return "{\"message\":\"" + message.replace("\"", "\\\"") + "\"}";
247+
}
248+
}
226249
}

src/main/java/io/jenkins/plugins/vigilnz/credentials/TokenCredentials.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
import org.kohsuke.stapler.QueryParameter;
1515
import org.kohsuke.stapler.verb.POST;
1616

17-
@SuppressWarnings("lgtm[jenkins/password-in-field]")
1817
public class TokenCredentials extends BaseStandardCredentials {
1918

2019
private final Secret token;
2120

21+
@SuppressWarnings("lgtm[jenkins/plaintext-storage]")
2222
private final String tokenId;
2323

24+
@SuppressWarnings("lgtm[jenkins/plaintext-storage]")
2425
private final String tokenDescription;
2526

2627
@DataBoundConstructor

src/main/java/io/jenkins/plugins/vigilnz/pipeline/PipelineStep.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,10 @@
1313
import java.util.List;
1414
import java.util.Set;
1515

16-
/**
17-
* Pipeline step for Vigilnz security scans.
18-
* Security: The 'credentialsId' field stores only a credential ID (identifier), not the actual token value.
19-
* The actual token is stored securely in TokenCredentials using Secret.
20-
*/
16+
2117
public class PipelineStep extends Step {
2218

23-
/**
24-
* Credential ID (not sensitive - just an identifier to look up the actual credential).
25-
* The actual token is stored securely in TokenCredentials using Secret.
26-
*/
19+
2720
private final String credentialsId;
2821
private final List<String> scanTypes;
2922
private String targetFile; // Optional parameter
@@ -34,11 +27,6 @@ public PipelineStep(String credentialsId, List<String> scanTypes) {
3427
this.scanTypes = scanTypes != null ? scanTypes : List.of();
3528
}
3629

37-
@DataBoundSetter
38-
public void setTargetFile(String targetFile) {
39-
this.targetFile = targetFile;
40-
}
41-
4230
public String getCredentialsId() {
4331
return credentialsId;
4432
}
@@ -47,6 +35,11 @@ public String getTargetFile() {
4735
return targetFile;
4836
}
4937

38+
@DataBoundSetter
39+
public void setTargetFile(String targetFile) {
40+
this.targetFile = targetFile;
41+
}
42+
5043
public List<String> getScanTypes() {
5144
return scanTypes != null ? scanTypes : List.of();
5245
}

src/main/java/io/jenkins/plugins/vigilnz/pipeline/PipelineStepExecution.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import hudson.FilePath;
77
import hudson.model.Run;
88
import hudson.model.TaskListener;
9+
import com.fasterxml.jackson.core.JsonProcessingException;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
911
import io.jenkins.plugins.vigilnz.api.ApiService;
1012
import io.jenkins.plugins.vigilnz.credentials.TokenCredentials;
13+
import io.jenkins.plugins.vigilnz.models.ApiResponse;
1114
import io.jenkins.plugins.vigilnz.ui.ScanResultAction;
1215
import org.jenkinsci.plugins.workflow.steps.StepContext;
1316
import org.jenkinsci.plugins.workflow.steps.StepExecution;
@@ -76,16 +79,17 @@ public boolean start() throws Exception {
7679

7780
TaskListener listener = getContext().get(TaskListener.class);
7881
Run<?, ?> run = getContext().get(Run.class);
79-
82+
8083
String credentialsId = step.getCredentialsId();
81-
84+
8285
// Validate credentials ID is provided
8386
if (credentialsId == null || credentialsId.trim().isEmpty()) {
8487
listener.error("Error: Credentials ID is required. Please provide a credential ID in the pipeline step.");
88+
attachResult(run, buildErrorResponse("Credentials ID is required."));
8589
getContext().onFailure(new AbortException("Credentials ID is required"));
8690
return false;
8791
}
88-
92+
8993
TokenCredentials creds =
9094
CredentialsProvider.findCredentialById(
9195
credentialsId,
@@ -112,6 +116,7 @@ public boolean start() throws Exception {
112116
// Validate at least one scan type is selected
113117
if (scanTypes == null || scanTypes.isEmpty()) {
114118
listener.error("Error: At least one scan type must be selected.");
119+
attachResult(run, buildErrorResponse("At least one scan type must be selected."));
115120
getContext().onFailure(new AbortException("At least one scan type must be selected"));
116121
return false;
117122
}
@@ -124,17 +129,37 @@ public boolean start() throws Exception {
124129
run.addAction(new ScanResultAction(result));
125130
} catch (Exception e) {
126131
listener.error("Scan failed");
132+
attachResult(run, buildErrorResponse("Scan failed: " + e.getMessage()));
127133
getContext().onFailure(new AbortException("Scan failed"));
128134
return false;
129135
}
130136

131137
} else {
132138
listener.error("Error: Vigilnz Token credential not found with ID: " + credentialsId);
139+
attachResult(run, buildErrorResponse("Vigilnz Token credential not found with ID: " + credentialsId));
133140
getContext().onFailure(new AbortException("No Vigilnz Token credential found with ID: " + credentialsId));
134141
return false;
135142
}
136143

137144
getContext().onSuccess(null);
138145
return true;
139146
}
147+
148+
private void attachResult(Run<?, ?> run, String json) {
149+
try {
150+
run.addAction(new ScanResultAction(json));
151+
} catch (Exception ignored) {
152+
// Swallow to avoid masking original error
153+
}
154+
}
155+
156+
private String buildErrorResponse(String message) {
157+
ApiResponse resp = new ApiResponse();
158+
resp.setMessage(message);
159+
try {
160+
return new ObjectMapper().writeValueAsString(resp);
161+
} catch (JsonProcessingException e) {
162+
return "{\"message\":\"" + message.replace("\"", "\\\"") + "\"}";
163+
}
164+
}
140165
}
File renamed without changes.

src/main/resources/io/jenkins/plugins/vigilnz/credentials/TokenCredentials/config.jelly

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
33

44
<f:entry title="Token" field="token">
5-
<f:password/>
5+
<f:password checkUrl="${descriptor.checkToken}" clazz="passwordClass"/>
6+
<script>
7+
console.log("The script is calling")
8+
const element = document.querySelector(".passwordClass");
9+
element.addEventListener("blur", myFunction);
10+
function myFunction() {
11+
// Manually trigger the onchange event
12+
const event = new Event("change", { bubbles: true });
13+
element.dispatchEvent(event);
14+
}
15+
</script>
616
</f:entry>
717

818
<f:entry field="tokenId" title="ID">
@@ -14,12 +24,11 @@
1424
value="${instance.tokenId != null and instance.tokenId != '' ? instance.tokenId : instance.id}"/>
1525
<!-- Display as read-only -->
1626
<f:readOnlyTextbox value="${idValue}"/>
17-
<!-- Submit the actual ID as hidden field (not using field="tokenId" to avoid conflict) -->
18-
<input type="hidden" name="tokenId" value="${idValue}"/>
27+
1928
</j:when>
2029
<!-- New credential: allow user to enter ID (optional) -->
2130
<j:otherwise>
22-
<f:textbox field="tokenId"/>
31+
<f:textbox/>
2332
</j:otherwise>
2433
</j:choose>
2534
</f:entry>

src/main/resources/io/jenkins/plugins/vigilnz/ui/ScanResultAction/index.jelly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<l:layout title="Vigilnz Security Scan Results">
44
<l:main-panel>
55
<h1 style="display:flex; align-items:center; gap:10px;font-family: 'Helvetica', sans-serif;">
6-
<l:icon src="symbol-vigilnz plugin-vigilnz-security" class="icon-xlg"/>
6+
<l:icon src="symbol-vigilnz plugin-vigilnz-security" style="width:45px;height:45px;" class="icon-xlg"/>
77
Vigilnz Security Scan Results
88
</h1>
99

0 commit comments

Comments
 (0)