go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/container.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package resources
     5  
     6  import (
     7  	"github.com/google/go-containerregistry/pkg/name"
     8  	"go.mondoo.com/cnquery/llx"
     9  	"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
    10  	"go.mondoo.com/cnquery/providers/os/connection"
    11  )
    12  
    13  func initContainerImage(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
    14  	if len(args) > 1 {
    15  		return args, nil, nil
    16  	}
    17  	conn := runtime.Connection.(*connection.TarConnection)
    18  	reference := conn.Metadata.Labels["docker.io/digests"]
    19  
    20  	ref, err := name.ParseReference(reference)
    21  	if err != nil {
    22  		return nil, nil, err
    23  	}
    24  
    25  	identifierType := ""
    26  	switch ref.(type) {
    27  	case name.Tag:
    28  		identifierType = "tag"
    29  	case name.Digest:
    30  		identifierType = "digest"
    31  	}
    32  
    33  	// "index.docker.io/library/coredns:latest"
    34  	// name: "index.docker.io/library/coredns:latest"
    35  	// identifier: latest
    36  	// identifierType: tag
    37  	r, err := CreateResource(runtime, "container.image", map[string]*llx.RawData{
    38  		"reference":      llx.StringData(reference),
    39  		"name":           llx.StringData(ref.Name()),
    40  		"identifier":     llx.StringData(ref.Identifier()),
    41  		"identifierType": llx.StringData(identifierType),
    42  	})
    43  	if err != nil {
    44  		return nil, nil, err
    45  	}
    46  
    47  	return nil, r, nil
    48  }
    49  
    50  func (k *mqlContainerImage) id() (string, error) {
    51  	return k.Name.Data, nil
    52  }
    53  
    54  func (k *mqlContainerImage) repository() (*mqlContainerRepository, error) {
    55  	if k.Name.Error != nil {
    56  		return nil, k.Name.Error
    57  	}
    58  
    59  	ref, err := name.ParseReference(k.Name.Data)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	return newLumiContainerRepository(k.MqlRuntime, ref.Context())
    65  }
    66  
    67  func newLumiContainerRepository(runtime *plugin.Runtime, repo name.Repository) (*mqlContainerRepository, error) {
    68  	r, err := CreateResource(runtime, "container.repository", map[string]*llx.RawData{
    69  		"name":     llx.StringData(repo.RepositoryStr()),
    70  		"scheme":   llx.StringData(repo.Scheme()),
    71  		"fullName": llx.StringData(repo.Name()),
    72  		"registry": llx.StringData(repo.RegistryStr()),
    73  	})
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	return r.(*mqlContainerRepository), nil
    79  }
    80  
    81  func (k *mqlContainerRepository) id() (string, error) {
    82  	return k.FullName.Data, nil
    83  }