Skip to content

Commit 8c2b36c

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 8c2b36c

4 files changed

Lines changed: 49 additions & 33 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(uvm.logSources, !uvm.disableDefaultLogSources, !uvm.HasConfidentialPolicy())
7473

7574
req := guestrequest.LogForwardServiceRPCRequest{
7675
RPCType: guestrequest.RPCModifyServiceSettings,
File renamed without changes.
Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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, "}")
@@ -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,7 +140,11 @@ 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.
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.
144148
func GetDefaultLogSources() (LogSourcesInfo, error) {
145149
onceLists.Do(func() {
146150

@@ -152,12 +156,21 @@ func GetDefaultLogSources() (LogSourcesInfo, error) {
152156
if err := json.Unmarshal(allList, &defaultLogSources); err != nil {
153157
return
154158
}
159+
160+
//Check if the default log sources have provider names and if they do, we do not include GUIDs in the default log sources with map as the handling of GUIDs is based on the configuration and is done in the UpdateEncodedLogSources function where we 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. The only exception to this is if the provider does not have any name and only has a GUID.
161+
for _, src := range defaultLogSources.LogConfig.Sources {
162+
for _, provider := range src.Providers {
163+
if provider.ProviderName != "" && provider.ProviderGUID != "" {
164+
provider.ProviderGUID = ""
165+
}
166+
}
167+
}
155168
})
156169
return defaultLogSources, nil
157170
}
158171

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) {
172+
// 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.
173+
func GetDefaultLogSourcesWithMappedGUID() (LogSourcesInfo, error) {
161174
onceListMap.Do(func() {
162175
_, err := GetDefaultLogSources()
163176
if err != nil {
@@ -173,7 +186,7 @@ func GetDefaultLogSourcesWithMappedGuid() (LogSourcesInfo, error) {
173186
etwProvider.Keywords = provider.Keywords
174187
etwProvider.Level = provider.Level
175188
etwProvider.ProviderName = provider.ProviderName
176-
etwProvider.ProviderGuid = GetProviderGuidFromName(provider.ProviderName)
189+
etwProvider.ProviderGUID = GetProviderGUIDFromName(provider.ProviderName)
177190
source.Providers = append(src.Providers, etwProvider)
178191
}
179192

@@ -185,32 +198,36 @@ func GetDefaultLogSourcesWithMappedGuid() (LogSourcesInfo, error) {
185198
return defaultLogSourcesWithMap, nil
186199
}
187200

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()
201+
// 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.
202+
func GetProviderGUIDFromName(providerName string) string {
203+
if _, _, err := LoadEtwMap(); err != nil {
204+
return ""
205+
}
191206
return nameToGUID[providerName]
192207
}
193208

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]
209+
// 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.
210+
func GetProviderNameFromGUID(providerGUID string) string {
211+
if _, _, err := LoadEtwMap(); err != nil {
212+
return ""
213+
}
214+
return guidToName[providerGUID]
198215
}
199216

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 {
217+
// 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.
218+
func UpdateEncodedLogSources(base64EncodedJSONLogConfig string, useDefaultLogSources bool, includeGUIDs bool) string {
202219

203220
var resultLogCfg LogSourcesInfo
204221
if useDefaultLogSources {
205-
if includeGuids {
206-
resultLogCfg, _ = GetDefaultLogSourcesWithMappedGuid()
222+
if includeGUIDs {
223+
resultLogCfg, _ = GetDefaultLogSourcesWithMappedGUID()
207224
} else {
208225
resultLogCfg, _ = GetDefaultLogSources()
209226
}
210227
}
211228

212-
if base64EncodedJsonLogConfig != "" {
213-
jsonBytes, err := base64.StdEncoding.DecodeString(base64EncodedJsonLogConfig)
229+
if base64EncodedJSONLogConfig != "" {
230+
jsonBytes, err := base64.StdEncoding.DecodeString(base64EncodedJSONLogConfig)
214231
if err == nil {
215232
var userLogConfig LogSourcesInfo
216233
if err := json.Unmarshal(jsonBytes, &userLogConfig); err == nil {
@@ -226,21 +243,21 @@ func UpdateEncodedLogSources(base64EncodedJsonLogConfig string, useDefaultLogSou
226243
if destSrc, ok := resultSrcMap[source.Type]; ok {
227244
// then update the source's providers
228245
for _, srcProvider := range source.Providers {
229-
if includeGuids {
230-
if srcProvider.ProviderGuid == "" {
231-
srcProvider.ProviderGuid = GetProviderGuidFromName(srcProvider.ProviderName)
246+
if includeGUIDs {
247+
if srcProvider.ProviderGUID == "" {
248+
srcProvider.ProviderGUID = GetProviderGUIDFromName(srcProvider.ProviderName)
232249
}
233250
} else {
234251
// If Include GUIDs is false, then
235252
// We still include GUIDs if that is the only identity present. Only when both Name and GUID is provided for a ETW provider, we
236253
// 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
237254
// 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 = ""
255+
if srcProvider.ProviderName != "" && srcProvider.ProviderGUID != "" {
256+
guid, _ := NormalizeGUID(srcProvider.ProviderGUID)
257+
if strings.EqualFold(guid, GetProviderGUIDFromName(srcProvider.ProviderName)) {
258+
srcProvider.ProviderGUID = ""
242259
} else {
243-
srcProvider.ProviderGuid = guid
260+
srcProvider.ProviderGUID = guid
244261
}
245262
}
246263
}
@@ -271,7 +288,7 @@ func UpdateEncodedLogSources(base64EncodedJsonLogConfig string, useDefaultLogSou
271288

272289
jsonBytes, err := json.Marshal(resultLogCfg)
273290
if err != nil {
274-
return base64EncodedJsonLogConfig
291+
return base64EncodedJSONLogConfig
275292
}
276293

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

0 commit comments

Comments
 (0)