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

Storing local clients to local storage paths ce changes #28958

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion command/command_testonly/operator_usage_testonly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestOperatorUsageCommandRun(t *testing.T) {

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

_, _, err = clientcountutil.NewActivityLogData(client).
_, _, _, err = clientcountutil.NewActivityLogData(client).
NewPreviousMonthData(1).
NewClientsSeen(6, clientcountutil.WithClientType("entity")).
NewClientsSeen(4, clientcountutil.WithClientType("non-entity-token")).
Expand Down
27 changes: 19 additions & 8 deletions sdk/helper/clientcountutil/clientcountutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,42 +282,53 @@ func (d *ActivityLogDataGenerator) ToProto() *generation.ActivityLogMockInput {
// Write writes the data to the API with the given write options. The method
// returns the new paths that have been written. Note that the API endpoint will
// only be present when Vault has been compiled with the "testonly" flag.
func (d *ActivityLogDataGenerator) Write(ctx context.Context, writeOptions ...generation.WriteOptions) ([]string, []string, error) {
func (d *ActivityLogDataGenerator) Write(ctx context.Context, writeOptions ...generation.WriteOptions) ([]string, []string, []string, error) {
d.data.Write = writeOptions
err := VerifyInput(d.data)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
data, err := d.ToJSON()
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
resp, err := d.client.Logical().WriteWithContext(ctx, "sys/internal/counters/activity/write", map[string]interface{}{"input": string(data)})
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
if resp.Data == nil {
return nil, nil, fmt.Errorf("received no data")
return nil, nil, nil, fmt.Errorf("received no data")
}
paths := resp.Data["paths"]
castedPaths, ok := paths.([]interface{})
if !ok {
return nil, nil, fmt.Errorf("invalid paths data: %v", paths)
return nil, nil, nil, fmt.Errorf("invalid paths data: %v", paths)
}
returnPaths := make([]string, 0, len(castedPaths))
for _, path := range castedPaths {
returnPaths = append(returnPaths, path.(string))
}

localPaths := resp.Data["local_paths"]
localCastedPaths, ok := localPaths.([]interface{})
if !ok {
return nil, nil, nil, fmt.Errorf("invalid local paths data: %v", localPaths)
}
returnLocalPaths := make([]string, 0, len(localCastedPaths))
for _, path := range localCastedPaths {
returnLocalPaths = append(returnLocalPaths, path.(string))
}

globalPaths := resp.Data["global_paths"]
globalCastedPaths, ok := globalPaths.([]interface{})
if !ok {
return nil, nil, fmt.Errorf("invalid global paths data: %v", globalPaths)
return nil, nil, nil, fmt.Errorf("invalid global paths data: %v", globalPaths)
}
returnGlobalPaths := make([]string, 0, len(globalCastedPaths))
for _, path := range globalCastedPaths {
returnGlobalPaths = append(returnGlobalPaths, path.(string))
}
return returnPaths, returnGlobalPaths, nil
return returnPaths, returnLocalPaths, returnGlobalPaths, nil
}

// VerifyInput checks that the input data is valid
Expand Down
5 changes: 3 additions & 2 deletions sdk/helper/clientcountutil/clientcountutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestNewCurrentMonthData_AddClients(t *testing.T) {
// sent to the server is correct.
func TestWrite(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := io.WriteString(w, `{"data":{"paths":["path1","path2"],"global_paths":["path2","path3"]}}`)
_, err := io.WriteString(w, `{"data":{"paths":["path1","path2"],"global_paths":["path2","path3"], "local_paths":["path3","path4"]}}`)
require.NoError(t, err)
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
Expand All @@ -131,7 +131,7 @@ func TestWrite(t *testing.T) {
Address: ts.URL,
})
require.NoError(t, err)
paths, globalPaths, err := NewActivityLogData(client).
paths, localPaths, globalPaths, err := NewActivityLogData(client).
NewPreviousMonthData(3).
NewClientSeen().
NewPreviousMonthData(2).
Expand All @@ -142,6 +142,7 @@ func TestWrite(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []string{"path1", "path2"}, paths)
require.Equal(t, []string{"path2", "path3"}, globalPaths)
require.Equal(t, []string{"path3", "path4"}, localPaths)
}

func testAddClients(t *testing.T, makeGenerator func() *ActivityLogDataGenerator, getClient func(data *ActivityLogDataGenerator) *generation.Client) {
Expand Down
Loading
Loading