Skip to content

Commit

Permalink
Extend ./bin/opensearch-plugin install <plugin_name> to include plugi…
Browse files Browse the repository at this point in the history
…ns in the default distribution

Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Nov 7, 2024
1 parent b5c3792 commit 9653ff2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@
*/
class InstallPluginCommand extends EnvironmentAwareCommand {

private static final String PROPERTY_STAGING_ID = "opensearch.plugins.staging";

// exit codes for install
/** A plugin with the same name is already installed. */
static final int PLUGIN_EXISTS = 1;
Expand Down Expand Up @@ -307,14 +305,7 @@ void execute(Terminal terminal, List<String> pluginIds, boolean isBatch, Environ
private Path download(Terminal terminal, String pluginId, Path tmpDir, boolean isBatch) throws Exception {

if (OFFICIAL_PLUGINS.contains(pluginId)) {
final String url = getOpenSearchUrl(
terminal,
getStagingHash(),
Version.CURRENT,
isSnapshot(),
pluginId,
Platforms.PLATFORM_NAME
);
final String url = getOpenSearchUrl(terminal, Version.CURRENT, isSnapshot(), pluginId, Platforms.PLATFORM_NAME);
terminal.println("-> Downloading " + pluginId + " from opensearch");
return downloadAndValidate(terminal, url, tmpDir, true, isBatch);
}
Expand All @@ -341,38 +332,35 @@ private Path download(Terminal terminal, String pluginId, Path tmpDir, boolean i
return downloadZip(terminal, pluginId, tmpDir, isBatch);
}

// pkg private so tests can override
String getStagingHash() {
return System.getProperty(PROPERTY_STAGING_ID);
}

boolean isSnapshot() {
return Build.CURRENT.isSnapshot();
}

/** Returns the url for an official opensearch plugin. */
private String getOpenSearchUrl(
final Terminal terminal,
final String stagingHash,
final Version version,
final boolean isSnapshot,
final String pluginId,
final String platform
) throws IOException, UserException {
) throws IOException {
final String baseUrl;
if (isSnapshot && stagingHash == null) {
throw new UserException(
ExitCodes.CONFIG,
"attempted to install release build of official plugin on snapshot build of OpenSearch"
);
}
if (stagingHash != null) {
baseUrl = String.format(
// default plugins start with opensearch-
if (pluginId.startsWith("opensearch-")) {
String repository = "releases";
if (isSnapshot) {
repository = "snapshots";
}
String defaultPluginVersion = version + ".0";
if (isSnapshot) {
defaultPluginVersion += "-SNAPSHOT";
}
return String.format(
Locale.ROOT,
"https://artifacts.opensearch.org/snapshots/plugins/%s/%s-%s",
"https://aws.oss.sonatype.org/service/local/artifact/maven/redirect?r=%s&g=org.opensearch.plugin&a=%s&v=%s&e=zip",
repository,
pluginId,
version,
stagingHash
defaultPluginVersion
);
} else {
baseUrl = String.format(
Expand Down Expand Up @@ -566,7 +554,7 @@ private Path downloadAndValidate(
final BufferedReader checksumReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
final String checksumLine = checksumReader.readLine();
final String[] fields = checksumLine.split(" {2}");
if (officialPlugin && fields.length != 2 || officialPlugin == false && fields.length > 2) {
if (fields.length == 0 || fields.length > 2) {
throw new UserException(ExitCodes.IO_ERROR, "Invalid checksum file at " + checksumUrl);
}
expectedChecksum = fields[0];
Expand Down Expand Up @@ -614,7 +602,7 @@ private Path downloadAndValidate(
}
}

if (officialPlugin) {
if (officialPlugin && urlString.contains("artifacts.opensearch.org")) {
verifySignature(zip, urlString);
}

Expand Down
Loading

0 comments on commit 9653ff2

Please sign in to comment.