use of org.opensearch.common.SuppressForbidden in project OpenSearch by opensearch-project.
the class DetectEsInstallationTaskTests method testTaskExecution.
@SuppressForbidden(reason = "Read config directory from test resources.")
public void testTaskExecution() throws Exception {
Path esConfig = new File(getClass().getResource("/config").getPath()).toPath();
// path for es_home
terminal.addTextInput(esConfig.getParent().toString());
// path for es_config
terminal.addTextInput(esConfig.toString());
TaskInput taskInput = new TaskInput(env);
Tuple<TaskInput, Terminal> input = new Tuple<>(taskInput, terminal);
task.accept(input);
assertThat(taskInput.getEsConfig(), is(esConfig));
assertThat(taskInput.getBaseUrl(), is("http://localhost:42123"));
assertThat(taskInput.getPlugins(), hasSize(0));
assertThat(taskInput.getNode(), is("node-x"));
assertThat(taskInput.getCluster(), is("my-cluster"));
}
use of org.opensearch.common.SuppressForbidden in project OpenSearch by opensearch-project.
the class InstallPluginCommand method downloadZip.
/**
* Downloads a zip from the url, into a temp file under the given temp dir.
*/
// pkg private for tests
@SuppressForbidden(reason = "We use getInputStream to download plugins")
Path downloadZip(Terminal terminal, String urlString, Path tmpDir, boolean isBatch) throws IOException {
terminal.println(VERBOSE, "Retrieving zip from " + urlString);
URL url = new URL(urlString);
Path zip = Files.createTempFile(tmpDir, null, ".zip");
URLConnection urlConnection = url.openConnection();
urlConnection.addRequestProperty("User-Agent", "opensearch-plugin-installer");
try (InputStream in = isBatch ? urlConnection.getInputStream() : new TerminalProgressInputStream(urlConnection.getInputStream(), urlConnection.getContentLength(), terminal)) {
// must overwrite since creating the temp file above actually created the file
Files.copy(in, zip, StandardCopyOption.REPLACE_EXISTING);
}
return zip;
}
use of org.opensearch.common.SuppressForbidden in project OpenSearch by opensearch-project.
the class InstallPluginCommand method urlExists.
/**
* Returns {@code true} if the given url exists, and {@code false} otherwise.
*
* The given url must be {@code https} and existing means a {@code HEAD} request returns 200.
*/
// pkg private for tests to manipulate
@SuppressForbidden(reason = "Make HEAD request using URLConnection.connect()")
boolean urlExists(Terminal terminal, String urlString) throws IOException {
terminal.println(VERBOSE, "Checking if url exists: " + urlString);
URL url = new URL(urlString);
assert "https".equals(url.getProtocol()) : "Use of https protocol is required";
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.addRequestProperty("User-Agent", "opensearch-plugin-installer");
urlConnection.setRequestMethod("HEAD");
urlConnection.connect();
return urlConnection.getResponseCode() == 200;
}
use of org.opensearch.common.SuppressForbidden in project OpenSearch by opensearch-project.
the class DetectEsInstallationTask method fetchInfoFromUrl.
@SuppressForbidden(reason = "Retrieve information on the installation.")
private Map<?, ?> fetchInfoFromUrl(final String url) {
try {
final URL esUrl = new URL(url);
final HttpURLConnection conn = (HttpURLConnection) esUrl.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(1000);
conn.connect();
final StringBuilder json = new StringBuilder();
final Scanner scanner = new Scanner(esUrl.openStream());
while (scanner.hasNext()) {
json.append(scanner.nextLine());
}
scanner.close();
final ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json.toString(), Map.class);
} catch (IOException e) {
throw new RuntimeException("Error retrieving elasticsearch cluster info, " + e);
}
}
use of org.opensearch.common.SuppressForbidden in project OpenSearch by opensearch-project.
the class JarHell method parseClassPath.
/**
* Parses the classpath into a set of URLs. For testing.
* @param classPath classpath to parse (typically the system property {@code java.class.path})
* @return array of URLs
* @throws IllegalStateException if the classpath contains empty elements
*/
@SuppressForbidden(reason = "resolves against CWD because that is how classpaths work")
static Set<URL> parseClassPath(String classPath) {
String pathSeparator = System.getProperty("path.separator");
String fileSeparator = System.getProperty("file.separator");
String[] elements = classPath.split(pathSeparator);
// order is already lost, but some filesystems have it
Set<URL> urlElements = new LinkedHashSet<>();
for (String element : elements) {
// Instead we just throw an exception, and keep it clean.
if (element.isEmpty()) {
throw new IllegalStateException("Classpath should not contain empty elements! (outdated shell script from a previous" + " version?) classpath='" + classPath + "'");
}
// specification which java seems to allow, explicitly, right here...
if (element.startsWith("/") && "\\".equals(fileSeparator)) {
// "correct" the entry to become a normal entry
// change to correct file separators
element = element.replace("/", "\\");
// if there is a drive letter, nuke the leading separator
if (element.length() >= 3 && element.charAt(2) == ':') {
element = element.substring(1);
}
}
// now just parse as ordinary file
try {
if (element.equals("/")) {
// Eclipse adds this to the classpath when running unit tests...
continue;
}
URL url = PathUtils.get(element).toUri().toURL();
// junit4.childvm.count
if (urlElements.add(url) == false && element.endsWith(".jar")) {
throw new IllegalStateException("jar hell!" + System.lineSeparator() + "duplicate jar [" + element + "] on classpath: " + classPath);
}
} catch (MalformedURLException e) {
// should not happen, as we use the filesystem API
throw new RuntimeException(e);
}
}
return Collections.unmodifiableSet(urlElements);
}
Aggregations