-
Notifications
You must be signed in to change notification settings - Fork 17
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 #20 from rapid7/dotnet-parser
Dotnet parser
- Loading branch information
Showing
9 changed files
with
224 additions
and
13 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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/rapid7/container/analyzer/docker/fingerprinter/DotNetFingerprinter.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,33 @@ | ||
package com.rapid7.container.analyzer.docker.fingerprinter; | ||
|
||
import com.rapid7.container.analyzer.docker.analyzer.LayerFileHandler; | ||
import com.rapid7.container.analyzer.docker.model.LayerPath; | ||
import com.rapid7.container.analyzer.docker.model.image.Image; | ||
import com.rapid7.container.analyzer.docker.model.json.Configuration; | ||
import com.rapid7.container.analyzer.docker.packages.DotNetParser; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Paths; | ||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry; | ||
|
||
public class DotNetFingerprinter implements LayerFileHandler { | ||
|
||
private final DotNetParser parser; | ||
|
||
public DotNetFingerprinter(DotNetParser parser) { | ||
this.parser = parser; | ||
} | ||
|
||
@Override | ||
public void handle(String name, TarArchiveEntry entry, InputStream contents, Image image, Configuration configuration, LayerPath layerPath) throws IOException { | ||
if (parser.supports(name, entry)) { | ||
File tmpFile = Paths.get(layerPath.getPath(), name).toFile(); | ||
if (tmpFile.isFile()) { | ||
layerPath.getLayer().addPackages(parser.parse(tmpFile, image.getOperatingSystem() == null | ||
? layerPath.getLayer().getOperatingSystem() : image.getOperatingSystem())); | ||
} | ||
} | ||
} | ||
|
||
} |
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
1 change: 1 addition & 0 deletions
1
src/main/java/com/rapid7/container/analyzer/docker/os/Fingerprinter.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
73 changes: 73 additions & 0 deletions
73
src/main/java/com/rapid7/container/analyzer/docker/packages/DotNetParser.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,73 @@ | ||
package com.rapid7.container.analyzer.docker.packages; | ||
|
||
import com.rapid7.container.analyzer.docker.model.image.OperatingSystem; | ||
import com.rapid7.container.analyzer.docker.model.image.Package; | ||
import com.rapid7.container.analyzer.docker.model.image.PackageType; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.SAXException; | ||
|
||
public class DotNetParser implements PackageParser<File> { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(DotNetParser.class); | ||
private static final Pattern DOT_NET_PATTERN = Pattern.compile(".*(?i)(\\.nuspec)$"); | ||
|
||
@Override | ||
public boolean supports(String name, TarArchiveEntry entry) { | ||
return !entry.isSymbolicLink() && DOT_NET_PATTERN.matcher(name).matches(); | ||
} | ||
|
||
@Override | ||
public Set<Package> parse(File input, OperatingSystem operatingSystem) throws IOException { | ||
|
||
Set<Package> packages = new HashSet<>(); | ||
try { | ||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder db = dbFactory.newDocumentBuilder(); | ||
Document document = db.parse(input); | ||
document.getDocumentElement().normalize(); | ||
|
||
NodeList nodeList = document.getElementsByTagName("package"); | ||
for (int i = 0; i < nodeList.getLength(); i++) { | ||
Node node = nodeList.item(i); | ||
|
||
if (node.getNodeType() == Node.ELEMENT_NODE) { | ||
Element element = (Element) node; | ||
|
||
String source = input.getName(); | ||
String name = getValueForAttribute(element, "id"); | ||
String version = getValueForAttribute(element, "version"); | ||
String description = getValueForAttribute(element, "description"); | ||
packages.add(new Package(source, PackageType.DOTNET, operatingSystem, name, version, description, null, null, null, null)); | ||
} | ||
} | ||
} catch (ParserConfigurationException | SAXException exception) { | ||
LOGGER.error("Could not parse .nuspec file", exception); | ||
} | ||
|
||
return packages; | ||
} | ||
|
||
|
||
private String getValueForAttribute(Element element, String attribute) { | ||
NodeList nodeList = element.getElementsByTagName(attribute); | ||
if (nodeList.getLength() > 0) { | ||
return nodeList.item(0).getTextContent(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...a/com/rapid7/container/analyzer/docker/packages/settings/CustomParserSettingsBuilder.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,49 @@ | ||
package com.rapid7.container.analyzer.docker.packages.settings; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.rapid7.container.analyzer.docker.analyzer.LayerFileHandler; | ||
import com.rapid7.container.analyzer.docker.fingerprinter.DotNetFingerprinter; | ||
import com.rapid7.container.analyzer.docker.model.image.PackageType; | ||
import com.rapid7.container.analyzer.docker.packages.DotNetParser; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class CustomParserSettingsBuilder { | ||
|
||
// Mappings for customer parsers | ||
private static final ImmutableMap<PackageType, LayerFileHandler> FINGERPRINTER_MAPPINGS = ImmutableMap.of( | ||
PackageType.DOTNET, new DotNetFingerprinter(new DotNetParser()) | ||
); | ||
|
||
public static final CustomParserSettingsBuilder ALL = CustomParserSettingsBuilder.builder() | ||
.addFingerprinters(FINGERPRINTER_MAPPINGS.keySet()); | ||
|
||
private final Set<LayerFileHandler> enabledFingerprinters = new HashSet<>(); | ||
|
||
private CustomParserSettingsBuilder() { | ||
} | ||
|
||
public static CustomParserSettingsBuilder builder() { | ||
return new CustomParserSettingsBuilder(); | ||
} | ||
|
||
public CustomParserSettingsBuilder addFingerprinter(PackageType packageType) { | ||
LayerFileHandler handler = FINGERPRINTER_MAPPINGS.get(packageType); | ||
if (handler != null) { | ||
enabledFingerprinters.add(handler); | ||
} | ||
return this; | ||
} | ||
|
||
public CustomParserSettingsBuilder addFingerprinters(Collection<PackageType> packageTypes) { | ||
for (PackageType packageType : packageTypes) { | ||
addFingerprinter(packageType); | ||
} | ||
return this; | ||
} | ||
|
||
public Set<LayerFileHandler> getFingerprinters() { | ||
return enabledFingerprinters; | ||
} | ||
} |
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
Binary file not shown.