Skip to content

Commit

Permalink
feat(cdk): expose cache to components (#1441)
Browse files Browse the repository at this point in the history
* feat(GROW-2498): expose cache to components

* chore(goimports): change imports-check from go list to find

The previous imports-check target uses `go list` to find code to scan.
The problem is that `go list` produces a listing of directories to input
into goimports.

/a/b/c
/a/b/c/d
/a/b/c/e ---> example we want to exclude

In the example listing above, we want to exclude `e`. That was previously
done by excluding `e` via a `grep -v` command that followed `go list`.
The result is that the path with `e` on the end is excluded, but `/a/b/c`
is still scanned causing the excluded path to get scanned anyway. This
commit swaps all this out for a find command that excludes the paths and
returns filenames as input, not directories.

* chore: add ci build of go-component test resources

* chore: add integration test binaries back

These can be refreshed using the make cdk-go-component-ci command,
however it'll cause local integration tests to fail unless folks rebuild
these manually everytime on their machines. In addition, its slowing the
integration tests down substantially on an already long-running job.
These resources should be fairly static so we're making the choice to
include these resources statically.

* refactor: update cdk cache interface based on feedback
  • Loading branch information
Matt Cadorette authored Dec 14, 2023
1 parent 5b898f7 commit af026bb
Show file tree
Hide file tree
Showing 17 changed files with 579 additions and 65 deletions.
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/*'))

.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

0 comments on commit af026bb

Please sign in to comment.