Skip to content
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 @@ -17,6 +17,8 @@

package org.apache.ignite.internal;

import java.io.Serializable;
import java.util.Map;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.cluster.ClusterNode;
import org.apache.ignite.lang.IgniteFuture;
Expand Down Expand Up @@ -81,7 +83,7 @@ public PluginProvider plugin() {

/** {@inheritDoc} */
@Nullable @Override public DiscoveryDataExchangeType discoveryDataType() {
return null;
return DiscoveryDataExchangeType.PLUGIN;
}

/** {@inheritDoc} */
Expand All @@ -106,8 +108,19 @@ public PluginProvider plugin() {

/** {@inheritDoc} */
@Nullable @Override public IgniteNodeValidationResult validateNode(ClusterNode node) {
return null;
}

/** {@inheritDoc} */
@Nullable @Override public IgniteNodeValidationResult validateNode(ClusterNode node,
JoiningNodeDiscoveryData discoData) {
try {
plugin.validateNewNode(node);
Map<String, Serializable> map = (Map<String, Serializable>)discoData.joiningNodeData();

if (map != null)
plugin.validateNewNode(node, map.get(plugin.name()));
else
plugin.validateNewNode(node, null);

return null;
}
Expand All @@ -116,11 +129,6 @@ public PluginProvider plugin() {
}
}

/** {@inheritDoc} */
@Nullable @Override public IgniteNodeValidationResult validateNode(ClusterNode node, JoiningNodeDiscoveryData discoData) {
return null;
}

/** {@inheritDoc} */
@Override public void printMemoryStats() {
// No-op.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ServiceLoader;
import java.util.UUID;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteCluster;
import org.apache.ignite.cluster.ClusterNode;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -141,6 +142,22 @@ public interface PluginProvider<C extends PluginConfiguration> {
*
* @param node Joining node.
* @throws PluginValidationException If cluster-wide plugin validation failed.
*
* @deprecated Use {@link #validateNewNode(ClusterNode, Serializable)} instead.
*/
@Deprecated
public void validateNewNode(ClusterNode node) throws PluginValidationException;

/**
* Validates that new node can join grid topology, this method is called on coordinator
* node before new node joins topology.
*
* @param node Joining node.
* @param data Discovery data object or {@code null} if nothing was
* sent for this component.
* @throws PluginValidationException If cluster-wide plugin validation failed.
*/
public default void validateNewNode(ClusterNode node, Serializable data) {
validateNewNode(node);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ org.apache.ignite.spi.discovery.tcp.TestReconnectPluginProvider
org.apache.ignite.internal.processors.cache.persistence.standbycluster.IgniteStandByClusterTest$StanByClusterTestProvider
org.apache.ignite.internal.processors.cache.persistence.wal.memtracker.PageMemoryTrackerPluginProvider
org.apache.ignite.internal.processors.configuration.distributed.TestDistibutedConfigurationPlugin
org.apache.ignite.plugin.NodeValidationPluginProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ignite.plugin;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

License is desired


import java.io.Serializable;
import java.util.UUID;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.cluster.ClusterNode;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.jetbrains.annotations.Nullable;

/**
* Validates node on join, it requires nodes to provide token that matches configured on primary node.
*/
public class NodeValidationPluginProvider implements PluginProvider, IgnitePlugin {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments everywhere as per CC


/** */
private NodeValidationPluginConfiguration pluginConfiguration;
/** */
private static volatile boolean enabled;

/** */
public static boolean isEnabled() {
return enabled;
}

/** */
public static void setEnabled(boolean enabled) {
NodeValidationPluginProvider.enabled = enabled;
}

/** {@inheritDoc} */
@Override public String name() {
return "NodeValidationPluginProvider";
}

/** {@inheritDoc} */
@Override public String version() {
return "1.0";
}

/** {@inheritDoc} */
@Override public String copyright() {
return "";
}

/** {@inheritDoc} */
@Override public IgnitePlugin plugin() {
return this;
}

/** {@inheritDoc} */
@Override public void initExtensions(PluginContext ctx, ExtensionRegistry registry) {
if (!enabled)
return;

IgniteConfiguration igniteCfg = ctx.igniteConfiguration();

if (igniteCfg.getPluginConfigurations() != null) {
for (PluginConfiguration pluginCfg : igniteCfg.getPluginConfigurations()) {
if (pluginCfg instanceof NodeValidationPluginConfiguration) {
pluginConfiguration = (NodeValidationPluginConfiguration)pluginCfg;

break;
}
}
}
}

/** {@inheritDoc} */
@Nullable @Override public Object createComponent(PluginContext ctx, Class cls) {
return null;
}

/** {@inheritDoc} */
@Override public CachePluginProvider createCacheProvider(CachePluginContext ctx) {
return null;
}

/** {@inheritDoc} */
@Override public void start(PluginContext ctx) throws IgniteCheckedException {
//no-op
}

/** {@inheritDoc} */
@Override public void stop(boolean cancel) throws IgniteCheckedException {
//no-op
}

/** {@inheritDoc} */
@Override public void onIgniteStart() throws IgniteCheckedException {
//no-op
}

/** {@inheritDoc} */
@Override public void onIgniteStop(boolean cancel) {
//no-op
}

/** {@inheritDoc} */
@Nullable @Override public Serializable provideDiscoveryData(UUID nodeId) {
if (!enabled)
return null;

MyDiscoData data = new MyDiscoData(pluginConfiguration.getToken());

return data;
}

/** {@inheritDoc} */
@Override public void receiveDiscoveryData(UUID nodeId, Serializable data) {
if (!enabled)
return;
}

/** {@inheritDoc} */
@Override public void validateNewNode(ClusterNode node) throws PluginValidationException {
// no-op
}

/** {@inheritDoc} */
@Override public void validateNewNode(ClusterNode node, Serializable serializable) {
if (!enabled)
return;

MyDiscoData newNodeDiscoData = serializable instanceof MyDiscoData ? (MyDiscoData)serializable : null;

if (newNodeDiscoData == null || !newNodeDiscoData.getToken().equals(pluginConfiguration.getToken())) {
String msg = newNodeDiscoData == null ? "no token provided" : "bad token provided: " + newNodeDiscoData.getToken();

throw new PluginValidationException(msg, msg, node.id());
}
}

/**
*
*/
private static class MyDiscoData implements Serializable {
/** */
String token;

/** */
MyDiscoData(String token) {
this.token = token;
}

/** */
public String getToken() {
return token;
}

/** */
@Override public String toString() {
return "MyDiscoData{" +
"token='" + token + '\'' +
'}';
}
}

/**
*
*/
public static class NodeValidationPluginConfiguration implements PluginConfiguration {
/** */
private final String token;

/** */
NodeValidationPluginConfiguration(String token) {
this.token = token;
}

/** */
public String getToken() {
return token;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ignite.plugin;

import org.apache.ignite.configuration.DataRegionConfiguration;
import org.apache.ignite.configuration.DataStorageConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.util.typedef.X;
import org.apache.ignite.spi.IgniteSpiException;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Test node validation on join by plugin.
*/
@RunWith(JUnit4.class)
public class PluginNodeValidationTest extends GridCommonAbstractTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments as per CC


/** */
private volatile String token;

/** {@inheritDoc} */
@Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);

cfg.setDataStorageConfiguration(new DataStorageConfiguration()
.setDefaultDataRegionConfiguration(new DataRegionConfiguration()
.setMaxSize(100L * 1024 * 1024)
.setPersistenceEnabled(true)));

cfg.setConsistentId(igniteInstanceName);

cfg.setPluginConfigurations(new NodeValidationPluginProvider.NodeValidationPluginConfiguration(token));

return cfg;
}

/** Tests that node join fails due failure in node validation. */
@Test
public void testValidationException() throws Exception {
token = "123456";

startGrid(0);

token = "abcdef";

try {
startGrid(1);
} catch (Exception ex) {
assertTrue("Wrong exception type for validation error", X.hasCause(ex, IgniteSpiException.class));

return;
}

fail("Exception is expected due validation error in plugin");
}

/** Tests that node joins on successful node validation by plugin. */
@Test
public void testSuccessfulValidation() throws Exception {
token = "123456";

startGrid(0);
startGrid(1);
}

/** Stop all nodes after each test. */
@After
public void after() {
stopAllGrids();
}

/** Enables plugin before test start. */
@BeforeClass
public static void enablePlugin() {
NodeValidationPluginProvider.setEnabled(true);
}

/** Disable plugin after test end. */
@AfterClass
public static void disablePlugin() {
NodeValidationPluginProvider.setEnabled(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public class TestReconnectPluginProvider implements PluginProvider {
@Override public void validateNewNode(ClusterNode node) throws PluginValidationException {
// No-op
}

/** {@inheritDoc} */
@Nullable @Override public Object createComponent(PluginContext ctx, Class cls) {
if (enabled && GridSecurityProcessor.class.equals(cls))
Expand Down
Loading