Skip to content

Commit

Permalink
Merge pull request #173 from sentrysoftware/111-develop-wbem-extension
Browse files Browse the repository at this point in the history
Issue #111: Develop wbem extension
  • Loading branch information
NassimBtk committed May 15, 2024
2 parents f1c7bec + 0e5cbd5 commit fe8fdf0
Show file tree
Hide file tree
Showing 35 changed files with 2,238 additions and 1,944 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public class ProtocolsConfig {
private IConfiguration ssh;

@JsonSetter(nulls = SKIP)
private WbemProtocolConfig wbem;
@JsonDeserialize(using = ExtensionConfigDeserializer.class)
private IConfiguration wbem;

@JsonSetter(nulls = SKIP)
@JsonDeserialize(using = ExtensionConfigDeserializer.class)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@
import org.sentrysoftware.metricshub.agent.config.ConnectorVariables;
import org.sentrysoftware.metricshub.agent.config.ResourceConfig;
import org.sentrysoftware.metricshub.agent.config.ResourceGroupConfig;
import org.sentrysoftware.metricshub.agent.config.protocols.AbstractProtocolConfig;
import org.sentrysoftware.metricshub.agent.config.protocols.ProtocolsConfig;
import org.sentrysoftware.metricshub.agent.config.protocols.WbemProtocolConfig;
import org.sentrysoftware.metricshub.agent.config.protocols.WinRmProtocolConfig;
import org.sentrysoftware.metricshub.agent.context.MetricDefinitions;
import org.sentrysoftware.metricshub.agent.security.PasswordEncrypt;
Expand Down Expand Up @@ -945,15 +943,9 @@ private static void validateProtocols(@NonNull final String resourceKey, final R
sshConfig.validateConfiguration(resourceKey);
}

final WbemProtocolConfig wbemConfig = protocolsConfig.getWbem();
final IConfiguration wbemConfig = protocolsConfig.getWbem();
if (wbemConfig != null) {
validateWbemInfo(
resourceKey,
wbemConfig.getUsername(),
wbemConfig.getTimeout(),
wbemConfig.getPort(),
wbemConfig.getVCenter()
);
wbemConfig.validateConfiguration(resourceKey);
}

final IConfiguration wmiConfig = protocolsConfig.getWmi();
Expand Down Expand Up @@ -1069,18 +1061,6 @@ static HostConfiguration buildHostConfiguration(
) {
final ProtocolsConfig protocols = resourceConfig.getProtocols();

final Map<Class<? extends IConfiguration>, IConfiguration> protocolConfigurationsDeprecated = protocols == null
? new HashMap<>()
: new HashMap<>(
Stream
.of(protocols.getWbem(), protocols.getWinrm())
.filter(Objects::nonNull)
.map(AbstractProtocolConfig::toConfiguration)
.filter(Objects::nonNull)
.collect(Collectors.toMap(IConfiguration::getClass, Function.identity()))
);

// TODO Temporary: when all the configurations are IConfiguration we will remove protocolConfigurationsDeprecated variable
final Map<Class<? extends IConfiguration>, IConfiguration> protocolConfigurations = protocols == null
? new HashMap<>()
: new HashMap<>(
Expand All @@ -1091,16 +1071,13 @@ static HostConfiguration buildHostConfiguration(
protocols.getIpmi(),
protocols.getOsCommand(),
protocols.getSsh(),
protocols.getWmi()
protocols.getWmi(),
protocols.getWbem()
)
.filter(Objects::nonNull)
.collect(Collectors.toMap(IConfiguration::getClass, Function.identity()))
);

// TODO remove when protocolConfigurationsDeprecated is removed, means all the configuration are IConfguration
// protocols
protocolConfigurations.putAll(protocolConfigurationsDeprecated);

final Map<String, String> attributes = resourceConfig.getAttributes();

// Get the host name and make sure it is always set because the engine needs a hostname
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@
* ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
*/

import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.sentrysoftware.metricshub.cli.service.converter.TransportProtocolConverter;
import org.sentrysoftware.metricshub.cli.service.CliExtensionManager;
import org.sentrysoftware.metricshub.engine.common.exception.InvalidConfigurationException;
import org.sentrysoftware.metricshub.engine.configuration.IConfiguration;
import org.sentrysoftware.metricshub.engine.configuration.TransportProtocols;
import org.sentrysoftware.metricshub.engine.configuration.WbemConfiguration;
import picocli.CommandLine.Option;

/**
* This class is used by MetricsHubCliService to configure WBEM protocol when using the MetricsHub CLI.
* It create the engine's {@link WbemConfiguration} object that is used to monitor a specific resource using WBEM.
* It creates the engine's {@link IConfiguration} object that is used to monitor a specific resource using WBEM.
*/
@Data
@EqualsAndHashCode(callSuper = true)
Expand All @@ -50,18 +53,17 @@ public class WbemConfigCli extends AbstractTransportProtocolCli {
order = 2,
defaultValue = "HTTPS",
paramLabel = "HTTP|HTTPS",
description = "Transport protocol for WBEM (default: ${DEFAULT-VALUE})",
converter = TransportProtocolConverter.class
description = "Transport protocol for WBEM (default: ${DEFAULT-VALUE})"
)
TransportProtocols protocol;
private String protocol;

@Option(
names = "--wbem-port",
order = 3,
paramLabel = "PORT",
description = "Port of the WBEM server (default: 5988 for HTTP, 5989 for HTTPS)"
)
Integer port;
private Integer port;

@Option(names = "--wbem-username", order = 4, paramLabel = "USER", description = "Username for WBEM authentication")
String username;
Expand All @@ -74,7 +76,7 @@ public class WbemConfigCli extends AbstractTransportProtocolCli {
interactive = true,
arity = "0..1"
)
char[] password;
private char[] password;

@Option(
names = "--wbem-timeout",
Expand All @@ -83,43 +85,54 @@ public class WbemConfigCli extends AbstractTransportProtocolCli {
paramLabel = "TIMEOUT",
description = "Timeout in seconds for WBEM operations (default: ${DEFAULT-VALUE} s)"
)
long timeout;
private String timeout;

@Option(
names = "--wbem-force-namespace",
order = 7,
paramLabel = "NAMESPACE",
description = "Forces a specific namespace for connectors that perform namespace auto-detection (advanced)"
)
String namespace;
private String namespace;

@Option(
names = "--wbem-vcenter",
order = 8,
paramLabel = "VCENTER",
description = "VCenter hostname providing the authentication ticket (if applicable)"
)
String vcenter;
private String vcenter;

/**
* This method creates an {@link WbemConfiguration} for a given username and a given password
* This method creates an {@link IConfiguration} for a given username and a given password
*
* @param defaultUsername Username specified at the top level of the CLI (with the --username option)
* @param defaultPassword Password specified at the top level of the CLI (with the --password option)
* @return an {@link WbemConfiguration} instance corresponding to the options specified by the user in the CLI
* @return an {@link IConfiguration} instance corresponding to the options specified by the user in the CLI
*/
@Override
public IConfiguration toProtocol(final String defaultUsername, final char[] defaultPassword) {
return WbemConfiguration
.builder()
.protocol(protocol)
.port(getOrDeducePortNumber())
.username(username == null ? defaultUsername : username)
.password(username == null ? defaultPassword : password)
.namespace(namespace)
.timeout(timeout)
.vCenter(vcenter)
.build();
public IConfiguration toProtocol(final String defaultUsername, final char[] defaultPassword)
throws InvalidConfigurationException {
final ObjectNode configuration = JsonNodeFactory.instance.objectNode();

final String finalUsername = username == null ? defaultUsername : username;
configuration.set("username", new TextNode(finalUsername));

final char[] finalPassword = username == null ? defaultPassword : password;
if (finalPassword != null) {
configuration.set("password", new TextNode(String.valueOf(finalPassword)));
}

configuration.set("timeout", new TextNode(timeout));
configuration.set("namespace", new TextNode(namespace));
configuration.set("vcenter", new TextNode(vcenter));
configuration.set("protocol", new TextNode(protocol));
configuration.set("port", new IntNode(getOrDeducePortNumber()));

return CliExtensionManager
.getExtensionManagerSingleton()
.buildConfigurationFromJsonNode("wbem", configuration, value -> value)
.orElseThrow();
}

/**
Expand All @@ -145,6 +158,6 @@ protected int defaultHttpPortNumber() {
*/
@Override
protected boolean isHttps() {
return TransportProtocols.HTTPS.equals(protocol);
return "https".equalsIgnoreCase(protocol);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.List;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.sentrysoftware.metricshub.cli.service.converter.TransportProtocolConverter;
import org.sentrysoftware.metricshub.engine.configuration.IConfiguration;
import org.sentrysoftware.metricshub.engine.configuration.TransportProtocols;
import org.sentrysoftware.metricshub.engine.configuration.WinRmConfiguration;
Expand Down Expand Up @@ -60,10 +59,9 @@ public class WinRmConfigCli extends AbstractTransportProtocolCli {
order = 2,
paramLabel = "HTTP|HTTPS",
defaultValue = "HTTP",
description = "Transport protocol for WinRM (default: ${DEFAULT-VALUE})",
converter = TransportProtocolConverter.class
description = "Transport protocol for WinRM (default: ${DEFAULT-VALUE})"
)
private TransportProtocols protocol;
private String protocol;

@Option(
names = { "--winrm-username" },
Expand Down Expand Up @@ -131,7 +129,7 @@ public IConfiguration toProtocol(String defaultUsername, char[] defaultPassword)
.password(username == null ? defaultPassword : password)
.namespace(namespace)
.port(getOrDeducePortNumber())
.protocol(protocol)
.protocol(TransportProtocols.interpretValueOf(protocol))
.authentications(authentications)
.timeout(timeout)
.build();
Expand All @@ -158,6 +156,6 @@ protected int defaultHttpPortNumber() {
*/
@Override
protected boolean isHttps() {
return TransportProtocols.HTTPS.equals(protocol);
return "https".equalsIgnoreCase(protocol);
}
}
Loading

0 comments on commit fe8fdf0

Please sign in to comment.