posture: ignore not found serial errors

Updates #5902

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2023-11-13 13:45:52 +00:00 committed by Kristoffer Dalby
parent 86c8ab7502
commit fe7f7bff4f

View File

@ -8,13 +8,11 @@
package posture package posture
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
"github.com/digitalocean/go-smbios/smbios" "github.com/digitalocean/go-smbios/smbios"
"tailscale.com/types/logger" "tailscale.com/types/logger"
"tailscale.com/util/multierr"
) )
// getByteFromSmbiosStructure retrieves a 8-bit unsigned integer at the given specOffset. // getByteFromSmbiosStructure retrieves a 8-bit unsigned integer at the given specOffset.
@ -31,17 +29,17 @@ func getByteFromSmbiosStructure(s *smbios.Structure, specOffset int) uint8 {
// getStringFromSmbiosStructure retrieves a string at the given specOffset. // getStringFromSmbiosStructure retrieves a string at the given specOffset.
// Returns an empty string if no string was present. // Returns an empty string if no string was present.
func getStringFromSmbiosStructure(s *smbios.Structure, specOffset int) (string, error) { func getStringFromSmbiosStructure(s *smbios.Structure, specOffset int) string {
index := getByteFromSmbiosStructure(s, specOffset) index := getByteFromSmbiosStructure(s, specOffset)
if index == 0 || int(index) > len(s.Strings) { if index == 0 || int(index) > len(s.Strings) {
return "", errors.New("specified offset does not exist in smbios structure") return ""
} }
str := s.Strings[index-1] str := s.Strings[index-1]
trimmed := strings.TrimSpace(str) trimmed := strings.TrimSpace(str)
return trimmed, nil return trimmed
} }
// Product Table (Type 1) structure // Product Table (Type 1) structure
@ -71,31 +69,6 @@ func init() {
validTables = append(validTables, table) validTables = append(validTables, table)
} }
numOfTables = len(validTables) numOfTables = len(validTables)
}
// serialFromSmbiosStructure extracts a serial number from a product,
// baseboard or chassis SMBIOS table.
func serialFromSmbiosStructure(s *smbios.Structure) (string, error) {
id := s.Header.Type
if (id != productID) && (id != baseboardID) && (id != chassisID) {
return "", fmt.Errorf(
"cannot get serial table type %d, supported tables are %v",
id,
validTables,
)
}
serial, err := getStringFromSmbiosStructure(s, serialNumberOffset)
if err != nil {
return "", fmt.Errorf(
"failed to get serial from %s table: %w",
idToTableName[int(s.Header.Type)],
err,
)
}
return serial, nil
} }
func GetSerialNumbers(logf logger.Logf) ([]string, error) { func GetSerialNumbers(logf logger.Logf) ([]string, error) {
@ -114,22 +87,17 @@ func GetSerialNumbers(logf logger.Logf) ([]string, error) {
} }
serials := make([]string, 0, numOfTables) serials := make([]string, 0, numOfTables)
errs := make([]error, 0, numOfTables)
for _, s := range ss { for _, s := range ss {
switch s.Header.Type { switch s.Header.Type {
case productID, baseboardID, chassisID: case productID, baseboardID, chassisID:
serial, err := serialFromSmbiosStructure(s) serial := getStringFromSmbiosStructure(s, serialNumberOffset)
if err != nil {
errs = append(errs, err)
continue
}
if serial != "" {
serials = append(serials, serial) serials = append(serials, serial)
} }
} }
}
err = multierr.New(errs...)
// if there were no serial numbers, check if any errors were // if there were no serial numbers, check if any errors were
// returned and combine them. // returned and combine them.