github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/os/apk/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 defines a Metadata struct for apk packages.
    16  package metadata
    17  
    18  import (
    19  	"fmt"
    20  	"strings"
    21  
    22  	"github.com/google/osv-scalibr/log"
    23  
    24  	pb "github.com/google/osv-scalibr/binary/proto/scan_result_go_proto"
    25  )
    26  
    27  // Metadata holds parsing information for an apk package.
    28  type Metadata struct {
    29  	PackageName  string
    30  	OriginName   string
    31  	OSID         string
    32  	OSVersionID  string
    33  	Maintainer   string
    34  	Architecture string
    35  }
    36  
    37  // ToNamespace extracts the PURL namespace from the metadata.
    38  func (m *Metadata) ToNamespace() string {
    39  	if m.OSID != "" {
    40  		return m.OSID
    41  	}
    42  	log.Errorf("os-release[ID] not set, fallback to 'alpine'")
    43  	return "alpine"
    44  }
    45  
    46  // ToDistro extracts the OS distro from the metadata.
    47  func (m *Metadata) ToDistro() string {
    48  	// e.g. 3.18.0
    49  	if m.OSVersionID != "" {
    50  		return m.OSVersionID
    51  	}
    52  	log.Errorf("VERSION_ID not set in os-release")
    53  	return ""
    54  }
    55  
    56  // TrimDistroVersion trims minor versions from the distro string.
    57  // The Alpine OS info might include minor versions such as 3.12.1 while advisories are
    58  // only published against the minor and major versions, i.e. v3.12. Therefore we trim
    59  // any minor versions before putting the value into the Ecosystem.
    60  func (Metadata) TrimDistroVersion(distro string) string {
    61  	parts := strings.Split(distro, ".")
    62  	if len(parts) < 2 {
    63  		return "v" + distro
    64  	}
    65  	return fmt.Sprintf("v%s.%s", parts[0], parts[1])
    66  }
    67  
    68  // SetProto sets the ApkMetadata field in the Package proto.
    69  func (m *Metadata) SetProto(p *pb.Package) {
    70  	if m == nil {
    71  		return
    72  	}
    73  	if p == nil {
    74  		return
    75  	}
    76  
    77  	p.Metadata = &pb.Package_ApkMetadata{
    78  		ApkMetadata: &pb.APKPackageMetadata{
    79  			PackageName:  m.PackageName,
    80  			OriginName:   m.OriginName,
    81  			OsId:         m.OSID,
    82  			OsVersionId:  m.OSVersionID,
    83  			Maintainer:   m.Maintainer,
    84  			Architecture: m.Architecture,
    85  		},
    86  	}
    87  }
    88  
    89  // ToStruct converts the APKPackageMetadata proto to a Metadata struct.
    90  func ToStruct(m *pb.APKPackageMetadata) *Metadata {
    91  	if m == nil {
    92  		return nil
    93  	}
    94  
    95  	return &Metadata{
    96  		PackageName:  m.GetPackageName(),
    97  		OriginName:   m.GetOriginName(),
    98  		OSID:         m.GetOsId(),
    99  		OSVersionID:  m.GetOsVersionId(),
   100  		Maintainer:   m.GetMaintainer(),
   101  		Architecture: m.GetArchitecture(),
   102  	}
   103  }