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

feature #73 key value search for capture meta #74

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/teragrep/cfe18/CaptureMetaMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
package com.teragrep.cfe18;

import com.teragrep.cfe18.handlers.entities.CaptureMeta;
import com.teragrep.cfe18.handlers.entities.CaptureRelp;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
Expand All @@ -63,4 +64,5 @@ CaptureMeta addNewCaptureMeta(

CaptureMeta deleteCaptureMeta(int capture_id);

List<CaptureRelp> getCaptureMetaByKeyValue(String key, String value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import com.teragrep.cfe18.CaptureMetaMapper;
import com.teragrep.cfe18.handlers.entities.CaptureMeta;
import com.teragrep.cfe18.handlers.entities.CaptureRelp;
import com.teragrep.cfe18.handlers.entities.Flow;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
Expand Down Expand Up @@ -201,4 +202,34 @@ public ResponseEntity<String> removeCaptureMeta(@PathVariable("capture_id") int
}


// Key value fetch
@RequestMapping(path="/{key}/{value}",method= RequestMethod.GET, produces="application/json")
@Operation(summary = "Fetch capture definitions by key and value")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Found the capture definitions",
content = {@Content(mediaType = "application/json",
schema = @Schema(implementation = CaptureMeta.class))}),
@ApiResponse(responseCode = "400", description = "Capture meta key or value does not exist",
Nefarious46 marked this conversation as resolved.
Show resolved Hide resolved
content = @Content)
})
public ResponseEntity<?> getApplicationMetaKeyValue(@PathVariable("key") String key, @PathVariable("value") String value) {
try {
// Using CaptureRelp is sufficient here since we are only returning capture definition details.
List<CaptureRelp> am = captureMetaMapper.getCaptureMetaByKeyValue(key,value);
return new ResponseEntity<>(am, HttpStatus.OK);
} catch(Exception ex){
JSONObject jsonErr = new JSONObject();
final Throwable cause = ex.getCause();
if (cause instanceof SQLException) {
LOGGER.error((cause).getMessage());
String state = ((SQLException) cause).getSQLState();
if (state.equals("42000")) {
jsonErr.put("message", "Capture meta KEY or VALUE does not exist with given parameters");
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.BAD_REQUEST);
}
}
return new ResponseEntity<>("Unexpected error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}

}
13 changes: 12 additions & 1 deletion src/main/resources/com/teragrep/cfe18/CaptureMetaMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,16 @@
<resultMap id="removeCaptureMeta" type="com.teragrep.cfe18.handlers.entities.CaptureMeta">
<result property="capture_id" column="capture_id"/>
</resultMap>
</mapper>

<!-- get Capture Meta by key value -->
<select id="getCaptureMetaByKeyValue" resultMap="keyValue" statementType="CALLABLE">
{CALL cfe_18.retrieve_capture_meta_key_value(#{param1},#{param2})}
</select>
<resultMap id="keyValue" type="com.teragrep.cfe18.handlers.entities.CaptureRelp">
<result column="capture_id" property="id"/>
<result column="tag" property="tag"/>
<result column="sourcetype" property="source_type"/>
<result column="application" property="application"/>
<result column="captureIndex" property="index"/>
</resultMap>
</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Main data management system (MDMS) cfe_18
* Copyright (C) 2021 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/
use cfe_18;
DELIMITER //
CREATE OR REPLACE PROCEDURE retrieve_capture_meta_key_value(meta_key varchar(1024),meta_value varchar(1024))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
START TRANSACTION;
-- check for existence of capture meta value before attempting retrieval. Throws custom error.
if((select count(cmk.meta_key_name) from cfe_18.capture_meta_key cmk where cmk.meta_key_name=meta_key)=0) then
-- standardized JSON error response
SELECT JSON_OBJECT('id', 0, 'message', 'Capture meta KEY does not exist with given KEY value') into @nokey;
Nefarious46 marked this conversation as resolved.
Show resolved Hide resolved
signal sqlstate '42000' set message_text = @nokey;
-- check for existence of capture meta key before attempting retrieval. Throws custom error.
elseif((select count(cm.meta_value) from cfe_18.capture_meta cm where cm.meta_value=meta_value)=0) then
-- standardized JSON error response
SELECT JSON_OBJECT('id', 0, 'message', 'Capture meta VALUE does not exist with given meta VALUE') into @nokey;
signal sqlstate '42000' set message_text = @nokey;
end if;
-- return list of capture_definitions which are linked to the given key value pair.
select cd.id as capture_id,
t.tag as tag,
cS.captureSourceType as sourcetype,
a.app as application,
cI.captureIndex as captureIndex
from cfe_18.capture_definition cd
inner join capture_meta c on cd.id = c.capture_id
inner join capture_meta_key cmk on c.meta_key_id = cmk.meta_key_id
inner join tags t on cd.tag_id=t.id
inner join captureSourcetype cS on cd.captureSourcetype_id = cS.id
inner join application a on cd.application_id = a.id
inner join captureIndex cI on cd.captureIndex_id = cI.id
WHERE c.meta_value=meta_value AND
cmk.meta_key_name=meta_key;

COMMIT;
END;
//
DELIMITER ;
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,97 @@ public void testGetAllCaptureMetas() {
assertEquals(json, responseStringGet);
}


@Test
@Order(6)
public void testCaptureMetaKeyValue() {
// expected capture
ArrayList<CaptureRelp> expected = new ArrayList<>();
CaptureRelp captureRelp = new CaptureRelp();
captureRelp.setId(1);
captureRelp.setTag("relpTag");
captureRelp.setApplication("relp");
captureRelp.setIndex("audit_relp");
captureRelp.setSource_type("relpsource1");
expected.add(captureRelp);
String jsonFile = gson.toJson(expected);

// Fetching capture definition via key value pair from capture meta
HttpGet requestGet = new HttpGet("http://localhost:" + port + "/capture/meta/relpKey2/relpValue2");

requestGet.setHeader("Authorization", "Bearer " + token);

HttpResponse responseGet = Assertions.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(requestGet));

HttpEntity entityGet = responseGet.getEntity();

String responseStringGet = Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entityGet, "UTF-8"));

// Assertions
assertEquals(jsonFile, responseStringGet);
assertThat(
responseGet.getStatusLine().getStatusCode(),
equalTo(HttpStatus.SC_OK));

}
@Test
@Order(7)
public void testCaptureMetaKeyValueNoKey() {
// Fetching capture definition via key value pair from capture meta
HttpGet requestGet = new HttpGet("http://localhost:" + port + "/capture/meta/missingKey/relpValue2");

requestGet.setHeader("Authorization", "Bearer " + token);

HttpResponse responseGet = Assertions.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(requestGet));

HttpEntity entityGet = responseGet.getEntity();

String responseStringGet = Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entityGet, "UTF-8"));

// Parsin respponse as JSONObject
JSONObject responseAsJson = Assertions.assertDoesNotThrow(() -> new JSONObject(responseStringGet));

// Creating string from Json that was given as a response
String actual = Assertions.assertDoesNotThrow(() -> responseAsJson.get("message").toString());

String expected = "Capture meta KEY or VALUE does not exist with given parameters";
// Assertions
assertEquals(expected, actual);
assertThat(
responseGet.getStatusLine().getStatusCode(),
equalTo(HttpStatus.SC_BAD_REQUEST));

}
@Test
@Order(8)
public void testCaptureMetaKeyValueNoValue() {
// Fetching capture definition via key value pair from capture meta
HttpGet requestGet = new HttpGet("http://localhost:" + port + "/capture/meta/relpKey2/missingValue");

requestGet.setHeader("Authorization", "Bearer " + token);

HttpResponse responseGet = Assertions.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(requestGet));

HttpEntity entityGet = responseGet.getEntity();

String responseStringGet = Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entityGet, "UTF-8"));

// Parsin respponse as JSONObject
JSONObject responseAsJson = Assertions.assertDoesNotThrow(() -> new JSONObject(responseStringGet));

// Creating string from Json that was given as a response
String actual = Assertions.assertDoesNotThrow(() -> responseAsJson.get("message").toString());

String expected = "Capture meta KEY or VALUE does not exist with given parameters";
// Assertions
assertEquals(expected, actual);
assertThat(
responseGet.getStatusLine().getStatusCode(),
equalTo(HttpStatus.SC_BAD_REQUEST));

}
@Test
@Order(9)
public void testDeleteCaptureMeta() {
HttpDelete delete = new HttpDelete("http://localhost:" + port + "/capture/meta/"+1);

Expand All @@ -416,7 +505,7 @@ public void testDeleteCaptureMeta() {
}

@Test
@Order(7)
@Order(10)
public void testDeleteNonExistentCaptureMeta() {
HttpDelete delete = new HttpDelete("http://localhost:" + port + "/capture/meta/"+122);

Expand All @@ -441,4 +530,5 @@ public void testDeleteNonExistentCaptureMeta() {
assertThat(deleteResponse.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_BAD_REQUEST));
assertEquals(expected, actual);
}

}