Skip to content

Commit 12be30a

Browse files
committed
set CPE of master OS component
Signed-off-by: djcrabhat <djcrabhat@sosimplerecords.com>
1 parent f4fae4b commit 12be30a

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

src/main/java/org/cyclonedx/contrib/com/lmco/efoss/unix/sbom/generator/SBomGenerator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ private static Component createMasterComponent(CommandLine cli) throws SBomExcep
296296
String name = cli.getOptionValue("name");
297297
String group = cli.getOptionValue("group");
298298
String version = cli.getOptionValue("version");
299+
String cpe = cli.getOptionValue("cpe");
299300

300301
if ((!StringUtils.isValid(name)) || (!StringUtils.isValid(version)))
301302
{
@@ -305,6 +306,8 @@ private static Component createMasterComponent(CommandLine cli) throws SBomExcep
305306
name = osUtils.getOsVendor();
306307
if (!StringUtils.isValid(version))
307308
version = osUtils.getOsVersion();
309+
if (!StringUtils.isValid(cpe))
310+
cpe = osUtils.getOsCpe();
308311
}
309312

310313
master = createMasterComponent(imageUrl, name, group, version);

src/main/java/org/cyclonedx/contrib/com/lmco/efoss/unix/sbom/utils/OperatingSystemUtils.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
public class OperatingSystemUtils
3131
{
3232
private static final Logger logger = Logger.getLogger(OperatingSystemUtils.class.getName());
33-
33+
3434
private static final String OS_RELEASE_FILE = "/etc/os-release";
35-
35+
3636
private Map<String, String> osMap = null;
3737

3838
/**
@@ -113,6 +113,25 @@ public String getOsVersion()
113113
return version;
114114
}
115115

116+
/**
117+
* (U) This method is used to get the operating system's CPE, if it has one defined.
118+
*
119+
* @return String the operating system CPE.
120+
*/
121+
public String getOsCpe()
122+
{
123+
String cpe = null;
124+
125+
if (osMap.containsKey("CPE_NAME"))
126+
cpe = osMap.get("CPE_NAME");
127+
if (StringUtils.isValid(cpe))
128+
cpe = CharMatcher.is('\"').trimFrom(cpe);
129+
130+
return cpe;
131+
}
132+
133+
134+
116135
/**
117136
* (U) This method is used to get the operating system. From the /etc/os-release
118137
* file.
@@ -133,12 +152,14 @@ public Map<String, String> getOs()
133152
catch (IOException ioe)
134153
{
135154
String error = "Unable to read file(" + OS_RELEASE_FILE + ") to get the " +
136-
"operating sytem!";
155+
"operating system!";
137156
logger.error(error, ioe);
138157
throw new SBomException(error, ioe);
139158
}
140159
return detailMap;
141160
}
161+
162+
142163

143164
/**
144165
* (U) This method is used to read the contents of the OS file.

src/test/java/org/cyclonedx/contrib/com/lmco/efoss/unix/sbom/utils/OperatingSystemUtilsTest.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,49 @@ void testOsVersion(String file, String expectedOsVersion)
570570
}
571571
}
572572

573+
/**
574+
* (U) Convenience method used to test the reading of the CPE from the
575+
* "/etc/os-release" file.
576+
*
577+
* @param file String value of the contents of the
578+
* "/etc/os-release" file.
579+
* @param expectedCpe String value of the expected CPE.
580+
*/
581+
void testCpe(String file, String expectedCpe)
582+
{
583+
try (InputStream inputStream = OperatingSystemUtilsTest.class.getResourceAsStream(file))
584+
{
585+
String osReleaseFileContents = IOUtils.toString(inputStream);
586+
587+
OperatingSystemUtils osUtils = new OperatingSystemUtils(osReleaseFileContents);
588+
589+
String actualCpe = osUtils.getOsCpe();
590+
591+
if (expectedCpe.equalsIgnoreCase(actualCpe))
592+
watcher.getLogger().debug("Got expected CPE (" +
593+
expectedCpe + ").");
594+
else
595+
watcher.getLogger().debug("Did NOT get expected CPE!\n" +
596+
" Expected: " + expectedCpe + "\n" +
597+
" Acutal: " + actualCpe);
598+
599+
Assert.assertEquals(expectedCpe, actualCpe);
600+
}
601+
catch (IOException ioe)
602+
{
603+
String error = "Our test case failed to read the operating system " +
604+
"etc/os-release file(" + file + ").";
605+
watcher.getLogger().error(error, ioe);
606+
Assert.fail("Unable to read /etc/os-release File (" + file + "). ");
607+
}
608+
catch (Exception e)
609+
{
610+
String error = "Our test case failed unexpectedly.";
611+
watcher.getLogger().error(error, e);
612+
Assert.fail(error);
613+
}
614+
}
615+
573616
/**
574617
* (U) This method is used to test the parsing of the Ubuntu Os File.
575618
*/
@@ -625,6 +668,33 @@ void testReadOsUbuntu()
625668
}
626669
}
627670

671+
/**
672+
* (U) This method is used to test the getting of the OS name from the
673+
* os-release file. For Redhat.
674+
*/
675+
@Test
676+
void getOsCpeRedhat()
677+
{
678+
// @formatter:off
679+
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
680+
// @formatter:on
681+
682+
Date startDate = DateUtils.rightNowDate();
683+
684+
TestUtils.logTestStart(methodName, watcher.getLogger());
685+
686+
String file = "/osReleaseFiles/redhat-os-release.txt";
687+
String expectedCpe = "cpe:/o:redhat:enterprise_linux:8.1:GA";
688+
try
689+
{
690+
testCpe(file, expectedCpe);
691+
}
692+
finally
693+
{
694+
TestUtils.logTestFinish(methodName, startDate, watcher.getLogger());
695+
}
696+
}
697+
628698
private Map<String, String> readOs(String content)
629699
{
630700
Map<String, String> detailMap = new HashMap<>();

0 commit comments

Comments
 (0)