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-24556: add secret syncs to vault operator usage output #25751

Merged
merged 6 commits into from
Mar 4, 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/25751.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
cli: include secret syncs counts in the `vault operator usage` command output
```
22 changes: 14 additions & 8 deletions command/operator_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (c *OperatorUsageCommand) Run(args []string) int {
c.outputTimestamps(resp.Data)

out := []string{
"Namespace path | Distinct entities | Non-Entity tokens | Active clients",
"Namespace path | Distinct entities | Non-Entity tokens | Secret syncs | Active clients",
}

out = append(out, c.namespacesOutput(resp.Data)...)
Expand Down Expand Up @@ -196,8 +196,8 @@ type UsageResponse struct {
entityCount int64
// As per 1.9, the tokenCount field will contain the distinct non-entity
// token clients instead of each individual token.
tokenCount int64

tokenCount int64
SecretSyncs int64
mpalmi marked this conversation as resolved.
Show resolved Hide resolved
clientCount int64
}

Expand Down Expand Up @@ -242,6 +242,9 @@ func (c *OperatorUsageCommand) parseNamespaceCount(rawVal interface{}) (UsageRes
return ret, errors.New("missing non_entity_tokens")
}

// don't error if secret syncs are 0
ret.SecretSyncs, _ = jsonNumberOK(counts, "secret_syncs")

ret.clientCount, ok = jsonNumberOK(counts, "clients")
if !ok {
return ret, errors.New("missing clients")
Expand Down Expand Up @@ -274,8 +277,8 @@ func (c *OperatorUsageCommand) namespacesOutput(data map[string]interface{}) []s
sortOrder = "2" + val.namespacePath
}

formattedLine := fmt.Sprintf("%s | %d | %d | %d",
val.namespacePath, val.entityCount, val.tokenCount, val.clientCount)
formattedLine := fmt.Sprintf("%s | %d | %d | %d | %d",
val.namespacePath, val.entityCount, val.tokenCount, val.SecretSyncs, val.clientCount)
nsOut = append(nsOut, UsageCommandNamespace{
formattedLine: formattedLine,
sortOrder: sortOrder,
Expand All @@ -296,7 +299,7 @@ func (c *OperatorUsageCommand) namespacesOutput(data map[string]interface{}) []s

func (c *OperatorUsageCommand) totalOutput(data map[string]interface{}) []string {
// blank line separating it from namespaces
out := []string{" | | | "}
out := []string{" | | | | "}

total, ok := data["total"].(map[string]interface{})
if !ok {
Expand All @@ -315,13 +318,16 @@ func (c *OperatorUsageCommand) totalOutput(data map[string]interface{}) []string
c.UI.Error("missing non_entity_tokens in total")
return out
}
// don't error if secret syncs are 0
secretSyncs, _ := jsonNumberOK(total, "secret_syncs")

clientCount, ok := jsonNumberOK(total, "clients")
if !ok {
c.UI.Error("missing clients in total")
return out
}

out = append(out, fmt.Sprintf("Total | %d | %d | %d",
entityCount, tokenCount, clientCount))
out = append(out, fmt.Sprintf("Total | %d | %d | %d | %d",
entityCount, tokenCount, secretSyncs, clientCount))
return out
}
87 changes: 87 additions & 0 deletions command/operator_usage_testonly_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

//go:build testonly

package command

import (
"context"
"fmt"
"strings"
"testing"
"time"

"github.com/hashicorp/cli"
"github.com/hashicorp/vault/helper/timeutil"
"github.com/hashicorp/vault/sdk/helper/clientcountutil"
"github.com/hashicorp/vault/sdk/helper/clientcountutil/generation"
"github.com/stretchr/testify/require"
)

func testOperatorUsageCommand(tb testing.TB) (*cli.MockUi, *OperatorUsageCommand) {
tb.Helper()

ui := cli.NewMockUi()
return ui, &OperatorUsageCommand{
BaseCommand: &BaseCommand{
UI: ui,
},
}
}

func TestOperatorUsageCommandRun(t *testing.T) {
t.Parallel()

t.Run("client types", func(t *testing.T) {
t.Parallel()

client, _, closer := testVaultServerUnseal(t)
defer closer()

_, err := client.Logical().Write("sys/internal/counters/config", map[string]interface{}{"enabled": "enable"})
require.NoError(t, err)

now := time.Now().UTC()

_, err = clientcountutil.NewActivityLogData(client).
NewPreviousMonthData(1).
NewClientsSeen(6, clientcountutil.WithClientType("entity")).
NewClientsSeen(4, clientcountutil.WithClientType("non-entity-token")).
NewClientsSeen(2, clientcountutil.WithClientType("secret-sync")).
NewCurrentMonthData().
NewClientsSeen(3, clientcountutil.WithClientType("entity")).
NewClientsSeen(4, clientcountutil.WithClientType("non-entity-token")).
NewClientsSeen(5, clientcountutil.WithClientType("secret-sync")).
Write(context.Background(), generation.WriteOptions_WRITE_ENTITIES, generation.WriteOptions_WRITE_PRECOMPUTED_QUERIES)
require.NoError(t, err)

ui, cmd := testOperatorUsageCommand(t)
cmd.client = client

start := timeutil.MonthsPreviousTo(1, now).Format(time.RFC3339)
end := timeutil.EndOfMonth(now).UTC().Format(time.RFC3339)
// Reset and check output
code := cmd.Run([]string{
"-start-time", start,
"-end-time", end,
})
require.Equal(t, 0, code)
output := ui.OutputWriter.String()
outputLines := strings.Split(output, "\n")
require.Equal(t, fmt.Sprintf("Period start: %s", start), outputLines[0])
require.Equal(t, fmt.Sprintf("Period end: %s", end), outputLines[1])

require.Contains(t, outputLines[3], "Secret sync")
nsCounts := strings.Fields(outputLines[5])
require.Equal(t, "[root]", nsCounts[0])
require.Equal(t, "9", nsCounts[1])
require.Equal(t, "8", nsCounts[2])
require.Equal(t, "7", nsCounts[3])
require.Equal(t, "24", nsCounts[4])

totalCounts := strings.Fields(outputLines[7])
require.Equal(t, "Total", totalCounts[0])
require.Equal(t, nsCounts[1:], totalCounts[1:])
})
}
Loading