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

Dashboards sanitizer #910

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions dashboardsSanitizer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Dashboard Sanitizer

### What is this tool?
If the Kibana objects are exported from 7.10.2 or later version, it may not be loaded into OpenSearch Dashboards successfully. This tool makes it compatible to OpenSearch Dashboards by fixing the version numbers for each kibana object and removes any incompatible objects. This does **NOT** attempt to convert non-compatible kibana objects like, *lens*, *canvas*, etc...

### How to use this tool?

From the parent project, run the following command. The input file should be in *ndjson* format.
```shell
./gradlew dashboardsSanitzer:run --args='--source <<Your kibana object file location>> [--output <<output file location>>]'
```

### Known limitations

* This has not been tested with Kibana version 8.x onwards
* It removes the summary line in the output
38 changes: 38 additions & 0 deletions dashboardsSanitizer/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
buildscript {
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.1'
}
}

plugins {
id 'application'
id 'org.opensearch.migrations.java-library-conventions'
id 'io.freefair.lombok'
id 'java'
}

java.sourceCompatibility = JavaVersion.VERSION_11
java.targetCompatibility = JavaVersion.VERSION_11

repositories {
mavenCentral()
}

dependencies {

implementation project(":commonDependencyVersionConstraints")

implementation project(':coreUtilities')
implementation project(":RFS")

implementation group: 'com.fasterxml.jackson.core', name:'jackson-databind'
implementation group: 'org.slf4j', name: 'slf4j-api'
implementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j2-impl'

implementation group: 'com.google.code.gson', name: 'gson', version: '2.11.0'
implementation group: 'info.picocli', name: 'picocli', version:'4.7.6'
}

application {
mainClassName = 'org.opensearch.migrations.dashboards.Sanitizer'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.opensearch.migrations.dashboards;

import java.io.*;
import java.util.Scanner;

import com.google.gson.Gson;

import org.opensearch.migrations.dashboards.model.Dashboard;
import org.opensearch.migrations.dashboards.util.Stats;

import lombok.extern.slf4j.Slf4j;
import picocli.CommandLine;
import picocli.CommandLine.Command;

@Command(name = "Dashboard Sanitizer", version = "0.1", mixinStandardHelpOptions = true)
@Slf4j
public class Sanitizer implements Runnable{

@CommandLine.Option(names = {"-V", "--version"}, versionHelp = true, description = "display version info")
boolean versionInfoRequested;

@CommandLine.Option(names = {"?", "-h", "--help"}, usageHelp = true, description = "display this help message")
boolean usageHelpRequested;

@CommandLine.Option(names = {"-s", "--source"}, required = true, description = "The Elastic dashboard object file in ndjson.")
private String sourceFile;

@CommandLine.Option(names = {"-o", "--output"}, required = true, description = "The sanitized OpenSearch dashboard object file in ndjson.", defaultValue = "os-dashboards.ndjson")
private String outputFile;

@Override
public void run() {
//check for sourceFile, if empty, print usage and return
if (sourceFile.isEmpty()) {
CommandLine.usage(this, System.out);
}
try {
Scanner scanner = new Scanner(new BufferedInputStream(new FileInputStream(sourceFile)));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
Stats stats = sanitizeDashboardsFromFile(scanner, writer);
System.out.printf("%s file is sanitized and output available at %s%n", sourceFile, outputFile);
stats.printStats();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static void main(String[] args) {
CommandLine cmd = new CommandLine(new Sanitizer());
cmd.parseArgs(args);
if (cmd.isUsageHelpRequested() ) {
cmd.usage(System.out);
return;
} else if (cmd.isVersionHelpRequested()) {
cmd.printVersionHelp(System.out);
return;
}
int exitCode = cmd.execute(args);
System.exit(exitCode);
}

public static Stats sanitizeDashboardsFromFile(Scanner source, BufferedWriter writer) throws IOException {

Gson gson = new Gson();
Stats counter = new Stats();

while (source.hasNextLine()) {
String line = source.nextLine();
Dashboard dashboardObject = gson.fromJson(line, Dashboard.class);
// if dashboard id is null, it could be summary line, skip the line
if (dashboardObject.getId() == null) {
counter.registerSkipped(dashboardObject);
continue;
} else if (!dashboardObject.isCompatibleType()) {
counter.registerSkipped(dashboardObject);
continue;
}

dashboardObject.makeCompatibleToOS();
writer.write(gson.toJson(dashboardObject));
writer.newLine();
writer.flush();
counter.registerProcessed(dashboardObject);
}
return counter;
}
}
Loading
Loading