diff options
Diffstat (limited to 'framework/src/main/java/org')
5 files changed, 98 insertions, 13 deletions
diff --git a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java index 6cee09de..13e67fc6 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java @@ -29,6 +29,7 @@ import org.onap.cli.fw.error.OnapCommandInvalidResultAttributeScope; import org.onap.cli.fw.error.OnapCommandInvalidSchema; import org.onap.cli.fw.error.OnapCommandInvalidSchemaVersion; import org.onap.cli.fw.error.OnapCommandNotInitialized; +import org.onap.cli.fw.error.OnapCommandParameterMissing; import org.onap.cli.fw.error.OnapCommandParameterNameConflict; import org.onap.cli.fw.error.OnapCommandParameterOptionConflict; import org.onap.cli.fw.error.OnapCommandRegistrationFailed; @@ -42,6 +43,7 @@ import org.onap.cli.fw.utils.OnapCommandUtils; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Set; /** * Onap Command. @@ -184,7 +186,22 @@ public abstract class OnapCommand { */ protected void validate() throws OnapCommandException { for (OnapCommandParameter param : this.getParameters()) { - param.validate(); + try { + param.validate(); + } catch (OnapCommandParameterMissing e) { + if (OnapCommandConfg.getExcludeParamsForNoAuthEnableExternalCmd().contains(param.getName())) { + OnapCommandParameter noAuthParam = this.getParameters().stream().filter(p -> p.getName() + .equalsIgnoreCase(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH)).findFirst().get(); + + if ("true".equalsIgnoreCase(noAuthParam.getValue().toString())) { + continue; + } + } + throw e; + } catch (OnapCommandException e) { + throw e; + } + } } diff --git a/framework/src/main/java/org/onap/cli/fw/conf/Constants.java b/framework/src/main/java/org/onap/cli/fw/conf/Constants.java index 8886de55..5f95518c 100644 --- a/framework/src/main/java/org/onap/cli/fw/conf/Constants.java +++ b/framework/src/main/java/org/onap/cli/fw/conf/Constants.java @@ -106,6 +106,10 @@ public class Constants { public static final String ONAP_CLI_VERSION = "cli.version"; public static final String HTTP_API_KEY_USE_COOKIES = "http.api_key_use_cookies"; public static final String HTTP_X_AUTH_TOKEN = "http.x_auth_token"; + public static final String EXCLUDE_PARAMS_INTERNAL_CMD = "cli.exclude_params_internal_cmd"; + public static final String NO_AUTH_DISABLE_INCLUDE_PARAMS_EXTERNAL_CMD = "cli.no_auth_disable_include_params_external_cmd"; + public static final String NO_AUTH_ENABLE_EXCLUDE_PARAMS_EXTERNAL_CMD = "cli.no_auth_enable_exclude_params_external_cmd"; + public static final String NO_AUTH_ENABLE_INCLUDE_PARAMS_EXTERNAL_CMD = "cli.no_auth_enable_include_params_external_cmd"; // Used while printing the column name during PORTRAIT mode print public static final String PORTRAINT_COLUMN_NAME_PROPERTY = "property"; @@ -148,7 +152,7 @@ public class Constants { public static final String DEFAULT_SCHEMA_FILE_NAME = "default_input_parameters.yaml"; // Error message - public static final String SCHEMA_INVALID_DEFAULT_PARAMS_SECTION = "Invalid default parameter section"; + public static final String SCHEMA_INVALID_DEFAULT_PARAMS_SECTION = "Invalid default_parameter section"; public static final String SCHEMA_FILE_EMPTY = "The schema file cann't be null or empty"; public static final String SCHEMA_FILE_WRONG_EXTN = "Schema file should be '.yaml' extension"; public static final String SCHEMA_FILE_NOT_EXIST = "Schema file doesn't exist"; diff --git a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java index bcfd6dfb..c2682cae 100644 --- a/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java +++ b/framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConfg.java @@ -17,7 +17,10 @@ package org.onap.cli.fw.conf; import java.io.IOException; +import java.util.Arrays; import java.util.Properties; +import java.util.Set; +import java.util.stream.Collectors; /** * Onap command constants. @@ -88,4 +91,23 @@ public final class OnapCommandConfg { return prps.getProperty(Constants.AUTH_SERVICE); } + public static Set<String> getExcludeParamsForInternalCmd() { + return Arrays.stream(prps.getProperty(Constants.EXCLUDE_PARAMS_INTERNAL_CMD) + .split(",")).map(String::trim).collect(Collectors.toSet()); + } + + public static Set<String> getIncludeParamsForNoAuthDisableExternalCmd() { + return Arrays.stream(prps.getProperty(Constants.NO_AUTH_DISABLE_INCLUDE_PARAMS_EXTERNAL_CMD) + .split(",")).map(String::trim).collect(Collectors.toSet()); + } + + public static Set<String> getExcludeParamsForNoAuthEnableExternalCmd() { + return Arrays.stream(prps.getProperty(Constants.NO_AUTH_ENABLE_EXCLUDE_PARAMS_EXTERNAL_CMD) + .split(",")).map(String::trim).collect(Collectors.toSet()); + } + + public static Set<String> getIncludeParamsForNoAuthEnableExternalCmd() { + return Arrays.stream(prps.getProperty(Constants.NO_AUTH_ENABLE_INCLUDE_PARAMS_EXTERNAL_CMD) + .split(",")).map(String::trim).collect(Collectors.toSet()); + } } diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidDefaultParameter.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidDefaultParameter.java index 4ebf04d3..7dc4374e 100644 --- a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidDefaultParameter.java +++ b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandInvalidDefaultParameter.java @@ -30,6 +30,6 @@ public class OnapCommandInvalidDefaultParameter extends OnapCommandException { * @param invalidParamsList message */ public OnapCommandInvalidDefaultParameter(List<String> invalidParamsList) { - super("0x0024", "Invalid default parameter: " + invalidParamsList.toString()); + super("0x0024", "Invalid default_parameter: " + invalidParamsList.toString()); } } diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java index 1ad588de..1a5aba0f 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java @@ -25,6 +25,7 @@ import org.onap.cli.fw.ad.OnapService; import org.onap.cli.fw.cmd.OnapHttpCommand; import org.onap.cli.fw.cmd.OnapSwaggerCommand; import org.onap.cli.fw.conf.Constants; +import org.onap.cli.fw.conf.OnapCommandConfg; import org.onap.cli.fw.error.OnapCommandDiscoveryFailed; import org.onap.cli.fw.error.OnapCommandException; import org.onap.cli.fw.error.OnapCommandHelpFailed; @@ -175,6 +176,42 @@ public class OnapCommandUtils { } } + private static void processNoAuth(Set<String> parameterSet, final OnapCommand cmd, final List<String> includeParams, + final List<String> excludeParams) throws OnapCommandInvalidDefaultParameter { + // processing for no-auth type + if (cmd.getService() != null) { + List<String> includeAuthParams = new ArrayList(); + List<String> excludeAuthParams = new ArrayList<>(); + boolean noAuth = cmd.getService().isNoAuth(); + + if (cmd.isCommandInternal()) { + excludeAuthParams.addAll(OnapCommandConfg.getExcludeParamsForInternalCmd()); + } else { + if (noAuth) { + includeAuthParams.addAll(OnapCommandConfg.getIncludeParamsForNoAuthEnableExternalCmd()); + excludeAuthParams.addAll(OnapCommandConfg.getExcludeParamsForNoAuthEnableExternalCmd()); + } else { + includeAuthParams.addAll(OnapCommandConfg.getIncludeParamsForNoAuthDisableExternalCmd()); + } + } + + List<String> invalidExclude = excludeAuthParams.stream().filter(includeParams::contains) + .collect(Collectors.toList()); + + List<String> invalidInclude = includeAuthParams.stream().filter(excludeParams::contains) + .filter(p->!includeParams.contains(p)).collect(Collectors.toList()); + + if (!invalidExclude.isEmpty() || !invalidInclude.isEmpty()) { + throw new OnapCommandInvalidDefaultParameter(Stream.concat(invalidExclude.stream(), invalidInclude.stream()) + .collect(Collectors.toList())); + } + + + parameterSet.addAll(includeAuthParams); + parameterSet.removeAll(excludeAuthParams); + } + } + private static void parseSchema(OnapCommand cmd, final Map<String, ?> values, final List<String> defaultParamNames) throws OnapCommandException { @@ -222,6 +259,8 @@ public class OnapCommandUtils { } else if (Constants.DEFAULT_PARAMETERS.equals(key)) { Map<String, List<String>> defParameters = (Map) values.get(Constants.DEFAULT_PARAMETERS); + List<String> includeParams = new ArrayList<>(); + List<String> excludeParams = new ArrayList<>(); if (values.containsKey(Constants.DEFAULT_PARAMETERS) && defParameters == null) { // if default parameter section is available then it must have either include @@ -229,22 +268,25 @@ public class OnapCommandUtils { throw new OnapCommandInvalidSchema(Constants.SCHEMA_INVALID_DEFAULT_PARAMS_SECTION); } + if (defParameters != null) { // validate default parameters - List<String> includeParams = defParameters.containsKey(Constants.DEFAULT_PARAMETERS_INCLUDE) ? - defParameters.get(Constants.DEFAULT_PARAMETERS_INCLUDE) : new ArrayList<>(); + if (defParameters.containsKey(Constants.DEFAULT_PARAMETERS_INCLUDE)) { + includeParams = defParameters.get(Constants.DEFAULT_PARAMETERS_INCLUDE); + } List<String> invInclude = includeParams.stream() .filter(p -> !defaultParamNames.contains(p)) .collect(Collectors.toList()); - List<String> excludeParams = defParameters.containsKey(Constants.DEFAULT_PARAMETERS_EXCLUDE) ? - defParameters.get(Constants.DEFAULT_PARAMETERS_EXCLUDE) : new ArrayList<>(); + if (defParameters.containsKey(Constants.DEFAULT_PARAMETERS_EXCLUDE)) { + excludeParams = defParameters.get(Constants.DEFAULT_PARAMETERS_EXCLUDE); + } - List<String> invExclude = excludeParams.stream() - .filter(p -> !defaultParamNames.contains(p)) + List<String> invExclude = excludeParams.stream().filter(p -> !defaultParamNames.contains(p)) .collect(Collectors.toList()); + if (!invExclude.isEmpty() || !invInclude.isEmpty()) { throw new OnapCommandInvalidDefaultParameter(Stream.concat(invInclude.stream(), invExclude.stream()) .collect(Collectors.toList())); @@ -253,14 +295,14 @@ public class OnapCommandUtils { if (!includeParams.isEmpty()) { filteredDefaultParams.addAll(includeParams); } else if (!excludeParams.isEmpty()) { - defaultParamNames.stream().filter(p -> !excludeParams.contains(p)) + List<String> finalExcludeParams = excludeParams; + defaultParamNames.stream().filter(p -> !finalExcludeParams.contains(p)) .forEach(filteredDefaultParams::add); } } else { filteredDefaultParams.addAll(defaultParamNames); - } - + processNoAuth(filteredDefaultParams, cmd, includeParams, excludeParams); } else if (Constants.PARAMETERS.equals(key)) { List<Map<String, String>> parameters = (List) values.get(key); @@ -570,7 +612,7 @@ public class OnapCommandUtils { if (param.isDefaultValueAnEnv()) { optSecondCol += defaultMsg + "read from environment variable " + param.getEnvVarNameFromDefaultValue() + "."; - } else if (param.getDefaultValue() != null && !param.getDefaultValue().isEmpty()) { + } else if (param.getDefaultValue() != null && !((String)param.getDefaultValue()).isEmpty()) { optSecondCol += defaultMsg + param.getDefaultValue() + "."; } |