Skip to content

Commit af026bb

Browse files
author
Matt Cadorette
authored
feat(cdk): expose cache to components (#1441)
* 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
1 parent 5b898f7 commit af026bb

17 files changed

+579
-65
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,3 @@ override.tf.json
3232
*_override.tf
3333
*_override.tf.json
3434
*.terraform.lock.hcl
35-

Makefile

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fmt-check: ## Lists formatting issues
148148

149149
.PHONY: imports-check
150150
imports-check: ## Lists imports issues
151-
@test -z $(shell goimports -l $(shell go list -f {{.Dir}} ./... | grep -v proto))
151+
@test -z $(shell goimports -l $(shell find . -type f -name '*.go' -not -path './vendor/*' -not -path './cli/cdk/go/proto/*'))
152152

153153
.PHONY: run-api-example
154154
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
186186
go-component-from := integration/test_resources/cdk/go-component/bin/go-component
187187
go-component-to := ~/.config/lacework/components/go-component/go-component
188188

189+
.PHONY: cdk-go-component-ci
190+
cdk-go-component-ci: ## Creates a go-component for development
191+
scripts/prepare_test_resources.sh go_component
192+
mkdir -p $(shell dirname $(go-component-to))
193+
echo '{"name":"go-component","description":"(dev-mode) A go-component for development","type":"CLI_COMMAND","artifacts":[],"breadcrumbs":{},"version":"0.0.0-dev"}' \
194+
> $(shell dirname $(go-component-to))/.dev
195+
ifeq (x86_64, $(shell uname -m))
196+
cp $(go-component-from)-$(shell uname -s | tr '[:upper:]' '[:lower:]')-amd64 $(go-component-to)
197+
else ifeq (arm64, $(shell uname -m))
198+
cp $(go-component-from)-$(shell uname -s | tr '[:upper:]' '[:lower:]')-arm64 $(go-component-to)
199+
else
200+
cp $(go-component-from)-$(shell uname -s | tr '[:upper:]' '[:lower:]')-386 $(go-component-to)
201+
endif
202+
189203
.PHONY: cdk-go-component
190204
cdk-go-component: install-cli ## Creates a go-component for development
191205
scripts/prepare_test_resources.sh go_component

cli/cdk/go/proto/v1/cdk.pb.go

Lines changed: 369 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/cdk/go/proto/v1/cdk_grpc.pb.go

Lines changed: 81 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/cmd/cache.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,27 @@ type cliAsset struct {
204204
ExpiresAt time.Time `json:"expires_at"`
205205
}
206206

207-
// WriteAssetToCache stores an asset with an expiration time
207+
// writeAssetToCache stores an asset with an expiration time and returns errors
208208
//
209209
// Simple Example: Having a struct named vulnReport
210210
//
211211
// ```go
212212
// cli.WriteAssetToCache("my-report", time.Now().Add(time.Hour * 1), vulnReport{Foo: "bar"})
213213
// ```
214-
func (c *cliState) WriteAssetToCache(key string, expiresAt time.Time, data interface{}) {
214+
// writeAssetToCache stores an asset with an expiration time
215+
//
216+
// Simple Example: Having a struct named vulnReport
217+
//
218+
// ```go
219+
// cli.WriteAssetToCache("my-report", time.Now().Add(time.Hour * 1), vulnReport{Foo: "bar"})
220+
// ```
221+
func (c *cliState) writeAssetToCache(key string, expiresAt time.Time, data interface{}) error {
215222
if c.noCache {
216-
return
223+
return nil
217224
}
218225

219226
if expiresAt.Before(time.Now()) {
220-
return // avoid writing assets that are already expired
227+
return nil // avoid writing assets that are already expired
221228
}
222229

223230
c.Log.Debugw("saving asset",
@@ -226,7 +233,12 @@ func (c *cliState) WriteAssetToCache(key string, expiresAt time.Time, data inter
226233
"data", data,
227234
"expires_at", expiresAt,
228235
)
229-
err := c.Cache.Write(key, structToString(cliAsset{data, expiresAt}))
236+
return c.Cache.Write(key, structToString(cliAsset{data, expiresAt}))
237+
}
238+
239+
// WriteAssetToCache wraps WriteAssetToCacheErroring and squashes errors
240+
func (c *cliState) WriteAssetToCache(key string, expiresAt time.Time, data interface{}) {
241+
err := c.writeAssetToCache(key, expiresAt, data)
230242
if err != nil {
231243
c.Log.Warnw("unable to write asset in cache",
232244
"feature", "cache",

cli/cmd/cdk.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ func (c *cliState) GrpcTarget() string {
6262
return fmt.Sprintf("localhost:%v", c.cdkServerPort)
6363
}
6464

65+
func (c *cliState) ReadCache(ctx context.Context, in *cdk.ReadCacheRequest) (*cdk.ReadCacheResponse, error) {
66+
if in.Key == "" {
67+
return nil, errors.New("cache key must be supplied")
68+
}
69+
70+
var data []byte
71+
if !c.ReadCachedAsset(in.Key, &data) { // not expired
72+
return &cdk.ReadCacheResponse{Hit: true, Data: data}, nil
73+
}
74+
return &cdk.ReadCacheResponse{
75+
Hit: false,
76+
Data: nil,
77+
}, nil
78+
}
79+
80+
func (c *cliState) WriteCache(ctx context.Context, in *cdk.WriteCacheRequest) (*cdk.WriteCacheResult, error) {
81+
if in.Key == "" {
82+
return nil, errors.New("cache key must be supplied")
83+
}
84+
85+
err := c.writeAssetToCache(in.Key, in.Expires.AsTime(), in.Data)
86+
if err != nil {
87+
msg := err.Error()
88+
return &cdk.WriteCacheResult{Error: true, Message: msg}, nil
89+
}
90+
return &cdk.WriteCacheResult{Error: false, Message: ""}, nil
91+
}
92+
6593
// Ping implements CDK.Ping
6694
func (c *cliState) Ping(ctx context.Context, in *cdk.PingRequest) (*cdk.PongReply, error) {
6795
c.Log.Debugw("message", "from", "CDK/Ping", "component_name", in.GetComponentName())

0 commit comments

Comments
 (0)