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

API for input categories and subcategories #20663

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
import org.graylog2.inputs.InputEventListener;
import org.graylog2.inputs.InputStateListener;
import org.graylog2.inputs.PersistedInputsImpl;
import org.graylog2.inputs.categories.InputCategoryService;
import org.graylog2.inputs.categories.InputCategoryServiceImpl;
import org.graylog2.lookup.LookupModule;
import org.graylog2.plugin.cluster.ClusterConfigService;
import org.graylog2.plugin.cluster.ClusterIdFactory;
Expand Down Expand Up @@ -216,6 +218,8 @@ private void bindInterfaces() {
OptionalBinder.newOptionalBinder(binder(), DataTieringStatusService.class).setDefault().to(DefaultDataTieringStatusService.class);

OptionalBinder.newOptionalBinder(binder(), TrafficUpdater.class).setDefault().to(TrafficCounterService.class).asEagerSingleton();

bind(InputCategoryService.class).to(InputCategoryServiceImpl.class);
}

private void bindDynamicFeatures() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* 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
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog2.inputs.categories;

import java.util.List;

public interface InputCategoryService {
public record InputCategorization(String category, String subCategory, String type) {}

public List<String> allCategories();

public List<String> subCategoryByCategory(String inputCategory);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* 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
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog2.inputs.categories;

import au.com.bytecode.opencsv.CSVParser;
import au.com.bytecode.opencsv.CSVReader;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class InputCategoryServiceImpl implements InputCategoryService {
public record InputCategorization(String category, String subCategory, String type) {}

private Set<InputCategorization> categorizationSet;

public List<String> allCategories() {
ensureCategorizationSet();
return categorizationSet.stream()
.map(inputCategorization -> inputCategorization.category)
.distinct()
.toList();
}

public List<String> subCategoryByCategory(String inputCategory) {
ensureCategorizationSet();
return categorizationSet.stream()
.filter(c -> c.category.equals(inputCategory))
.map(c -> c.subCategory)
.distinct()
.toList();
}

private void ensureCategorizationSet() {
try {
categorizationSet = getFromCSV();
} catch (URISyntaxException | IOException e) {
throw new RuntimeException("Unable to get input categories data", e);
}
}

private Set<InputCategorization> getFromCSV() throws URISyntaxException, IOException {
Set<InputCategorization> result = new HashSet<>();
Path filePath = Paths.get(getClass().getResource("input_categories.csv").toURI());

try (CSVReader csvReader = new CSVReader(Files.newBufferedReader(filePath), CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, 1);) {
csvReader.readAll().forEach(
array -> result.add(new InputCategorization(array[0], array[1], array[2]))
);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.graylog2.shared.rest.resources.system.SystemResource;
import org.graylog2.shared.rest.resources.system.ThroughputResource;
import org.graylog2.shared.rest.resources.system.codecs.CodecTypesResource;
import org.graylog2.shared.rest.resources.system.inputs.InputCategoriesResource;
import org.graylog2.shared.rest.resources.system.inputs.InputTypesResource;

public class RestResourcesSharedModule extends Graylog2Module {
Expand All @@ -34,6 +35,7 @@ protected void configure() {
addSystemRestResource(DocumentationResource.class);
addSystemRestResource(CodecTypesResource.class);
addSystemRestResource(InputTypesResource.class);
addSystemRestResource(InputCategoriesResource.class);
addSystemRestResource(LoadBalancerStatusResource.class);
addSystemRestResource(MetricsResource.class);
addSystemRestResource(SystemPluginResource.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* 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
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog2.shared.rest.resources.system.inputs;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.graylog2.inputs.categories.InputCategoryService;
import org.graylog2.shared.rest.resources.RestResource;

import java.util.Collection;

@RequiresAuthentication
@Api(value = "System/Inputs/Categories", description = "Input categories")
@Path("/system/inputs/categories")
@Produces(MediaType.APPLICATION_JSON)
public class InputCategoriesResource extends RestResource {
private final InputCategoryService inputCategoryService;

@Inject
public InputCategoriesResource(InputCategoryService inputCategoryService) {
this.inputCategoryService = inputCategoryService;
}

@GET
@ApiOperation(value = "Get all available input categories")
public CategoriesResponse categories() {
return new CategoriesResponse(inputCategoryService.allCategories());
}

@GET
@Path("subcategories/{category}")
@ApiOperation(value = "Get all available subcategories for specified category")
public SubCategoriesResponse subCategories(
@ApiParam(name = "category", required = true)
@PathParam("category") String category
) {
return new SubCategoriesResponse(inputCategoryService.subCategoryByCategory(category));
}

public record CategoriesResponse(Collection<String> categories) {}

public record SubCategoriesResponse(Collection<String> subCategories) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category,subcategory,description,inputType,illuminatePack;dateAdded;version;licenseType
Web Server,Apache Web Server,Apache Web Server,Beats,1.0,14-10-2024 10:07:59,1.0,enterprise
Web Server,Apache Web Server,Apache Web Server,Syslog,1.0,14-10-2024 10:07:59,1.0,security
Data Lake,Amazon Security Lake,Amazon Security Lake,API,,14-10-2024 10:07:59,1.0,open
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* 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
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog2.inputs.categories;

import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class InputCategoryServiceTest {

@Test
void testCSVReader() {
InputCategoryService inputCategoryService = new InputCategoryServiceImpl();
final List<String> categories = inputCategoryService.allCategories();
assertThat(categories).hasSize(2);

final List<String> subcategories = inputCategoryService.subCategoryByCategory("Web Server");
assertThat(subcategories).hasSize(1);
}
}
Loading