handle mount fields for non-entity clients; prevent null values (#28202)

This commit is contained in:
Chris Capurso
2024-08-28 09:49:03 -04:00
committed by GitHub
parent 747b395da5
commit de0c724d72
2 changed files with 57 additions and 21 deletions

View File

@@ -3067,6 +3067,13 @@ func (a *ActivityLog) writeExport(ctx context.Context, rw http.ResponseWriter, f
NamespacePath: nsDisplayPath, NamespacePath: nsDisplayPath,
Timestamp: ts.UTC().Format(time.RFC3339), Timestamp: ts.UTC().Format(time.RFC3339),
MountAccessor: e.MountAccessor, MountAccessor: e.MountAccessor,
// Default following to empty versus nil, will be overwritten if necessary
Policies: []string{},
EntityMetadata: map[string]string{},
EntityAliasMetadata: map[string]string{},
EntityAliasCustomMetadata: map[string]string{},
EntityGroupIDs: []string{},
} }
if e.MountAccessor != "" { if e.MountAccessor != "" {
@@ -3106,24 +3113,34 @@ func (a *ActivityLog) writeExport(ctx context.Context, rw http.ResponseWriter, f
return fmt.Errorf("failed to process entity name") return fmt.Errorf("failed to process entity name")
} }
record.Policies, ok = entityResp.Data["policies"].([]string) policies, ok := entityResp.Data["policies"].([]string)
if !ok { if !ok {
return fmt.Errorf("failed to process policies") return fmt.Errorf("failed to process policies")
} }
slices.Sort(record.Policies) if policies != nil {
record.Policies = policies
slices.Sort(record.Policies)
}
record.EntityMetadata, ok = entityResp.Data["metadata"].(map[string]string) entityMetadata, ok := entityResp.Data["metadata"].(map[string]string)
if !ok { if !ok {
return fmt.Errorf("failed to process entity metadata") return fmt.Errorf("failed to process entity metadata")
} }
record.EntityGroupIDs, ok = entityResp.Data["group_ids"].([]string) if entityMetadata != nil {
record.EntityMetadata = entityMetadata
}
entityGroupIDs, ok := entityResp.Data["group_ids"].([]string)
if !ok { if !ok {
return fmt.Errorf("failed to process entity group IDs") return fmt.Errorf("failed to process entity group IDs")
} }
slices.Sort(record.EntityGroupIDs) if entityGroupIDs != nil {
record.EntityGroupIDs = entityGroupIDs
slices.Sort(record.EntityGroupIDs)
}
aliases, ok := entityResp.Data["aliases"].([]interface{}) aliases, ok := entityResp.Data["aliases"].([]interface{})
if !ok { if !ok {
@@ -3165,15 +3182,31 @@ func (a *ActivityLog) writeExport(ctx context.Context, rw http.ResponseWriter, f
return fmt.Errorf("failed to process mount path") return fmt.Errorf("failed to process mount path")
} }
record.EntityAliasMetadata, ok = alias["metadata"].(map[string]string) entityAliasMetadata, ok := alias["metadata"].(map[string]string)
if !ok { if !ok {
return fmt.Errorf("failed to process entity alias metadata") return fmt.Errorf("failed to process entity alias metadata")
} }
record.EntityAliasCustomMetadata, ok = alias["custom_metadata"].(map[string]string) if entityAliasMetadata != nil {
record.EntityAliasMetadata = entityAliasMetadata
}
entityAliasCustomMetadata, ok := alias["custom_metadata"].(map[string]string)
if !ok { if !ok {
return fmt.Errorf("failed to process entity alias custom metadata") return fmt.Errorf("failed to process entity alias custom metadata")
} }
if entityAliasCustomMetadata != nil {
record.EntityAliasCustomMetadata = entityAliasCustomMetadata
}
}
} else {
// fetch mount directly to ensure mount type and path are populated
// this will be necessary for non-entity client types (e.g. non-entity-token)
validateResp := a.core.router.ValidateMountByAccessor(e.MountAccessor)
if validateResp != nil {
record.MountPath = validateResp.MountPath
record.MountType = validateResp.MountType
} }
} }
} }

View File

@@ -585,8 +585,22 @@ func getCSVExport(t *testing.T, client *api.Client, monthsPreviousTo int, now ti
// skip initial row as it is header // skip initial row as it is header
for rowIdx := 1; rowIdx < len(csvRecords); rowIdx++ { for rowIdx := 1; rowIdx < len(csvRecords); rowIdx++ {
baseRecord := vault.ActivityLogExportRecord{
Policies: []string{},
EntityMetadata: map[string]string{},
EntityAliasMetadata: map[string]string{},
EntityAliasCustomMetadata: map[string]string{},
EntityGroupIDs: []string{},
}
recordMap := make(map[string]interface{}) recordMap := make(map[string]interface{})
// create base map
err = mapstructure.Decode(baseRecord, &recordMap)
if err != nil {
return nil, err
}
for columnIdx, columnName := range csvHeader { for columnIdx, columnName := range csvHeader {
val := csvRecords[rowIdx][columnIdx] val := csvRecords[rowIdx][columnIdx]
@@ -597,13 +611,7 @@ func getCSVExport(t *testing.T, client *api.Client, monthsPreviousTo int, now ti
prefix := columnNameParts[0] prefix := columnNameParts[0]
if _, ok := mapFields[prefix]; ok { if _, ok := mapFields[prefix]; ok {
m, mOK := recordMap[prefix] m := recordMap[prefix]
// ensure output contains non-nil map
if !mOK {
m = make(map[string]string)
recordMap[prefix] = m
}
// ignore empty CSV column value // ignore empty CSV column value
if val != "" { if val != "" {
@@ -611,12 +619,7 @@ func getCSVExport(t *testing.T, client *api.Client, monthsPreviousTo int, now ti
recordMap[prefix] = m recordMap[prefix] = m
} }
} else if _, ok := sliceFields[prefix]; ok { } else if _, ok := sliceFields[prefix]; ok {
// ensure output contains non-nil slice s := recordMap[prefix]
s, sOK := recordMap[prefix]
if !sOK {
s = make([]string, 0)
recordMap[prefix] = s
}
// ignore empty CSV column value // ignore empty CSV column value
if val != "" { if val != "" {
@@ -624,7 +627,7 @@ func getCSVExport(t *testing.T, client *api.Client, monthsPreviousTo int, now ti
recordMap[prefix] = s recordMap[prefix] = s
} }
} else { } else {
t.Fatalf("unexpected CSV field: %s", columnName) t.Fatalf("unexpected CSV field: %q", columnName)
} }
} else if _, ok := boolFields[columnName]; ok { } else if _, ok := boolFields[columnName]; ok {
recordMap[columnName], err = strconv.ParseBool(val) recordMap[columnName], err = strconv.ParseBool(val)