Skip to content

Commit

Permalink
Merge pull request #26 from yashviagrawal/main
Browse files Browse the repository at this point in the history
Jenkins Plugin with region
  • Loading branch information
ginilpg authored Sep 26, 2024
2 parents 233e66f + 5e39ca6 commit d04cd1b
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 31 deletions.
7 changes: 6 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
buildPlugin()
buildPlugin(
configurations: [
[platform: 'linux', jdk: 21], // Update to the desired JDK version
[platform: 'windows', jdk: 21] // Update to the desired JDK version
]
)
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ stages {
steps {
script {
// Perform Appknox scan using AppknoxScanner
step([
$class: 'AppknoxScanner',
appKnoxScanner(
credentialsId: 'your-appknox-access-token-ID', //Specify the Appknox Access Token ID. Ensure the ID matches with the ID given while configuring Appknox Access Token in the credentials.
filePath: FILE_PATH,
riskThreshold: params.RISK_THRESHOLD.toUpperCase()
])
riskThreshold: params.RISK_THRESHOLD.toUpperCase(),
region: params.Region // Pass the region parameter as expected
)
}
}
Expand All @@ -95,11 +95,12 @@ stages {

## Inputs

| Key | Value |
|-------------------------|------------------------------|
| `credentialsId` | Personal appknox access token ID |
| `file_path` | Specify the build file name or path for the mobile application binary to upload, E.g. app-debug.apk, app/build/apk/app-debug.apk |
| `risk_threshold` | Risk threshold value for which the CI should fail. <br><br>Accepted values: `CRITICAL, HIGH, MEDIUM & LOW` <br><br>Default: `LOW` |
| Key | Value |
|-------------------|------------------------------|
| `credentialsId` | Personal appknox access token ID |
| `file_path` | Specify the build file name or path for the mobile application binary to upload, E.g. app-debug.apk, app/build/apk/app-debug.apk |
| `risk_threshold` | Risk threshold value for which the CI should fail. <br><br>Accepted values: `CRITICAL, HIGH, MEDIUM & LOW` <br><br>Default: `LOW` |
| `region` | Specify the Appknox region. <br><br>Accepted values: 'Global, Saudi' <br><br>Default: 'Global' |

---

Expand All @@ -109,6 +110,7 @@ pipeline {
agent any
parameters {
choice(name: 'RISK_THRESHOLD', choices: ['LOW', 'MEDIUM', 'HIGH', 'CRITICAL'], description: 'Risk Threshold')
choice(name: 'Region', choices: ['global', 'saudi'], description: 'Appknox Regions')
}
stages {
stage('Checkout') {
Expand All @@ -129,17 +131,17 @@ pipeline {
steps {
script {
// Perform Appknox scan using AppknoxScanner
step([
$class: 'AppknoxScanner',
credentialsId: 'your-appknox-access-token-id', //Specify the Appknox Access Token ID. Ensure the ID matches with the ID given while configuring Appknox Access Token in the credentials.
appKnoxScanner(
credentialsId: 'your-appknox-access-token-ID', //Specify the Appknox Access Token ID. Ensure the ID matches with the ID given while configuring Appknox Access Token in the credentials.
filePath: FILE_PATH,
riskThreshold: params.RISK_THRESHOLD.toUpperCase()
])
riskThreshold: params.RISK_THRESHOLD.toUpperCase(),
region: params.Region // Pass the region parameter as expected
)
}
}
}
}
}
```
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.jenkins.plugins;
package io.jenkins.plugins.scanner;

import hudson.Extension;
import hudson.FilePath;
Expand Down Expand Up @@ -57,20 +57,23 @@

import org.apache.commons.io.FileUtils;

@Symbol("appKnoxScanner")
public class AppknoxScanner extends Builder implements SimpleBuildStep {
private final String credentialsId;
private final String filePath;
private final String riskThreshold;
private final String apiHost;

private static final String binaryVersion = "1.3.1";
private static final String binaryVersion = "1.6.0";
private static final String osName = System.getProperty("os.name").toLowerCase();
private static final String CLI_DOWNLOAD_PATH = System.getProperty("user.home") + File.separator + "appknox";

@DataBoundConstructor
public AppknoxScanner(String credentialsId, String filePath, String riskThreshold) {
public AppknoxScanner(String credentialsId, String filePath, String riskThreshold, String apiHost) {
this.credentialsId = credentialsId;
this.filePath = filePath;
this.riskThreshold = riskThreshold;
this.apiHost = apiHost;
}

public String getCredentialsId() {
Expand All @@ -85,6 +88,10 @@ public String getRiskThreshold() {
return riskThreshold;
}

public String getApiHost() {
return apiHost;
}

@Override
public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener)
throws InterruptedException, IOException {
Expand All @@ -111,6 +118,8 @@ private boolean executeAppknoxCommands(Run<?, ?> run, FilePath workspace, String
env.put("APPKNOX_ACCESS_TOKEN", accessToken);
String appknoxPath = downloadAndInstallAppknox(osName, listener);

listener.getLogger().println("Selected Region: " + apiHost);

// Determine if the file is an APK or IPA based on extension
String appFilePath = findAppFilePath(workspace.getRemote(), filePath, listener);

Expand Down Expand Up @@ -214,6 +223,7 @@ private String findAppFilePathRecursive(File dir, String fileName, TaskListener
return null;
}


private String extractFileID(String uploadOutput, TaskListener listener) {
String[] lines = uploadOutput.split("\n");
if (lines.length > 0) {
Expand Down Expand Up @@ -259,7 +269,7 @@ private String downloadAndInstallAppknox(String os, TaskListener listener)
listener.getLogger().println("Appknox CLI already exists at: " + CLI_DOWNLOAD_PATH);
}

addPathToEnvironment(CLI_DOWNLOAD_PATH, listener);
listener.getLogger().println("Appknox CLI located at: " + CLI_DOWNLOAD_PATH);
return CLI_DOWNLOAD_PATH;
}

Expand Down Expand Up @@ -293,12 +303,6 @@ private void downloadFile(String url, String destinationPath, TaskListener liste
}
}

private void addPathToEnvironment(String path, TaskListener listener) {
String existingPath = System.getenv("PATH");
String newPath = path + File.pathSeparator + existingPath;
System.setProperty("PATH", newPath);
}

private String uploadFile(String appknoxPath, TaskListener listener, Map<String, String> env, String appFilePath)
throws IOException, InterruptedException {
String accessToken = getAccessToken(listener);
Expand All @@ -309,6 +313,8 @@ private String uploadFile(String appknoxPath, TaskListener listener, Map<String,
command.add(appknoxPath);
command.add("upload");
command.add(appFilePath);
command.add("--region");
command.add(apiHost);

ProcessBuilder pb = new ProcessBuilder(command);
pb.environment().putAll(env);
Expand Down Expand Up @@ -349,6 +355,8 @@ private boolean runCICheck(String appknoxPath, Run<?, ?> run, String fileID, Tas
command.add(fileID);
command.add("--risk-threshold");
command.add(riskThreshold);
command.add("--region");
command.add(apiHost);

ProcessBuilder pb = new ProcessBuilder(command);
pb.environment().putAll(env);
Expand Down Expand Up @@ -398,6 +406,8 @@ private String createReport(String appknoxPath, String fileID, TaskListener list
command.add("reports");
command.add("create");
command.add(fileID);
command.add("--region");
command.add(apiHost);

ProcessBuilder pb = new ProcessBuilder(command);
pb.environment().putAll(env);
Expand Down Expand Up @@ -439,6 +449,8 @@ private void downloadReportSummaryCSV(String appknoxPath, String reportName, Str
command.add(reportID);
command.add("--output");
command.add(workspace.child(reportName).getRemote());
command.add("--region");
command.add(apiHost);

ProcessBuilder pb = new ProcessBuilder(command);
pb.environment().putAll(env);
Expand Down Expand Up @@ -508,6 +520,14 @@ public String getDisplayName() {
return "Appknox Security Scanner";
}

@POST
public ListBoxModel doFillApiHostItems() {
return new ListBoxModel(
new ListBoxModel.Option("Global", "global"),
new ListBoxModel.Option("Saudi", "saudi")
);
}

@SuppressWarnings("deprecation")
@POST
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath ItemGroup<?> context) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/index.jelly
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?jelly escape-by-default='true'?>
<div>
The Appknox Security Scan Plugin allows you to perform Appknox security scan on your mobile application binary.
This plugin allows you to perform Appknox security scan on your mobile application binary.
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
<option value="CRITICAL" st:bind="true">CRITICAL</option>
</select>
</f:entry>
<f:entry title="${%region}" field="apiHost"> <!-- Still using 'apiHost' as the field name -->
<f:select field="apiHost">
<f:option value="global">Global</f:option>
<f:option value="saudi">Saudi</f:option>
</f:select>
</f:entry>
<f:entry title="${%note}">
<p>${%requiredFields} </p>
</f:entry>
<p>${%requiredFields}</p>
</f:entry>
</f:section>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
filePath = File Path *
note: Note
note = Note
riskThreshold = Risk Threshold *
credentialsId = Appknox Access Token *
requiredFields=* indicates required fields
requiredFields = * indicates required fields
region = Appknox Regions *
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Select a region if you're using a different cloud instance of Appknox than the default global region.
</div>

0 comments on commit d04cd1b

Please sign in to comment.