open-vault/vault/logical_system_activity_write_testonly.go
miagilepner 1ea85c56d7
VAULT-14734: activity log write endpoint (#20019)
* add noop endpoint with testonly build flag

* add tests for endpoint

* cleanup

* fix test name

* add changelog

* pr fixes
2023-04-12 18:26:26 +02:00

54 lines
1.6 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
//go:build testonly
package vault
import (
"context"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/vault/activity/generation"
"google.golang.org/protobuf/encoding/protojson"
)
const helpText = "Create activity log data for testing purposes"
func (b *SystemBackend) activityWritePath() *framework.Path {
return &framework.Path{
Pattern: "internal/counters/activity/write$",
HelpDescription: helpText,
HelpSynopsis: helpText,
Fields: map[string]*framework.FieldSchema{
"input": {
Type: framework.TypeString,
Description: "JSON input for generating mock data",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.CreateOperation: &framework.PathOperation{
Callback: b.handleActivityWriteData,
Summary: "Write activity log data",
},
},
}
}
func (b *SystemBackend) handleActivityWriteData(ctx context.Context, request *logical.Request, data *framework.FieldData) (*logical.Response, error) {
json := data.Get("input")
input := &generation.ActivityLogMockInput{}
err := protojson.Unmarshal([]byte(json.(string)), input)
if err != nil {
return logical.ErrorResponse("Invalid input data: %s", err), logical.ErrInvalidRequest
}
if len(input.Write) == 0 {
return logical.ErrorResponse("Missing required \"write\" values"), logical.ErrInvalidRequest
}
if len(input.Data) == 0 {
return logical.ErrorResponse("Missing required \"data\" values"), logical.ErrInvalidRequest
}
return nil, nil
}