Skip to content

Commit 6c230cb

Browse files
author
Manish Ranjan Mahanta
committed
Moving to VMUtils and static analysis fix
Signed-off-by: Manish Ranjan Mahanta <mmahanta@microsoft.com>
1 parent 8f00207 commit 6c230cb

4 files changed

Lines changed: 68 additions & 44 deletions

File tree

internal/uvm/log_wcow.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,12 @@ func (uvm *UtilityVM) SetLogSources(ctx context.Context) error {
6464
if wcaps != nil && wcaps.IsLogForwardingSupported() {
6565
// Make a call to the GCS to set the ETW providers
6666

67-
var settings string
6867
// Determines the log sources to be set based on the configuration. If default log sources are enabled,
6968
// we only include them along with user specified log sources.
7069
// For confidential WCOw, we skip the adding guids to the log sources as the sidecar-GCS will verify the
7170
// allowed log sources against policy and append the necessary GUIDs to the ones allowed. Rest are dropped.
7271
// For non-confidential WCOW, we include the GUIDs in the log sources as the hcsshim communicates directly with the inboxGCS.
73-
settings = etw.UpdateEncodedLogSources(uvm.logSources, !uvm.disableDefaultLogSources, !uvm.HasConfidentialPolicy())
72+
settings := etw.UpdateEncodedLogSources(ctx, uvm.logSources, !uvm.disableDefaultLogSources, !uvm.HasConfidentialPolicy())
7473

7574
req := guestrequest.LogForwardServiceRPCRequest{
7675
RPCType: guestrequest.RPCModifyServiceSettings,
File renamed without changes.
Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package etw
22

33
import (
4+
"context"
45
"embed"
56
"encoding/base64"
67
"encoding/json"
78
"fmt"
89
"strings"
910
"sync"
10-
)
1111

12-
//go:embed etw-map.json
13-
//go:embed default-logsources.json
12+
"github.com/Microsoft/hcsshim/internal/log"
13+
)
1414

15-
var etwFS embed.FS
16-
var listFS embed.FS
15+
//go:embed etw-map.json default-logsources.json
16+
var embeddedFiles embed.FS
1717

1818
const (
1919
EtwMapFileName = "etw-map.json"
@@ -49,7 +49,7 @@ type Source struct {
4949

5050
type EtwProvider struct {
5151
ProviderName string `json:"providerName,omitempty"`
52-
ProviderGuid string `json:"providerGuid,omitempty"`
52+
ProviderGUID string `json:"providerGuid,omitempty"`
5353
Level string `json:"level,omitempty"`
5454
Keywords string `json:"keywords,omitempty"`
5555
}
@@ -61,11 +61,11 @@ type EtwInfo struct {
6161

6262
type EtwProviderMap struct {
6363
ProviderName string `json:"providerName"`
64-
ProviderGuid string `json:"providerGuid"`
64+
ProviderGUID string `json:"providerGuid"`
6565
}
6666

67-
// NormalizeGuid takes a GUID string in various formats and normalizes it to the standard 8-4-4-4-12 format with uppercase letters. It returns an error if the input string is not a valid GUID.
68-
func NormalizeGuid(in string) (string, error) {
67+
// NormalizeGUID takes a GUID string in various formats and normalizes it to the standard 8-4-4-4-12 format with uppercase letters. It returns an error if the input string is not a valid GUID.
68+
func NormalizeGUID(in string) (string, error) {
6969
s := strings.TrimSpace(in)
7070
s = strings.TrimPrefix(s, "{")
7171
s = strings.TrimSuffix(s, "}")
@@ -97,7 +97,7 @@ func NormalizeGuid(in string) (string, error) {
9797
// LoadEtwMap loads the ETW provider name to GUID mapping from the embedded JSON file. It returns two maps, one for name to GUID and another for GUID to name. If there is an error in loading or parsing the file, it returns empty maps and the error.
9898
func LoadEtwMap() (map[string]string, map[string]string, error) {
9999
onceProvider.Do(func() {
100-
b, err := etwFS.ReadFile(EtwMapFileName)
100+
b, err := embeddedFiles.ReadFile(EtwMapFileName)
101101
if err != nil {
102102
return
103103
}
@@ -112,7 +112,7 @@ func LoadEtwMap() (map[string]string, map[string]string, error) {
112112

113113
for _, p := range cfg.EtwMap {
114114
name := strings.TrimSpace(p.ProviderName)
115-
guid, err := NormalizeGuid(p.ProviderGuid)
115+
guid, err := NormalizeGUID(p.ProviderGUID)
116116
if name == "" || err != nil {
117117
// skip invalid entries
118118
continue
@@ -140,27 +140,48 @@ func LoadEtwMap() (map[string]string, map[string]string, error) {
140140
return nameToGUID, guidToName, nil
141141
}
142142

143-
// GetDefaultLogSources returns the default log sources from the embedded json file. If there is an error in loading or parsing the file, it returns an empty LogSourcesInfo struct and the error.
144-
func GetDefaultLogSources() (LogSourcesInfo, error) {
143+
// GetDefaultLogSources returns the default log sources from the embedded JSON file. If there is an error in loading or parsing the file, it returns an empty LogSourcesInfo struct and the error.
144+
// The default log sources are defined in the "default-logsources.json" file and are loaded only once using sync.Once to ensure thread safety and performance.
145+
// The providers in the default-logsources.json file should only have Provider Names and must not contain GUIDs as the handling of GUIDs is based on the configuration and is done in the UpdateEncodedLogSources function where we
146+
// check if we need to include GUIDs for the log sources based on the configuration and if needed, we map the provider names to their corresponding GUIDs using the ETW map loaded from the "etw-map.json" file.
147+
// The only exception to this is if the provider does not have any name and only has a GUID.
148+
func GetDefaultLogSources(ctx context.Context) (LogSourcesInfo, error) {
145149
onceLists.Do(func() {
146150

147-
allList, err := listFS.ReadFile(DefaultLogSourcesFile)
151+
allList, err := embeddedFiles.ReadFile(DefaultLogSourcesFile)
148152
if err != nil {
153+
log.G(ctx).Errorf("Error reading default log sources file: %v", err)
149154
return
150155
}
151156

152157
if err := json.Unmarshal(allList, &defaultLogSources); err != nil {
158+
log.G(ctx).Errorf("Error unmarshalling default log sources file: %v", err)
153159
return
154160
}
161+
162+
// Check if the default log sources have provider names. If they do, do not include GUIDs in the
163+
// default log sources, because GUID handling is based on configuration and is done in the
164+
// UpdateEncodedLogSources function. There we check if GUIDs are needed for the log sources and,
165+
// if so, map provider names to their corresponding GUIDs using the ETW map from "etw-map.json".
166+
// The only exception is when a provider has no name and only a GUID.
167+
for i := range defaultLogSources.LogConfig.Sources {
168+
for j := range defaultLogSources.LogConfig.Sources[i].Providers {
169+
if defaultLogSources.LogConfig.Sources[i].Providers[j].ProviderName != "" &&
170+
defaultLogSources.LogConfig.Sources[i].Providers[j].ProviderGUID != "" {
171+
defaultLogSources.LogConfig.Sources[i].Providers[j].ProviderGUID = ""
172+
}
173+
}
174+
}
155175
})
156176
return defaultLogSources, nil
157177
}
158178

159-
// GetDefaultLogSourcesWithMappedGuid returns the default log sources with provider GUIDs included in the providers. If there is an error in loading the default log sources or the ETW map, it returns the default log sources without GUIDs.
160-
func GetDefaultLogSourcesWithMappedGuid() (LogSourcesInfo, error) {
179+
// GetDefaultLogSourcesWithMappedGUID returns the default log sources with provider GUIDs included in the providers. If there is an error in loading the default log sources or the ETW map, it returns the default log sources without GUIDs.
180+
func GetDefaultLogSourcesWithMappedGUID(ctx context.Context) (LogSourcesInfo, error) {
161181
onceListMap.Do(func() {
162-
_, err := GetDefaultLogSources()
182+
_, err := GetDefaultLogSources(ctx)
163183
if err != nil {
184+
log.G(ctx).Errorf("Error getting default log sources: %v", err)
164185
return
165186
}
166187

@@ -173,8 +194,8 @@ func GetDefaultLogSourcesWithMappedGuid() (LogSourcesInfo, error) {
173194
etwProvider.Keywords = provider.Keywords
174195
etwProvider.Level = provider.Level
175196
etwProvider.ProviderName = provider.ProviderName
176-
etwProvider.ProviderGuid = GetProviderGuidFromName(provider.ProviderName)
177-
source.Providers = append(src.Providers, etwProvider)
197+
etwProvider.ProviderGUID = GetProviderGUIDFromName(provider.ProviderName)
198+
source.Providers = append(source.Providers, etwProvider)
178199
}
179200

180201
logConfig.Sources = append(logConfig.Sources, source)
@@ -185,32 +206,36 @@ func GetDefaultLogSourcesWithMappedGuid() (LogSourcesInfo, error) {
185206
return defaultLogSourcesWithMap, nil
186207
}
187208

188-
// GetProviderGuidFromName returns the provider guid for a given provider name. If the provider name is not found in the map, it returns an empty string.
189-
func GetProviderGuidFromName(providerName string) string {
190-
LoadEtwMap()
209+
// GetProviderGUIDFromName returns the provider GUID for a given provider name. If the provider name is not found in the map, it returns an empty string.
210+
func GetProviderGUIDFromName(providerName string) string {
211+
if _, _, err := LoadEtwMap(); err != nil {
212+
return ""
213+
}
191214
return nameToGUID[providerName]
192215
}
193216

194-
// GetProviderNameFromGuid returns the provider name for a given provider guid. If the provider guid is not found in the map, it returns an empty string.
195-
func GetProviderNameFromGuid(providerGuid string) string {
196-
LoadEtwMap()
197-
return guidToName[providerGuid]
217+
// GetProviderNameFromGUID returns the provider name for a given provider GUID. If the provider GUID is not found in the map, it returns an empty string.
218+
func GetProviderNameFromGUID(providerGUID string) string {
219+
if _, _, err := LoadEtwMap(); err != nil {
220+
return ""
221+
}
222+
return guidToName[providerGUID]
198223
}
199224

200-
// Updates the user provided log sources with the default log sources based on the configuration and returns the updated log sources as a base64 encoded json string. If there is an error in the process, it returns the original user provided log sources string.
201-
func UpdateEncodedLogSources(base64EncodedJsonLogConfig string, useDefaultLogSources bool, includeGuids bool) string {
225+
// Updates the user provided log sources with the default log sources based on the configuration and returns the updated log sources as a base64 encoded JSON string. If there is an error in the process, it returns the original user provided log sources string.
226+
func UpdateEncodedLogSources(ctx context.Context, base64EncodedJSONLogConfig string, useDefaultLogSources bool, includeGUIDs bool) string {
202227

203228
var resultLogCfg LogSourcesInfo
204229
if useDefaultLogSources {
205-
if includeGuids {
206-
resultLogCfg, _ = GetDefaultLogSourcesWithMappedGuid()
230+
if includeGUIDs {
231+
resultLogCfg, _ = GetDefaultLogSourcesWithMappedGUID(ctx)
207232
} else {
208-
resultLogCfg, _ = GetDefaultLogSources()
233+
resultLogCfg, _ = GetDefaultLogSources(ctx)
209234
}
210235
}
211236

212-
if base64EncodedJsonLogConfig != "" {
213-
jsonBytes, err := base64.StdEncoding.DecodeString(base64EncodedJsonLogConfig)
237+
if base64EncodedJSONLogConfig != "" {
238+
jsonBytes, err := base64.StdEncoding.DecodeString(base64EncodedJSONLogConfig)
214239
if err == nil {
215240
var userLogConfig LogSourcesInfo
216241
if err := json.Unmarshal(jsonBytes, &userLogConfig); err == nil {
@@ -226,21 +251,21 @@ func UpdateEncodedLogSources(base64EncodedJsonLogConfig string, useDefaultLogSou
226251
if destSrc, ok := resultSrcMap[source.Type]; ok {
227252
// then update the source's providers
228253
for _, srcProvider := range source.Providers {
229-
if includeGuids {
230-
if srcProvider.ProviderGuid == "" {
231-
srcProvider.ProviderGuid = GetProviderGuidFromName(srcProvider.ProviderName)
254+
if includeGUIDs {
255+
if srcProvider.ProviderGUID == "" {
256+
srcProvider.ProviderGUID = GetProviderGUIDFromName(srcProvider.ProviderName)
232257
}
233258
} else {
234259
// If Include GUIDs is false, then
235260
// We still include GUIDs if that is the only identity present. Only when both Name and GUID is provided for a ETW provider, we
236261
// check if the provided GUID is valid and remove it if we can fetch the same from our well known list of guids by using the name
237262
// This is because the sidecar-GCS prefers verification of log providers by name against the policy.
238-
if srcProvider.ProviderName != "" && srcProvider.ProviderGuid != "" {
239-
guid, _ := NormalizeGuid(srcProvider.ProviderGuid)
240-
if strings.EqualFold(guid, GetProviderGuidFromName(srcProvider.ProviderName)) {
241-
srcProvider.ProviderGuid = ""
263+
if srcProvider.ProviderName != "" && srcProvider.ProviderGUID != "" {
264+
guid, _ := NormalizeGUID(srcProvider.ProviderGUID)
265+
if strings.EqualFold(guid, GetProviderGUIDFromName(srcProvider.ProviderName)) {
266+
srcProvider.ProviderGUID = ""
242267
} else {
243-
srcProvider.ProviderGuid = guid
268+
srcProvider.ProviderGUID = guid
244269
}
245270
}
246271
}
@@ -271,7 +296,7 @@ func UpdateEncodedLogSources(base64EncodedJsonLogConfig string, useDefaultLogSou
271296

272297
jsonBytes, err := json.Marshal(resultLogCfg)
273298
if err != nil {
274-
return base64EncodedJsonLogConfig
299+
return base64EncodedJSONLogConfig
275300
}
276301

277302
encodedCfg := base64.StdEncoding.EncodeToString(jsonBytes)

0 commit comments

Comments
 (0)