Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devsbom 062023 #49

Merged
merged 3 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/LCT.PackageIdentifier/DebianProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,36 @@ public class DebianProcessor : IParser

public Bom ParsePackageFile(CommonAppSettings appSettings)
{
List<string> configFiles = FolderScanner.FileScanner(appSettings.PackageFilePath, appSettings.Debian);
List<string> configFiles = new();
List<DebianPackage> listofComponents = new List<DebianPackage>();
Bom bom = new Bom();
List<Component> listComponentForBOM;
foreach (string filepath in configFiles)

if (string.IsNullOrEmpty(appSettings.CycloneDxBomFilePath))
{
if (filepath.EndsWith(".xml") || filepath.EndsWith(".json"))
configFiles = FolderScanner.FileScanner(appSettings.PackageFilePath, appSettings.Debian);

foreach (string filepath in configFiles)
{
listofComponents.AddRange(ParseCycloneDX(filepath));
if (filepath.EndsWith(".xml") || filepath.EndsWith(".json"))
{
listofComponents.AddRange(ParseCycloneDX(filepath));
}
}
}
else if (!string.IsNullOrEmpty(appSettings.CycloneDxBomFilePath)) //todo: need to add a folder in the docker , to mount this
{

configFiles = FolderScanner.FileScanner(appSettings.CycloneDxBomFilePath, appSettings.Debian);
foreach (string filepath in configFiles)
{
if (filepath.EndsWith(".xml") || filepath.EndsWith(".json")) //todo: decide the end string . is it only .json or _Bom.cdx.json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider .xml?Add file extension as .cdx.json

{
listofComponents.AddRange(ParseCycloneDX(filepath));
}
}
}
//todo:testing is pending for the new logic addition

int initialCount = listofComponents.Count;
GetDistinctComponentList(ref listofComponents);
Expand Down
69 changes: 69 additions & 0 deletions src/LCT.PackageIdentifier/NpmProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ public List<Component> ParsePackageLockJson(string filepath, CommonAppSettings a
GetComponentsForBom(filepath, appSettings, ref bundledComponents, ref lstComponentForBOM, ref noOfDevDependent, depencyComponentList);
}

// the below logic for angular 16+version due to package-lock.json file format change
if (dependencies == null)
{
var pacakages = jsonDeserialized["packages"];
if (pacakages?.Children() != null)
{
IEnumerable<JProperty> depencyComponentList = pacakages?.Children().OfType<JProperty>();
GetPackagesForBom(filepath, ref bundledComponents, ref lstComponentForBOM, ref noOfDevDependent, depencyComponentList);
}
}

if (appSettings.Npm.ExcludedComponents != null)
{
lstComponentForBOM = CommonHelper.RemoveExcludedComponents(lstComponentForBOM, appSettings.Npm.ExcludedComponents, ref noOfExcludedComponents);
Expand Down Expand Up @@ -117,6 +128,64 @@ public List<Component> ParsePackageLockJson(string filepath, CommonAppSettings a
return lstComponentForBOM;
}

private static void GetPackagesForBom(string filepath, ref List<BundledComponents> bundledComponents, ref List<Component> lstComponentForBOM, ref int noOfDevDependent, IEnumerable<JProperty> depencyComponentList)
{
BomCreator.bomKpiData.ComponentsinPackageLockJsonFile += depencyComponentList.Count();

foreach (JProperty prop in depencyComponentList)
{
if (string.IsNullOrEmpty(prop.Name))
{
BomCreator.bomKpiData.ComponentsinPackageLockJsonFile--;
continue;
}

Component components = new Component();
var properties = JObject.Parse(Convert.ToString(prop.Value));

// ignoring the dev= true components, because they are not needed in clearing
if (IsDevDependency(prop.Value[Dev], ref noOfDevDependent))
{
continue;
}

string folderPath = CommonHelper.TrimEndOfString(filepath, $"\\{FileConstant.PackageLockFileName}");
string packageName = CommonHelper.GetSubstringOfLastOccurance(prop.Name, $"node_modules/");
string componentName = packageName.StartsWith('@') ? packageName.Replace("@", "%40") : packageName;

if (packageName.Contains('@'))
{
components.Group = packageName.Split('/')[0];
components.Name = packageName.Split('/')[1];
}
else
{
components.Name = packageName;
}

components.Description = folderPath;
components.Version = Convert.ToString(properties[Version]);
components.Purl = $"{ApiConstant.NPMExternalID}{componentName}@{components.Version}";
components.BomRef = $"{ApiConstant.NPMExternalID}{componentName}@{components.Version}";

CheckAndAddToBundleComponents(bundledComponents, prop, components);

lstComponentForBOM.Add(components);
lstComponentForBOM = RemoveBundledComponentFromList(bundledComponents, lstComponentForBOM);
}
}

private static void CheckAndAddToBundleComponents(List<BundledComponents> bundledComponents, JProperty prop, Component components)
{
if (prop.Value[Bundled] != null &&
!(bundledComponents.Any(x => x.Name == components.Name && x.Version.ToLowerInvariant() == components.Version)))
{
BundledComponents component = new() { Name = components.Name, Version = components.Version };
bundledComponents.Add(component);
}
}


private void GetComponentsForBom(string filepath, CommonAppSettings appSettings,
ref List<BundledComponents> bundledComponents, ref List<Component> lstComponentForBOM,
ref int noOfDevDependent, IEnumerable<JProperty> depencyComponentList)
Expand Down