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

feat(cdk): expose cache to components #1441

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ override.tf.json
*_override.tf
*_override.tf.json
*.terraform.lock.hcl

16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/*'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not platform agnostic but it's fine, I don't think we run this test in CI on windows...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at the commit message on e043b66

It doesn't seem like go-list has a better way to solve this, but I'm open to ideas!


.PHONY: run-api-example
run-api-example: ## Run an API example like 'make run-api-example example=api/_examples/active-containers/main.go'
Expand Down Expand Up @@ -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
Expand Down
423 changes: 369 additions & 54 deletions cli/cdk/go/proto/v1/cdk.pb.go

Large diffs are not rendered by default.

85 changes: 81 additions & 4 deletions cli/cdk/go/proto/v1/cdk_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 17 additions & 5 deletions cli/cmd/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions cli/cmd/cdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading