-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix OS fingerprinting, add tests, remove dead code
Fix cases where OS fingerprinting differed too much from the old recog-java matching. Added tests for OS fingerprinting. Added test for analyzing an image, including a fake alpine image which only contains the os-release file and installed packages file.
- Loading branch information
1 parent
931778f
commit 3214fc7
Showing
14 changed files
with
236 additions
and
247 deletions.
There are no files selected for viewing
Binary file not shown.
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
121 changes: 121 additions & 0 deletions
121
src/test/java/com/rapid7/container/analyzer/docker/os/FingerprinterTest.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,121 @@ | ||
package com.rapid7.container.analyzer.docker.os; | ||
|
||
import com.rapid7.container.analyzer.docker.model.image.OperatingSystem; | ||
import java.io.IOException; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class FingerprinterTest { | ||
|
||
@Test | ||
void parseAlpine() throws IOException { | ||
// Given | ||
Fingerprinter fp = new Fingerprinter(); | ||
|
||
// When | ||
OperatingSystem os = fp.parse(FingerprinterTest.class.getResourceAsStream("alpine.txt"), "/etc/os-release", "x86_64"); | ||
|
||
// Then | ||
assertEquals("Alpine", os.getVendor()); | ||
assertEquals("3.8.0", os.getVersion()); | ||
assertEquals("Alpine Linux 3.8.0", os.getDescription()); | ||
} | ||
|
||
@Test | ||
void parseAlpineRelease() throws IOException { | ||
// Given | ||
Fingerprinter fp = new Fingerprinter(); | ||
|
||
// When | ||
OperatingSystem os = fp.parse(FingerprinterTest.class.getResourceAsStream("alpine-release.txt"), "/etc/alpine-release", "x86_64"); | ||
|
||
// Then | ||
assertEquals("Alpine", os.getVendor()); | ||
assertEquals("3.8.0", os.getVersion()); | ||
assertEquals("Alpine Linux 3.8.0", os.getDescription()); | ||
} | ||
|
||
@Test | ||
void parseDebian() throws IOException { | ||
// Given | ||
Fingerprinter fp = new Fingerprinter(); | ||
|
||
// When | ||
OperatingSystem os = fp.parse(FingerprinterTest.class.getResourceAsStream("debian.txt"), "/etc/os-release", "x86_64"); | ||
|
||
// Then | ||
assertEquals("Debian", os.getVendor()); | ||
assertEquals("8", os.getVersion()); | ||
assertEquals("Debian Linux 8", os.getDescription()); | ||
} | ||
|
||
@Test | ||
void parseOracle() throws IOException { | ||
// Given | ||
Fingerprinter fp = new Fingerprinter(); | ||
|
||
// When | ||
OperatingSystem os = fp.parse(FingerprinterTest.class.getResourceAsStream("oracle.txt"), "/etc/os-release", "x86_64"); | ||
|
||
// Then | ||
assertEquals("Oracle", os.getVendor()); | ||
assertEquals("7.6", os.getVersion()); | ||
assertEquals("Oracle Linux 7.6", os.getDescription()); | ||
} | ||
|
||
@Test | ||
void parsePhoton() throws IOException { | ||
// Given | ||
Fingerprinter fp = new Fingerprinter(); | ||
|
||
// When | ||
OperatingSystem os = fp.parse(FingerprinterTest.class.getResourceAsStream("photon.txt"), "/etc/os-release", "x86_64"); | ||
|
||
// Then | ||
assertEquals("VMWare", os.getVendor()); | ||
assertEquals("3.0", os.getVersion()); | ||
assertEquals("VMWare Photon Linux 3.0", os.getDescription()); | ||
} | ||
|
||
@Test | ||
void parsePhotonRelease() throws IOException { | ||
// Given | ||
Fingerprinter fp = new Fingerprinter(); | ||
|
||
// When | ||
OperatingSystem os = fp.parse(FingerprinterTest.class.getResourceAsStream("photon-release.txt"), "/etc/photon-release", "x86_64"); | ||
|
||
// Then | ||
assertEquals("VMWare", os.getVendor()); | ||
assertEquals("3.0", os.getVersion()); | ||
assertEquals("VMWare Photon Linux 3.0", os.getDescription()); | ||
} | ||
|
||
@Test | ||
void parseRedhatRelease() throws IOException { | ||
// Given | ||
Fingerprinter fp = new Fingerprinter(); | ||
|
||
// When | ||
OperatingSystem os = fp.parse(FingerprinterTest.class.getResourceAsStream("redhat-release.txt"), "/etc/redhat-release", "x86_64"); | ||
|
||
// Then | ||
assertEquals("Red Hat", os.getVendor()); | ||
assertEquals("7.6", os.getVersion()); | ||
assertEquals("Red Hat Linux 7.6", os.getDescription()); | ||
} | ||
|
||
@Test | ||
void parseUbuntu() throws IOException { | ||
// Given | ||
Fingerprinter fp = new Fingerprinter(); | ||
|
||
// When | ||
OperatingSystem os = fp.parse(FingerprinterTest.class.getResourceAsStream("ubuntu.txt"), "/etc/os-release", "x86_64"); | ||
|
||
// Then | ||
assertEquals("Ubuntu", os.getVendor()); | ||
assertEquals("16.04", os.getVersion()); | ||
assertEquals("Ubuntu Linux 16.04", os.getDescription()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...est/java/com/rapid7/container/analyzer/docker/service/DockerImageAnalyzerServiceTest.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,38 @@ | ||
package com.rapid7.container.analyzer.docker.service; | ||
|
||
import com.rapid7.container.analyzer.docker.model.image.Image; | ||
import com.rapid7.container.analyzer.docker.model.image.ImageId; | ||
import com.rapid7.container.analyzer.docker.model.image.OperatingSystem; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class DockerImageAnalyzerServiceTest { | ||
|
||
@Test | ||
public void test() throws IOException { | ||
// Given | ||
File tarFile = new File("fakealpine.tar"); | ||
ImageId expectedId = new ImageId("sha256:7be494284b1dea6cb2012a5ef99676b4ec22868d9ee235c60e48181542d70fd5"); | ||
OperatingSystem expectedOs = new OperatingSystem("Alpine", "Linux", "Linux", "x86_64", "3.8.0", "Alpine Linux 3.8.0"); | ||
long expectedSize = 119296; | ||
long expectedLayers = 2; | ||
long expectedPackages = 66; | ||
|
||
// When | ||
DockerImageAnalyzerService analyzer = new DockerImageAnalyzerService(null); | ||
Path tmpdir = Files.createTempDirectory("r7dia"); | ||
Image image = analyzer.analyze(tarFile, tmpdir.toString()); | ||
|
||
// Then | ||
assertEquals(expectedId, image.getId()); | ||
assertEquals(expectedOs, image.getOperatingSystem()); | ||
assertEquals(expectedSize, image.getSize()); | ||
assertEquals(expectedLayers, image.getLayers().size()); | ||
assertEquals(expectedPackages, image.getPackages().size()); | ||
} | ||
|
||
} |
Oops, something went wrong.