From dc6fa7deee8f926511f3e160aff4b96bf0d9c55c Mon Sep 17 00:00:00 2001 From: David Hazekamp Date: Mon, 7 Jun 2021 14:37:40 -0500 Subject: [PATCH 1/4] refactor(cli): improved LQL service func names ALLY-458 --- api/lql.go | 8 ++--- api/lql_compile.go | 2 +- api/lql_compile_test.go | 8 ++--- api/lql_delete.go | 2 +- api/lql_delete_test.go | 13 +++----- api/lql_test.go | 55 ++++++++++++++++++-------------- api/lql_update.go | 2 +- api/lql_update_test.go | 13 +++----- cli/cmd/lql.go | 4 +-- cli/cmd/lql_create.go | 2 +- cli/cmd/lql_delete.go | 4 +-- cli/cmd/lql_show.go | 2 +- cli/cmd/lql_update.go | 2 +- cli/cmd/lql_validate.go | 2 +- integration/lql_delete_test.go | 2 +- integration/lql_describe_test.go | 2 +- integration/lql_show_test.go | 2 +- 17 files changed, 63 insertions(+), 62 deletions(-) diff --git a/api/lql.go b/api/lql.go index 178dc5d95..4fb3e815d 100644 --- a/api/lql.go +++ b/api/lql.go @@ -174,7 +174,7 @@ type LQLService struct { client *Client } -func (svc *LQLService) CreateQuery(query string) ( +func (svc *LQLService) Create(query string) ( response LQLQueryResponse, err error, ) { @@ -188,10 +188,10 @@ func (svc *LQLService) CreateQuery(query string) ( } func (svc *LQLService) GetQueries() (LQLQueryResponse, error) { - return svc.GetQueryByID("") + return svc.GetByID("") } -func (svc *LQLService) GetQueryByID(queryID string) ( +func (svc *LQLService) GetByID(queryID string) ( response LQLQueryResponse, err error, ) { @@ -205,7 +205,7 @@ func (svc *LQLService) GetQueryByID(queryID string) ( return } -func (svc *LQLService) RunQuery(query, start, end string) ( +func (svc *LQLService) Run(query, start, end string) ( response map[string]interface{}, err error, ) { diff --git a/api/lql_compile.go b/api/lql_compile.go index 69d8a7d48..56d209fd8 100644 --- a/api/lql_compile.go +++ b/api/lql_compile.go @@ -24,7 +24,7 @@ type LQLCompileResponse struct { Message string `json:"message"` } -func (svc *LQLService) CompileQuery(query string) ( +func (svc *LQLService) Compile(query string) ( response LQLCompileResponse, err error, ) { diff --git a/api/lql_compile_test.go b/api/lql_compile_test.go index a9f6637d4..29a62bf46 100644 --- a/api/lql_compile_test.go +++ b/api/lql_compile_test.go @@ -96,7 +96,7 @@ func TestLQLCompileMethod(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.CompileQuery(lqlQueryStr) + _, err = c.LQL.Compile(lqlQueryStr) assert.Nil(t, err) } @@ -116,7 +116,7 @@ func TestLQLCompileBadInput(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.CompileQuery("") + _, err = c.LQL.Compile("") assert.Equal(t, api.LQLQueryTranslateError, err.Error()) } @@ -142,7 +142,7 @@ func TestLQLCompileOK(t *testing.T) { _ = json.Unmarshal([]byte(mockResponse), &compileExpected) var compileActual api.LQLCompileResponse - compileActual, err = c.LQL.CompileQuery(lqlQueryStr) + compileActual, err = c.LQL.Compile(lqlQueryStr) assert.Nil(t, err) assert.Equal(t, compileExpected, compileActual) } @@ -163,6 +163,6 @@ func TestLQLCompileError(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.CompileQuery(lqlQueryStr) + _, err = c.LQL.Compile(lqlQueryStr) assert.NotNil(t, err) } diff --git a/api/lql_delete.go b/api/lql_delete.go index f413fcb29..f4b865693 100644 --- a/api/lql_delete.go +++ b/api/lql_delete.go @@ -38,7 +38,7 @@ type LQLDeleteMessage struct { ID string `json:"lqlDeleted"` } -func (svc *LQLService) DeleteQuery(queryID string) ( +func (svc *LQLService) Delete(queryID string) ( response LQLDeleteResponse, err error, ) { diff --git a/api/lql_delete_test.go b/api/lql_delete_test.go index 24b73ff2f..92c20163a 100644 --- a/api/lql_delete_test.go +++ b/api/lql_delete_test.go @@ -52,7 +52,7 @@ func TestLQLDeleteMethod(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.DeleteQuery(lqlQueryID) + _, err = c.LQL.Delete(lqlQueryID) assert.Nil(t, err) } @@ -72,15 +72,12 @@ func TestLQLDeleteBadInput(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.DeleteQuery("") + _, err = c.LQL.Delete("") assert.NotNil(t, err) } func TestLQLDeleteOK(t *testing.T) { - mockResponse := mockLQLMessageResponse( - fmt.Sprintf(`"lqlDeleted": "%s"`, lqlQueryID), - "true", - ) + mockResponse := mockLQLMessageResponse(fmt.Sprintf(`"lqlDeleted": "%s"`, lqlQueryID)) fakeServer := lacework.MockServer() fakeServer.MockAPI( @@ -101,7 +98,7 @@ func TestLQLDeleteOK(t *testing.T) { _ = json.Unmarshal([]byte(mockResponse), &deleteExpected) var deleteActual api.LQLDeleteResponse - deleteActual, err = c.LQL.DeleteQuery(lqlQueryID) + deleteActual, err = c.LQL.Delete(lqlQueryID) assert.Nil(t, err) assert.Equal(t, deleteExpected, deleteActual) @@ -123,6 +120,6 @@ func TestLQLDeleteNotFound(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.DeleteQuery(lqlQueryID) + _, err = c.LQL.Delete(lqlQueryID) assert.NotNil(t, err) } diff --git a/api/lql_test.go b/api/lql_test.go index 9baa1d547..950fdb9f1 100644 --- a/api/lql_test.go +++ b/api/lql_test.go @@ -48,8 +48,8 @@ var ( lqlErrorReponse = mockLQLDataResponse( `{ "message": "Error Serving Request" }`, ) - lqlUnableResponse = mockLQLMessageResponse( - `"message": "{\"error\":\"Error: Unable to locate lql query NoSuchQuery, please double check the query exists and has not already been updated.\"}"`, + lqlUnableResponse = mockLQLErrorResponse( + `"{\"error\":\"Error: Unable to locate lql query NoSuchQuery, please double check the query exists and has not already been updated.\"}"`, "false", ) ) @@ -385,9 +385,16 @@ func mockLQLDataResponse(data string) string { }` } -func mockLQLMessageResponse(message string, ok string) string { +func mockLQLErrorResponse(message string, ok string) string { return `{ - "ok": ` + ok + `, + "ok": ` + ok + `, + "data": { "message": {` + message + `} } + }` +} + +func mockLQLMessageResponse(message string) string { + return `{ + "ok": true, "message": {` + message + ` } }` @@ -410,7 +417,7 @@ func TestLQLCreateMethod(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.CreateQuery(lqlQueryStr) + _, err = c.LQL.Create(lqlQueryStr) assert.Nil(t, err) } @@ -430,7 +437,7 @@ func TestLQLCreateBadInput(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.CreateQuery("") + _, err = c.LQL.Create("") assert.Equal(t, api.LQLQueryTranslateError, err.Error()) } @@ -456,7 +463,7 @@ func TestLQLCreateOK(t *testing.T) { _ = json.Unmarshal([]byte(mockResponse), &createExpected) var createActual api.LQLQueryResponse - createActual, err = c.LQL.CreateQuery(lqlQueryStr) + createActual, err = c.LQL.Create(lqlQueryStr) assert.Nil(t, err) assert.Equal(t, createExpected, createActual) @@ -478,7 +485,7 @@ func TestLQLCreateError(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.CreateQuery(lqlQueryStr) + _, err = c.LQL.Create(lqlQueryStr) assert.NotNil(t, err) } @@ -509,7 +516,7 @@ func TestLQLGetQueriesMethod(t *testing.T) { assert.Nil(t, err) } -func TestLQLGetQueryByIDOK(t *testing.T) { +func TestLQLGetByIDOK(t *testing.T) { mockResponse := mockLQLDataResponse(lqlCreateData) fakeServer := lacework.MockServer() @@ -527,17 +534,17 @@ func TestLQLGetQueryByIDOK(t *testing.T) { ) assert.Nil(t, err) - createExpected := api.LQLQueryResponse{} - _ = json.Unmarshal([]byte(mockResponse), &createExpected) + getExpected := api.LQLQueryResponse{} + _ = json.Unmarshal([]byte(mockResponse), &getExpected) - var createActual api.LQLQueryResponse - createActual, err = c.LQL.GetQueryByID(lqlQueryStr) + var getActual api.LQLQueryResponse + getActual, err = c.LQL.GetByID(lqlQueryID) assert.Nil(t, err) - assert.Equal(t, createExpected, createActual) + assert.Equal(t, getExpected, getActual) } -func TestLQLGetQueryByIDNotFound(t *testing.T) { +func TestLQLGetByIDNotFound(t *testing.T) { fakeServer := lacework.MockServer() fakeServer.MockAPI( "external/lql", @@ -553,11 +560,11 @@ func TestLQLGetQueryByIDNotFound(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.GetQueryByID("NoSuchQuery") + _, err = c.LQL.GetByID("NoSuchQuery") assert.NotNil(t, err) } -func TestLQLRunQueryMethod(t *testing.T) { +func TestLQLRunMethod(t *testing.T) { fakeServer := lacework.MockServer() fakeServer.MockAPI( "external/lql/query", @@ -574,11 +581,11 @@ func TestLQLRunQueryMethod(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.RunQuery(lqlQueryStr, "0", "1") + _, err = c.LQL.Run(lqlQueryStr, "0", "1") assert.Nil(t, err) } -func TestLQLRunQueryBadInput(t *testing.T) { +func TestLQLRunBadInput(t *testing.T) { fakeServer := lacework.MockServer() fakeServer.MockAPI( "external/lql/query", @@ -594,11 +601,11 @@ func TestLQLRunQueryBadInput(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.RunQuery("", "", "") + _, err = c.LQL.Run("", "", "") assert.Equal(t, api.LQLQueryTranslateError, err.Error()) } -func TestLQLRunQueryOK(t *testing.T) { +func TestLQLRunOK(t *testing.T) { mockResponse := mockLQLDataResponse(lqlRunData) fakeServer := lacework.MockServer() @@ -620,13 +627,13 @@ func TestLQLRunQueryOK(t *testing.T) { _ = json.Unmarshal([]byte(mockResponse), &runExpected) var runActual map[string]interface{} - runActual, err = c.LQL.RunQuery(lqlQueryStr, "0", "1") + runActual, err = c.LQL.Run(lqlQueryStr, "0", "1") assert.Nil(t, err) assert.Equal(t, runExpected, runActual) } -func TestLQLRunQueryError(t *testing.T) { +func TestLQLRunError(t *testing.T) { fakeServer := lacework.MockServer() fakeServer.MockAPI( "external/lql", @@ -642,6 +649,6 @@ func TestLQLRunQueryError(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.CreateQuery(lqlQueryStr) + _, err = c.LQL.Create(lqlQueryStr) assert.NotNil(t, err) } diff --git a/api/lql_update.go b/api/lql_update.go index 846652632..70ba94e48 100644 --- a/api/lql_update.go +++ b/api/lql_update.go @@ -27,7 +27,7 @@ type LQLUpdateMessage struct { ID string `json:"lqlUpdated"` } -func (svc *LQLService) UpdateQuery(query string) ( +func (svc *LQLService) Update(query string) ( response LQLUpdateResponse, err error, ) { diff --git a/api/lql_update_test.go b/api/lql_update_test.go index 687974a71..59eb63a28 100644 --- a/api/lql_update_test.go +++ b/api/lql_update_test.go @@ -46,7 +46,7 @@ func TestLQLUpdateMethod(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.UpdateQuery(lqlQueryStr) + _, err = c.LQL.Update(lqlQueryStr) assert.Nil(t, err) } @@ -66,15 +66,12 @@ func TestLQLUpdateBadInput(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.UpdateQuery("") + _, err = c.LQL.Update("") assert.Equal(t, api.LQLQueryTranslateError, err.Error()) } func TestLQLUpdateOK(t *testing.T) { - mockResponse := mockLQLMessageResponse( - fmt.Sprintf(`"lqlUpdated": "%s"`, lqlQueryID), - "true", - ) + mockResponse := mockLQLMessageResponse(fmt.Sprintf(`"lqlUpdated": "%s"`, lqlQueryID)) fakeServer := lacework.MockServer() fakeServer.MockAPI( @@ -95,7 +92,7 @@ func TestLQLUpdateOK(t *testing.T) { _ = json.Unmarshal([]byte(mockResponse), &updateExpected) var updateActual api.LQLUpdateResponse - updateActual, err = c.LQL.UpdateQuery(lqlQueryStr) + updateActual, err = c.LQL.Update(lqlQueryStr) assert.Nil(t, err) assert.Equal(t, updateExpected, updateActual) @@ -117,6 +114,6 @@ func TestLQLUpdateNotFound(t *testing.T) { ) assert.Nil(t, err) - _, err = c.LQL.UpdateQuery(lqlQueryStr) + _, err = c.LQL.Update(lqlQueryStr) assert.NotNil(t, err) } diff --git a/cli/cmd/lql.go b/cli/cmd/lql.go index 684c2734c..48a433f6c 100644 --- a/cli/cmd/lql.go +++ b/cli/cmd/lql.go @@ -184,7 +184,7 @@ func inputQuery(cmd *cobra.Command, args []string) (string, error) { func inputQueryFromEnv(queryID string) (query string, err error) { var queryResponse api.LQLQueryResponse - queryResponse, err = cli.LwApi.LQL.GetQueryByID(queryID) + queryResponse, err = cli.LwApi.LQL.GetByID(queryID) if err == nil && len(queryResponse.Data) != 0 { query = queryResponse.Data[0].QueryText } @@ -296,7 +296,7 @@ func runQuery(cmd *cobra.Command, args []string) error { return compileQueryAndOutput(query) } // !validate_only should should run - response, err := cli.LwApi.LQL.RunQuery(query, lqlCmdState.Start, lqlCmdState.End) + response, err := cli.LwApi.LQL.Run(query, lqlCmdState.Start, lqlCmdState.End) if err != nil { err = queryErrorCrumbs(query, err) diff --git a/cli/cmd/lql_create.go b/cli/cmd/lql_create.go index fd33d27a7..13426b0b6 100644 --- a/cli/cmd/lql_create.go +++ b/cli/cmd/lql_create.go @@ -50,7 +50,7 @@ func createQuery(cmd *cobra.Command, args []string) error { } cli.Log.Debugw("creating LQL query", "query", query) - create, err := cli.LwApi.LQL.CreateQuery(query) + create, err := cli.LwApi.LQL.Create(query) if err != nil { err = queryErrorCrumbs(query, err) diff --git a/cli/cmd/lql_delete.go b/cli/cmd/lql_delete.go index e07c24567..14fbe45d1 100644 --- a/cli/cmd/lql_delete.go +++ b/cli/cmd/lql_delete.go @@ -44,7 +44,7 @@ func init() { func deleteQuery(_ *cobra.Command, args []string) error { cli.Log.Debugw("deleting LQL query", "queryID", args[0]) - delete, err := cli.LwApi.LQL.DeleteQuery(args[0]) + delete, err := cli.LwApi.LQL.Delete(args[0]) if err != nil { return errors.Wrap(err, "unable to delete LQL query") @@ -53,6 +53,6 @@ func deleteQuery(_ *cobra.Command, args []string) error { return cli.OutputJSON(delete.Message) } cli.OutputHuman( - fmt.Sprintf("LQL query (%s) deleted successfully.\n", delete.Message.ID)) + fmt.Sprintf("LQL query (%s) deleted successfully.\n", args[0])) return nil } diff --git a/cli/cmd/lql_show.go b/cli/cmd/lql_show.go index 18e27926e..474aff1f8 100644 --- a/cli/cmd/lql_show.go +++ b/cli/cmd/lql_show.go @@ -41,7 +41,7 @@ func init() { func showQuery(_ *cobra.Command, args []string) error { cli.Log.Debugw("retrieving LQL query", "queryID", args[0]) - queryResponse, err := cli.LwApi.LQL.GetQueryByID(args[0]) + queryResponse, err := cli.LwApi.LQL.GetByID(args[0]) if err != nil { return errors.Wrap(err, "unable to show LQL query") diff --git a/cli/cmd/lql_update.go b/cli/cmd/lql_update.go index 81550782c..b5b25aa78 100644 --- a/cli/cmd/lql_update.go +++ b/cli/cmd/lql_update.go @@ -52,7 +52,7 @@ func updateQuery(cmd *cobra.Command, args []string) error { } cli.Log.Debugw("updating LQL query", "query", query) - update, err := cli.LwApi.LQL.UpdateQuery(query) + update, err := cli.LwApi.LQL.Update(query) if err != nil { err = queryErrorCrumbs(query, err) diff --git a/cli/cmd/lql_validate.go b/cli/cmd/lql_validate.go index 0fef3d1ef..e6e1f93b8 100644 --- a/cli/cmd/lql_validate.go +++ b/cli/cmd/lql_validate.go @@ -55,7 +55,7 @@ func validateQuery(cmd *cobra.Command, args []string) error { func compileQueryAndOutput(query string) error { cli.Log.Debugw("validating LQL query", "query", query) - compile, err := cli.LwApi.LQL.CompileQuery(query) + compile, err := cli.LwApi.LQL.Compile(query) if err != nil { err = queryErrorCrumbs(query, err) diff --git a/integration/lql_delete_test.go b/integration/lql_delete_test.go index 501d43494..05a1c298c 100644 --- a/integration/lql_delete_test.go +++ b/integration/lql_delete_test.go @@ -42,7 +42,7 @@ func TestQueryDeleteNoInput(t *testing.T) { out, err, exitcode := LaceworkCLIWithTOMLConfig("query", "delete") assert.Empty(t, out.String(), "STDOUT should be empty") - assert.Contains(t, err.String(), "ERROR unable to delete LQL query: Please specify a valid query ID.") + assert.Contains(t, err.String(), "ERROR accepts 1 arg(s), received 0") assert.Equal(t, 1, exitcode, "EXITCODE is not the expected one") } diff --git a/integration/lql_describe_test.go b/integration/lql_describe_test.go index 55e9a79d2..82796ee45 100644 --- a/integration/lql_describe_test.go +++ b/integration/lql_describe_test.go @@ -43,7 +43,7 @@ func TestQueryDescribeNoInput(t *testing.T) { out, err, exitcode := LaceworkCLIWithTOMLConfig("query", "describe") assert.Empty(t, out.String(), "STDOUT should be empty") - assert.Contains(t, err.String(), "ERROR unable to describe LQL data source: Please specify a valid data source.") + assert.Contains(t, err.String(), "ERROR accepts 1 arg(s), received 0") assert.Equal(t, 1, exitcode, "EXITCODE is not the expected one") } diff --git a/integration/lql_show_test.go b/integration/lql_show_test.go index 548248f40..611b0c6f3 100644 --- a/integration/lql_show_test.go +++ b/integration/lql_show_test.go @@ -42,7 +42,7 @@ func TestQueryShowNoInput(t *testing.T) { out, err, exitcode := LaceworkCLIWithTOMLConfig("query", "show") assert.Empty(t, out.String(), "STDOUT should be empty") - assert.Contains(t, err.String(), "ERROR unable to show LQL query: Please specify a valid query ID.") + assert.Contains(t, err.String(), "ERROR accepts 1 arg(s), received 0") assert.Equal(t, 1, exitcode, "EXITCODE is not the expected one") } From 69d115bfc4544d3f742d6c61a44254bbec353eb3 Mon Sep 17 00:00:00 2001 From: David Hazekamp Date: Tue, 15 Jun 2021 11:06:25 -0500 Subject: [PATCH 2/4] fix(cli): putting back create/update response ALLY-458 RAIN-17805 --- api/lql.go | 4 ++-- api/lql_test.go | 20 +++++++++++--------- api/lql_update_test.go | 18 ++++++++++-------- cli/cmd/lql_create.go | 8 ++++++-- cli/cmd/lql_update.go | 10 +++++++--- 5 files changed, 36 insertions(+), 24 deletions(-) diff --git a/api/lql.go b/api/lql.go index ec201db71..399159d83 100644 --- a/api/lql.go +++ b/api/lql.go @@ -185,7 +185,7 @@ type LQLService struct { } func (svc *LQLService) Create(query string) ( - response LQLQuery, + response LQLQueryResponse, err error, ) { lqlQuery := LQLQuery{QueryBlob: query} @@ -198,7 +198,7 @@ func (svc *LQLService) Create(query string) ( } func (svc *LQLService) Update(query string) ( - response LQLQuery, + response LQLQueryResponse, err error, ) { lqlQuery := LQLQuery{QueryBlob: query} diff --git a/api/lql_test.go b/api/lql_test.go index f5cfbc85a..247b0f30a 100644 --- a/api/lql_test.go +++ b/api/lql_test.go @@ -32,12 +32,12 @@ import ( ) var ( - lqlQueryID = "my_lql" - lqlQueryStr = "my_lql { source { CloudTrailRawEvents } return { INSERT_ID } }" - lqlCreateResponse = `{ + lqlQueryID = "my_lql" + lqlQueryStr = "my_lql { source { CloudTrailRawEvents } return { INSERT_ID } }" + lqlCreateData = `[{ "lql_id": "my_lql", "query_text": "my_lql { source { CloudTrailRawEvents } return { INSERT_ID } }" -}` +}]` lqlRunData = `[ { "INSERT_ID": "35308423" @@ -451,11 +451,13 @@ func TestLQLCreateBadInput(t *testing.T) { } func TestLQLCreateOK(t *testing.T) { + mockResponse := mockLQLDataResponse(lqlCreateData) + fakeServer := lacework.MockServer() fakeServer.MockAPI( "external/lql", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, lqlCreateResponse) + fmt.Fprint(w, mockResponse) }, ) defer fakeServer.Close() @@ -466,10 +468,10 @@ func TestLQLCreateOK(t *testing.T) { ) assert.Nil(t, err) - createExpected := api.LQLQuery{} - _ = json.Unmarshal([]byte(lqlCreateResponse), &createExpected) + createExpected := api.LQLQueryResponse{} + _ = json.Unmarshal([]byte(mockResponse), &createExpected) - var createActual api.LQLQuery + var createActual api.LQLQueryResponse createActual, err = c.LQL.Create(lqlQueryStr) assert.Nil(t, err) @@ -524,7 +526,7 @@ func TestLQLGetQueriesMethod(t *testing.T) { } func TestLQLGetQueryByIDOK(t *testing.T) { - mockResponse := mockLQLDataResponse("[" + lqlCreateResponse + "]") + mockResponse := mockLQLDataResponse(lqlCreateData) fakeServer := lacework.MockServer() fakeServer.MockAPI( diff --git a/api/lql_update_test.go b/api/lql_update_test.go index 90281ba05..02e2925b7 100644 --- a/api/lql_update_test.go +++ b/api/lql_update_test.go @@ -30,10 +30,10 @@ import ( ) var ( - lqlUpdateResponse = `{ - "lql_id": "my_lql", - "query_text": "my_lql { source { CloudTrailRawEvents } return { INSERT_ID, INSERT_TIME } }" - }` + lqlUpdateData = `[{ + "lql_id": "my_lql", + "query_text": "my_lql { source { CloudTrailRawEvents } return { INSERT_ID, INSERT_TIME } }" +}]` ) func TestLQLUpdateMethod(t *testing.T) { @@ -78,11 +78,13 @@ func TestLQLUpdateBadInput(t *testing.T) { } func TestLQLUpdateOK(t *testing.T) { + mockResponse := mockLQLDataResponse(lqlUpdateData) + fakeServer := lacework.MockServer() fakeServer.MockAPI( "external/lql", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, lqlUpdateResponse) + fmt.Fprint(w, mockResponse) }, ) defer fakeServer.Close() @@ -93,10 +95,10 @@ func TestLQLUpdateOK(t *testing.T) { ) assert.Nil(t, err) - updateExpected := api.LQLQuery{} - _ = json.Unmarshal([]byte(lqlUpdateResponse), &updateExpected) + updateExpected := api.LQLQueryResponse{} + _ = json.Unmarshal([]byte(mockResponse), &updateExpected) - var updateActual api.LQLQuery + var updateActual api.LQLQueryResponse updateActual, err = c.LQL.Update(lqlQueryStr) assert.Nil(t, err) diff --git a/cli/cmd/lql_create.go b/cli/cmd/lql_create.go index f20a2f3cf..13426b0b6 100644 --- a/cli/cmd/lql_create.go +++ b/cli/cmd/lql_create.go @@ -57,8 +57,12 @@ func createQuery(cmd *cobra.Command, args []string) error { return errors.Wrap(err, "unable to create LQL query") } if cli.JSONOutput() { - return cli.OutputJSON(create) + return cli.OutputJSON(create.Data) } - cli.OutputHuman(fmt.Sprintf("LQL query (%s) created successfully.\n", create.ID)) + queryID := "unknown" + if len(create.Data) > 0 { + queryID = create.Data[0].ID + } + cli.OutputHuman(fmt.Sprintf("LQL query (%s) created successfully.\n", queryID)) return nil } diff --git a/cli/cmd/lql_update.go b/cli/cmd/lql_update.go index 8bf7b8826..86c683957 100644 --- a/cli/cmd/lql_update.go +++ b/cli/cmd/lql_update.go @@ -59,9 +59,13 @@ func updateQuery(cmd *cobra.Command, args []string) error { return errors.Wrap(err, lqlUpdateUnableMsg) } if cli.JSONOutput() { - return cli.OutputJSON(update) + return cli.OutputJSON(update.Data) } - cli.OutputHuman( - fmt.Sprintf("LQL query (%s) updated successfully.\n", update.ID)) + queryID := "unknown" + if len(update.Data) > 0 { + queryID = update.Data[0].ID + } + cli.OutputHuman(fmt.Sprintf("LQL query (%s) updated successfully.\n", queryID)) return nil } +} From c78627ca13290bac4dbd6e293b59189089166c38 Mon Sep 17 00:00:00 2001 From: David Hazekamp Date: Tue, 15 Jun 2021 11:11:06 -0500 Subject: [PATCH 3/4] fix(cli): lql_update extra closing brace ALLY-458 --- cli/cmd/lql_update.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/cmd/lql_update.go b/cli/cmd/lql_update.go index 86c683957..f52254702 100644 --- a/cli/cmd/lql_update.go +++ b/cli/cmd/lql_update.go @@ -68,4 +68,3 @@ func updateQuery(cmd *cobra.Command, args []string) error { cli.OutputHuman(fmt.Sprintf("LQL query (%s) updated successfully.\n", queryID)) return nil } -} From c0d2f6cebd67337bbc0196659b3549c954dd39de Mon Sep 17 00:00:00 2001 From: David Hazekamp Date: Thu, 17 Jun 2021 13:06:41 -0500 Subject: [PATCH 4/4] test(cli): updated tests for new resource naming ALLY-525 --- integration/lql_list_test.go | 4 ++-- integration/lql_test.go | 8 ++++---- integration/policy_test.go | 24 ++++++++++++------------ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/integration/lql_list_test.go b/integration/lql_list_test.go index bf992ccdf..94bf1aee4 100644 --- a/integration/lql_list_test.go +++ b/integration/lql_list_test.go @@ -47,12 +47,12 @@ func TestQueryList(t *testing.T) { out, err, exitcode := LaceworkCLIWithTOMLConfig("query", "list") assert.Contains(t, out.String(), "QUERY ID") - assert.Contains(t, out.String(), "MyLQL") + assert.Contains(t, out.String(), "LW_CLI_AWS_CTA_IntegrationTest") assert.Empty(t, err.String(), "STDERR should be empty") assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one") out, err, exitcode = LaceworkCLIWithTOMLConfig("query", "list", "--json") - assert.Contains(t, out.String(), `"MyLQL"`) + assert.Contains(t, out.String(), `"LW_CLI_AWS_CTA_IntegrationTest"`) assert.Empty(t, err.String(), "STDERR should be empty") assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one") } diff --git a/integration/lql_test.go b/integration/lql_test.go index a24202335..ed06c9468 100644 --- a/integration/lql_test.go +++ b/integration/lql_test.go @@ -28,10 +28,10 @@ import ( ) const ( - lqlQueryID string = "MyLQL" - lqlQueryText string = "MyLQL { source { CloudTrailRawEvents } return { INSERT_ID } }" - lqlQueryUpdate string = "MyLQL { source { CloudTrailRawEvents } return { INSERT_ID, INSERT_TIME } }" - lqlQueryURL string = "https://raw.githubusercontent.com/lacework/go-sdk/main/integration/test_resources/lql/MyLQL.lql" + lqlQueryID string = "LW_CLI_AWS_CTA_IntegrationTest" + lqlQueryText string = "LW_CLI_AWS_CTA_IntegrationTest { source { CloudTrailRawEvents } return { INSERT_ID } }" + lqlQueryUpdate string = "LW_CLI_AWS_CTA_IntegrationTest { source { CloudTrailRawEvents } return { INSERT_ID, INSERT_TIME } }" + lqlQueryURL string = "https://raw.githubusercontent.com/lacework/go-sdk/main/integration/test_resources/lql/LW_CLI_AWS_CTA_IntegrationTest.lql" ) var ( diff --git a/integration/policy_test.go b/integration/policy_test.go index 55d838015..685405fa8 100644 --- a/integration/policy_test.go +++ b/integration/policy_test.go @@ -28,16 +28,16 @@ import ( const ( policyText string = `{ - "policy_id": "my-policy-1", + "policy_id": "lacework-clitest-1", "title": "My Policy Title", "enabled": false, "alert_enabled": false, - "lql_id": "MyLQL", + "lql_id": "LW_CLI_AWS_CTA_IntegrationTest", "severity": "low", "description": "My Policy Description", "remediation": "Check yourself..." }` - policyURL string = "https://raw.githubusercontent.com/lacework/go-sdk/main/integration/test_resources/policy/my-policy-1.json" + policyURL string = "https://raw.githubusercontent.com/lacework/go-sdk/main/integration/test_resources/policy/lacework-clitest-1.json" ) func TestPolicyHelp(t *testing.T) { @@ -118,27 +118,27 @@ func TestPolicyCreateFile(t *testing.T) { // update-url (output human) out, stderr, exitcode = LaceworkCLIWithTOMLConfig("policy", "update", "-u", policyURL) - assert.Contains(t, out.String(), "Policy (my-policy-1) updated successfully.") + assert.Contains(t, out.String(), "Policy (lacework-clitest-1) updated successfully.") assert.Empty(t, stderr.String(), "STDERR should be empty") assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one") // list enabled-only out, stderr, exitcode = LaceworkCLIWithTOMLConfig("policy", "list", "--enabled") assert.Contains(t, out.String(), "lacework-global-1") - assert.NotContains(t, out.String(), "my-policy-1") + assert.NotContains(t, out.String(), "lacework-clitest-1") assert.Empty(t, stderr.String(), "STDERR should be empty") assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one") // list alert_enabled-only out, stderr, exitcode = LaceworkCLIWithTOMLConfig("policy", "list", "--alert_enabled") assert.Contains(t, out.String(), "lacework-global-1") - assert.NotContains(t, out.String(), "my-policy-1") + assert.NotContains(t, out.String(), "lacework-clitest-1") assert.Empty(t, stderr.String(), "STDERR should be empty") assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one") // delete - out, stderr, exitcode = LaceworkCLIWithTOMLConfig("policy", "delete", "my-policy-1") - assert.Contains(t, out.String(), "Policy (my-policy-1) deleted successfully.") + out, stderr, exitcode = LaceworkCLIWithTOMLConfig("policy", "delete", "lacework-clitest-1") + assert.Contains(t, out.String(), "Policy (lacework-clitest-1) deleted successfully.") assert.Empty(t, stderr.String(), "STDERR should be empty") assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one") } @@ -155,9 +155,9 @@ func TestPolicyCreateURL(t *testing.T) { // create (output human) out, stderr, exitcode := LaceworkCLIWithTOMLConfig("policy", "create", "-u", policyURL) // teardown policy - defer LaceworkCLIWithTOMLConfig("policy", "delete", "my-policy-1") + defer LaceworkCLIWithTOMLConfig("policy", "delete", "lacework-clitest-1") - assert.Contains(t, out.String(), "Policy (my-policy-1) created successfully.") + assert.Contains(t, out.String(), "Policy (lacework-clitest-1) created successfully.") assert.Empty(t, stderr.String(), "STDERR should be empty") assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one") @@ -345,8 +345,8 @@ func TestPolicyDelete(t *testing.T) { // human delete tested by virtue of TestPolicyCreateFile // json - out, err, exitcode := LaceworkCLIWithTOMLConfig("policy", "delete", "my-policy-1", "--json") - assert.Contains(t, out.String(), `"my-policy-1"`) + out, err, exitcode := LaceworkCLIWithTOMLConfig("policy", "delete", "lacework-clitest-1", "--json") + assert.Contains(t, out.String(), `"lacework-clitest-1"`) assert.Empty(t, err.String(), "STDERR should be empty") assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one") }