Skip to content

Commit

Permalink
backport of commit 4b8ecf5 (#28104)
Browse files Browse the repository at this point in the history
Co-authored-by: miagilepner <[email protected]>
  • Loading branch information
1 parent 64081db commit d827833
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
3 changes: 3 additions & 0 deletions changelog/27790.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:change
activity (enterprise): filter all fields in client count responses by the request namespace
```
31 changes: 18 additions & 13 deletions vault/activity_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2762,23 +2762,23 @@ func (a *ActivityLog) prepareMonthsResponseForQuery(ctx context.Context, byMonth
for _, monthsRecord := range byMonth {
newClientsResponse := &ResponseNewClients{}
if monthsRecord.NewClients.Counts.HasCounts() {
newClientsNSResponse, err := a.prepareNamespaceResponse(ctx, monthsRecord.NewClients.Namespaces)
newClientsTotal, newClientsNSResponse, err := a.prepareNamespaceResponse(ctx, monthsRecord.NewClients.Namespaces)
if err != nil {
return nil, err
}
newClientsResponse.Counts = a.countsRecordToCountsResponse(monthsRecord.NewClients.Counts, false)
newClientsResponse.Counts = newClientsTotal
newClientsResponse.Namespaces = newClientsNSResponse
}

monthResponse := &ResponseMonth{
Timestamp: time.Unix(monthsRecord.Timestamp, 0).UTC().Format(time.RFC3339),
}
if monthsRecord.Counts.HasCounts() {
nsResponse, err := a.prepareNamespaceResponse(ctx, monthsRecord.Namespaces)
monthTotal, nsResponse, err := a.prepareNamespaceResponse(ctx, monthsRecord.Namespaces)
if err != nil {
return nil, err
}
monthResponse.Counts = a.countsRecordToCountsResponse(monthsRecord.Counts, false)
monthResponse.Counts = monthTotal
monthResponse.Namespaces = nsResponse
monthResponse.NewClients = newClientsResponse
months = append(months, monthResponse)
Expand All @@ -2787,22 +2787,24 @@ func (a *ActivityLog) prepareMonthsResponseForQuery(ctx context.Context, byMonth
return months, nil
}

// prepareNamespaceResponse populates the namespace portion of the activity log response struct
// from
func (a *ActivityLog) prepareNamespaceResponse(ctx context.Context, nsRecords []*activity.MonthlyNamespaceRecord) ([]*ResponseNamespace, error) {
// prepareNamespaceResponse takes monthly namespace records and converts them
// into the response namespace format. The function also returns counts for the
// total number of clients per type seen that month.
func (a *ActivityLog) prepareNamespaceResponse(ctx context.Context, nsRecords []*activity.MonthlyNamespaceRecord) (*ResponseCounts, []*ResponseNamespace, error) {
queryNS, err := namespace.FromContext(ctx)
if err != nil {
return nil, err
return nil, nil, err
}
nsResponse := make([]*ResponseNamespace, 0, len(nsRecords))
totalCounts := &ResponseCounts{}
nsResponses := make([]*ResponseNamespace, 0, len(nsRecords))
for _, nsRecord := range nsRecords {
if !nsRecord.Counts.HasCounts() {
continue
}

ns, err := NamespaceByID(ctx, nsRecord.NamespaceID, a.core)
if err != nil {
return nil, err
return nil, nil, err
}
if a.includeInResponse(queryNS, ns) {
mountResponse := make([]*ResponseMount, 0, len(nsRecord.Mounts))
Expand All @@ -2823,15 +2825,18 @@ func (a *ActivityLog) prepareNamespaceResponse(ctx context.Context, nsRecords []
} else {
displayPath = ns.Path
}
nsResponse = append(nsResponse, &ResponseNamespace{
nsResponse := &ResponseNamespace{
NamespaceID: nsRecord.NamespaceID,
NamespacePath: displayPath,
Counts: *a.countsRecordToCountsResponse(nsRecord.Counts, false),
Mounts: mountResponse,
})
}
nsResponses = append(nsResponses, nsResponse)

totalCounts.Add(&nsResponse.Counts)
}
}
return nsResponse, nil
return totalCounts, nsResponses, nil
}

// partialMonthClientCount returns the number of clients used so far this month.
Expand Down

0 comments on commit d827833

Please sign in to comment.