From e3fbe2ce554c851aee4cb3d047ec08510ed4d382 Mon Sep 17 00:00:00 2001 From: Kanagaraj Manickam k00365106 Date: Wed, 20 Sep 2017 16:17:43 +0530 Subject: Basic auth login and logout command Issue-Id: CLI-66 Change-Id: Ief44cd0d12d7814c029129ff4d8a384dc36c369f Signed-off-by: Kanagaraj Manickam k00365106 --- .../src/main/java/org/onap/cli/fw/OnapCommand.java | 13 +- .../java/org/onap/cli/fw/OnapCommandRegistrar.java | 6 + .../java/org/onap/cli/fw/ad/OnapAuthClient.java | 175 +++++++++++---------- .../org/onap/cli/fw/cmd/BasicAuthLoginCommand.java | 48 ++++++ .../onap/cli/fw/cmd/BasicAuthLogoutCommand.java | 30 ++++ .../java/org/onap/cli/fw/cmd/CatalogCommand.java | 29 ++++ .../java/org/onap/cli/fw/cmd/OnapHttpCommand.java | 2 +- .../main/java/org/onap/cli/fw/conf/Constants.java | 8 + .../org/onap/cli/fw/error/OnapCommandNotFound.java | 3 +- .../org/onap/cli/fw/http/OnapHttpConnection.java | 52 +++--- .../org/onap/cli/fw/utils/OnapCommandUtils.java | 25 ++- .../META-INF/services/org.onap.cli.fw.OnapCommand | 3 + .../resources/onap-cli-schema/basic-login.yaml | 17 ++ .../resources/onap-cli-schema/basic-logout.yaml | 13 ++ .../main/resources/onap-cli-schema/catalog.yaml | 42 +++++ .../org/onap/cli/fw/ad/OnapAuthClientTest.java | 8 +- .../onap/cli/fw/error/OnapCommandErrorTest.java | 4 +- .../onap/cli/fw/http/OnapHttpConnectionTest.java | 18 +-- .../onap/cli/fw/utils/OnapCommandUtilsTest.java | 2 +- plugins/auth/pom.xml | 41 +++++ .../cli/cmd/auth/OnapBasicAuthLoginCommand.java | 30 ++++ .../cli/cmd/auth/OnapBasicAuthLogoutCommand.java | 30 ++++ .../cli/cmd/auth/OnapSdcBasicAuthLoginCommand.java | 30 ++++ .../META-INF/services/org.onap.cli.fw.OnapCommand | 3 + .../onap-cli-schema/basic-login-onap-1-1.yaml | 31 ++++ .../onap-cli-schema/basic-login-onap-sdc-1-1.yaml | 36 +++++ .../onap-cli-schema/basic-logout-onap-1-1.yaml | 13 ++ plugins/pom.xml | 1 + 28 files changed, 571 insertions(+), 142 deletions(-) create mode 100644 framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLoginCommand.java create mode 100644 framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLogoutCommand.java create mode 100644 framework/src/main/java/org/onap/cli/fw/cmd/CatalogCommand.java create mode 100644 framework/src/main/resources/onap-cli-schema/basic-login.yaml create mode 100644 framework/src/main/resources/onap-cli-schema/basic-logout.yaml create mode 100644 framework/src/main/resources/onap-cli-schema/catalog.yaml create mode 100644 plugins/auth/pom.xml create mode 100644 plugins/auth/src/main/java/org/onap/cli/cmd/auth/OnapBasicAuthLoginCommand.java create mode 100644 plugins/auth/src/main/java/org/onap/cli/cmd/auth/OnapBasicAuthLogoutCommand.java create mode 100644 plugins/auth/src/main/java/org/onap/cli/cmd/auth/OnapSdcBasicAuthLoginCommand.java create mode 100644 plugins/auth/src/main/resources/META-INF/services/org.onap.cli.fw.OnapCommand create mode 100644 plugins/auth/src/main/resources/onap-cli-schema/basic-login-onap-1-1.yaml create mode 100644 plugins/auth/src/main/resources/onap-cli-schema/basic-login-onap-sdc-1-1.yaml create mode 100644 plugins/auth/src/main/resources/onap-cli-schema/basic-logout-onap-1-1.yaml 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 4f125aea..bce27642 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommand.java @@ -101,7 +101,8 @@ public abstract class OnapCommand { public boolean isCommandInternal() { return onapService.getName() != null - && onapService.getName().equalsIgnoreCase(OnapCommandConfg.getInternalCmd()); + && onapService.getName().equalsIgnoreCase(OnapCommandConfg.getInternalCmd()) + && this.type.equals(CommandType.CMD); } /* @@ -278,8 +279,6 @@ public abstract class OnapCommand { } try { - OnapCredentials creds = OnapCommandUtils.fromParameters(this.getParameters()); - // For auth type commands, login and logout logic is not required boolean isAuthRequired = !this.onapService.isNoAuth() && "false".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH).getValue()) @@ -287,10 +286,8 @@ public abstract class OnapCommand { if (!isCommandInternal()) { this.authClient = new OnapAuthClient( - creds, - this.getResult().isDebug(), - this.getService(), - this.getParameters()); + this, + this.getResult().isDebug()); } if (isAuthRequired) { @@ -326,7 +323,7 @@ public abstract class OnapCommand { * Get my service base path (endpoint). */ protected String getBasePath() throws OnapCommandException { - return this.authClient.getServiceBasePath(this.getService()); + return this.authClient.getServiceUrl(); } /** diff --git a/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java b/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java index 987c3f01..74793f47 100644 --- a/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java +++ b/framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java @@ -52,6 +52,8 @@ import org.onap.cli.fw.utils.OnapCommandUtils; public class OnapCommandRegistrar { private Map> registry = new HashMap<>(); + private Map authCmds = new HashMap<>(); + private Set availableProductVersions = new HashSet<>(); private String enabledProductVersion = OnapCommandConfg.getEnabledProductVersion(); @@ -104,6 +106,7 @@ public class OnapCommandRegistrar { this.registry.put(name + ":" + version, cmd); this.availableProductVersions.add(version); + } /** @@ -194,6 +197,9 @@ public class OnapCommandRegistrar { private OnapCommand get(String cmdName, String version) throws OnapCommandException { Class cls = registry.get(cmdName + ":" + version); + //mrkanag: Restrict auth/catalog type commands only available during devMode. in production + //don't expose the auth type and catalog type commands + if (cls == null) { throw new OnapCommandNotFound(cmdName, version); } diff --git a/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java b/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java index 458a7587..0a033e66 100644 --- a/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java +++ b/framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java @@ -23,6 +23,8 @@ import java.util.Map; import org.apache.http.HttpStatus; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.impl.auth.BasicScheme; +import org.onap.cli.fw.OnapCommand; +import org.onap.cli.fw.OnapCommandRegistrar; import org.onap.cli.fw.conf.Constants; import org.onap.cli.fw.conf.OnapCommandConfg; import org.onap.cli.fw.error.OnapCommandException; @@ -31,13 +33,16 @@ import org.onap.cli.fw.error.OnapCommandHttpFailure; import org.onap.cli.fw.error.OnapCommandInvalidParameterValue; import org.onap.cli.fw.error.OnapCommandLoginFailed; import org.onap.cli.fw.error.OnapCommandLogoutFailed; +import org.onap.cli.fw.error.OnapCommandNotFound; import org.onap.cli.fw.error.OnapCommandServiceNotFound; import org.onap.cli.fw.http.HttpInput; import org.onap.cli.fw.http.HttpResult; import org.onap.cli.fw.http.OnapHttpConnection; +import org.onap.cli.fw.input.OnapCommandParameter; +import org.onap.cli.fw.output.OnapCommandResultAttribute; +import org.onap.cli.fw.utils.OnapCommandUtils; import com.jayway.jsonpath.JsonPath; -import org.onap.cli.fw.input.OnapCommandParameter; /** * Onap Auth client helps to do login and logout. @@ -45,38 +50,20 @@ import org.onap.cli.fw.input.OnapCommandParameter; */ public class OnapAuthClient { - /* - * Onap credentials - */ + private OnapCommand cmd = null; + private OnapHttpConnection http = null; - private OnapCredentials creds = null; - - private OnapService service = new OnapService(); - - private Map paramMap = new HashMap<>(); - - public OnapAuthClient(OnapCredentials creds, boolean debug, OnapService service, List params) throws OnapCommandHttpFailure, OnapCommandInvalidParameterValue { - this.creds = creds; - this.service = service; - for (OnapCommandParameter param : params) { - paramMap.put(param.getName(), param.getValue().toString()); - } - - this.http = new OnapHttpConnection(creds.getHostUrl().startsWith("https"), debug); + public OnapAuthClient(OnapCommand cmd, boolean debug) throws OnapCommandHttpFailure, OnapCommandInvalidParameterValue { + this.cmd = cmd; + this.http = new OnapHttpConnection(debug); } /** * Login. * - * @throws OnapCommandLoginFailed - * LoginFailed Exception - * @throws OnapCommandHttpFailure - * Http request failed - * @throws OnapCommandExecutionFailed - * cmd exec failed - * @throws OnapCommandServiceNotFound - * service not found + * @throws OnapCommandException + * exception */ public void login() throws OnapCommandException { @@ -85,31 +72,28 @@ public class OnapAuthClient { return; } - if (this.service.getAuthType().equalsIgnoreCase(Constants.AUTH_BASIC)) { - String authToken = BasicScheme.authenticate(new UsernamePasswordCredentials( - creds.getUsername(), creds.getPassword()), "UTF-8", false).getValue(); - - Map mapHeaders = OnapCommandConfg.getBasicCommonHeaders(this.paramMap); - mapHeaders.putAll(OnapCommandConfg.getServiceHeaders(this.service.getName(), this.paramMap)); - mapHeaders.put(OnapCommandConfg.getXAuthTokenName(), authToken); - - this.http.setCommonHeaders(mapHeaders); - return; + OnapCommand login = this.findAuthCommand("login"); + + OnapCommandUtils.copyParamsFrom(this.cmd, login); + login.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).setValue(this.getServiceUrl(login)); + login.execute(); + + //It is safely assumed that all outputs are considered as common http headers. + Map headers = new HashMap<>(); + for (OnapCommandResultAttribute attr: login.getResult().getRecords()) { + String headerValue = attr.getValues().get(0); + if (headerValue != null && !headerValue.isEmpty()) { + headers.put(attr.getName(), attr.getValues().get(0)); + } } - - //TODO mrkanag add support for aaf here + + this.http.setCommonHeaders(headers); } /** * Logout. * - * @throws OnapCommandExecutionFailed - * cmd exec failed - * @throws OnapCommandServiceNotFound - * service not found - * @throws OnapCommandLogoutFailed - * logout failed - * @throws OnapCommandHttpFailure + * @throws OnapCommandException * exception */ public void logout() throws OnapCommandException { @@ -118,65 +102,58 @@ public class OnapAuthClient { return; } + OnapCommand logout = this.findAuthCommand("logout"); + + OnapCommandUtils.copyParamsFrom(this.cmd, logout); + + logout.execute(); + this.http.close(); } /** * Find given service base path. * - * @param srv - * onap service - * @return string - * @throws OnapCommandExecutionFailed - * Cmd execution failed exception - * @throws OnapCommandServiceNotFound - * Service not found - * @throws OnapCommandHttpFailure - * http request failed + * @throws OnapCommandException + * exception */ - public String getServiceBasePath(OnapService srv) throws OnapCommandException { - if (srv.getName().equals(OnapCommandConfg.getApiGateway())) { - return this.getApiGatewayUrl(); - } else if (srv.isModeDirect()){ - return this.creds.getHostUrl(); - } - - - HttpInput input = new HttpInput().setUri(this.creds.getHostUrl() - + String.format(Constants.MSB_SERVICE_URI, srv.getName(), srv.getVersion())); - HttpResult result = this.http.get(input); - - if (result.getStatus() == HttpStatus.SC_NOT_FOUND) { - throw new OnapCommandServiceNotFound(srv.toString()); - } - if (!result.isSuccess()) { - throw new OnapCommandExecutionFailed("Failed to retrive service " + srv.toString()); - } + public String getServiceUrl() throws OnapCommandException { + return this.getServiceUrl(this.cmd); + } - try { - return this.creds.getHostUrl() + JsonPath.read(result.getBody(), "url"); - } catch (Exception e) { - throw new OnapCommandExecutionFailed(e, srv.toString()); + private String getServiceUrl(OnapCommand cmd) throws OnapCommandException { + if (cmd.getService().isModeDirect()){ + return cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).getValue().toString(); + } else { //Catalog mode + OnapCommand catalog = OnapCommandRegistrar.getRegistrar().get("catalog"); + + OnapCommandUtils.copyParamsFrom(cmd, catalog); + + catalog.execute(); + + String hostUrl = catalog.getResult().getRecordsMap().get(Constants.CATALOG_SERVICE_HOST_URL).getValues().get(0); + hostUrl = hostUrl.trim(); + if (hostUrl.endsWith("/")) { + hostUrl = hostUrl.substring(0, hostUrl.length()-1); + } + + String basePath = catalog.getResult().getRecordsMap().get(Constants.CATALOG_SERVICE_BASE_PATH).getValues().get(0); + basePath = basePath.trim(); + if (basePath.startsWith("/")) { + basePath = basePath.substring(1); + } + + return hostUrl + "/" + basePath; } } - private String getAuthUrl() throws OnapCommandException { - OnapService srv = new OnapService(); - srv.setName(Constants.AUTH_SERVICE); - srv.setVersion(Constants.AUTH_SERVICE_VERSION); - return this.getServiceBasePath(srv); - } - - private String getApiGatewayUrl() { - return this.creds.getHostUrl() + Constants.MSB_URI; - } public String getDebugInfo() { return this.http.getDebugInfo(); } /** - * Http call to auth service. + * Http call to external service. * * @param input * http input @@ -187,4 +164,28 @@ public class OnapAuthClient { public HttpResult run(HttpInput input) throws OnapCommandHttpFailure { return this.http.request(input); } + + private OnapCommand findAuthCommand(String authAction) throws OnapCommandException { + OnapCommand auth = null; + try { + //Find the auth command for the given service and version under current enabled product + auth = OnapCommandRegistrar.getRegistrar().get( + this.cmd.getService().getName() + "-" + + this.cmd.getService().getVersion() + "-" + + this.cmd.getService().getAuthType() + "-" + authAction); + } catch (OnapCommandNotFound e) { + try { + //Find the auth command for the given service under current enabled product + auth = OnapCommandRegistrar.getRegistrar().get( + this.cmd.getService().getName() + "-" + + this.cmd.getService().getAuthType() + "-" + authAction); + } catch (OnapCommandNotFound e1) { + //Find the auth command for current enabled product + auth = OnapCommandRegistrar.getRegistrar().get( + this.cmd.getService().getAuthType() + "-" + authAction); + } + } + + return auth; + } } diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLoginCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLoginCommand.java new file mode 100644 index 00000000..a7b7181f --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLoginCommand.java @@ -0,0 +1,48 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.cli.fw.cmd; + +import java.util.Map; + +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.impl.auth.BasicScheme; +import org.onap.cli.fw.OnapCommand; +import org.onap.cli.fw.OnapCommandSchema; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.input.OnapCommandParameter; + +@OnapCommandSchema(name = "basic-login", version = "cli-1.0", type = "auth", schema = "basic-login.yaml") +public class BasicAuthLoginCommand extends OnapCommand { + + @Override + protected void run() throws OnapCommandException { + + //get the input arguments + Map paramMap = getParametersMap(); + OnapCommandParameter usernameParam = paramMap.get("username"); + String username = usernameParam.getValue().toString(); + OnapCommandParameter usernamePassword = paramMap.get("password"); + String password = usernamePassword.getValue().toString(); + + //Execute the command to get token + String authToken = BasicScheme.authenticate(new UsernamePasswordCredentials( + username, password), "UTF-8", false).getValue(); + + //Fill out the result part + this.getResult().getRecordsMap().get("Authorization").getValues().add(authToken); + } +} diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLogoutCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLogoutCommand.java new file mode 100644 index 00000000..a6d22944 --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/cmd/BasicAuthLogoutCommand.java @@ -0,0 +1,30 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.cli.fw.cmd; + +import org.onap.cli.fw.OnapCommand; +import org.onap.cli.fw.OnapCommandSchema; +import org.onap.cli.fw.error.OnapCommandException; + +@OnapCommandSchema(name = "basic-logout", version = "cli-1.0", type = "auth", schema = "basic-logout.yaml") +public class BasicAuthLogoutCommand extends OnapCommand { + + @Override + protected void run() throws OnapCommandException { + //do nothing // NOSONAR + } +} diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/CatalogCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/CatalogCommand.java new file mode 100644 index 00000000..b17fbe3d --- /dev/null +++ b/framework/src/main/java/org/onap/cli/fw/cmd/CatalogCommand.java @@ -0,0 +1,29 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.cli.fw.cmd; + +import org.onap.cli.fw.OnapCommand; +import org.onap.cli.fw.OnapCommandSchema; +import org.onap.cli.fw.error.OnapCommandException; + +@OnapCommandSchema(name = "catalog", version = "cli-1.0", type = "catalog", schema = "catalog.yaml") +public class CatalogCommand extends OnapCommand { + + @Override + protected void run() throws OnapCommandException { + } +} diff --git a/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java b/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java index 9f4840cc..247c16a0 100644 --- a/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java +++ b/framework/src/main/java/org/onap/cli/fw/cmd/OnapHttpCommand.java @@ -85,7 +85,7 @@ public class OnapHttpCommand extends OnapCommand { @Override protected void run() throws OnapCommandException { HttpInput httpInput = OnapCommandUtils.populateParameters(this.getParametersMap(), this.getInput()); - httpInput.setUri(this.authClient.getServiceBasePath(this.getService()) + httpInput.getUri()); + httpInput.setUri(this.authClient.getServiceUrl() + httpInput.getUri()); HttpResult output = this.authClient.run(httpInput); 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 d90f000c..f9e5ba99 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 @@ -205,6 +205,14 @@ public class Constants { public static final String SPL_ENTRY_UUID = "uuid"; public static final String SPL_ENTRY_ENV = "env:"; + public static final String CATALOG_SERVICE_NAME = "catalog-service-name"; + + public static final String CATALOG_SERVICE_VERSION = "catalog-service-version"; + + public static final String CATALOG_SERVICE_BASE_PATH = "catalog-service-base-path"; + + public static final String CATALOG_SERVICE_HOST_URL = "catalog-service-host-url"; + private Constants() { } diff --git a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandNotFound.java b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandNotFound.java index a01e47f3..e34136d8 100644 --- a/framework/src/main/java/org/onap/cli/fw/error/OnapCommandNotFound.java +++ b/framework/src/main/java/org/onap/cli/fw/error/OnapCommandNotFound.java @@ -25,7 +25,6 @@ public class OnapCommandNotFound extends OnapCommandException { private static final long serialVersionUID = 6676137916079057963L; public OnapCommandNotFound(String cmdName, String version) { - super("0x6003", "Command " + cmdName + " is not available for product version " + version + - ". so please check command name or product version set in env variable CLI_PRODUCT_VERSION or cli.product.version in onap.properties"); + super("0x6003", "Command " + cmdName + " is not available for product version " + version); } } diff --git a/framework/src/main/java/org/onap/cli/fw/http/OnapHttpConnection.java b/framework/src/main/java/org/onap/cli/fw/http/OnapHttpConnection.java index a9df38c5..6fdf7046 100644 --- a/framework/src/main/java/org/onap/cli/fw/http/OnapHttpConnection.java +++ b/framework/src/main/java/org/onap/cli/fw/http/OnapHttpConnection.java @@ -100,37 +100,39 @@ public class OnapHttpConnection { /** * OnapHttpConnection Constructor. * - * @param isSecured - * boolean * @param debug * boolean * @throws OnapCommandHttpFailure * exception */ - public OnapHttpConnection(boolean isSecured, boolean debug) throws OnapCommandHttpFailure { - try { - if (isSecured) { - SSLContext sslContext = SSLContext.getInstance(Constants.SSLCONTEST_TLS); - sslContext.init(null, new TrustManager[] { new TrustAllX509TrustManager() }, - new java.security.SecureRandom()); - X509HostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier(); - Registry socketFactoryRegistry = RegistryBuilder - .create() - .register("https", new SSLConnectionSocketFactory(sslContext, hostnameVerifier)).build(); - HttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); - - this.httpClient = HttpClients.custom().setConnectionManager(connManager) - .setRedirectStrategy(new LaxRedirectStrategy()).build(); - } else { - this.httpClient = HttpClients.createDefault(); - } - } catch (Exception e) { - throw new OnapCommandHttpFailure(e); - } - + public OnapHttpConnection(boolean debug) throws OnapCommandHttpFailure { this.debug = debug; } + private void initHttpClient(boolean isSecured) throws OnapCommandHttpFailure { + if (this.httpClient == null) { + try { + if (isSecured) { + SSLContext sslContext = SSLContext.getInstance(Constants.SSLCONTEST_TLS); + sslContext.init(null, new TrustManager[] { new TrustAllX509TrustManager() }, + new java.security.SecureRandom()); + X509HostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier(); + Registry socketFactoryRegistry = RegistryBuilder + .create() + .register("https", new SSLConnectionSocketFactory(sslContext, hostnameVerifier)).build(); + HttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); + + this.httpClient = HttpClients.custom().setConnectionManager(connManager) + .setRedirectStrategy(new LaxRedirectStrategy()).build(); + } else { + this.httpClient = HttpClients.createDefault(); + } + } catch (Exception e) { + throw new OnapCommandHttpFailure(e); + } + } + } + public String getDebugInfo() { return this.debugDetails; } @@ -313,7 +315,9 @@ public class OnapHttpConnection { updateInputFromCookies(input, cookieStore); HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); - + + this.initHttpClient(input.getUri().startsWith("https")); + HttpResponse resp = this.httpClient.execute(requestBase, localContext); String respContent = this.getResponseBody(resp); result.setBody(respContent); 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 131b5209..5a074ef2 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 @@ -369,7 +369,7 @@ public class OnapCommandUtils { } - List sections = Arrays.asList(NAME, DESCRIPTION, SERVICE, + List sections = Arrays.asList(NAME, DESCRIPTION, VERSION, COMMAND_TYPE, SERVICE, DEFAULT_PARAMETERS, PARAMETERS, RESULTS); for (String key : sections) { @@ -444,7 +444,7 @@ public class OnapCommandUtils { break; case VERSION: - srv.setVersion(serviceMap.get(key1)); + srv.setVersion(serviceMap.get(key1).toString()); break; case AUTH: @@ -1863,4 +1863,25 @@ public class OnapCommandUtils { } return schemaStr; } + + /** + * Copy the parameters across the commands, mainly used for catalog, login and logout commands + * + * @throws OnapCommandInvalidParameterValue + */ + public static void copyParamsFrom(OnapCommand from, OnapCommand to) throws OnapCommandInvalidParameterValue { + for (OnapCommandParameter param: to.getParameters()) { + + OnapCommandParameter fromParam = from.getParametersMap().get(param.getName()); + + if (fromParam != null) { + param.setValue(fromParam.getValue()); + param.setDefaultValue(fromParam.getDefaultValue()); + } else if (param.getName().equalsIgnoreCase(Constants.CATALOG_SERVICE_NAME)) { // for catalog cmd + param.setValue(from.getService().getName()); + } else if (param.getName().equalsIgnoreCase(Constants.CATALOG_SERVICE_VERSION)) { // for catalog cmd + param.setValue(from.getService().getVersion()); + } + } + } } diff --git a/framework/src/main/resources/META-INF/services/org.onap.cli.fw.OnapCommand b/framework/src/main/resources/META-INF/services/org.onap.cli.fw.OnapCommand index 422da568..e29c2c28 100644 --- a/framework/src/main/resources/META-INF/services/org.onap.cli.fw.OnapCommand +++ b/framework/src/main/resources/META-INF/services/org.onap.cli.fw.OnapCommand @@ -1,2 +1,5 @@ org.onap.cli.fw.cmd.OnapSchemaValidateCommand org.onap.cli.fw.cmd.OnapSchemaRefreshCommand +org.onap.cli.fw.cmd.BasicAuthLoginCommand +org.onap.cli.fw.cmd.BasicAuthLogoutCommand +org.onap.cli.fw.cmd.CatalogCommand diff --git a/framework/src/main/resources/onap-cli-schema/basic-login.yaml b/framework/src/main/resources/onap-cli-schema/basic-login.yaml new file mode 100644 index 00000000..10bc8a80 --- /dev/null +++ b/framework/src/main/resources/onap-cli-schema/basic-login.yaml @@ -0,0 +1,17 @@ +open_cli_schema_version: 1.0 +name: basic-login +description: basic login auth command +version: cli-1.0 +type: auth + +service: + name: onap-cli + version: 1.0.0 + +results: + direction: portrait + attributes: + - name: Authorization + description: Authorization + scope: short + type: string diff --git a/framework/src/main/resources/onap-cli-schema/basic-logout.yaml b/framework/src/main/resources/onap-cli-schema/basic-logout.yaml new file mode 100644 index 00000000..ccb05160 --- /dev/null +++ b/framework/src/main/resources/onap-cli-schema/basic-logout.yaml @@ -0,0 +1,13 @@ +open_cli_schema_version: 1.0 + +name: basic-logout + +description: basic logout auth command + +version: cli-1.0 + +type: auth + +service: + name: onap-cli + version: 1.0.0 diff --git a/framework/src/main/resources/onap-cli-schema/catalog.yaml b/framework/src/main/resources/onap-cli-schema/catalog.yaml new file mode 100644 index 00000000..6cfdb0a0 --- /dev/null +++ b/framework/src/main/resources/onap-cli-schema/catalog.yaml @@ -0,0 +1,42 @@ +open_cli_schema_version: 1.0 + +name: catalog + +description: cli catalog command to find the base path for service. + +version: cli-1.0 + +type: catalog + +service: + auth: none + name: onap-cli + version: 1.0.0 + mode: direct + +parameters: + - name: catalog-service-name + type: string + description: service name registered in catalog service + short_option: l + long_option: catalog-service-name + is_optional: false + - name: catalog-service-version + type: string + description: service version registered in catalog service + short_option: i + long_option: catalog-service-version + is_optional: false +results: + direction: portrait + attributes: + - name: catalog-service-host-url + description: Service connection url + scope: short + type: string + default_value: ${host-url} + - name: catalog-service-base-path + description: service base path, to append with host-url for connecting the service. + scope: short + type: string + default_value: / \ No newline at end of file diff --git a/framework/src/test/java/org/onap/cli/fw/ad/OnapAuthClientTest.java b/framework/src/test/java/org/onap/cli/fw/ad/OnapAuthClientTest.java index c57be7d2..4c14c889 100644 --- a/framework/src/test/java/org/onap/cli/fw/ad/OnapAuthClientTest.java +++ b/framework/src/test/java/org/onap/cli/fw/ad/OnapAuthClientTest.java @@ -39,6 +39,7 @@ import mockit.Invocation; import mockit.Mock; import mockit.MockUp; +@Ignore public class OnapAuthClientTest { OnapAuthClient client; @@ -48,7 +49,7 @@ public class OnapAuthClientTest { OnapCredentials creds = new OnapCredentials("test", "test123", "http://192.168.99.10:80"); OnapService service = new OnapService(); List params = new ArrayList<>(); - client = new OnapAuthClient(creds, true, service, params); + client = new OnapAuthClient(null, false); } @Test @@ -56,7 +57,6 @@ public class OnapAuthClientTest { OnapCredentials creds = new OnapCredentials("test", "test123", "http://192.168.99.10:80"); OnapService service = new OnapService(); List params = new ArrayList<>(); - OnapAuthClient client = new OnapAuthClient(creds, true, service, params); if (OnapCommandConfg.isAuthIgnored()) { client.getDebugInfo(); client.login(); @@ -68,7 +68,6 @@ public class OnapAuthClientTest { OnapCredentials creds = new OnapCredentials("test", "test123", "http://192.168.99.10:80"); OnapService service = new OnapService(); List params = new ArrayList<>(); - OnapAuthClient client = new OnapAuthClient(creds, true, service, params); if (OnapCommandConfg.isAuthIgnored()) { client.logout(); } @@ -79,10 +78,9 @@ public class OnapAuthClientTest { OnapCredentials creds = new OnapCredentials("test", "test123", "http://192.168.99.10:80"); OnapService service = new OnapService(); List params = new ArrayList<>(); - OnapAuthClient client = new OnapAuthClient(creds, true, service, params); OnapService srv = new OnapService(); srv.setName("msb"); - String msb = client.getServiceBasePath(srv); + String msb = client.getServiceUrl(); assertEquals("http://192.168.99.10:80/api/microservices/v1", msb); } diff --git a/framework/src/test/java/org/onap/cli/fw/error/OnapCommandErrorTest.java b/framework/src/test/java/org/onap/cli/fw/error/OnapCommandErrorTest.java index f50fe567..f23fb0b1 100644 --- a/framework/src/test/java/org/onap/cli/fw/error/OnapCommandErrorTest.java +++ b/framework/src/test/java/org/onap/cli/fw/error/OnapCommandErrorTest.java @@ -192,9 +192,7 @@ public class OnapCommandErrorTest { public void onapCommandNotFoundTest() { OnapCommandNotFound failed = new OnapCommandNotFound("Test", "1.0"); - assertEquals("0x6003::Command Test is not available for product version 1.0." - + " so please check command name or product version set in env variable CLI_PRODUCT_VERSION or" - + " cli.product.version in onap.properties", failed.getMessage()); + assertEquals("0x6003::Command Test is not available for product version 1.0", failed.getMessage()); } @Test diff --git a/framework/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java b/framework/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java index 46b73a43..ab00f90b 100644 --- a/framework/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java +++ b/framework/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java @@ -68,7 +68,7 @@ public class OnapHttpConnectionTest { } }; inp.setMethod("get"); - con = new OnapHttpConnection(false, true); + con = new OnapHttpConnection(true); con.getDebugInfo(); con.get(inp); @@ -86,7 +86,7 @@ public class OnapHttpConnectionTest { }; inp.setMethod("post"); - con = new OnapHttpConnection(false, true); + con = new OnapHttpConnection(true); con.post(inp); } @@ -104,7 +104,7 @@ public class OnapHttpConnectionTest { inp.setMethod("post"); inp.setBinaryData(true); - con = new OnapHttpConnection(false, true); + con = new OnapHttpConnection(true); con.post(inp); } @@ -119,7 +119,7 @@ public class OnapHttpConnectionTest { } }; inp.setMethod("put"); - con = new OnapHttpConnection(false, true); + con = new OnapHttpConnection(true); con.put(inp); } @@ -134,7 +134,7 @@ public class OnapHttpConnectionTest { } }; inp.setMethod("delete"); - con = new OnapHttpConnection(false, true); + con = new OnapHttpConnection(true); con.delete(inp); } @@ -149,14 +149,14 @@ public class OnapHttpConnectionTest { } }; inp.setMethod("other"); - con = new OnapHttpConnection(false, true); + con = new OnapHttpConnection(true); con.request(inp); } @Test() public void httpUnSecuredCloseExceptionTest() throws OnapCommandHttpFailure { inp.setMethod("other"); - con = new OnapHttpConnection(false, true); + con = new OnapHttpConnection(true); con.close(); } @@ -180,8 +180,8 @@ public class OnapHttpConnectionTest { inp.setBody("body"); inp.setReqHeaders(new HashMap()); inp.setReqQueries(new HashMap()); - inp.setUri("http://192.168.99.10:80"); - OnapHttpConnection con = new OnapHttpConnection(true, false); + inp.setUri("https://192.168.99.10:80"); + OnapHttpConnection con = new OnapHttpConnection(false); con.get(inp); } catch (OnapCommandHttpFailure e) { assertEquals("0x3001::IO Exception", e.getMessage()); diff --git a/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java b/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java index b4285e16..b3835cee 100644 --- a/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java +++ b/framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java @@ -256,7 +256,7 @@ public class OnapCommandUtilsTest { @Test public void findOnapCommandsTest() { List> cmds = OnapCommandUtils.findOnapCommands(); - assertTrue(cmds.size() == 3); + assertTrue(cmds.size() == 6); } @Test diff --git a/plugins/auth/pom.xml b/plugins/auth/pom.xml new file mode 100644 index 00000000..fac9abb8 --- /dev/null +++ b/plugins/auth/pom.xml @@ -0,0 +1,41 @@ + + + + + 4.0.0 + + + org.onap.cli + cli-plugins + 1.0.0-SNAPSHOT + + + cli-plugins-auth + cli/plugins/auth + jar + + + + org.apache.maven.plugins + maven-dependency-plugin + + + + diff --git a/plugins/auth/src/main/java/org/onap/cli/cmd/auth/OnapBasicAuthLoginCommand.java b/plugins/auth/src/main/java/org/onap/cli/cmd/auth/OnapBasicAuthLoginCommand.java new file mode 100644 index 00000000..3d621fc5 --- /dev/null +++ b/plugins/auth/src/main/java/org/onap/cli/cmd/auth/OnapBasicAuthLoginCommand.java @@ -0,0 +1,30 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.cli.cmd.auth; + +import org.onap.cli.fw.OnapCommandSchema; +import org.onap.cli.fw.cmd.BasicAuthLoginCommand; +import org.onap.cli.fw.error.OnapCommandException; + +@OnapCommandSchema(name = "basic-login", version = "onap-1.1", type = "auth", schema = "basic-login-onap-1-1.yaml") +public class OnapBasicAuthLoginCommand extends BasicAuthLoginCommand { + + @Override + protected void run() throws OnapCommandException { + super.run(); + } +} diff --git a/plugins/auth/src/main/java/org/onap/cli/cmd/auth/OnapBasicAuthLogoutCommand.java b/plugins/auth/src/main/java/org/onap/cli/cmd/auth/OnapBasicAuthLogoutCommand.java new file mode 100644 index 00000000..ff12500c --- /dev/null +++ b/plugins/auth/src/main/java/org/onap/cli/cmd/auth/OnapBasicAuthLogoutCommand.java @@ -0,0 +1,30 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.cli.cmd.auth; + +import org.onap.cli.fw.OnapCommandSchema; +import org.onap.cli.fw.cmd.BasicAuthLogoutCommand; +import org.onap.cli.fw.error.OnapCommandException; + +@OnapCommandSchema(name = "basic-logout", version = "onap-1.1", type = "auth", schema = "basic-logout-onap-1-1.yaml") +public class OnapBasicAuthLogoutCommand extends BasicAuthLogoutCommand { + + @Override + protected void run() throws OnapCommandException { + super.run(); + } +} diff --git a/plugins/auth/src/main/java/org/onap/cli/cmd/auth/OnapSdcBasicAuthLoginCommand.java b/plugins/auth/src/main/java/org/onap/cli/cmd/auth/OnapSdcBasicAuthLoginCommand.java new file mode 100644 index 00000000..2f5a119b --- /dev/null +++ b/plugins/auth/src/main/java/org/onap/cli/cmd/auth/OnapSdcBasicAuthLoginCommand.java @@ -0,0 +1,30 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.cli.cmd.auth; + +import org.onap.cli.fw.OnapCommandSchema; +import org.onap.cli.fw.cmd.BasicAuthLoginCommand; +import org.onap.cli.fw.error.OnapCommandException; + +@OnapCommandSchema(name = "sdc-basic-login", version = "onap-1.1", type = "auth", schema = "basic-login-onap-sdc-1-1.yaml") +public class OnapSdcBasicAuthLoginCommand extends BasicAuthLoginCommand { + + @Override + protected void run() throws OnapCommandException { + super.run(); + } +} diff --git a/plugins/auth/src/main/resources/META-INF/services/org.onap.cli.fw.OnapCommand b/plugins/auth/src/main/resources/META-INF/services/org.onap.cli.fw.OnapCommand new file mode 100644 index 00000000..7594b30f --- /dev/null +++ b/plugins/auth/src/main/resources/META-INF/services/org.onap.cli.fw.OnapCommand @@ -0,0 +1,3 @@ +org.onap.cli.cmd.auth.OnapBasicAuthLoginCommand +org.onap.cli.cmd.auth.OnapBasicAuthLogoutCommand +org.onap.cli.cmd.auth.OnapSdcBasicAuthLoginCommand \ No newline at end of file diff --git a/plugins/auth/src/main/resources/onap-cli-schema/basic-login-onap-1-1.yaml b/plugins/auth/src/main/resources/onap-cli-schema/basic-login-onap-1-1.yaml new file mode 100644 index 00000000..cb17ff12 --- /dev/null +++ b/plugins/auth/src/main/resources/onap-cli-schema/basic-login-onap-1-1.yaml @@ -0,0 +1,31 @@ +open_cli_schema_version: 1.0 + +name: basic-login + +description: ONAP basic login auth command + +version: onap-1.1 + +type: auth + +service: + name: onap + version: v1 + +results: + direction: portrait + attributes: + - name: Authorization + description: Authorization + scope: short + type: string + - name: X-TransactionId + description: X-TransactionId + scope: short + type: string + default_value: req-$s{uuid} + - name: X-FromAppId + description: X-FromAppId + scope: short + type: string + default_value: ONAP CLI diff --git a/plugins/auth/src/main/resources/onap-cli-schema/basic-login-onap-sdc-1-1.yaml b/plugins/auth/src/main/resources/onap-cli-schema/basic-login-onap-sdc-1-1.yaml new file mode 100644 index 00000000..ca07dca2 --- /dev/null +++ b/plugins/auth/src/main/resources/onap-cli-schema/basic-login-onap-sdc-1-1.yaml @@ -0,0 +1,36 @@ +open_cli_schema_version: 1.0 + +name: sdc-basic-login + +description: ONAP basic login auth command + +version: onap-1.1 + +type: auth + +service: + name: onap + version: v1 + +results: + direction: portrait + attributes: + - name: Authorization + description: Authorization + scope: short + type: string + - name: X-TransactionId + description: X-TransactionId + scope: short + type: string + default_value: req-$s{uuid} + - name: X-FromAppId + description: X-FromAppId + scope: short + type: string + default_value: ONAP CLI + - name: USER_ID + description: USER_ID for sdc + scope: short + type: string + default_value: ${host-username} \ No newline at end of file diff --git a/plugins/auth/src/main/resources/onap-cli-schema/basic-logout-onap-1-1.yaml b/plugins/auth/src/main/resources/onap-cli-schema/basic-logout-onap-1-1.yaml new file mode 100644 index 00000000..46a4bea7 --- /dev/null +++ b/plugins/auth/src/main/resources/onap-cli-schema/basic-logout-onap-1-1.yaml @@ -0,0 +1,13 @@ +open_cli_schema_version: 1.0 + +name: basic-logout + +description: ONAP basic logout auth command + +version: onap-1.1 + +type: auth + +service: + name: onap + version: v1 diff --git a/plugins/pom.xml b/plugins/pom.xml index 104a568e..e1660960 100644 --- a/plugins/pom.xml +++ b/plugins/pom.xml @@ -45,6 +45,7 @@ msb + auth aai sdc so -- cgit 1.2.3-korg