Skip to content

Commit

Permalink
Updated package identifier with requested changes UT and IT
Browse files Browse the repository at this point in the history
  • Loading branch information
ragavareddychalapala committed Jan 10, 2025
1 parent be6f263 commit 41be36c
Show file tree
Hide file tree
Showing 28 changed files with 205 additions and 185 deletions.
30 changes: 3 additions & 27 deletions src/LCT.Common/CommonAppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public string ProjectType
m_ProjectType = value;
}
}
public bool MultipleProjectType { get; set; } = false;
public SW360 SW360 { get; set; }
public Directory Directory { get; set; }
public Jfrog Jfrog { get; set; }
Expand Down Expand Up @@ -212,8 +213,7 @@ public class Directory
private readonly IFolderAction folderAction;
private readonly IFileOperations _fileOperations;
private string m_InputFolder;
private string m_OutputFolder;
private string m_BomFilePath;
private string m_OutputFolder;

public Directory(IFolderAction folderAction, IFileOperations fileOperations)
{
Expand All @@ -236,30 +236,7 @@ public string InputFolder
}
}
}

public string BomFilePath
{
get
{
return m_BomFilePath;
}
set
{
if (AppDomain.CurrentDomain.FriendlyName.Contains("PackageIdentifier"))
{
if (!string.IsNullOrEmpty(value))
{
m_BomFilePath = value;
_fileOperations.ValidateFilePath(m_BomFilePath);
}
}
else
{
m_BomFilePath = value;
_fileOperations.ValidateFilePath(m_BomFilePath);
}
}
}

public string OutputFolder
{
get
Expand All @@ -280,7 +257,6 @@ public string OutputFolder
}
}

public string CycloneDxSBomTemplatePath { get; set; }
}

}
16 changes: 15 additions & 1 deletion src/LCT.Common/CommonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,21 @@ public static bool IsAzureDevOpsDebugEnabled()
public static List<Component> RemoveExcludedComponents(List<Component> ComponentList, List<string> ExcludedComponents, ref int noOfExcludedComponents)
{
List<Component> ExcludedList = new List<Component>();
foreach (string excludedComponent in ExcludedComponents)
List<string> ExcludedComponentsFromPurl = ExcludedComponents?.Where(ec => ec.StartsWith("pkg:")).ToList();
List<string> otherExcludedComponents = ExcludedComponents?.Where(ec => !ec.StartsWith("pkg:")).ToList();

foreach (string excludedComponent in ExcludedComponentsFromPurl)
{
foreach (var component in ComponentList)
{
if (component.Purl != null && component.Purl.Equals(excludedComponent, StringComparison.OrdinalIgnoreCase))
{
noOfExcludedComponents++;
ExcludedList.Add(component);
}
}
}
foreach (string excludedComponent in otherExcludedComponents)
{
string[] excludedcomponent = excludedComponent.ToLower().Split(':');
foreach (var component in ComponentList)
Expand Down
2 changes: 1 addition & 1 deletion src/LCT.Common/Constants/Dataconstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class Dataconstant
{"NUGET", "pkg:nuget"},
{"DEBIAN", "pkg:deb/debian"},
{"MAVEN", "pkg:maven"},
{"PYTHON", "pkg:pypi"},
{"POETRY", "pkg:pypi"},
{"CONAN", "pkg:conan"},
{"ALPINE", "pkg:apk/alpine"},
};
Expand Down
33 changes: 26 additions & 7 deletions src/LCT.Common/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,13 @@ public void CheckRequiredArgsToRun(CommonAppSettings appSettings, string current
"Directory.OutputFolder",
"ProjectType"
};
// Check if ProjectType contains a value and add InternalRepos key accordingly
//Check if ProjectType contains a value and add InternalRepos key accordingly
if (!string.IsNullOrWhiteSpace(appSettings.ProjectType))
{
identifierReqParameters.Add($"{appSettings.ProjectType}.Artifactory.InternalRepos");
if (!appSettings.ProjectType.Equals("ALPINE", StringComparison.InvariantCultureIgnoreCase))
{
identifierReqParameters.Add($"{appSettings.ProjectType}.Artifactory.InternalRepos");
}
}
CheckForMissingParameter(appSettings, properties, identifierReqParameters);
}
Expand Down Expand Up @@ -180,15 +183,31 @@ private static void CheckForMissingParameter(CommonAppSettings appSettings, Prop
break;
}

property = currentObject.GetType().GetProperty(part);
property = currentObject.GetType().GetProperty(part, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
currentObject = property?.GetValue(currentObject);
}

string value = currentObject?.ToString();

if (string.IsNullOrWhiteSpace(value))
if (currentObject is Array array)
{
if (array.Length == 0 || string.IsNullOrWhiteSpace(array.GetValue(0)?.ToString()))
{
missingParameters.Append(key + "\n");
}
}
else if (currentObject is IList<object> list)
{
missingParameters.Append(key + "\n");
if (list.Count == 0 || string.IsNullOrWhiteSpace(list[0]?.ToString()))
{
missingParameters.Append(key + "\n");
}
}
else
{
string value = currentObject?.ToString();
if (string.IsNullOrWhiteSpace(value))
{
missingParameters.Append(key + "\n");
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/LCT.Common/appSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{
"TimeOut": 400,
"ProjectType": "<Insert ProjectType>",
"MultipleProjectType": false,
"SW360": {
"URL": "<Insert SW360URL>",
"ProjectName": "<Insert SW360 Project Name>",
Expand All @@ -24,7 +25,7 @@
"Directory": {
"InputFolder": "/PathToInputDirectory", //Input File path. For Docker run set as /mnt/Input
"OutputFolder": "/PathToOutputDirectory", //Output Bom Folder path. For Docker run set as /mnt/Output
"BomFilePath": "/<OutputFolder>/<SW360 Project Name>_Bom.cdx.json", //For multiple project type
"BomFilePath": "", //For multiple project type
"CycloneDxSBomTemplatePath": ""//CycloneDxBomFilePath: For Providing Customer maintained SBOM as input.Can be used along with Packagefilepath or individually
},
"Jfrog": {
Expand Down
6 changes: 2 additions & 4 deletions src/LCT.PackageIdentifier.UTest/AlpineParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ public void ParsePackageConfig_GivenAInputFilePathAlongWithSBOMTemplate_ReturnTo
SW360 = new SW360() { IgnoreDevDependency = true },
Directory = new LCT.Common.Directory(folderAction, fileOperations)
{
InputFolder = OutFolder + @"\PackageIdentifierUTTestFiles",
CycloneDxSBomTemplatePath = packagefilepath + "\\SBOMTemplates\\SBOM_AlpineCATemplate.cdx.json"
InputFolder = OutFolder + @"\PackageIdentifierUTTestFiles",
}
};

Expand Down Expand Up @@ -206,8 +205,7 @@ public void ParsePackageConfig_GivenAInputFilePathAlongWithSBOMTemplate_ReturnUp
SW360 = new SW360() { IgnoreDevDependency = true },
Directory = new LCT.Common.Directory(folderAction, fileOperations)
{
InputFolder = packagefilepath,
CycloneDxSBomTemplatePath = packagefilepath + "\\SBOMTemplates\\SBOMTemplate_Alpine.cdx.json"
InputFolder = packagefilepath
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/LCT.PackageIdentifier.UTest/DebianParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public void ParsePackageConfig_GivenAInputFilePathAlongWithSBOMTemplate_ReturnTo
Directory = new LCT.Common.Directory(folderAction, fileOperations)
{
InputFolder = packagefilepath,
CycloneDxSBomTemplatePath= packagefilepath + "\\SBOMTemplates\\SBOM_DebianCATemplate.cdx.json"

}
};

Expand Down Expand Up @@ -290,7 +290,7 @@ public void ParsePackageConfig_GivenAInputFilePathAlongWithSBOMTemplate_ReturnUp
Directory = new LCT.Common.Directory(folderAction, fileOperations)
{
InputFolder = packagefilepath,
CycloneDxSBomTemplatePath = packagefilepath + "\\SBOMTemplates\\SBOMTemplate_Debian.cdx.json"

}
};

Expand Down
4 changes: 2 additions & 2 deletions src/LCT.PackageIdentifier.UTest/MavenParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public void ParsePackageFile_GivenAInputFilePathAlongWithSBOMTemplate_ReturnTota
Directory = new LCT.Common.Directory(folderAction, fileOperations)
{
InputFolder = filepath,
CycloneDxSBomTemplatePath= filepath + "\\SBOMTemplates\\SBOM_MavenCATemplate.cdx.json"

}
};

Expand Down Expand Up @@ -523,7 +523,7 @@ public void ParsePackageFile_GivenAInputFilePathAlongWithSBOMTemplate_ReturnUpda
Directory = new LCT.Common.Directory(folderAction, fileOperations)
{
InputFolder = filepath,
CycloneDxSBomTemplatePath = filepath + "\\SBOMTemplates\\SBOMTemplate_Maven.cdx.json"

}
};

Expand Down
4 changes: 2 additions & 2 deletions src/LCT.PackageIdentifier.UTest/NPMParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public void ParseCycloneDXFile_GivenAInputFilePathAlongWithSBOMTemplate_ReturnTo
{
InputFolder = packagefilepath,
OutputFolder = outFolder,
CycloneDxSBomTemplatePath = packagefilepath + "\\SBOMTemplates\\SBOM_NpmCATemplate.cdx.json"

}
};

Expand Down Expand Up @@ -214,7 +214,7 @@ public void ParseCycloneDXFile_GivenAInputFilePathAlongWithSBOMTemplate_ReturnUp
{
InputFolder = packagefilepath,
OutputFolder = outFolder,
CycloneDxSBomTemplatePath = packagefilepath + "\\SBOMTemplates\\SBOMTemplate_Npm.cdx.json"

}
};

Expand Down
4 changes: 2 additions & 2 deletions src/LCT.PackageIdentifier.UTest/PythonParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void ParseCycloneDXFile_GivenAInputFilePathAlongWithSBOMTemplate_ReturnTo
Directory = new LCT.Common.Directory(folderAction, fileOperations)
{
InputFolder = OutFolder + @"\PackageIdentifierUTTestFiles",
CycloneDxSBomTemplatePath = packagefilepath + "\\SBOMTemplates\\SBOM_PythonCATemplate.cdx.json"

}
};

Expand Down Expand Up @@ -204,7 +204,7 @@ public void ParseCycloneDXFile_GivenAInputFilePathAlongWithSBOMTemplate_ReturnUp
Directory = new LCT.Common.Directory(folderAction, fileOperations)
{
InputFolder = packagefilepath,
CycloneDxSBomTemplatePath = packagefilepath + "\\SBOMTemplates\\SBOMTemplate_Python.cdx.json"

}
};

Expand Down
2 changes: 1 addition & 1 deletion src/LCT.PackageIdentifier.UTest/SBomTemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void AddComponentDetails_InputTemplateDetails_ReturnsTemplateWithDetailsu
SbomTemplate.AddComponentDetails(componentsForBOM, templateDetails);

//Assert
Assert.That(BomCreator.bomKpiData.ComponentsUpdatedFromSBOMTemplateFile, Is.EqualTo(3));
Assert.That(BomCreator.bomKpiData.ComponentsUpdatedFromSBOMTemplateFile, Is.EqualTo(1));
}
}
}
26 changes: 14 additions & 12 deletions src/LCT.PackageIdentifier/AlpineProcesser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,19 @@ public Bom ParsePackageFile(CommonAppSettings appSettings)

foreach (string filepath in configFiles)
{
Logger.Debug($"ParsePackageFile():FileName: " + filepath);
listofComponents.AddRange(ParseCycloneDX(filepath, dependenciesForBOM));
if (filepath.EndsWith(FileConstant.SBOMTemplateFileExtension))
{
Bom templateDetails;
templateDetails = CycloneDXBomParser.ExtractSBOMDetailsFromTemplate(_cycloneDXBomParser.ParseCycloneDXBom(filepath));
CycloneDXBomParser.CheckValidComponentsForProjectType(templateDetails.Components, appSettings.ProjectType);
//Adding Template Component Details & MetaData
SbomTemplate.AddComponentDetails(bom.Components, templateDetails);
}
else
{
Logger.Debug($"ParsePackageFile():FileName: " + filepath);
listofComponents.AddRange(ParseCycloneDX(filepath, dependenciesForBOM));
}
}

int initialCount = listofComponents.Count;
Expand All @@ -56,16 +67,7 @@ public Bom ParsePackageFile(CommonAppSettings appSettings)
BomCreator.bomKpiData.DuplicateComponents = initialCount - listComponentForBOM.Count;

bom.Components = listComponentForBOM;
bom.Dependencies = dependenciesForBOM;

if (File.Exists(appSettings.Directory.CycloneDxSBomTemplatePath) && appSettings.Directory.CycloneDxSBomTemplatePath.EndsWith(FileConstant.SBOMTemplateFileExtension))
{
Bom templateDetails;
templateDetails = CycloneDXBomParser.ExtractSBOMDetailsFromTemplate(_cycloneDXBomParser.ParseCycloneDXBom(appSettings.Directory.CycloneDxSBomTemplatePath));
CycloneDXBomParser.CheckValidComponentsForProjectType(templateDetails.Components, appSettings.ProjectType);
//Adding Template Component Details & MetaData
SbomTemplate.AddComponentDetails(bom.Components, templateDetails);
}
bom.Dependencies = dependenciesForBOM;

bom = RemoveExcludedComponents(appSettings, bom);
bom.Dependencies = bom.Dependencies?.GroupBy(x => new { x.Ref }).Select(y => y.First()).ToList();
Expand Down
22 changes: 15 additions & 7 deletions src/LCT.PackageIdentifier/BomCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
Expand Down Expand Up @@ -101,17 +102,24 @@ private static void WriteContentToCycloneDxBOM(CommonAppSettings appSettings, Bo
{
IFileOperations fileOperations = new FileOperations();
string bomFileName = $"{appSettings.SW360.ProjectName}_Bom.cdx.json";
if (string.IsNullOrEmpty(appSettings.Directory.BomFilePath))

string outputFolderPath = appSettings.Directory.OutputFolder;
string[] files = System.IO.Directory.GetFiles(outputFolderPath);

bool fileExists = files.Length > 0 && files.Any(file => Path.GetFileName(file).Equals(bomFileName, StringComparison.OrdinalIgnoreCase));

if (fileExists && appSettings.MultipleProjectType)
{
string existingFilePath = files.FirstOrDefault(file => Path.GetFileName(file).Equals(bomFileName, StringComparison.OrdinalIgnoreCase));
listOfComponentsToBom = fileOperations.CombineComponentsFromExistingBOM(listOfComponentsToBom, existingFilePath);
bomKpiData.ComponentsInComparisonBOM = listOfComponentsToBom.Components.Count;
string formattedString = CommonHelper.AddSpecificValuesToBOMFormat(listOfComponentsToBom);
fileOperations.WriteContentToOutputBomFile(formattedString, appSettings.Directory.OutputFolder, FileConstant.BomFileName, appSettings.SW360.ProjectName);
fileOperations.WriteContentToOutputBomFile(formattedString, outputFolderPath, FileConstant.BomFileName, appSettings.SW360.ProjectName);
}
else if(Path.GetFileName(appSettings.Directory.BomFilePath).Equals(bomFileName, StringComparison.OrdinalIgnoreCase))
else
{
listOfComponentsToBom = fileOperations.CombineComponentsFromExistingBOM(listOfComponentsToBom, appSettings.Directory.BomFilePath);
bomKpiData.ComponentsInComparisonBOM = listOfComponentsToBom.Components.Count;
string formattedString = CommonHelper.AddSpecificValuesToBOMFormat(listOfComponentsToBom);
fileOperations.WriteContentToOutputBomFile(formattedString, appSettings.Directory.OutputFolder, FileConstant.BomFileName, appSettings.SW360.ProjectName);
fileOperations.WriteContentToOutputBomFile(formattedString, outputFolderPath, FileConstant.BomFileName, appSettings.SW360.ProjectName);
}

}
Expand All @@ -137,7 +145,7 @@ private async Task<Bom> CallPackageParser(CommonAppSettings appSettings)
case "ALPINE":
parser = new AlpineProcessor(CycloneDXBomParser);
return await ComponentIdentification(appSettings, parser);
case "PYTHON":
case "POETRY":
parser = new PythonProcessor(CycloneDXBomParser);
return await ComponentIdentification(appSettings, parser);
case "CONAN":
Expand Down
Loading

0 comments on commit 41be36c

Please sign in to comment.