From a87dc9c351b0c08d20adc428c215cf3934bb79bb Mon Sep 17 00:00:00 2001 From: Ganesh Chandrasekaran Date: Thu, 14 Jun 2018 08:02:31 +0900 Subject: [PATCH] CCSDK plugin has authType param for DG to sel type Issue-ID: CCSDK-299 Change-Id: I61a56c873189ccc6ba9985c5121d50e2ad1f3f48 Signed-off-by: Ganesh Chandrasekaran --- .../ccsdk/sli/plugins/restapicall/AuthType.java | 19 +++ .../ccsdk/sli/plugins/restapicall/Parameters.java | 1 + .../sli/plugins/restapicall/RestapiCallNode.java | 92 +++++++++++--- .../plugins/restapicall/TestRestapiCallNode.java | 137 +++++++++++++++++++++ 4 files changed, 232 insertions(+), 17 deletions(-) create mode 100644 restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/AuthType.java diff --git a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/AuthType.java b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/AuthType.java new file mode 100644 index 00000000..851dc9cc --- /dev/null +++ b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/AuthType.java @@ -0,0 +1,19 @@ +package org.onap.ccsdk.sli.plugins.restapicall; + +public enum AuthType { + NONE, BASIC, DIGEST, OAUTH, Unspecified; + + public static AuthType fromString(String s) { + if ("basic".equalsIgnoreCase(s)) + return BASIC; + if ("digest".equalsIgnoreCase(s)) + return DIGEST; + if ("oauth".equalsIgnoreCase(s)) + return OAUTH; + if ("none".equalsIgnoreCase(s)) + return NONE; + if ("unspecified".equalsIgnoreCase(s)) + return Unspecified; + throw new IllegalArgumentException("Invalid value for format: " + s); + } +} diff --git a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/Parameters.java b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/Parameters.java index 3a4bc76c..c170cfc7 100644 --- a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/Parameters.java +++ b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/Parameters.java @@ -48,4 +48,5 @@ public class Parameters { public String oAuthConsumerSecret; public String oAuthSignatureMethod; public String oAuthVersion; + public AuthType authtype; } diff --git a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java index bd2fc826..d215d909 100644 --- a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java +++ b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java @@ -62,6 +62,7 @@ import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; +import com.sun.jersey.api.client.filter.HTTPDigestAuthFilter; import com.sun.jersey.oauth.client.OAuthClientFilter; import com.sun.jersey.oauth.signature.OAuthParameters; import com.sun.jersey.oauth.signature.OAuthSecrets; @@ -229,6 +230,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null); p.contentType = parseParam(paramMap, "contentType", false, null); p.format = Format.fromString(parseParam(paramMap, "format", false, "json")); + p.authtype = AuthType.fromString(parseParam(paramMap, "authType", false, "unspecified")); p.httpMethod = HttpMethod.fromString(parseParam(paramMap, "httpMethod", false, "post")); p.responsePrefix = parseParam(paramMap, "responsePrefix", false, null); p.listNameList = getListNameList(paramMap); @@ -431,6 +433,67 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { } } + protected Client addAuthType(Client c, FileParam fp) throws SvcLogicException { + Parameters p = new Parameters(); + p.restapiUser = fp.user; + p.restapiPassword = fp.password; + p.oAuthConsumerKey = fp.oAuthConsumerKey; + p.oAuthVersion = fp.oAuthVersion; + p.oAuthConsumerSecret = fp.oAuthConsumerSecret; + p.oAuthSignatureMethod = fp.oAuthSignatureMethod; + p.authtype = fp.authtype; + return addAuthType(c,p); + } + + protected Client addAuthType(Client client, Parameters p) throws SvcLogicException { + if (p.authtype == AuthType.Unspecified){ + if (p.restapiUser != null && p.restapiPassword != null) + client.addFilter(new HTTPBasicAuthFilter(p.restapiUser, p.restapiPassword)); + else if(p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null + && p.oAuthSignatureMethod != null) { + OAuthParameters params = new OAuthParameters() + .signatureMethod(p.oAuthSignatureMethod) + .consumerKey(p.oAuthConsumerKey) + .version(p.oAuthVersion); + + OAuthSecrets secrets = new OAuthSecrets() + .consumerSecret(p.oAuthConsumerSecret); + client.addFilter(new OAuthClientFilter(client.getProviders(), params, secrets)); + } + } else { + if (p.authtype == AuthType.DIGEST) { + if (p.restapiUser != null && p.restapiPassword != null) { + client.addFilter(new HTTPDigestAuthFilter(p.restapiUser, p.restapiPassword)); + } else { + throw new SvcLogicException("oAUTH authentication type selected but all restapiUser and restapiPassword " + + "parameters doesn't exist", new Throwable()); + } + } else if (p.authtype == AuthType.BASIC){ + if (p.restapiUser != null && p.restapiPassword != null) { + client.addFilter(new HTTPBasicAuthFilter(p.restapiUser, p.restapiPassword)); + } else { + throw new SvcLogicException("oAUTH authentication type selected but all restapiUser and restapiPassword " + + "parameters doesn't exist", new Throwable()); + } + } else if(p.authtype == AuthType.OAUTH ) { + if(p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null) { + OAuthParameters params = new OAuthParameters() + .signatureMethod(p.oAuthSignatureMethod) + .consumerKey(p.oAuthConsumerKey) + .version(p.oAuthVersion); + + OAuthSecrets secrets = new OAuthSecrets() + .consumerSecret(p.oAuthConsumerSecret); + client.addFilter(new OAuthClientFilter(client.getProviders(), params, secrets)); + } else { + throw new SvcLogicException("oAUTH authentication type selected but all oAuthConsumerKey, oAuthConsumerSecret " + + "and oAuthSignatureMethod parameters doesn't exist", new Throwable()); + } + } + } + return client; + } + protected HttpResponse sendHttpRequest(String request, Parameters p) throws SvcLogicException { ClientConfig config = new DefaultClientConfig(); @@ -448,20 +511,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { Client client = Client.create(config); client.setConnectTimeout(5000); - if (p.restapiUser != null && p.restapiPassword != null) - client.addFilter(new HTTPBasicAuthFilter(p.restapiUser, p.restapiPassword)); - else if(p.oAuthConsumerKey != null && p.oAuthConsumerSecret != null && p.oAuthSignatureMethod != null && p.oAuthVersion != null) - { - OAuthParameters params = new OAuthParameters() - .signatureMethod(p.oAuthSignatureMethod) - .consumerKey(p.oAuthConsumerKey) - .version(p.oAuthVersion); - - OAuthSecrets secrets = new OAuthSecrets() - .consumerSecret(p.oAuthConsumerSecret); - client.addFilter(new OAuthClientFilter(client.getProviders(), params, secrets)); - } - WebResource webResource = client.resource(p.restapiUrl); + WebResource webResource = addAuthType(client,p).resource(p.restapiUrl); log.info("Sending request:"); log.info(request); @@ -592,6 +642,11 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { public HttpMethod httpMethod; public String responsePrefix; public boolean skipSending; + public String oAuthConsumerKey; + public String oAuthConsumerSecret; + public String oAuthSignatureMethod; + public String oAuthVersion; + public AuthType authtype; } private FileParam getFileParameters(Map paramMap) throws SvcLogicException { @@ -604,6 +659,11 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { p.responsePrefix = parseParam(paramMap, "responsePrefix", false, null); String skipSendingStr = paramMap.get("skipSending"); p.skipSending = "true".equalsIgnoreCase(skipSendingStr); + p.oAuthConsumerKey = parseParam(paramMap, "oAuthConsumerKey", false, null); + p.oAuthVersion = parseParam(paramMap, "oAuthVersion", false, null); + p.oAuthConsumerSecret = parseParam(paramMap, "oAuthConsumerSecret", false, null); + p.oAuthSignatureMethod = parseParam(paramMap, "oAuthSignatureMethod", false, null); + p.authtype = AuthType.fromString(parseParam(paramMap, "authType", false, "unspecified")); return p; } @@ -611,9 +671,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { Client client = Client.create(); client.setConnectTimeout(5000); client.setFollowRedirects(true); - if (p.user != null) - client.addFilter(new HTTPBasicAuthFilter(p.user, p.password)); - WebResource webResource = client.resource(p.url); + WebResource webResource = addAuthType(client,p).resource(p.url); log.info("Sending file"); long t1 = System.currentTimeMillis(); diff --git a/restapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java b/restapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java index 88a1eeae..81806f6d 100644 --- a/restapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java +++ b/restapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java @@ -435,4 +435,141 @@ public class TestRestapiCallNode { RestapiCallNode rcn = new RestapiCallNode(); rcn.sendRequest(p, ctx); } + + @Test + public void testDeleteAuthTypeBasic() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("restapiUrl", "https://echo.getpostman.com/delete"); + p.put("authType", "basic"); + p.put("restapiUser", "admin"); + p.put("restapiPassword", "admin123"); + p.put("httpMethod", "delete"); + p.put("format", "none"); + p.put("skipSending", "true"); + + RestapiCallNode rcn = new RestapiCallNode(); + rcn.sendRequest(p, ctx); + } + + @Test + public void testDeleteAuthTypeDigest() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("restapiUrl", "https://echo.getpostman.com/delete"); + p.put("authType", "digest"); + p.put("restapiUser", "admin"); + p.put("restapiPassword", "admin123"); + p.put("httpMethod", "delete"); + p.put("format", "none"); + p.put("skipSending", "true"); + + RestapiCallNode rcn = new RestapiCallNode(); + rcn.sendRequest(p, ctx); + } + + @Test + public void testDeleteAuthTypeOAuth() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("restapiUrl", "https://echo.getpostman.com/delete"); + p.put("authType", "oauth"); + p.put("oAuthConsumerKey", "f2a1ed52710d4533bde25be6da03b6e3"); + p.put("oAuthConsumerSecret", "secret"); + p.put("oAuthSignatureMethod", "plainTEXT"); + p.put("oAuthVersion", "1.0"); + p.put("httpMethod", "delete"); + p.put("format", "none"); + p.put("skipSending", "true"); + + RestapiCallNode rcn = new RestapiCallNode(); + rcn.sendRequest(p, ctx); + } + + @Test + public void testDeleteAuthTypeNoneOAuth() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("restapiUrl", "https://echo.getpostman.com/delete"); + p.put("oAuthConsumerKey", "f2a1ed52710d4533bde25be6da03b6e3"); + p.put("oAuthConsumerSecret", "secret"); + p.put("oAuthSignatureMethod", "plainTEXT"); + p.put("oAuthVersion", "1.0"); + p.put("httpMethod", "delete"); + p.put("format", "none"); + p.put("skipSending", "true"); + + RestapiCallNode rcn = new RestapiCallNode(); + rcn.sendRequest(p, ctx); + } + @Test + public void testDeleteAuthTypeNoneBasic() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("restapiUrl", "https://echo.getpostman.com/delete"); + p.put("restapiUser", "admin"); + p.put("restapiPassword", "admin123"); + p.put("httpMethod", "delete"); + p.put("format", "none"); + p.put("skipSending", "true"); + + RestapiCallNode rcn = new RestapiCallNode(); + rcn.sendRequest(p, ctx); + } + + @Test(expected = SvcLogicException.class) + public void testInvalidDeleteAuthTypeOAuth() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("restapiUrl", "https://echo.getpostman.com/delete"); + p.put("authType", "oauth"); + p.put("oAuthConsumerKey", "f2a1ed52710d4533bde25be6da03b6e3"); + p.put("oAuthConsumerSecret", "secret"); + p.put("httpMethod", "delete"); + p.put("format", "none"); + p.put("skipSending", "true"); + + RestapiCallNode rcn = new RestapiCallNode(); + rcn.sendRequest(p, ctx); + } + + @Test(expected = SvcLogicException.class) + public void testInvalidDeleteAuthTypeBasic() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("restapiUrl", "https://echo.getpostman.com/delete"); + p.put("authType", "basic"); + p.put("oAuthConsumerKey", "f2a1ed52710d4533bde25be6da03b6e3"); + p.put("oAuthConsumerSecret", "secret"); + p.put("httpMethod", "delete"); + p.put("format", "none"); + p.put("skipSending", "true"); + + RestapiCallNode rcn = new RestapiCallNode(); + rcn.sendRequest(p, ctx); + } + + @Test(expected = SvcLogicException.class) + public void testInvalidDeleteAuthTypeDigest() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + + Map p = new HashMap(); + p.put("restapiUrl", "https://echo.getpostman.com/delete"); + p.put("authType", "digest"); + p.put("oAuthConsumerKey", "f2a1ed52710d4533bde25be6da03b6e3"); + p.put("oAuthConsumerSecret", "secret"); + p.put("httpMethod", "delete"); + p.put("format", "none"); + p.put("skipSending", "true"); + + RestapiCallNode rcn = new RestapiCallNode(); + rcn.sendRequest(p, ctx); + } } -- 2.16.6