go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/id/platformid/osx.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package platformid
     5  
     6  import (
     7  	"errors"
     8  	"io"
     9  	"regexp"
    10  	"strings"
    11  
    12  	"go.mondoo.com/cnquery/providers/os/connection/shared"
    13  )
    14  
    15  // MacOSIdProvider read the operating system id by calling
    16  // "ioreg -rd1 -c IOPlatformExpertDevice" and extracting
    17  // the IOPlatformUUID
    18  type MacOSIdProvider struct {
    19  	connection shared.Connection
    20  }
    21  
    22  func (p *MacOSIdProvider) Name() string {
    23  	return "MacOS Platform ID"
    24  }
    25  
    26  var MACOS_ID_REGEX = regexp.MustCompile(`\"IOPlatformUUID\"\s*=\s*\"(.*)\"`)
    27  
    28  func (p *MacOSIdProvider) ID() (string, error) {
    29  	c, err := p.connection.RunCommand("ioreg -rd1 -c IOPlatformExpertDevice")
    30  	if err != nil || c.ExitStatus != 0 {
    31  		return "", err
    32  	}
    33  
    34  	// parse string with regex with \"IOPlatformUUID\"\s*=\s*\"(.*)\"
    35  	content, err := io.ReadAll(c.Stdout)
    36  	if err != nil {
    37  		return "", err
    38  	}
    39  
    40  	m := MACOS_ID_REGEX.FindStringSubmatch(string(content))
    41  	if m == nil {
    42  		return "", errors.New("could not detect the machine id")
    43  	}
    44  
    45  	return strings.TrimSpace(strings.ToLower(m[1])), nil
    46  }