Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VAULT-28329: Fix months activity log counts when querying for a namespace #27790

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:improvement
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree that this fixes buggy behaviour, this is, strictly speaking, an API behaviour change -- so maybe this should be a change instead of an improvement?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this. The current output is inconsistent but it is always possible that someone has become dependent on the existing behavior.

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 @@ -2779,23 +2779,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 @@ -2804,22 +2804,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 @@ -2840,15 +2842,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
Loading