-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1039 from ricekot/sbom-website-pages
Generate add-on SBOM website pages on release
- Loading branch information
Showing
5 changed files
with
272 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
buildSrc/src/main/java/org/zaproxy/gradle/GenerateWebsiteSbomPages.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Zed Attack Proxy (ZAP) and its related class files. | ||
* | ||
* ZAP is an HTTP/HTTPS proxy for assessing web application security. | ||
* | ||
* Copyright 2023 The ZAP Development Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.zaproxy.gradle; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.file.DirectoryProperty; | ||
import org.gradle.api.file.RegularFileProperty; | ||
import org.gradle.api.tasks.InputFile; | ||
import org.gradle.api.tasks.OutputDirectory; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.zaproxy.gradle.website.WebsiteSbomPageGenerator; | ||
import org.zaproxy.zap.utils.ZapXmlConfiguration; | ||
|
||
public abstract class GenerateWebsiteSbomPages extends DefaultTask { | ||
|
||
private static final String ADDON_ELEMENT_PREFIX = "addon_"; | ||
private static final String URL_ELEMENT = ".url"; | ||
private static final String NAME_ELEMENT = ".name"; | ||
|
||
@InputFile | ||
public abstract RegularFileProperty getReleaseState(); | ||
|
||
@InputFile | ||
public abstract RegularFileProperty getZapVersions(); | ||
|
||
@OutputDirectory | ||
public abstract DirectoryProperty getOutputDir(); | ||
|
||
@TaskAction | ||
public void generate() throws Exception { | ||
ReleaseState releaseState = ReleaseState.read(getReleaseState().getAsFile().get()); | ||
List<ReleaseState.AddOnChange> addOns = releaseState.getAddOns(); | ||
if (addOns == null || addOns.isEmpty()) { | ||
return; | ||
} | ||
|
||
Path outputDir = getOutputDir().getAsFile().get().toPath(); | ||
ZapXmlConfiguration zapVersions = | ||
new ZapXmlConfiguration(getZapVersions().getAsFile().get()); | ||
|
||
for (ReleaseState.AddOnChange addOn : addOns) { | ||
if (!addOn.isNewVersion()) { | ||
continue; | ||
} | ||
String addOnId = addOn.getId(); | ||
String url = getString(zapVersions, addOnId, URL_ELEMENT); | ||
if (!url.startsWith("https://github.com/zaproxy/zap-extensions")) { | ||
continue; | ||
} | ||
|
||
String bomUrl = url.substring(0, url.lastIndexOf("/")) + "/bom.json"; | ||
Path bomPath = getTemporaryDir().toPath().resolve(addOnId + ".cdx.json"); | ||
TaskUtils.downloadFile(this, bomUrl, bomPath); | ||
|
||
String pageTitle = getString(zapVersions, addOnId, NAME_ELEMENT) + " Add-on SBOM"; | ||
Path bomPageOutputPath = outputDir.resolve(Path.of("docs", "sbom", addOnId + ".md")); | ||
Files.createDirectories(bomPageOutputPath.getParent()); | ||
WebsiteSbomPageGenerator.generate( | ||
bomPath, | ||
bomUrl, | ||
pageTitle, | ||
addOnId, | ||
addOn.getCurrentVersion(), | ||
outputDir.resolve(bomPageOutputPath)); | ||
} | ||
} | ||
|
||
private static String getString( | ||
ZapXmlConfiguration zapVersions, String addOnId, String element) { | ||
return zapVersions.getString(ADDON_ELEMENT_PREFIX + addOnId + element); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
buildSrc/src/main/java/org/zaproxy/gradle/website/WebsiteSbomPageGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Zed Attack Proxy (ZAP) and its related class files. | ||
* | ||
* ZAP is an HTTP/HTTPS proxy for assessing web application security. | ||
* | ||
* Copyright 2023 The ZAP Development Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.zaproxy.gradle.website; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import java.io.StringWriter; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class WebsiteSbomPageGenerator { | ||
|
||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
private static final String NOTICE = | ||
"This page was automatically generated from the add-on's SBOM."; | ||
|
||
public static void generate( | ||
Path bomPath, | ||
String bomUrl, | ||
String pageTitle, | ||
String addOnId, | ||
String addOnVersion, | ||
Path outputFile) | ||
throws Exception { | ||
PageFrontMatter frontMatter = new PageFrontMatter("sbom", pageTitle, 1); | ||
JsonNode bomJson = MAPPER.readTree(bomPath.toFile()); | ||
List<PageFrontMatter.SbomDataComponent> resultComponents = new ArrayList<>(); | ||
var componentsJsonArray = (ArrayNode) bomJson.get("components"); | ||
List<JsonNode> sortedComponentsList = | ||
StreamSupport.stream(componentsJsonArray.spliterator(), false) | ||
.sorted(Comparator.comparing(jsonNode -> jsonNode.get("name").asText())) | ||
.collect(Collectors.toList()); | ||
for (JsonNode component : sortedComponentsList) { | ||
var licenses = (ArrayNode) component.get("licenses"); | ||
String licensesStr = | ||
StreamSupport.stream(licenses.spliterator(), false) | ||
.map(l -> l.get("license")) | ||
.map( | ||
l -> | ||
l.has("id") | ||
? l.get("id").asText() | ||
: l.has("name") ? l.get("name").asText() : "") | ||
.collect(Collectors.joining(", ")); | ||
resultComponents.add( | ||
new PageFrontMatter.SbomDataComponent( | ||
component.get("name").asText(), | ||
component.get("version").asText(), | ||
licensesStr)); | ||
} | ||
frontMatter.setSbomData( | ||
new PageFrontMatter.SbomData( | ||
bomJson.get("bomFormat").asText(), bomUrl, resultComponents)); | ||
frontMatter.setAddOnData(new PageFrontMatter.AddOnData(addOnId, addOnVersion)); | ||
var writer = new StringWriter(); | ||
frontMatter.writeTo(NOTICE, writer); | ||
Files.write(outputFile, writer.toString().getBytes(StandardCharsets.UTF_8)); | ||
} | ||
} |