go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers-sdk/v1/inventory/asset.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package inventory
     5  
     6  import (
     7  	"encoding/json"
     8  	"errors"
     9  	fmt "fmt"
    10  	"regexp"
    11  	"strings"
    12  
    13  	"github.com/rs/zerolog/log"
    14  )
    15  
    16  func (a *Asset) HumanName() string {
    17  	if a == nil {
    18  		return ""
    19  	}
    20  
    21  	if a.Platform != nil {
    22  		return fmt.Sprintf(a.Name + " (" + a.Platform.Title + ")")
    23  	}
    24  
    25  	return a.Name
    26  }
    27  
    28  func (a *Asset) EnsurePlatformID(ids ...string) {
    29  	if a.PlatformIds == nil {
    30  		a.PlatformIds = ids
    31  		return
    32  	}
    33  
    34  	// check if the id is already included
    35  	keys := map[string]bool{}
    36  	for _, k := range a.PlatformIds {
    37  		keys[k] = true
    38  	}
    39  
    40  	// append entry
    41  	for _, id := range ids {
    42  		_, ok := keys[id]
    43  		if !ok {
    44  			a.PlatformIds = append(a.PlatformIds, id)
    45  		}
    46  	}
    47  }
    48  
    49  func (a *Asset) AddPlatformID(ids ...string) {
    50  	if a.PlatformIds == nil {
    51  		a.PlatformIds = []string{}
    52  	}
    53  
    54  	a.PlatformIds = append(a.PlatformIds, ids...)
    55  }
    56  
    57  // AddLabels adds the provided labels
    58  // existing labels with the same key will be overwritten
    59  func (a *Asset) AddLabels(labels map[string]string) {
    60  	if a.Labels == nil {
    61  		a.Labels = map[string]string{}
    62  	}
    63  
    64  	// copy labels
    65  	for k := range labels {
    66  		a.Labels[k] = labels[k]
    67  	}
    68  }
    69  
    70  func (a *Asset) AddAnnotations(annotations map[string]string) {
    71  	if a.Annotations == nil {
    72  		a.Annotations = map[string]string{}
    73  	}
    74  
    75  	// copy annotations
    76  	for k := range annotations {
    77  		a.Annotations[k] = annotations[k]
    78  	}
    79  }
    80  
    81  func NewState(state string) State {
    82  	switch state {
    83  	case "STATE_UNKNOWN":
    84  		return State_STATE_UNKNOWN
    85  	case "STATE_ERROR":
    86  		return State_STATE_ERROR
    87  	case "STATE_PENDING":
    88  		return State_STATE_PENDING
    89  	case "STATE_RUNNING":
    90  		return State_STATE_RUNNING
    91  	case "STATE_STOPPING":
    92  		return State_STATE_STOPPING
    93  	case "STATE_STOPPED":
    94  		return State_STATE_STOPPED
    95  	case "STATE_SHUTDOWN":
    96  		return State_STATE_SHUTDOWN
    97  	case "STATE_TERMINATED":
    98  		return State_STATE_TERMINATED
    99  	case "STATE_REBOOT":
   100  		return State_STATE_REBOOT
   101  	case "STATE_ONLINE":
   102  		return State_STATE_ONLINE
   103  	case "STATE_OFFLINE":
   104  		return State_STATE_OFFLINE
   105  	case "STATE_DELETED":
   106  		return State_STATE_DELETED
   107  	default:
   108  		log.Debug().Str("state", state).Msg("unknown asset state")
   109  		return State_STATE_UNKNOWN
   110  	}
   111  }
   112  
   113  var AssetCategory_schemevalue = map[string]AssetCategory{
   114  	// FIXME: DEPRECATED, remove in v11.0 vv
   115  	"fleet": AssetCategory_CATEGORY_INVENTORY,
   116  	// ^^
   117  	"inventory": AssetCategory_CATEGORY_INVENTORY,
   118  	"cicd":      AssetCategory_CATEGORY_CICD,
   119  }
   120  
   121  // UnmarshalJSON parses either an int or a string representation of
   122  // CredentialType into the struct
   123  func (s *AssetCategory) UnmarshalJSON(data []byte) error {
   124  	// check if we have a number
   125  	var code int32
   126  	err := json.Unmarshal(data, &code)
   127  	if err == nil {
   128  		*s = AssetCategory(code)
   129  	} else {
   130  		var name string
   131  		// we can ignore the error here because we just look it up and otherwise
   132  		// tell users that we can't find the backend value
   133  		_ = json.Unmarshal(data, &name)
   134  		code, ok := AssetCategory_schemevalue[strings.TrimSpace(name)]
   135  		if !ok {
   136  			return errors.New("unknown backend value: " + string(data))
   137  		}
   138  		*s = code
   139  	}
   140  	return nil
   141  }
   142  
   143  var mondooLabelRegex = regexp.MustCompile(`^[a-z0-9]*\.?(mondoo.com\/)[a-z0-9\-]*`)
   144  
   145  // Merges the mondoo-specific labels from the provided root into the provided asset
   146  func (a *Asset) AddMondooLabels(root *Asset) {
   147  	if a.Labels == nil {
   148  		a.Labels = map[string]string{}
   149  	}
   150  	for k, v := range root.Labels {
   151  		if mondooLabelRegex.MatchString(k) {
   152  			a.Labels[k] = v
   153  		}
   154  	}
   155  }