-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathhash.go
More file actions
110 lines (99 loc) · 3.37 KB
/
Copy pathhash.go
File metadata and controls
110 lines (99 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package inventory
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"sort"
"time"
)
// ComputeHash returns a deterministic hash for the stable inventory contents.
func ComputeHash(snap *Snapshot) (string, error) {
if snap == nil {
return "", fmt.Errorf("inventory snapshot is nil")
}
normalized := *snap
normalized.CollectedAt = time.Time{}
normalized.InventoryHash = ""
normalized.AgentConfig.EnabledComponents = sortedStrings(snap.AgentConfig.EnabledComponents)
normalized.AgentConfig.DisabledComponents = sortedStrings(snap.AgentConfig.DisabledComponents)
var err error
normalized.Resources.GPUInfo.GPUs, err = sortedByCanonicalJSON(snap.Resources.GPUInfo.GPUs)
if err != nil {
return "", fmt.Errorf("canonicalize GPU inventory: %w", err)
}
normalized.Resources.DiskInfo.BlockDevices, err = normalizedBlockDevices(snap.Resources.DiskInfo.BlockDevices)
if err != nil {
return "", fmt.Errorf("canonicalize disk inventory: %w", err)
}
normalized.Resources.NICInfo.PrivateIPInterfaces, err = sortedByCanonicalJSON(snap.Resources.NICInfo.PrivateIPInterfaces)
if err != nil {
return "", fmt.Errorf("canonicalize NIC inventory: %w", err)
}
payload, err := json.Marshal(normalized)
if err != nil {
return "", fmt.Errorf("marshal inventory snapshot: %w", err)
}
sum := sha256.Sum256(payload)
return hex.EncodeToString(sum[:]), nil
}
func normalizedBlockDevices(values []BlockDevice) ([]BlockDevice, error) {
if values == nil {
return nil, nil
}
normalized := append([]BlockDevice(nil), values...)
for i := range normalized {
normalized[i].Parents = sortedStrings(values[i].Parents)
}
return sortedByCanonicalJSON(normalized)
}
func sortedStrings(values []string) []string {
if values == nil {
return nil
}
sorted := append([]string(nil), values...)
sort.Strings(sorted)
return sorted
}
// sortedByCanonicalJSON treats a slice as order-insensitive for hashing purposes.
// Comparing the complete serialized values avoids relying on optional identity
// fields and automatically includes fields added to an inventory item in the future.
func sortedByCanonicalJSON[T any](values []T) ([]T, error) {
if values == nil {
return nil, nil
}
type sortableValue struct {
value T
encoded string
}
items := make([]sortableValue, len(values))
for i, value := range values {
encoded, err := json.Marshal(value)
if err != nil {
return nil, fmt.Errorf("marshal inventory list item: %w", err)
}
items[i] = sortableValue{value: value, encoded: string(encoded)}
}
sort.SliceStable(items, func(i, j int) bool {
return items[i].encoded < items[j].encoded
})
sorted := make([]T, len(items))
for i := range items {
sorted[i] = items[i].value
}
return sorted, nil
}