github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/os/dpkg/metadata/metadata.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package metadata defined a Metadata struct for DPKG packages.
    16  package metadata
    17  
    18  import (
    19  	"github.com/google/osv-scalibr/log"
    20  
    21  	pb "github.com/google/osv-scalibr/binary/proto/scan_result_go_proto"
    22  )
    23  
    24  // Metadata holds parsing information for a dpkg package.
    25  type Metadata struct {
    26  	PackageName       string
    27  	Status            string
    28  	SourceName        string
    29  	SourceVersion     string
    30  	PackageSource     string
    31  	PackageVersion    string
    32  	OSID              string
    33  	OSVersionCodename string
    34  	OSVersionID       string
    35  	Maintainer        string
    36  	Architecture      string
    37  }
    38  
    39  // ToNamespace extracts the PURL namespace from the metadata.
    40  func (m *Metadata) ToNamespace() string {
    41  	if m.OSID != "" {
    42  		return m.OSID
    43  	}
    44  	log.Errorf("os-release[ID] not set, fallback to 'linux'")
    45  	// TODO(b/298152210): Implement metric
    46  	return "linux"
    47  }
    48  
    49  // ToDistro extracts the OS distro from the metadata.
    50  func (m *Metadata) ToDistro() string {
    51  	// e.g. jammy
    52  	if m.OSVersionCodename != "" {
    53  		return m.OSVersionCodename
    54  	}
    55  	// fallback: e.g. 22.04
    56  	if m.OSVersionID != "" {
    57  		log.Warnf("VERSION_CODENAME not set in os-release, fallback to VERSION_ID")
    58  		return m.OSVersionID
    59  	}
    60  	log.Errorf("VERSION_CODENAME and VERSION_ID not set in os-release")
    61  	return ""
    62  }
    63  
    64  // SetProto sets the DPKGPackageMetadata field in the Package proto.
    65  func (m *Metadata) SetProto(p *pb.Package) {
    66  	if m == nil {
    67  		return
    68  	}
    69  	if p == nil {
    70  		return
    71  	}
    72  
    73  	p.Metadata = &pb.Package_DpkgMetadata{
    74  		DpkgMetadata: &pb.DPKGPackageMetadata{
    75  			PackageName:       m.PackageName,
    76  			Status:            m.Status,
    77  			SourceName:        m.SourceName,
    78  			SourceVersion:     m.SourceVersion,
    79  			PackageSource:     m.PackageSource,
    80  			PackageVersion:    m.PackageVersion,
    81  			OsId:              m.OSID,
    82  			OsVersionCodename: m.OSVersionCodename,
    83  			OsVersionId:       m.OSVersionID,
    84  			Maintainer:        m.Maintainer,
    85  			Architecture:      m.Architecture,
    86  		},
    87  	}
    88  }
    89  
    90  // ToStruct converts the NetportsMetadata proto to a Metadata struct.
    91  func ToStruct(m *pb.DPKGPackageMetadata) *Metadata {
    92  	if m == nil {
    93  		return nil
    94  	}
    95  
    96  	return &Metadata{
    97  		PackageName:       m.GetPackageName(),
    98  		Status:            m.GetStatus(),
    99  		SourceName:        m.GetSourceName(),
   100  		SourceVersion:     m.GetSourceVersion(),
   101  		PackageSource:     m.GetPackageSource(),
   102  		PackageVersion:    m.GetPackageVersion(),
   103  		OSID:              m.GetOsId(),
   104  		OSVersionCodename: m.GetOsVersionCodename(),
   105  		OSVersionID:       m.GetOsVersionId(),
   106  		Maintainer:        m.GetMaintainer(),
   107  		Architecture:      m.GetArchitecture(),
   108  	}
   109  }