Skip to content

Commit 1a787e0

Browse files
authored
feat: CDK component install telemetry (#1384)
1 parent 336c1b9 commit 1a787e0

File tree

2 files changed

+80
-34
lines changed

2 files changed

+80
-34
lines changed

cli/cmd/component.go

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package cmd
2121
import (
2222
"fmt"
2323
"os"
24+
"time"
2425

2526
"github.com/Masterminds/semver"
2627
"github.com/fatih/color"
@@ -349,7 +350,18 @@ func componentsToTable() [][]string {
349350
return out
350351
}
351352

352-
func runComponentsInstall(_ *cobra.Command, args []string) (err error) {
353+
func runComponentsInstall(cmd *cobra.Command, args []string) (err error) {
354+
var (
355+
componentName string = args[0]
356+
version string = versionArg
357+
params map[string]interface{} = make(map[string]interface{})
358+
start time.Time
359+
)
360+
361+
cli.Event.Component = componentName
362+
cli.Event.Feature = "install_component"
363+
defer cli.SendHoneyvent()
364+
353365
cli.StartProgress("Loading components state...")
354366
// @afiune maybe move the state to the cache and fetch if it if has expired
355367
cli.LwComponents, err = lwcomponent.LoadState(cli.LwApi)
@@ -359,21 +371,45 @@ func runComponentsInstall(_ *cobra.Command, args []string) (err error) {
359371
return
360372
}
361373

362-
component, found := cli.LwComponents.GetComponent(args[0])
374+
component, found := cli.LwComponents.GetComponent(componentName)
363375
if !found {
364-
err = errors.New("component not found. Try running 'lacework component list'")
376+
err = errors.New(fmt.Sprintf("component %s not found. Try running 'lacework component list'", componentName))
365377
return
366378
}
367379

380+
cli.OutputChecklist(successIcon, fmt.Sprintf("Component %s found\n", componentName))
381+
382+
if version == "" {
383+
version = component.LatestVersion.String()
384+
}
385+
386+
start = time.Now()
387+
368388
cli.StartProgress(fmt.Sprintf("Installing component %s...", component.Name))
369-
err = cli.LwComponents.Install(args[0], versionArg)
389+
err = cli.LwComponents.Install(component, version)
370390
cli.StopProgress()
371391
if err != nil {
372392
err = errors.Wrap(err, "unable to install component")
373393
return
374394
}
375395
cli.OutputChecklist(successIcon, "Component %s installed\n", color.HiYellowString(component.Name))
376-
cli.OutputChecklist(successIcon, "Signature verified\n")
396+
397+
params["install_duration_ms"] = time.Since(start).Milliseconds()
398+
399+
start = time.Now()
400+
401+
cli.StartProgress("Verifing component signature...")
402+
err = cli.LwComponents.Verify(component, version)
403+
cli.StopProgress()
404+
if err != nil {
405+
err = errors.Wrap(err, "verification of component signature failed")
406+
return
407+
}
408+
cli.OutputChecklist(successIcon, "Component signature verified\n")
409+
410+
params["verify_duration_ms"] = time.Since(start).Milliseconds()
411+
412+
start = time.Now()
377413

378414
cli.StartProgress(fmt.Sprintf("Configuring component %s...", component.Name))
379415
// component life cycle: initialize
@@ -389,6 +425,10 @@ func runComponentsInstall(_ *cobra.Command, args []string) (err error) {
389425
cli.OutputChecklist(successIcon, "Component configured\n")
390426
cli.OutputHuman("\nInstallation completed.\n")
391427

428+
params["configure_duration_ms"] = time.Since(start).Milliseconds()
429+
430+
cli.Event.FeatureData = params
431+
392432
if component.Breadcrumbs.InstallationMessage != "" {
393433
cli.OutputHuman("\n")
394434
cli.OutputHuman(component.Breadcrumbs.InstallationMessage)
@@ -407,13 +447,17 @@ func runComponentsUpdate(_ *cobra.Command, args []string) (err error) {
407447
return
408448
}
409449

450+
component_name := args[0]
451+
410452
component, found := cli.LwComponents.GetComponent(args[0])
411453
if !found {
412-
err = errors.New("component not found. Try running 'lacework component list'")
454+
err = errors.New(fmt.Sprintf("component %s not found. Try running 'lacework component list'", component_name))
413455
return
414456
}
415457
// @afiune end boilerplate load components
416458

459+
cli.OutputChecklist(successIcon, fmt.Sprintf("Component %s found\n", component_name))
460+
417461
updateTo := component.LatestVersion
418462
if versionArg != "" {
419463
parsedVersion, err := semver.NewVersion(versionArg)
@@ -434,8 +478,8 @@ func runComponentsUpdate(_ *cobra.Command, args []string) (err error) {
434478
return nil
435479
}
436480

437-
cli.StartProgress(fmt.Sprintf("Updating component %s...", component.Name))
438-
err = cli.LwComponents.Install(args[0], updateTo.String())
481+
cli.StartProgress(fmt.Sprintf("Updating component %s to version %s...", component.Name, &updateTo))
482+
err = cli.LwComponents.Install(component, updateTo.String())
439483
cli.StopProgress()
440484
if err != nil {
441485
err = errors.Wrap(err, "unable to update component")
@@ -444,7 +488,15 @@ func runComponentsUpdate(_ *cobra.Command, args []string) (err error) {
444488
cli.OutputChecklist(successIcon, "Component %s updated to %s\n",
445489
color.HiYellowString(component.Name),
446490
color.HiCyanString(fmt.Sprintf("v%s", updateTo.String())))
447-
cli.OutputChecklist(successIcon, "Signature verified\n")
491+
492+
cli.StartProgress("Verifing component signature...")
493+
err = cli.LwComponents.Verify(component, updateTo.String())
494+
cli.StopProgress()
495+
if err != nil {
496+
err = errors.Wrap(err, "verification of component signature failed")
497+
return
498+
}
499+
cli.OutputChecklist(successIcon, "Component signature verified\n")
448500

449501
cli.StartProgress(fmt.Sprintf("Reconfiguring %s component...", component.Name))
450502
// component life cycle: reconfigure

lwcomponent/component.go

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -222,30 +222,7 @@ func Dir() (string, error) {
222222
return filepath.Join(cacheDir, "components"), nil
223223
}
224224

225-
func (s State) Install(name string, version string) error {
226-
component, found := s.GetComponent(name)
227-
if !found {
228-
return errors.New("component not found")
229-
}
230-
231-
if version == "" {
232-
version = component.LatestVersion.String()
233-
}
234-
235-
// @edoardopirovano, December 2022: Temporary workaround while there are still old
236-
// APIs out there. In particular, 0.2.0 is the version of the API result before
237-
// https://github.com/lacework/rainbow/pull/10874 which added the capability for
238-
// the API to return multiple versions. At the time of writing, that PR is merged
239-
// but not fully rolled out. In the old version of the API result, we would only
240-
// return the latest version, and wouldn't set the version field of its artifacts.
241-
// So, if we're looking for the latest version we should set what we're looking
242-
// for to the empty string. If we're looking for anything else, we can leave it
243-
// set as it is: we'll fail to find it and report an error which is the right
244-
// behaviour as only the latest version can be installed in this case.
245-
if s.Version == "0.2.0" && version == component.LatestVersion.String() {
246-
version = ""
247-
}
248-
225+
func (s State) Install(component *Component, version string) error {
249226
rPath, err := component.RootPath()
250227
if err != nil {
251228
return err
@@ -285,6 +262,10 @@ func (s State) Install(name string, version string) error {
285262
return errors.Wrap(err, "unable to download component artifact")
286263
}
287264

265+
if err := component.WriteVersion(artifact.Version); err != nil {
266+
return err
267+
}
268+
288269
// @afiune check 1) cross-platform and 2) correct permissions
289270
// if the file has permissions already, can we avoid this?
290271
if component.IsExecutable() {
@@ -293,11 +274,24 @@ func (s State) Install(name string, version string) error {
293274
}
294275
}
295276

277+
return nil
278+
}
279+
280+
func (s State) Verify(component *Component, version string) error {
281+
artifact, found := component.ArtifactForRunningHost(version)
282+
if !found {
283+
return errors.Errorf(
284+
"could not find an artifact for version %s on the current platform (%s/%s)",
285+
version, runtime.GOOS, runtime.GOARCH,
286+
)
287+
}
288+
296289
if err := component.WriteSignature([]byte(artifact.Signature)); err != nil {
297290
return err
298291
}
299292

300-
if err := component.WriteVersion(artifact.Version); err != nil {
293+
rPath, err := component.RootPath()
294+
if err != nil {
301295
return err
302296
}
303297

0 commit comments

Comments
 (0)