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

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package resources
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/cockroachdb/errors"
    10  	"go.mondoo.com/cnquery/llx"
    11  	"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
    12  	"go.mondoo.com/cnquery/providers/os/connection/shared"
    13  	"go.mondoo.com/cnquery/providers/os/resources/smbios"
    14  )
    15  
    16  type mqlMachineInternal struct {
    17  	smbiosInfo plugin.TValue[*smbios.SmBiosInfo]
    18  }
    19  
    20  func getbiosinfo(runtime *plugin.Runtime) (*smbios.SmBiosInfo, error) {
    21  	obj, err := CreateResource(runtime, "machine", map[string]*llx.RawData{})
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	machine := obj.(*mqlMachine)
    26  
    27  	if machine.smbiosInfo.State&plugin.StateIsSet != 0 {
    28  		return machine.smbiosInfo.Data, machine.smbiosInfo.Error
    29  	}
    30  
    31  	conn := runtime.Connection.(shared.Connection)
    32  	pf := conn.Asset().Platform
    33  	pm, err := smbios.ResolveManager(conn, pf)
    34  	if pm == nil || err != nil {
    35  		return nil, fmt.Errorf("could not detect suitable smbios manager for platform")
    36  	}
    37  
    38  	// retrieve smbios info
    39  	biosInfo, err := pm.Info()
    40  	if err != nil || biosInfo == nil {
    41  		if err == nil {
    42  			err = errors.New("could not retrieve smbios info")
    43  		}
    44  		machine.smbiosInfo = plugin.TValue[*smbios.SmBiosInfo]{Error: err, State: plugin.StateIsSet}
    45  		return nil, errors.Wrap(err, "could not retrieve smbios info for platform")
    46  	}
    47  
    48  	machine.smbiosInfo = plugin.TValue[*smbios.SmBiosInfo]{Data: biosInfo, State: plugin.StateIsSet}
    49  	return machine.smbiosInfo.Data, machine.smbiosInfo.Error
    50  }
    51  
    52  func initMachineBios(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
    53  	if len(args) > 2 {
    54  		return args, nil, nil
    55  	}
    56  
    57  	biosInfo, err := getbiosinfo(runtime)
    58  	if err != nil {
    59  		return nil, nil, err
    60  	}
    61  
    62  	return map[string]*llx.RawData{
    63  		"vendor":      llx.StringData(biosInfo.BIOS.Vendor),
    64  		"version":     llx.StringData(biosInfo.BIOS.Version),
    65  		"releaseDate": llx.StringData(biosInfo.BIOS.ReleaseDate),
    66  	}, nil, nil
    67  }
    68  
    69  func initMachineSystem(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
    70  	if len(args) > 2 {
    71  		return args, nil, nil
    72  	}
    73  
    74  	biosInfo, err := getbiosinfo(runtime)
    75  	if err != nil {
    76  		return nil, nil, err
    77  	}
    78  
    79  	return map[string]*llx.RawData{
    80  		"manufacturer": llx.StringData(biosInfo.SysInfo.Vendor),
    81  		"product":      llx.StringData(biosInfo.SysInfo.Model),
    82  		"version":      llx.StringData(biosInfo.SysInfo.Version),
    83  		"serial":       llx.StringData(biosInfo.SysInfo.SerialNumber),
    84  		"uuid":         llx.StringData(biosInfo.SysInfo.UUID),
    85  		"sku":          llx.StringData(biosInfo.SysInfo.SKU),
    86  		"family":       llx.StringData(biosInfo.SysInfo.Familiy),
    87  	}, nil, nil
    88  }
    89  
    90  func initMachineBaseboard(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
    91  	if len(args) > 2 {
    92  		return args, nil, nil
    93  	}
    94  
    95  	biosInfo, err := getbiosinfo(runtime)
    96  	if err != nil {
    97  		return nil, nil, err
    98  	}
    99  
   100  	return map[string]*llx.RawData{
   101  		"manufacturer": llx.StringData(biosInfo.BaseBoardInfo.Vendor),
   102  		"product":      llx.StringData(biosInfo.BaseBoardInfo.Model),
   103  		"version":      llx.StringData(biosInfo.BaseBoardInfo.Version),
   104  		"serial":       llx.StringData(biosInfo.BaseBoardInfo.SerialNumber),
   105  		"assetTag":     llx.StringData(biosInfo.BaseBoardInfo.AssetTag),
   106  	}, nil, nil
   107  }
   108  
   109  func initMachineChassis(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
   110  	if len(args) > 2 {
   111  		return args, nil, nil
   112  	}
   113  
   114  	biosInfo, err := getbiosinfo(runtime)
   115  	if err != nil {
   116  		return nil, nil, err
   117  	}
   118  
   119  	return map[string]*llx.RawData{
   120  		"manufacturer": llx.StringData(biosInfo.ChassisInfo.Vendor),
   121  		"version":      llx.StringData(biosInfo.ChassisInfo.Version),
   122  		"serial":       llx.StringData(biosInfo.ChassisInfo.SerialNumber),
   123  		"assetTag":     llx.StringData(biosInfo.ChassisInfo.AssetTag),
   124  	}, nil, nil
   125  }