Skip to content

Commit

Permalink
feat(cli): update query by loading it from the platform (#721)
Browse files Browse the repository at this point in the history
As a Lacework CLI user,
I want to update a query without having to type the query again,
So I can modify my queries faster via inline modifications.

This change is adding the ability to load a query into the default
editor from the user so that they can easily make inline modifications.

Simply run the command `lacework query update [query_id]` and an editor
will open with the query preloaded, modify it, save and close the file
for the Lacework CLI to apply those modifications.

* test(integration): update query id
* fix: detect query id changes
* test(integration): new runFakeTerminalTestFromDir() func
* test(lql): enable policies tests
* test: avoid running in parallel
* test: update all TestGeneration tests
* test: use vi rather than the default vim

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Mar 8, 2022
1 parent 4e246d9 commit 2f68adb
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 299 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ integration-only: install-tools ## Run integration tests
alert_rules \
compliance \
configure \
query \
policy \
event \
help \
integration \
Expand Down
2 changes: 1 addition & 1 deletion api/lql.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
type NewQuery struct {
QueryID string `json:"queryId" yaml:"queryId"`
QueryText string `json:"queryText" yaml:"queryText"`
EvaluatorID string `json:"evaluatorId,omitempty" yaml:"evaluatorId"`
EvaluatorID string `json:"evaluatorId,omitempty" yaml:"evaluatorId,omitempty"`
}

type UpdateQuery struct {
Expand Down
67 changes: 58 additions & 9 deletions cli/cmd/lql_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,27 @@
package cmd

import (
"fmt"

"github.com/AlecAivazis/survey/v2"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

"github.com/lacework/go-sdk/api"
)

var (
// queryUpdateCmd represents the lql update command
queryUpdateCmd = &cobra.Command{
Use: "update",
Use: "update [query_id]",
Short: "Update a query",
Args: cobra.RangeArgs(0, 1),
Long: `
There are multiple ways you can update a query:
* Typing the query into your default editor (via $EDITOR)
* Passing a query id to load it into your default editor
* From a local file on disk using the flag '--file'
* From a URL using the flag '--url'
Expand All @@ -46,7 +52,6 @@ To launch your default editor and update a query.
lacework query update
`,
Args: cobra.NoArgs,
RunE: updateQuery,
}
)
Expand All @@ -61,23 +66,67 @@ func init() {
func updateQuery(cmd *cobra.Command, args []string) error {
msg := "unable to update query"

// input query
queryString, err := inputQuery(cmd)
if err != nil {
return errors.Wrap(err, msg)
var (
queryString string
err error
)

if len(args) != 0 {
// query id via argument
cli.StartProgress("Retrieving query...")
queryRes, err := cli.LwApi.V2.Query.Get(args[0])
cli.StopProgress()
if err != nil {
return errors.Wrap(err, "unable to load query from your account")
}

queryYaml, err := yaml.Marshal(&api.NewQuery{
QueryID: queryRes.Data.QueryID,
QueryText: queryRes.Data.QueryText,
EvaluatorID: queryRes.Data.EvaluatorID,
})
if err != nil {
return errors.Wrap(err, msg)
}

prompt := &survey.Editor{
Message: fmt.Sprintf("Update query %s", args[0]),
Default: string(queryYaml),
HideDefault: true,
AppendDefault: true,
FileName: "query*.yaml",
}
var queryStr string
err = survey.AskOne(prompt, &queryStr)
if err != nil {
return errors.Wrap(err, msg)
}

queryString = queryStr
} else {
// input query
queryString, err = inputQuery(cmd)
if err != nil {
return errors.Wrap(err, msg)
}
}

// parse query
newQuery, err := parseQuery(queryString)
if err != nil {
return errors.Wrap(err, msg)
}
updateQuery := api.UpdateQuery{
QueryText: newQuery.QueryText,

// avoid letting the user change the query id
if len(args) != 0 && newQuery.QueryID != args[0] {
return errors.New("changes to query id not supported")
}

cli.Log.Debugw("updating query", "query", queryString)
cli.StartProgress(" Updating query...")
update, err := cli.LwApi.V2.Query.Update(newQuery.QueryID, updateQuery)
update, err := cli.LwApi.V2.Query.Update(newQuery.QueryID, api.UpdateQuery{
QueryText: newQuery.QueryText,
})
cli.StopProgress()
if err != nil {
return errors.Wrap(err, msg)
Expand Down
Loading

0 comments on commit 2f68adb

Please sign in to comment.