Skip to content

Commit

Permalink
Prepare plugin for production use (#18)
Browse files Browse the repository at this point in the history
* Adding tests

* Load Resource files from tests to compare output

* Adding tests for getting mappings and settings

* Intercepting AcknowledgedResponse when calling putTemplate

* Adding unit tests for putTemplate, create and indexExists methods

* Adding mock for indexExists() method

* Adding a listener to create() that ensures the index is created

* Adding javadocs and for-each loop for multiple index creation.

* Adding missing javadoc

* Adding functionality to load mappings and index settings into a template.

* Adding javadocs to stringToMap

* Split getting mappings and settings to separate methods

* Add getter for templates Map

* Read the templates file only once

* Add exception handling

* Fix tests to fit latest changes in WazuhIndices

* Fix tests to fit latest changes in WazuhIndices

* Split yaml tests to separate files

* Instantiate the WazuhIndices class upon node startup.

* Simplify WazuhIndices class

* Add DEVELOPER_GUIDE.md

* Fix stuff

* Add Javadoc

* Add some unit tests

* Update DEVELOPER_GUIDE.md

* Skip failing checks

---------

Signed-off-by: Álex Ruiz <[email protected]>
Co-authored-by: Fede Tux <[email protected]>
Co-authored-by: Álex Ruiz <[email protected]>
  • Loading branch information
3 people authored Aug 28, 2024
1 parent d2cef9e commit 31f0c36
Show file tree
Hide file tree
Showing 15 changed files with 782 additions and 280 deletions.
56 changes: 56 additions & 0 deletions DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
- [Developer Guide](#developer-guide)
- [Forking and Cloning](#forking-and-cloning)
- [Install Prerequisites](#install-prerequisites)
- [JDK 11](#jdk-11)
- [JDK 14](#jdk-14)
- [Setup](#setup)
- [Build](#build)
- [Building from the command line](#building-from-the-command-line)
- [Building from the IDE](#building-from-the-ide)

## Developer Guide

### Forking and Cloning

Fork this repository on GitHub, and clone locally with `git clone`. Plugins's code is in the `plugins/` folder.

### Install Prerequisites

#### JDK 11

OpenSearch builds using Java 11 at a minimum, using the Adoptium distribution. This means you must have a JDK 11 installed with the environment variable `JAVA_HOME` referencing the path to Java home for your JDK 11 installation, e.g. `JAVA_HOME=/usr/lib/jvm/jdk-11`. This is configured in `build.gradle`.

```
allprojects {
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_11
}
```

Download Java 11 from [here](https://adoptium.net/releases.html?variant=openjdk11).

#### JDK 14

To run the full suite of tests, download and install [JDK 14](https://jdk.java.net/archive/) and set `JAVA11_HOME`, and `JAVA14_HOME`.

### Setup

1. Clone the repository (see [Forking and Cloning](#forking-and-cloning))
2. Make sure `JAVA_HOME` is pointing to a Java 11 JDK (see [Install Prerequisites](#install-prerequisites))
3. Launch Intellij IDEA, Choose Import Project and select the settings.gradle file in the root of this package.

### Build

This package uses the [Gradle](https://docs.gradle.org/current/userguide/userguide.html) build system. Gradle comes with excellent documentation that should be your first stop when trying to figure out how to operate or modify the build. We also use the OpenSearch build tools for Gradle. These tools are idiosyncratic and don't always follow the conventions and instructions for building regular Java code using Gradle. Not everything in this package will work the way it's described in the Gradle documentation. If you encounter such a situation, the OpenSearch build tools [source code](https://github.com/opensearch-project/OpenSearch/tree/main/buildSrc/src/main/groovy/org/opensearch/gradle) is your best bet for figuring out what's going on.

#### Building from the command line

1. `./gradlew build` builds and tests all subprojects.
2. `./gradlew run` launches a single node cluster with the plugin installed.
3. `./gradlew publishToMavenLocal` promotes the plugin into your local maven repository.

When launching a cluster using one of the above commands, logs are placed in `build/testclusters/integTest-0/logs/`. Though the logs are teed to the console, in practice it's best to check the actual log file.

#### Building from the IDE

Currently, the only IDE we support is IntelliJ IDEA. It's free, it's open source, it works. The gradle tasks above can also be launched from IntelliJ's Gradle toolbar and the extra parameters can be passed in via the Launch Configurations VM arguments.
45 changes: 21 additions & 24 deletions plugins/wazuh-indexer-setup/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,38 @@ def pluginName = 'wazuh-indexer-setup'
def pluginDescription = 'Wazuh Indexer setup plugin'
def projectPath = 'org.wazuh'
def pathToPlugin = 'setup'
def pluginClassName = 'WazuhIndexerSetupPlugin'
def pluginClassName = 'SetupPlugin'

tasks.register("preparePluginPathDirs") {
mustRunAfter clean
doLast {
def newPath = pathToPlugin.replace(".", "/")
mkdir "src/main/java/org/wazuh/$newPath"
mkdir "src/test/java/org/wazuh/$newPath"
mkdir "src/yamlRestTest/java/org/wazuh/$newPath"
}
}

publishing {
publications {
pluginZip(MavenPublication) { publication ->
pom {
name = pluginName
description = pluginDescription
licenses {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
name = pluginName
description = pluginDescription
licenses {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
}
developers {
developer {
name = "Wazuh"
url = "https://github.com/opensearch-project/opensearch-plugin-template-java"
developers {
developer {
name = "Wazuh "
url = "https://wazuh.com"
}
}
}
}
}
}
}

allprojects {
group = "${projectPath}"
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_11
}

opensearchplugin {
name pluginName
description pluginDescription
Expand Down Expand Up @@ -126,8 +123,8 @@ task updateVersion {
doLast {
ext.newVersion = System.getProperty('newVersion')
println "Setting version to ${newVersion}."
// String tokenization to support -SNAPSHOT
ant.replaceregexp(file:'build.gradle', match: '"opensearch.version", "\\d.*"', replace: '"opensearch.version", "' + newVersion.tokenize('-')[0] + '-SNAPSHOT"', flags:'g', byline:true)
// String tokenization to support -SNAPSHOT
ant.replaceregexp(file: 'build.gradle', match: '"opensearch.version", "\\d.*"', replace: '"opensearch.version", "' + newVersion.tokenize('-')[0] + '-SNAPSHOT"', flags: 'g', byline: true)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@
*/
package org.wazuh.setup;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.action.admin.indices.create.CreateIndexResponse;
import org.opensearch.action.support.master.AcknowledgedResponse;
import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.env.Environment;
Expand All @@ -28,15 +23,21 @@
import org.opensearch.watcher.ResourceWatcherService;
import org.wazuh.setup.index.WazuhIndices;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

/**
* Main class of the Indexer Setup plugin. This plugin is responsible for
* the creation of the index templates and indices required by Wazuh to
* work properly.
*/
public class SetupPlugin extends Plugin implements ClusterPlugin {

public class WazuhIndexerSetupPlugin extends Plugin implements ClusterPlugin {
// Implement the relevant Plugin Interfaces here
private static final Logger log = LogManager.getLogger(WazuhIndexerSetupPlugin.class);
/**
* Default constructor
*/
public SetupPlugin() {}

private WazuhIndices indices;

Expand All @@ -55,41 +56,11 @@ public Collection<Object> createComponents(
Supplier<RepositoriesService> repositoriesServiceSupplier
) {
this.indices = new WazuhIndices(client, clusterService);

return Collections.emptyList();
return List.of(this.indices);
}

@Override
public void onNodeStarted(DiscoveryNode localNode) {

try {
this.indices.putTemplate(new ActionListener<>() {
@Override
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
log.info("template created");
}

@Override
public void onFailure(Exception e) {
log.error("template creation failed");
}
});

this.indices.create(new ActionListener<>() {
@Override
public void onResponse(CreateIndexResponse createIndexResponse) {
log.info("{} index created", WazuhIndices.INDEX_NAME);
}

@Override
public void onFailure(Exception e) {
log.error("error creating index: {}", e.toString());
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}

ClusterPlugin.super.onNodeStarted(localNode);
this.indices.initialize();
}
}
Loading

0 comments on commit 31f0c36

Please sign in to comment.