Events: Add outbound detail protobufs, request status code, and update event encrypt filter. (#1569)

* feat (events): Add status code to audit events

* feat (events): Add the resp proto msg to every audit event

* chore (events): Update eventlogger dep

This latest version will redact any map and/or map field which isn't
explicitly classified via a Taggable interface

* feat (events): Begin to add tagging to response proto msgs
This commit is contained in:
Jim authored and GitHub committed 2021-09-30 12:55:47 -04:00
1 parent 8d6dee09a9
commit c8b29ded1c
23 files changed
+914 -129

No files matched your search

@@ -873,14 +873,14 @@ type AuthenticateRequest struct {
unknownFields protoimpl.UnknownFields
// The ID of the Auth Method in the system that should be used for authentication.
AuthMethodId string `protobuf:"bytes,1,opt,name=auth_method_id,proto3" json:"auth_method_id,omitempty"`
AuthMethodId string `protobuf:"bytes,1,opt,name=auth_method_id,proto3" json:"auth_method_id,omitempty" class:"public"` // @gotags: `class:"public"`
// This can be "cookie" or "token". If not provided, "token" will be used. "cookie" activates a split-cookie method where the token is split partially between http-only and regular cookies in order
// to keep it safe from rogue JS in the browser.
TokenType string `protobuf:"bytes,2,opt,name=token_type,proto3" json:"token_type,omitempty"`
// Attributes are passed to the Auth Method; the valid keys and values depend on the type of Auth Method as well as the command.
Attributes *structpb.Struct `protobuf:"bytes,4,opt,name=attributes,proto3" json:"attributes,omitempty"`
// The command to perform.
Command string `protobuf:"bytes,5,opt,name=command,proto3" json:"command,omitempty"`
Command string `protobuf:"bytes,5,opt,name=command,proto3" json:"command,omitempty" class:"public"` // @gotags: `class:"public"`
}
func (x *AuthenticateRequest) Reset() {
@@ -951,7 +951,7 @@ type AuthenticateResponse struct {
// Valid keys and values depend on the type of Auth Method as well as the command.
Attributes *structpb.Struct `protobuf:"bytes,4,opt,name=attributes,proto3" json:"attributes,omitempty"`
// The command that was performed.
Command string `protobuf:"bytes,5,opt,name=command,proto3" json:"command,omitempty"`
Command string `protobuf:"bytes,5,opt,name=command,proto3" json:"command,omitempty" class:"public"` // @gotags: `class:"public"`
}
func (x *AuthenticateResponse) Reset() {
@@ -0,0 +1,63 @@
package services
import "github.com/hashicorp/eventlogger/filters/encrypt"
// Tags implements the encrypt.Taggable interface which allows
// AuthenticateResponse Attributes to be classified for the encrypt filter.
func (req *AuthenticateResponse) Tags() ([]encrypt.PointerTag, error) {
if req.Attributes == nil {
return nil, nil
}
return []encrypt.PointerTag{
// public fields
{
Pointer: "/Attributes/Fields/account_id",
Classification: encrypt.PublicClassification,
},
{
Pointer: "/Attributes/Fields/approximate_last_used_time",
Classification: encrypt.PublicClassification,
},
{
Pointer: "/Attributes/Fields/auth_method_id",
Classification: encrypt.PublicClassification,
},
{
Pointer: "/Attributes/Fields/authorized_actions",
Classification: encrypt.PublicClassification,
},
{
Pointer: "/Attributes/Fields/created_time",
Classification: encrypt.PublicClassification,
},
{
Pointer: "/Attributes/Fields/expiration_time",
Classification: encrypt.PublicClassification,
},
{
Pointer: "/Attributes/Fields/id",
Classification: encrypt.PublicClassification,
},
{
Pointer: "/Attributes/Fields/scope",
Classification: encrypt.PublicClassification,
},
{
Pointer: "/Attributes/Fields/token_type",
Classification: encrypt.PublicClassification,
},
{
Pointer: "/Attributes/Fields/updated_time",
Classification: encrypt.PublicClassification,
},
{
Pointer: "/Attributes/Fields/user_id",
Classification: encrypt.PublicClassification,
},
// secret fields
{
Pointer: "/Attributes/Fields/token",
Classification: encrypt.SecretClassification,
},
}, nil
}
@@ -0,0 +1,97 @@
package services
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/hashicorp/boundary/sdk/wrapper"
"github.com/hashicorp/eventlogger"
"github.com/hashicorp/eventlogger/filters/encrypt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
structpb "google.golang.org/protobuf/types/known/structpb"
)
func TestScope_Tags(t *testing.T) {
ctx := context.Background()
now := time.Now()
wrapper := wrapper.TestWrapper(t)
testEncryptingFilter := &encrypt.Filter{
Wrapper: wrapper,
HmacSalt: []byte("salt"),
HmacInfo: []byte("info"),
}
tests := []struct {
name string
testEvent *eventlogger.Event
wantEvent *eventlogger.Event
}{
{
name: "validate-filtering",
testEvent: &eventlogger.Event{
Type: "test",
CreatedAt: now,
Payload: &AuthenticateResponse{
Command: "public-command",
Attributes: &structpb.Struct{
Fields: map[string]*structpb.Value{
"account_id": structpb.NewStringValue("public-account_id"),
"approximate_last_used_time": structpb.NewStringValue("public-approximate_last_used_time"),
"auth_method_id": structpb.NewStringValue("public-auth_method_id"),
"authorized_actions": structpb.NewStringValue("public-authorized_actions"),
"created_time": structpb.NewStringValue("public-created_time"),
"expiration_time": structpb.NewStringValue("public-expiration_time"),
"id": structpb.NewStringValue("public-id"),
"scope": structpb.NewStringValue("public-scope"),
"token_type": structpb.NewStringValue("public-token_type"),
"updated_time": structpb.NewStringValue("public-updated_time"),
"user_id": structpb.NewStringValue("public-user_id"),
"token": structpb.NewStringValue("secret-token"),
},
},
},
},
wantEvent: &eventlogger.Event{
Type: "test",
CreatedAt: now,
Payload: &AuthenticateResponse{
Command: "public-command",
Attributes: &structpb.Struct{
Fields: map[string]*structpb.Value{
"account_id": structpb.NewStringValue("public-account_id"),
"approximate_last_used_time": structpb.NewStringValue("public-approximate_last_used_time"),
"auth_method_id": structpb.NewStringValue("public-auth_method_id"),
"authorized_actions": structpb.NewStringValue("public-authorized_actions"),
"created_time": structpb.NewStringValue("public-created_time"),
"expiration_time": structpb.NewStringValue("public-expiration_time"),
"id": structpb.NewStringValue("public-id"),
"scope": structpb.NewStringValue("public-scope"),
"token_type": structpb.NewStringValue("public-token_type"),
"updated_time": structpb.NewStringValue("public-updated_time"),
"user_id": structpb.NewStringValue("public-user_id"),
"token": structpb.NewStringValue("<REDACTED>"),
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
got, err := testEncryptingFilter.Process(ctx, tt.testEvent)
require.NoError(err)
require.NotNil(got)
actualJson, err := json.Marshal(got)
require.NoError(err)
t.Log(string(actualJson))
wantJson, err := json.Marshal(tt.wantEvent)
require.NoError(err)
assert.JSONEq(string(wantJson), string(actualJson))
})
}
}
+15 -17
View File
@@ -30,16 +30,15 @@ const (
)
type testAudit struct {
Id string `json:"id"` // std audit/boundary field
Version string `json:"version"` // std audit/boundary field
Type string `json:"type"` // std audit field
Timestamp time.Time `json:"timestamp"` // std audit field
RequestInfo *event.RequestInfo `json:"request_info,omitempty"` // boundary field
Auth *event.Auth `json:"auth,omitempty"` // std audit field
Request *event.Request `json:"request,omitempty"` // std audit field
Response *event.Response `json:"response,omitempty"` // std audit field
SerializedHMAC string `json:"serialized_hmac"` // boundary field
Flush bool `json:"-"`
Id string `json:"id"` // std audit/boundary field
Version string `json:"version"` // std audit/boundary field
Type string `json:"type"` // std audit field
Timestamp time.Time `json:"timestamp"` // std audit field
RequestInfo *event.RequestInfo `json:"request_info,omitempty"` // boundary field
Auth *event.Auth `json:"auth,omitempty"` // std audit field
Request *event.Request `json:"request,omitempty"` // std audit field
Response *event.Response `json:"response,omitempty"` // std audit field
Flush bool `json:"-"`
}
func Test_NewRequestInfoContext(t *testing.T) {
@@ -922,13 +921,12 @@ func Test_WriteAudit(t *testing.T) {
Time: gotAudit.Time,
Type: "audit",
Data: map[string]interface{}{
"auth": tt.wantAudit.Auth,
"id": gotAudit.Data.(map[string]interface{})["id"],
"timestamp": now,
"request": tt.wantAudit.Request,
"serialized_hmac": "",
"type": apiRequest,
"version": testAuditVersion,
"auth": tt.wantAudit.Auth,
"id": gotAudit.Data.(map[string]interface{})["id"],
"timestamp": now,
"request": tt.wantAudit.Request,
"type": apiRequest,
"version": testAuditVersion,
},
}
if tt.wantAudit.Id != "" {
+18 -11
View File
@@ -19,16 +19,15 @@ const (
// audit defines the data of audit events
type audit struct {
Id string `json:"id"` // std audit/boundary field
Version string `json:"version"` // std audit/boundary field
Type string `json:"type"` // std audit field
Timestamp time.Time `json:"timestamp"` // std audit field
RequestInfo *RequestInfo `json:"request_info,omitempty"` // boundary field
Auth *Auth `json:"auth,omitempty"` // std audit field
Request *Request `json:"request,omitempty"` // std audit field
Response *Response `json:"response,omitempty"` // std audit field
SerializedHMAC string `json:"serialized_hmac"` // boundary field
Flush bool `json:"-"`
Id string `json:"id"` // std audit/boundary field
Version string `json:"version"` // std audit/boundary field
Type string `json:"type"` // std audit field
Timestamp time.Time `json:"timestamp"` // std audit field
RequestInfo *RequestInfo `json:"request_info,omitempty"` // boundary field
Auth *Auth `json:"auth,omitempty"` // std audit field
Request *Request `json:"request,omitempty"` // std audit field
Response *Response `json:"response,omitempty"` // std audit field
Flush bool `json:"-"`
}
func newAudit(fromOperation Op, opt ...Option) (*audit, error) {
@@ -133,7 +132,15 @@ func (a *audit) ComposeFrom(events []*eventlogger.Event) (eventlogger.EventType,
payload.Request = gated.Request
}
if gated.Response != nil {
payload.Response = gated.Response
if payload.Response == nil {
payload.Response = &Response{}
}
if gated.Response.StatusCode != 0 {
payload.Response.StatusCode = gated.Response.StatusCode
}
if gated.Response.Details != nil {
payload.Response.Details = gated.Response.Details
}
}
if !gated.Timestamp.IsZero() {
payload.Timestamp = gated.Timestamp
@@ -13,42 +13,43 @@ import "controller/custom_options/v1/options.proto";
// AuthMethod contains all fields related to an Auth Method resource
message AuthMethod {
// Output only. The ID of the Auth Method.
string id = 10;
string id = 10; // @gotags: `class:"public"`
// The ID of the Scope of which this Auth Method is a part.
string scope_id = 20 [json_name = "scope_id"];
string scope_id = 20 [json_name = "scope_id"]; // @gotags: `class:"public"`
// Output only. Scope information for this Auth method.
resources.scopes.v1.ScopeInfo scope = 30;
resources.scopes.v1.ScopeInfo scope = 30; // @gotags: `class:"public"`
// Optional name for identification purposes.
google.protobuf.StringValue name = 40 [(custom_options.v1.generate_sdk_option) = true, (custom_options.v1.mask_mapping) = { this: "name" that: "Name" }];
google.protobuf.StringValue name = 40 [(custom_options.v1.generate_sdk_option) = true, (custom_options.v1.mask_mapping) = { this: "name" that: "Name" }]; // @gotags: `class:"public"`
// Optional user-set description for identification purposes.
google.protobuf.StringValue description = 50 [(custom_options.v1.generate_sdk_option) = true, (custom_options.v1.mask_mapping) = { this: "description" that: "Description" }];
google.protobuf.StringValue description = 50
[(custom_options.v1.generate_sdk_option) = true, (custom_options.v1.mask_mapping) = { this: "description" that: "Description" }]; // @gotags: `class:"public"`
// Output only. The time this resource was created.
google.protobuf.Timestamp created_time = 60 [json_name = "created_time"];
google.protobuf.Timestamp created_time = 60 [json_name = "created_time"]; // @gotags: `class:"public"`
// Output only. The time this resource was last updated.
google.protobuf.Timestamp updated_time = 70 [json_name = "updated_time"];
google.protobuf.Timestamp updated_time = 70 [json_name = "updated_time"]; // @gotags: `class:"public"`
// Version is used in mutation requests, after the initial creation, to ensure this resource has not changed.
// The mutation will fail if the version does not match the latest known good version.
uint32 version = 80;
uint32 version = 80; // @gotags: `class:"public"`
// The Auth Method type.
string type = 90;
string type = 90; // @gotags: `class:"public"`
// The attributes that are applicable for the specific Auth Method type.
google.protobuf.Struct attributes = 100 [(custom_options.v1.generate_sdk_option) = true];
// Output only. Whether this auth method is the primary auth method for it's scope.
// To change this value update the primary_auth_method_id field on the scope.
bool is_primary = 110 [json_name = "is_primary"];
bool is_primary = 110 [json_name = "is_primary"]; // @gotags: `class:"public"`
// Output only. The available actions on this resource for this user.
repeated string authorized_actions = 300 [json_name = "authorized_actions"];
repeated string authorized_actions = 300 [json_name = "authorized_actions"]; // @gotags: `class:"public"`
// Output only. The authorized actions for the scope's collections.
map<string, google.protobuf.ListValue> authorized_collection_actions = 310 [json_name = "authorized_collection_actions"];
@@ -11,58 +11,62 @@ import "controller/custom_options/v1/options.proto";
message ScopeInfo {
// Output only. The ID of the Scope.
string id = 1;
string id = 1; // @gotags: `class:"public"`
// Output only. The type of the Scope.
string type = 2;
string type = 2; // @gotags: `class:"public"`
// Output only. The name of the Scope, if any.
string name = 3;
string name = 3; // @gotags: `class:"public"`
// Output only. The description of the Scope, if any.
string description = 4;
string description = 4; // @gotags: `class:"public"`
// Output only. The ID of the parent Scope, if any. This field will be empty if this is the "global" scope.
string parent_scope_id = 5 [json_name = "parent_scope_id"];
string parent_scope_id = 5 [json_name = "parent_scope_id"]; // @gotags: `class:"public"`
}
// Scope contains all fields related to a Scope resource
message Scope {
// Output only. The ID of the Scope.
string id = 10;
string id = 10; // @gotags: `class:"public"`
// The ID of the Scope this resource is in. If this is the "global" Scope this field will be empty.
string scope_id = 20 [json_name = "scope_id"];
string scope_id = 20 [json_name = "scope_id"]; // @gotags: `class:"public"`
// Output only. Scope information for this resource.
ScopeInfo scope = 30;
// Optional name for identification purposes.
google.protobuf.StringValue name = 40 [(custom_options.v1.generate_sdk_option) = true, (custom_options.v1.mask_mapping) = { this: "name" that: "name" }];
google.protobuf.StringValue name = 40 [(custom_options.v1.generate_sdk_option) = true, (custom_options.v1.mask_mapping) = { this: "name" that: "name" }]; // @gotags: `class:"public"`
// Optional user-set descripton for identification purposes.
google.protobuf.StringValue description = 50 [(custom_options.v1.generate_sdk_option) = true, (custom_options.v1.mask_mapping) = { this: "description" that: "description" }];
google.protobuf.StringValue description = 50
[(custom_options.v1.generate_sdk_option) = true, (custom_options.v1.mask_mapping) = { this: "description" that: "description" }]; // @gotags: `class:"public"`
// Output only. The time this resource was created.
google.protobuf.Timestamp created_time = 60 [json_name = "created_time"];
google.protobuf.Timestamp created_time = 60 [json_name = "created_time"]; // @gotags: `class:"public"`
// Output only. The time this resource was last updated.
google.protobuf.Timestamp updated_time = 70 [json_name = "updated_time"];
google.protobuf.Timestamp updated_time = 70 [json_name = "updated_time"]; // @gotags: `class:"public"`
// Version is used in mutation requests, after the initial creation, to ensure this resource has not changed.
// The mutation will fail if the version does not match the latest known good version.
uint32 version = 80;
uint32 version = 80; // @gotags: `class:"public"`
// The type of the resource.
string type = 90;
string type = 90; // @gotags: `class:"public"`
// The ID of the primary auth method for this scope. A primary auth method
// is allowed to vivify users when new accounts are created and is the source for the users account info
google.protobuf.StringValue primary_auth_method_id = 100
[json_name = "primary_auth_method_id", (custom_options.v1.generate_sdk_option) = true, (custom_options.v1.mask_mapping) = { this: "primary_auth_method_id" that: "PrimaryAuthMethodId" }];
google.protobuf.StringValue primary_auth_method_id = 100 [
json_name = "primary_auth_method_id",
(custom_options.v1.generate_sdk_option) = true,
(custom_options.v1.mask_mapping) = { this: "primary_auth_method_id" that: "PrimaryAuthMethodId" }
]; // @gotags: `class:"public"`
// Output only. The available actions on this resource for this user.
repeated string authorized_actions = 300 [json_name = "authorized_actions"];
repeated string authorized_actions = 300 [json_name = "authorized_actions"]; // @gotags: `class:"public"`
// Output only. The authorized actions for the scope's collections.
map<string, google.protobuf.ListValue> authorized_collection_actions = 310 [json_name = "authorized_collection_actions"];
@@ -200,14 +200,14 @@ message OidcTokenAttributes {
message AuthenticateRequest {
// The ID of the Auth Method in the system that should be used for authentication.
string auth_method_id = 1 [json_name = "auth_method_id"];
string auth_method_id = 1 [json_name = "auth_method_id"]; // @gotags: `class:"public"`
// This can be "cookie" or "token". If not provided, "token" will be used. "cookie" activates a split-cookie method where the token is split partially between http-only and regular cookies in order
// to keep it safe from rogue JS in the browser.
string token_type = 2 [json_name = "token_type"];
// Attributes are passed to the Auth Method; the valid keys and values depend on the type of Auth Method as well as the command.
google.protobuf.Struct attributes = 4 [json_name = "attributes"];
// The command to perform.
string command = 5 [json_name = "command"];
string command = 5 [json_name = "command"]; // @gotags: `class:"public"`
// Deprecated fields
reserved "credentials";
@@ -220,5 +220,5 @@ message AuthenticateResponse {
// Valid keys and values depend on the type of Auth Method as well as the command.
google.protobuf.Struct attributes = 4 [json_name = "attributes"];
// The command that was performed.
string command = 5 [json_name = "command"];
string command = 5 [json_name = "command"]; // @gotags: `class:"public"`
}
+1 -1
View File
@@ -216,7 +216,7 @@ func flushGatedEvents(ctx context.Context, method, url string, statusCode int, s
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to write and flush observation event"))
}
err = event.WriteAudit(ctx, "handler", event.WithFlush())
err = event.WriteAudit(ctx, "handler", event.WithFlush(), event.WithResponse(&event.Response{StatusCode: statusCode}))
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to write and flush audit event"))
}
+3 -3
View File
@@ -332,9 +332,9 @@ func Test_WrapWithEventsHandler(t *testing.T) {
Id: got.Data.(map[string]interface{})["request_info"].(map[string]interface{})["id"].(string),
}
hdr := map[string]interface{}{
"id": got.Data.(map[string]interface{})["id"].(string),
"timestamp": got.Data.(map[string]interface{})["timestamp"].(string),
"serialized_hmac": got.Data.(map[string]interface{})["serialized_hmac"].(string),
"id": got.Data.(map[string]interface{})["id"].(string),
"timestamp": got.Data.(map[string]interface{})["timestamp"].(string),
"response": got.Data.(map[string]interface{})["response"].(map[string]interface{}),
}
wantJson := testJson(t, event.AuditType, &info, event.Op(tt.name), got, hdr, nil)
assert.JSONEq(string(wantJson), string(actualJson))
@@ -7,6 +7,7 @@ import (
"strings"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/observability/event"
authtokenpb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/authtokens"
"google.golang.org/protobuf/proto"
@@ -20,13 +21,21 @@ const (
statusField = "status"
)
func OutgoingInterceptor(_ context.Context, w http.ResponseWriter, m proto.Message) error {
func OutgoingInterceptor(ctx context.Context, w http.ResponseWriter, m proto.Message) error {
const op = "handlers.OutgoingInterceptor"
m = m.ProtoReflect().Interface()
if !m.ProtoReflect().IsValid() {
// This would be the case if it's a nil pointer
w.WriteHeader(http.StatusNoContent)
return nil
}
// add the outgoing resp proto. See: common.flushGatedEvents is where the
// event.Response.StatusCode is added.
if err := event.WriteAudit(ctx, op, event.WithResponse(&event.Response{Details: m})); err != nil {
return fmt.Errorf("%s: unable to write audit event: %w", op, err)
}
switch m := m.(type) {
case *pbs.AuthenticateResponse:
if m.GetAttributes() == nil || m.GetAttributes().GetFields() == nil {