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(cli): update query by loading it from the platform #721

Merged
merged 9 commits into from
Mar 8, 2022
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