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

Added null-value handling for script arguments and updated properties #2

Merged
merged 1 commit into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ The `argumentStyle` configuration property can take following values:

|====

The `handleNullValues` configuration property can take the following values:
|====
| handleNullValues | Description | Combination with `argumentStyle` | Example where `foo` is `null`

| `asEmptyString`
| Arguments that contain a `null` value will be interpreted as an empty string.
| `dash` | `command -foo -bar baz`
||| `slash` | `command /foo /bar baz`
||| `variables-bash` | `foo=''; bar='baz'; command $foo $bar`
||| `variables-powershell` | `$foo=''; $bar='baz'; command $foo $bar`

| `asGone`
| Arguments that contain a `null` value removed from the argument list.
| `dash` | `command -bar baz`
||| `slash` | `command /bar baz`
||| `variables-bash` | `bar='baz'; command $foo $bar`
||| `variables-powershell` | `$bar='baz'; command $foo $bar`

|====


== Limitations

* Only password authentication is supported, at least for now.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,25 @@ private String encodeArgumentsAndCommandToString(String command, Map<String, Obj
// we want this to go last
continue;
}
commandLineBuilder.append(" ");
commandLineBuilder.append(paramPrefix).append(argEntry.getKey());
if (argEntry.getValue() != null) {
Object value = argEntry.getValue();
boolean insertAttribute = true;
if(value == null) {
switch (configuration.getHandleNullValues()) {
case SshConfiguration.HANDLE_NULL_AS_EMPTY_STRING:
value = "";
break;
case SshConfiguration.HANDLE_NULL_AS_GONE:
insertAttribute = false;
break;
default:
throw new ConfigurationException("Unknown value of handleNullValues: " + configuration.getHandleNullValues());
}
}
if(insertAttribute) {
commandLineBuilder.append(" ");
commandLineBuilder.append(paramPrefix).append(argEntry.getKey());
commandLineBuilder.append(" ");
commandLineBuilder.append(argEntry.getValue().toString());
commandLineBuilder.append(value);
}
}
if (arguments.get(null) != null) {
Expand All @@ -86,14 +100,30 @@ private String encodeVariablesAndCommandToString(String command, Map<String, Obj
// we want this to go last
continue;
}
commandLineBuilder.append(variablePrefix).append(argEntry.getKey());
if (spaces) {
commandLineBuilder.append(" = ");
} else {
commandLineBuilder.append("=");
Object value = argEntry.getValue();
boolean insertAttribute = true;
if(value == null) {
switch (configuration.getHandleNullValues()) {
case SshConfiguration.HANDLE_NULL_AS_EMPTY_STRING:
value = "";
break;
case SshConfiguration.HANDLE_NULL_AS_GONE:
insertAttribute = false;
break;
default:
throw new ConfigurationException("Unknown value of handleNullValues: " + configuration.getHandleNullValues());
}
}
if(insertAttribute) {
commandLineBuilder.append(variablePrefix).append(argEntry.getKey());
if (spaces) {
commandLineBuilder.append(" = ");
} else {
commandLineBuilder.append("=");
}
commandLineBuilder.append(quoteSingle(value));
commandLineBuilder.append("; ");
}
commandLineBuilder.append(quoteSingle(argEntry.getValue().toString()));
commandLineBuilder.append("; ");
}
commandLineBuilder.append(command);
if (arguments.get(null) != null) {
Expand All @@ -104,9 +134,6 @@ private String encodeVariablesAndCommandToString(String command, Map<String, Obj
}

private String quoteSingle(Object value) {
if (value == null) {
return "";
}
return "'" + value.toString().replaceAll("'", "''") + "'";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package com.evolveum.polygon.connector.ssh;

import org.identityconnectors.common.logging.Log;
import org.identityconnectors.common.security.GuardedString;
import org.identityconnectors.framework.common.exceptions.ConfigurationException;
import org.identityconnectors.framework.spi.AbstractConfiguration;
import org.identityconnectors.framework.spi.ConfigurationProperty;

Expand Down Expand Up @@ -84,6 +82,15 @@ public class SshConfiguration extends AbstractConfiguration {
// fu='foo'; bar='baz'; command $foo $bar
public static final String ARGUMENT_STYLE_VARIABLES_BASH = "variables-bash";

/**
* Defines how to handle NULL value arguments.
* If a script argument is NULL, it can be inserted as an empty string ("asEmpty") or it can be removed from the argument list ("asGone").
*/
private String handleNullValues = HANDLE_NULL_AS_GONE;

public static final String HANDLE_NULL_AS_EMPTY_STRING = "asEmptyString";
public static final String HANDLE_NULL_AS_GONE = "asGone";

@ConfigurationProperty(order = 100)
public String getHost() {
return host;
Expand Down Expand Up @@ -138,6 +145,15 @@ public void setArgumentStyle(String argumentStyle) {
this.argumentStyle = argumentStyle;
}

@ConfigurationProperty(order = 130)
public String getHandleNullValues() {
return handleNullValues;
}

public void setHandleNullValues(String handleNullValues) {
this.handleNullValues = handleNullValues;
}

@Override
public void validate() {
}
Expand Down