Skip to content

Commit

Permalink
Merge pull request #41 from kubescape/update-filter-SBOM-correction
Browse files Browse the repository at this point in the history
validate we have the resource version before update filtered SBOM
  • Loading branch information
dwertent committed Mar 27, 2023
2 parents fba2c45 + 21a3241 commit 5077882
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/conthandler/container_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func getInstanceID(instanceIDs []instanceidhandler.IInstanceID, name string) ins
}

func (containerWatcher *ContainerWatcher) parsePodData(pod *core.Pod, containerIndex int) (*conthandlerV1.ContainerEventData, error) {
pod.TypeMeta.APIVersion = "v1"
pod.TypeMeta.Kind = "Pod"
podBytes, err := json.Marshal(pod)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func (sc *SBOMStructure) StoreFilterSBOM(instanceID string) error {
err := sc.storageClient.client.PostData(instanceID, data)
if err != nil {
if storageclient.IsAlreadyExist(err) {
sc.SBOMData.AddResourceVersionIfNeeded(sc.storageClient.client.GetResourceVersion(instanceID))
data = sc.SBOMData.GetFilterSBOMData()
err = sc.storageClient.client.PutData(instanceID, data)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions pkg/sbom/sbom_format_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type SBOMFormat interface {
FilterSBOM(sbomFileRelevantMap map[string]bool) error
IsNewRelevantSBOMDataExist() bool
IsSBOMAlreadyExist() bool
AddResourceVersionIfNeeded(string)
StoreFilteredSBOMName(string)
StoreMetadata(instanceID instanceidhandler.IInstanceID)
}
6 changes: 6 additions & 0 deletions pkg/sbom/v1/sbom_spdx_storage_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,9 @@ func (sbom *SBOMData) StoreFilteredSBOMName(name string) {
func (sbom *SBOMData) StoreMetadata(instanceID instanceidhandler.IInstanceID) {
sbom.filteredSpdxData.ObjectMeta.SetLabels(instanceID.GetLabels())
}

func (sc *SBOMData) AddResourceVersionIfNeeded(resourceVersion string) {
if sc.filteredSpdxData.GetResourceVersion() == "" {
sc.filteredSpdxData.SetResourceVersion(resourceVersion)
}
}
8 changes: 8 additions & 0 deletions pkg/storageclient/storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ func (sc *StorageK8SAggregatedAPIClient) PostData(key string, data any) error {
SBOM.ObjectMeta = retSBOM.ObjectMeta
return nil
}
func (sc *StorageK8SAggregatedAPIClient) GetResourceVersion(key string) string {
SBOM, err := sc.clientset.SpdxV1beta1().SBOMSPDXv2p3Filtereds(KubescapeNamespace).Get(gcontext.TODO(), key, metav1.GetOptions{})
if err != nil {
return ""
}
return SBOM.GetResourceVersion()
}

func IsAlreadyExist(err error) bool {
return apimachineryerrors.IsAlreadyExists(err)
}
1 change: 1 addition & 0 deletions pkg/storageclient/storage_client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ type StorageClient interface {
GetData(key string) (any, error)
PutData(key string, data any) error
PostData(key string, data any) error
GetResourceVersion(key string) string
}
7 changes: 6 additions & 1 deletion pkg/storageclient/storage_client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func (sc *StorageHttpClientMock) PutData(key string, data any) error {
func (sc *StorageHttpClientMock) PostData(key string, data any) error {
return nil
}
func (sc *StorageHttpClientMock) GetResourceVersion(key string) string {
return "123"
}

func CreateStorageHttpClientFailureMock() *StorageHttpClientFailureMock {
var data spdxv1beta1.SBOMSPDXv2p3
Expand Down Expand Up @@ -83,7 +86,9 @@ func (sc *StorageHttpClientFailureMock) PutData(key string, data any) error {
func (sc *StorageHttpClientFailureMock) PostData(key string, data any) error {
return fmt.Errorf("error already exist")
}

func (sc *StorageHttpClientFailureMock) GetResourceVersion(key string) string {
return "123"
}
func (sc *StorageHttpClientFailureMock) IsAlreadyExist(err error) bool {
return true
}

0 comments on commit 5077882

Please sign in to comment.