diff --git a/.gitignore b/.gitignore index 2d4571bcc..b936a8064 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,3 @@ override.tf.json *_override.tf *_override.tf.json *.terraform.lock.hcl - diff --git a/Makefile b/Makefile index 030c80384..71469f843 100644 --- a/Makefile +++ b/Makefile @@ -148,7 +148,7 @@ fmt-check: ## Lists formatting issues .PHONY: imports-check imports-check: ## Lists imports issues - @test -z $(shell goimports -l $(shell go list -f {{.Dir}} ./... | grep -v proto)) + @test -z $(shell goimports -l $(shell find . -type f -name '*.go' -not -path './vendor/*' -not -path './cli/cdk/go/proto/*')) .PHONY: run-api-example run-api-example: ## Run an API example like 'make run-api-example example=api/_examples/active-containers/main.go' @@ -186,6 +186,20 @@ test-resources: ## *CI ONLY* Prepares CI test containers go-component-from := integration/test_resources/cdk/go-component/bin/go-component go-component-to := ~/.config/lacework/components/go-component/go-component +.PHONY: cdk-go-component-ci +cdk-go-component-ci: ## Creates a go-component for development + scripts/prepare_test_resources.sh go_component + mkdir -p $(shell dirname $(go-component-to)) + echo '{"name":"go-component","description":"(dev-mode) A go-component for development","type":"CLI_COMMAND","artifacts":[],"breadcrumbs":{},"version":"0.0.0-dev"}' \ + > $(shell dirname $(go-component-to))/.dev +ifeq (x86_64, $(shell uname -m)) + cp $(go-component-from)-$(shell uname -s | tr '[:upper:]' '[:lower:]')-amd64 $(go-component-to) +else ifeq (arm64, $(shell uname -m)) + cp $(go-component-from)-$(shell uname -s | tr '[:upper:]' '[:lower:]')-arm64 $(go-component-to) +else + cp $(go-component-from)-$(shell uname -s | tr '[:upper:]' '[:lower:]')-386 $(go-component-to) +endif + .PHONY: cdk-go-component cdk-go-component: install-cli ## Creates a go-component for development scripts/prepare_test_resources.sh go_component diff --git a/cli/cdk/go/proto/v1/cdk.pb.go b/cli/cdk/go/proto/v1/cdk.pb.go index 8a43308eb..2e5716254 100644 --- a/cli/cdk/go/proto/v1/cdk.pb.go +++ b/cli/cdk/go/proto/v1/cdk.pb.go @@ -19,17 +19,17 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.28.1 -// protoc v3.21.9 +// protoc v3.12.4 // source: proto/v1/cdk.proto package cdk import ( - reflect "reflect" - sync "sync" - + timestamp "github.com/golang/protobuf/ptypes/timestamp" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" ) const ( @@ -244,43 +244,300 @@ func (x *HoneyventRequest) GetDurationMs() int64 { return 0 } +// WriteCacheRequest is used to submit data that should be written to cache, included its expiry +type WriteCacheRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // name of the cache key to write + Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` + Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` + Expires *timestamp.Timestamp `protobuf:"bytes,3,opt,name=expires,proto3" json:"expires,omitempty"` +} + +func (x *WriteCacheRequest) Reset() { + *x = WriteCacheRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_v1_cdk_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *WriteCacheRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*WriteCacheRequest) ProtoMessage() {} + +func (x *WriteCacheRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_v1_cdk_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use WriteCacheRequest.ProtoReflect.Descriptor instead. +func (*WriteCacheRequest) Descriptor() ([]byte, []int) { + return file_proto_v1_cdk_proto_rawDescGZIP(), []int{4} +} + +func (x *WriteCacheRequest) GetKey() string { + if x != nil { + return x.Key + } + return "" +} + +func (x *WriteCacheRequest) GetData() []byte { + if x != nil { + return x.Data + } + return nil +} + +func (x *WriteCacheRequest) GetExpires() *timestamp.Timestamp { + if x != nil { + return x.Expires + } + return nil +} + +// ReadCacheRequest is used to fetch data from the cache +type ReadCacheRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // name of the cache key to read + Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` +} + +func (x *ReadCacheRequest) Reset() { + *x = ReadCacheRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_v1_cdk_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ReadCacheRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ReadCacheRequest) ProtoMessage() {} + +func (x *ReadCacheRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_v1_cdk_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ReadCacheRequest.ProtoReflect.Descriptor instead. +func (*ReadCacheRequest) Descriptor() ([]byte, []int) { + return file_proto_v1_cdk_proto_rawDescGZIP(), []int{5} +} + +func (x *ReadCacheRequest) GetKey() string { + if x != nil { + return x.Key + } + return "" +} + +// ReadCacheResponse is the response type after a ReadCacheRequest is made +type ReadCacheResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // hit is true when data was found, false otherwise + Hit bool `protobuf:"varint,1,opt,name=hit,proto3" json:"hit,omitempty"` + // empty if cache miss (hit == false) + Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` +} + +func (x *ReadCacheResponse) Reset() { + *x = ReadCacheResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_v1_cdk_proto_msgTypes[6] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ReadCacheResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ReadCacheResponse) ProtoMessage() {} + +func (x *ReadCacheResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_v1_cdk_proto_msgTypes[6] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ReadCacheResponse.ProtoReflect.Descriptor instead. +func (*ReadCacheResponse) Descriptor() ([]byte, []int) { + return file_proto_v1_cdk_proto_rawDescGZIP(), []int{6} +} + +func (x *ReadCacheResponse) GetHit() bool { + if x != nil { + return x.Hit + } + return false +} + +func (x *ReadCacheResponse) GetData() []byte { + if x != nil { + return x.Data + } + return nil +} + +// WriteCacheResult is the response type after a WriteCacheRequest is made +type WriteCacheResult struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Error bool `protobuf:"varint,1,opt,name=error,proto3" json:"error,omitempty"` + // message stores the error body if error == true, otherwise empty + Message string `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"` +} + +func (x *WriteCacheResult) Reset() { + *x = WriteCacheResult{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_v1_cdk_proto_msgTypes[7] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *WriteCacheResult) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*WriteCacheResult) ProtoMessage() {} + +func (x *WriteCacheResult) ProtoReflect() protoreflect.Message { + mi := &file_proto_v1_cdk_proto_msgTypes[7] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use WriteCacheResult.ProtoReflect.Descriptor instead. +func (*WriteCacheResult) Descriptor() ([]byte, []int) { + return file_proto_v1_cdk_proto_rawDescGZIP(), []int{7} +} + +func (x *WriteCacheResult) GetError() bool { + if x != nil { + return x.Error + } + return false +} + +func (x *WriteCacheResult) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + var File_proto_v1_cdk_proto protoreflect.FileDescriptor var file_proto_v1_cdk_proto_rawDesc = []byte{ 0x0a, 0x12, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x76, 0x31, 0x2f, 0x63, 0x64, 0x6b, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, 0x22, 0x34, 0x0a, 0x0b, - 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x25, 0x0a, 0x0e, 0x63, - 0x6f, 0x6d, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x0d, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x4e, 0x61, - 0x6d, 0x65, 0x22, 0x25, 0x0a, 0x09, 0x50, 0x6f, 0x6e, 0x67, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, - 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, 0x07, 0x0a, 0x05, 0x52, 0x65, 0x70, - 0x6c, 0x79, 0x22, 0xf1, 0x01, 0x0a, 0x10, 0x48, 0x6f, 0x6e, 0x65, 0x79, 0x76, 0x65, 0x6e, 0x74, - 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x66, 0x65, 0x61, 0x74, 0x75, - 0x72, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x66, 0x65, 0x61, 0x74, 0x75, 0x72, - 0x65, 0x12, 0x4c, 0x0a, 0x0c, 0x66, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65, 0x5f, 0x64, 0x61, 0x74, - 0x61, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x29, 0x2e, 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, - 0x2e, 0x48, 0x6f, 0x6e, 0x65, 0x79, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x2e, 0x46, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65, 0x44, 0x61, 0x74, 0x61, 0x45, 0x6e, 0x74, - 0x72, 0x79, 0x52, 0x0b, 0x66, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65, 0x44, 0x61, 0x74, 0x61, 0x12, - 0x14, 0x0a, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, - 0x65, 0x72, 0x72, 0x6f, 0x72, 0x12, 0x1f, 0x0a, 0x0b, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, - 0x6e, 0x5f, 0x6d, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0a, 0x64, 0x75, 0x72, 0x61, - 0x74, 0x69, 0x6f, 0x6e, 0x4d, 0x73, 0x1a, 0x3e, 0x0a, 0x10, 0x46, 0x65, 0x61, 0x74, 0x75, 0x72, - 0x65, 0x44, 0x61, 0x74, 0x61, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, - 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, - 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, - 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x32, 0x70, 0x0a, 0x04, 0x43, 0x6f, 0x72, 0x65, 0x12, 0x30, - 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x13, 0x2e, 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, 0x2e, - 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x11, 0x2e, 0x63, 0x64, - 0x6b, 0x2e, 0x76, 0x31, 0x2e, 0x50, 0x6f, 0x6e, 0x67, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, - 0x12, 0x36, 0x0a, 0x09, 0x48, 0x6f, 0x6e, 0x65, 0x79, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x18, 0x2e, - 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, 0x2e, 0x48, 0x6f, 0x6e, 0x65, 0x79, 0x76, 0x65, 0x6e, 0x74, - 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0d, 0x2e, 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, - 0x2e, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x42, 0x26, 0x5a, 0x24, 0x6c, 0x61, 0x63, 0x65, - 0x77, 0x6f, 0x72, 0x6b, 0x2f, 0x67, 0x6f, 0x2d, 0x73, 0x64, 0x6b, 0x2f, 0x63, 0x6c, 0x69, 0x2f, - 0x63, 0x64, 0x6b, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x64, 0x6b, - 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, 0x1a, 0x1f, 0x67, 0x6f, + 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x74, 0x69, + 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x34, 0x0a, + 0x0b, 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x25, 0x0a, 0x0e, + 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x4e, + 0x61, 0x6d, 0x65, 0x22, 0x25, 0x0a, 0x09, 0x50, 0x6f, 0x6e, 0x67, 0x52, 0x65, 0x70, 0x6c, 0x79, + 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, 0x07, 0x0a, 0x05, 0x52, 0x65, + 0x70, 0x6c, 0x79, 0x22, 0xf1, 0x01, 0x0a, 0x10, 0x48, 0x6f, 0x6e, 0x65, 0x79, 0x76, 0x65, 0x6e, + 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x66, 0x65, 0x61, 0x74, + 0x75, 0x72, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x66, 0x65, 0x61, 0x74, 0x75, + 0x72, 0x65, 0x12, 0x4c, 0x0a, 0x0c, 0x66, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65, 0x5f, 0x64, 0x61, + 0x74, 0x61, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x29, 0x2e, 0x63, 0x64, 0x6b, 0x2e, 0x76, + 0x31, 0x2e, 0x48, 0x6f, 0x6e, 0x65, 0x79, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x2e, 0x46, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65, 0x44, 0x61, 0x74, 0x61, 0x45, 0x6e, + 0x74, 0x72, 0x79, 0x52, 0x0b, 0x66, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65, 0x44, 0x61, 0x74, 0x61, + 0x12, 0x14, 0x0a, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x12, 0x1f, 0x0a, 0x0b, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, + 0x6f, 0x6e, 0x5f, 0x6d, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0a, 0x64, 0x75, 0x72, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x4d, 0x73, 0x1a, 0x3e, 0x0a, 0x10, 0x46, 0x65, 0x61, 0x74, 0x75, + 0x72, 0x65, 0x44, 0x61, 0x74, 0x61, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, + 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, + 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, + 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x6f, 0x0a, 0x11, 0x57, 0x72, 0x69, 0x74, 0x65, + 0x43, 0x61, 0x63, 0x68, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, + 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x12, + 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, + 0x74, 0x61, 0x12, 0x34, 0x0a, 0x07, 0x65, 0x78, 0x70, 0x69, 0x72, 0x65, 0x73, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, + 0x07, 0x65, 0x78, 0x70, 0x69, 0x72, 0x65, 0x73, 0x22, 0x24, 0x0a, 0x10, 0x52, 0x65, 0x61, 0x64, + 0x43, 0x61, 0x63, 0x68, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, + 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x39, + 0x0a, 0x11, 0x52, 0x65, 0x61, 0x64, 0x43, 0x61, 0x63, 0x68, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x68, 0x69, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, + 0x52, 0x03, 0x68, 0x69, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x42, 0x0a, 0x10, 0x57, 0x72, 0x69, + 0x74, 0x65, 0x43, 0x61, 0x63, 0x68, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x12, 0x14, 0x0a, + 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x65, 0x72, + 0x72, 0x6f, 0x72, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x32, 0xf9, 0x01, + 0x0a, 0x04, 0x43, 0x6f, 0x72, 0x65, 0x12, 0x30, 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x13, + 0x2e, 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, 0x2e, 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x1a, 0x11, 0x2e, 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, 0x2e, 0x50, 0x6f, 0x6e, + 0x67, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x36, 0x0a, 0x09, 0x48, 0x6f, 0x6e, 0x65, + 0x79, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x18, 0x2e, 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, 0x2e, 0x48, + 0x6f, 0x6e, 0x65, 0x79, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x0d, 0x2e, 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, 0x2e, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, + 0x12, 0x42, 0x0a, 0x09, 0x52, 0x65, 0x61, 0x64, 0x43, 0x61, 0x63, 0x68, 0x65, 0x12, 0x18, 0x2e, + 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, 0x2e, 0x52, 0x65, 0x61, 0x64, 0x43, 0x61, 0x63, 0x68, 0x65, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, + 0x2e, 0x52, 0x65, 0x61, 0x64, 0x43, 0x61, 0x63, 0x68, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x22, 0x00, 0x12, 0x43, 0x0a, 0x0a, 0x57, 0x72, 0x69, 0x74, 0x65, 0x43, 0x61, 0x63, + 0x68, 0x65, 0x12, 0x19, 0x2e, 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, 0x2e, 0x57, 0x72, 0x69, 0x74, + 0x65, 0x43, 0x61, 0x63, 0x68, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x18, 0x2e, + 0x63, 0x64, 0x6b, 0x2e, 0x76, 0x31, 0x2e, 0x57, 0x72, 0x69, 0x74, 0x65, 0x43, 0x61, 0x63, 0x68, + 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x00, 0x42, 0x26, 0x5a, 0x24, 0x6c, 0x61, 0x63, + 0x65, 0x77, 0x6f, 0x72, 0x6b, 0x2f, 0x67, 0x6f, 0x2d, 0x73, 0x64, 0x6b, 0x2f, 0x63, 0x6c, 0x69, + 0x2f, 0x63, 0x64, 0x6b, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x64, + 0x6b, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -295,25 +552,35 @@ func file_proto_v1_cdk_proto_rawDescGZIP() []byte { return file_proto_v1_cdk_proto_rawDescData } -var file_proto_v1_cdk_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_proto_v1_cdk_proto_msgTypes = make([]protoimpl.MessageInfo, 9) var file_proto_v1_cdk_proto_goTypes = []interface{}{ - (*PingRequest)(nil), // 0: cdk.v1.PingRequest - (*PongReply)(nil), // 1: cdk.v1.PongReply - (*Reply)(nil), // 2: cdk.v1.Reply - (*HoneyventRequest)(nil), // 3: cdk.v1.HoneyventRequest - nil, // 4: cdk.v1.HoneyventRequest.FeatureDataEntry + (*PingRequest)(nil), // 0: cdk.v1.PingRequest + (*PongReply)(nil), // 1: cdk.v1.PongReply + (*Reply)(nil), // 2: cdk.v1.Reply + (*HoneyventRequest)(nil), // 3: cdk.v1.HoneyventRequest + (*WriteCacheRequest)(nil), // 4: cdk.v1.WriteCacheRequest + (*ReadCacheRequest)(nil), // 5: cdk.v1.ReadCacheRequest + (*ReadCacheResponse)(nil), // 6: cdk.v1.ReadCacheResponse + (*WriteCacheResult)(nil), // 7: cdk.v1.WriteCacheResult + nil, // 8: cdk.v1.HoneyventRequest.FeatureDataEntry + (*timestamp.Timestamp)(nil), // 9: google.protobuf.Timestamp } var file_proto_v1_cdk_proto_depIdxs = []int32{ - 4, // 0: cdk.v1.HoneyventRequest.feature_data:type_name -> cdk.v1.HoneyventRequest.FeatureDataEntry - 0, // 1: cdk.v1.Core.Ping:input_type -> cdk.v1.PingRequest - 3, // 2: cdk.v1.Core.Honeyvent:input_type -> cdk.v1.HoneyventRequest - 1, // 3: cdk.v1.Core.Ping:output_type -> cdk.v1.PongReply - 2, // 4: cdk.v1.Core.Honeyvent:output_type -> cdk.v1.Reply - 3, // [3:5] is the sub-list for method output_type - 1, // [1:3] is the sub-list for method input_type - 1, // [1:1] is the sub-list for extension type_name - 1, // [1:1] is the sub-list for extension extendee - 0, // [0:1] is the sub-list for field type_name + 8, // 0: cdk.v1.HoneyventRequest.feature_data:type_name -> cdk.v1.HoneyventRequest.FeatureDataEntry + 9, // 1: cdk.v1.WriteCacheRequest.expires:type_name -> google.protobuf.Timestamp + 0, // 2: cdk.v1.Core.Ping:input_type -> cdk.v1.PingRequest + 3, // 3: cdk.v1.Core.Honeyvent:input_type -> cdk.v1.HoneyventRequest + 5, // 4: cdk.v1.Core.ReadCache:input_type -> cdk.v1.ReadCacheRequest + 4, // 5: cdk.v1.Core.WriteCache:input_type -> cdk.v1.WriteCacheRequest + 1, // 6: cdk.v1.Core.Ping:output_type -> cdk.v1.PongReply + 2, // 7: cdk.v1.Core.Honeyvent:output_type -> cdk.v1.Reply + 6, // 8: cdk.v1.Core.ReadCache:output_type -> cdk.v1.ReadCacheResponse + 7, // 9: cdk.v1.Core.WriteCache:output_type -> cdk.v1.WriteCacheResult + 6, // [6:10] is the sub-list for method output_type + 2, // [2:6] is the sub-list for method input_type + 2, // [2:2] is the sub-list for extension type_name + 2, // [2:2] is the sub-list for extension extendee + 0, // [0:2] is the sub-list for field type_name } func init() { file_proto_v1_cdk_proto_init() } @@ -370,6 +637,54 @@ func file_proto_v1_cdk_proto_init() { return nil } } + file_proto_v1_cdk_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*WriteCacheRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_v1_cdk_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ReadCacheRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_v1_cdk_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ReadCacheResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_v1_cdk_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*WriteCacheResult); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } type x struct{} out := protoimpl.TypeBuilder{ @@ -377,7 +692,7 @@ func file_proto_v1_cdk_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_proto_v1_cdk_proto_rawDesc, NumEnums: 0, - NumMessages: 5, + NumMessages: 9, NumExtensions: 0, NumServices: 1, }, diff --git a/cli/cdk/go/proto/v1/cdk_grpc.pb.go b/cli/cdk/go/proto/v1/cdk_grpc.pb.go index 2336f22b4..7431fd74e 100644 --- a/cli/cdk/go/proto/v1/cdk_grpc.pb.go +++ b/cli/cdk/go/proto/v1/cdk_grpc.pb.go @@ -1,14 +1,13 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: // - protoc-gen-go-grpc v1.2.0 -// - protoc v3.21.9 +// - protoc v3.12.4 // source: proto/v1/cdk.proto package cdk import ( context "context" - grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" @@ -24,10 +23,15 @@ const _ = grpc.SupportPackageIsVersion7 // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type CoreClient interface { // Sends a ping -> pong between server and client - // Component -> CDK Server + // + // Component -> CDK Server Ping(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*PongReply, error) // Sends a Honeyvent Honeyvent(ctx context.Context, in *HoneyventRequest, opts ...grpc.CallOption) (*Reply, error) + // Read from CLI cache + ReadCache(ctx context.Context, in *ReadCacheRequest, opts ...grpc.CallOption) (*ReadCacheResponse, error) + // Write to CLI cache + WriteCache(ctx context.Context, in *WriteCacheRequest, opts ...grpc.CallOption) (*WriteCacheResult, error) } type coreClient struct { @@ -56,15 +60,38 @@ func (c *coreClient) Honeyvent(ctx context.Context, in *HoneyventRequest, opts . return out, nil } +func (c *coreClient) ReadCache(ctx context.Context, in *ReadCacheRequest, opts ...grpc.CallOption) (*ReadCacheResponse, error) { + out := new(ReadCacheResponse) + err := c.cc.Invoke(ctx, "/cdk.v1.Core/ReadCache", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *coreClient) WriteCache(ctx context.Context, in *WriteCacheRequest, opts ...grpc.CallOption) (*WriteCacheResult, error) { + out := new(WriteCacheResult) + err := c.cc.Invoke(ctx, "/cdk.v1.Core/WriteCache", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // CoreServer is the server API for Core service. // All implementations must embed UnimplementedCoreServer // for forward compatibility type CoreServer interface { // Sends a ping -> pong between server and client - // Component -> CDK Server + // + // Component -> CDK Server Ping(context.Context, *PingRequest) (*PongReply, error) // Sends a Honeyvent Honeyvent(context.Context, *HoneyventRequest) (*Reply, error) + // Read from CLI cache + ReadCache(context.Context, *ReadCacheRequest) (*ReadCacheResponse, error) + // Write to CLI cache + WriteCache(context.Context, *WriteCacheRequest) (*WriteCacheResult, error) mustEmbedUnimplementedCoreServer() } @@ -78,6 +105,12 @@ func (UnimplementedCoreServer) Ping(context.Context, *PingRequest) (*PongReply, func (UnimplementedCoreServer) Honeyvent(context.Context, *HoneyventRequest) (*Reply, error) { return nil, status.Errorf(codes.Unimplemented, "method Honeyvent not implemented") } +func (UnimplementedCoreServer) ReadCache(context.Context, *ReadCacheRequest) (*ReadCacheResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method ReadCache not implemented") +} +func (UnimplementedCoreServer) WriteCache(context.Context, *WriteCacheRequest) (*WriteCacheResult, error) { + return nil, status.Errorf(codes.Unimplemented, "method WriteCache not implemented") +} func (UnimplementedCoreServer) mustEmbedUnimplementedCoreServer() {} // UnsafeCoreServer may be embedded to opt out of forward compatibility for this service. @@ -127,6 +160,42 @@ func _Core_Honeyvent_Handler(srv interface{}, ctx context.Context, dec func(inte return interceptor(ctx, in, info, handler) } +func _Core_ReadCache_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ReadCacheRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(CoreServer).ReadCache(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/cdk.v1.Core/ReadCache", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(CoreServer).ReadCache(ctx, req.(*ReadCacheRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _Core_WriteCache_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(WriteCacheRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(CoreServer).WriteCache(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/cdk.v1.Core/WriteCache", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(CoreServer).WriteCache(ctx, req.(*WriteCacheRequest)) + } + return interceptor(ctx, in, info, handler) +} + // Core_ServiceDesc is the grpc.ServiceDesc for Core service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) @@ -142,6 +211,14 @@ var Core_ServiceDesc = grpc.ServiceDesc{ MethodName: "Honeyvent", Handler: _Core_Honeyvent_Handler, }, + { + MethodName: "ReadCache", + Handler: _Core_ReadCache_Handler, + }, + { + MethodName: "WriteCache", + Handler: _Core_WriteCache_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "proto/v1/cdk.proto", diff --git a/cli/cmd/cache.go b/cli/cmd/cache.go index bea704e0d..4b81f8a7a 100644 --- a/cli/cmd/cache.go +++ b/cli/cmd/cache.go @@ -204,20 +204,27 @@ type cliAsset struct { ExpiresAt time.Time `json:"expires_at"` } -// WriteAssetToCache stores an asset with an expiration time +// writeAssetToCache stores an asset with an expiration time and returns errors // // Simple Example: Having a struct named vulnReport // // ```go // cli.WriteAssetToCache("my-report", time.Now().Add(time.Hour * 1), vulnReport{Foo: "bar"}) // ``` -func (c *cliState) WriteAssetToCache(key string, expiresAt time.Time, data interface{}) { +// writeAssetToCache stores an asset with an expiration time +// +// Simple Example: Having a struct named vulnReport +// +// ```go +// cli.WriteAssetToCache("my-report", time.Now().Add(time.Hour * 1), vulnReport{Foo: "bar"}) +// ``` +func (c *cliState) writeAssetToCache(key string, expiresAt time.Time, data interface{}) error { if c.noCache { - return + return nil } if expiresAt.Before(time.Now()) { - return // avoid writing assets that are already expired + return nil // avoid writing assets that are already expired } c.Log.Debugw("saving asset", @@ -226,7 +233,12 @@ func (c *cliState) WriteAssetToCache(key string, expiresAt time.Time, data inter "data", data, "expires_at", expiresAt, ) - err := c.Cache.Write(key, structToString(cliAsset{data, expiresAt})) + return c.Cache.Write(key, structToString(cliAsset{data, expiresAt})) +} + +// WriteAssetToCache wraps WriteAssetToCacheErroring and squashes errors +func (c *cliState) WriteAssetToCache(key string, expiresAt time.Time, data interface{}) { + err := c.writeAssetToCache(key, expiresAt, data) if err != nil { c.Log.Warnw("unable to write asset in cache", "feature", "cache", diff --git a/cli/cmd/cdk.go b/cli/cmd/cdk.go index 93ad01cff..684d1299d 100644 --- a/cli/cmd/cdk.go +++ b/cli/cmd/cdk.go @@ -62,6 +62,34 @@ func (c *cliState) GrpcTarget() string { return fmt.Sprintf("localhost:%v", c.cdkServerPort) } +func (c *cliState) ReadCache(ctx context.Context, in *cdk.ReadCacheRequest) (*cdk.ReadCacheResponse, error) { + if in.Key == "" { + return nil, errors.New("cache key must be supplied") + } + + var data []byte + if !c.ReadCachedAsset(in.Key, &data) { // not expired + return &cdk.ReadCacheResponse{Hit: true, Data: data}, nil + } + return &cdk.ReadCacheResponse{ + Hit: false, + Data: nil, + }, nil +} + +func (c *cliState) WriteCache(ctx context.Context, in *cdk.WriteCacheRequest) (*cdk.WriteCacheResult, error) { + if in.Key == "" { + return nil, errors.New("cache key must be supplied") + } + + err := c.writeAssetToCache(in.Key, in.Expires.AsTime(), in.Data) + if err != nil { + msg := err.Error() + return &cdk.WriteCacheResult{Error: true, Message: msg}, nil + } + return &cdk.WriteCacheResult{Error: false, Message: ""}, nil +} + // Ping implements CDK.Ping func (c *cliState) Ping(ctx context.Context, in *cdk.PingRequest) (*cdk.PongReply, error) { c.Log.Debugw("message", "from", "CDK/Ping", "component_name", in.GetComponentName()) diff --git a/integration/component_test.go b/integration/component_test.go index 50360b74b..24d6ef80c 100644 --- a/integration/component_test.go +++ b/integration/component_test.go @@ -189,6 +189,17 @@ func TestComponentDevModeGolangFromScratch(t *testing.T) { assert.Contains(t, out.String(), fmt.Sprintf("> %s v0.0.0-dev", cName), "the test component SHOULD be shown in version command, check!") }) + + t.Run("execute component to test cache", func(t *testing.T) { + out, err, exitcode := LaceworkCLIWithHome(dir, cName, "writecache") + assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one") + assert.Empty(t, err.String(), "STDERR should be empty") + + out, err, exitcode = LaceworkCLIWithHome(dir, cName, "readcache") + assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one") + assert.Empty(t, err.String(), "STDERR should be empty") + assert.Contains(t, out.String(), "data data data") + }) } func TestComponentDevModeGolangScaffolding(t *testing.T) { diff --git a/integration/test_resources/cdk/go-component/bin/go-component-darwin-amd64 b/integration/test_resources/cdk/go-component/bin/go-component-darwin-amd64 index 1666e7c8b..591b98684 100755 Binary files a/integration/test_resources/cdk/go-component/bin/go-component-darwin-amd64 and b/integration/test_resources/cdk/go-component/bin/go-component-darwin-amd64 differ diff --git a/integration/test_resources/cdk/go-component/bin/go-component-darwin-arm64 b/integration/test_resources/cdk/go-component/bin/go-component-darwin-arm64 index b9b696433..723abbfda 100755 Binary files a/integration/test_resources/cdk/go-component/bin/go-component-darwin-arm64 and b/integration/test_resources/cdk/go-component/bin/go-component-darwin-arm64 differ diff --git a/integration/test_resources/cdk/go-component/bin/go-component-linux-386 b/integration/test_resources/cdk/go-component/bin/go-component-linux-386 index e630f1389..959b58f4c 100755 Binary files a/integration/test_resources/cdk/go-component/bin/go-component-linux-386 and b/integration/test_resources/cdk/go-component/bin/go-component-linux-386 differ diff --git a/integration/test_resources/cdk/go-component/bin/go-component-linux-amd64 b/integration/test_resources/cdk/go-component/bin/go-component-linux-amd64 index 7bfcc6e38..ae0b4e8f4 100755 Binary files a/integration/test_resources/cdk/go-component/bin/go-component-linux-amd64 and b/integration/test_resources/cdk/go-component/bin/go-component-linux-amd64 differ diff --git a/integration/test_resources/cdk/go-component/bin/go-component-linux-arm b/integration/test_resources/cdk/go-component/bin/go-component-linux-arm index d11065cb6..78a43a7f2 100755 Binary files a/integration/test_resources/cdk/go-component/bin/go-component-linux-arm and b/integration/test_resources/cdk/go-component/bin/go-component-linux-arm differ diff --git a/integration/test_resources/cdk/go-component/bin/go-component-linux-arm64 b/integration/test_resources/cdk/go-component/bin/go-component-linux-arm64 index 1fbe602f8..221ffd8e0 100755 Binary files a/integration/test_resources/cdk/go-component/bin/go-component-linux-arm64 and b/integration/test_resources/cdk/go-component/bin/go-component-linux-arm64 differ diff --git a/integration/test_resources/cdk/go-component/bin/go-component-windows-386.exe b/integration/test_resources/cdk/go-component/bin/go-component-windows-386.exe index d5753dda1..ae65035e0 100755 Binary files a/integration/test_resources/cdk/go-component/bin/go-component-windows-386.exe and b/integration/test_resources/cdk/go-component/bin/go-component-windows-386.exe differ diff --git a/integration/test_resources/cdk/go-component/bin/go-component-windows-amd64.exe b/integration/test_resources/cdk/go-component/bin/go-component-windows-amd64.exe index cd2588f6a..c30d29828 100755 Binary files a/integration/test_resources/cdk/go-component/bin/go-component-windows-amd64.exe and b/integration/test_resources/cdk/go-component/bin/go-component-windows-amd64.exe differ diff --git a/integration/test_resources/cdk/go-component/main.go b/integration/test_resources/cdk/go-component/main.go index f281c2aae..2a8923274 100644 --- a/integration/test_resources/cdk/go-component/main.go +++ b/integration/test_resources/cdk/go-component/main.go @@ -28,6 +28,7 @@ import ( cdk "github.com/lacework/go-sdk/cli/cdk/go/proto/v1" "github.com/lacework/go-sdk/lwlogger" "github.com/pkg/errors" + "google.golang.org/protobuf/types/known/timestamppb" ) var log = lwlogger.New("").Sugar() @@ -105,6 +106,27 @@ func app() error { case "fail": return errors.New("Purposely failing...") + case "writecache": + r, err := cdkClient.WriteCache(context.Background(), &cdk.WriteCacheRequest{ + Key: "test_this_cache", + Data: []byte("data data data"), + Expires: timestamppb.New(time.Now().Add(time.Hour * 1)), + }) + if err != nil { + return err + } + if r.Error { + return fmt.Errorf("failed to write to cache: %s", r.Message) + } + return nil + + case "readcache": + r, _ := cdkClient.ReadCache(context.Background(), &cdk.ReadCacheRequest{ + Key: "test_this_cache", + }) + fmt.Println(string(r.Data)) + return nil + default: return help() } diff --git a/proto/v1/cdk.proto b/proto/v1/cdk.proto index caf30496f..57b20cead 100644 --- a/proto/v1/cdk.proto +++ b/proto/v1/cdk.proto @@ -18,6 +18,8 @@ syntax = "proto3"; +import "google/protobuf/timestamp.proto"; + // defines the go package that will be generated with protoc option go_package = "lacework/go-sdk/cli/cdk/proto/v1;cdk"; @@ -31,6 +33,12 @@ service Core { // Sends a Honeyvent rpc Honeyvent(HoneyventRequest) returns (Reply) {} + + // Read from CLI cache + rpc ReadCache(ReadCacheRequest) returns (ReadCacheResponse) {} + + // Write to CLI cache + rpc WriteCache(WriteCacheRequest) returns (WriteCacheResult) {} } message PingRequest { @@ -52,3 +60,31 @@ message HoneyventRequest { int64 duration_ms = 4; } +// WriteCacheRequest is used to submit data that should be written to cache, included its expiry +message WriteCacheRequest { + // name of the cache key to write + string key = 1; + bytes data = 2; + google.protobuf.Timestamp expires = 3; +} + +// ReadCacheRequest is used to fetch data from the cache +message ReadCacheRequest { + // name of the cache key to read + string key = 1; +} + +// ReadCacheResponse is the response type after a ReadCacheRequest is made +message ReadCacheResponse { + // hit is true when data was found, false otherwise + bool hit = 1; + // empty if cache miss (hit == false) + bytes data = 2; +} + +// WriteCacheResult is the response type after a WriteCacheRequest is made +message WriteCacheResult { + bool error = 1; + // message stores the error body if error == true, otherwise empty + string message = 2; +}