CCSDK plugin has authType param for DG to sel type 99/54699/1
authorGanesh Chandrasekaran <ganesh.c@samsung.com>
Wed, 13 Jun 2018 23:02:31 +0000 (08:02 +0900)
committerGanesh Chandrasekaran <ganesh.c@samsung.com>
Wed, 13 Jun 2018 23:07:33 +0000 (08:07 +0900)
Issue-ID: CCSDK-299
Change-Id: I61a56c873189ccc6ba9985c5121d50e2ad1f3f48
Signed-off-by: Ganesh Chandrasekaran <ganesh.c@samsung.com>
restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/AuthType.java [new file with mode: 0644]
restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/Parameters.java
restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java
restapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.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 (file)
index 0000000..851dc9c
--- /dev/null
@@ -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);
+    }
+}
index bd2fc82..d215d90 100644 (file)
@@ -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<String, String> 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();
index 88a1eea..81806f6 100644 (file)
@@ -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<String, String> p = new HashMap<String, String>();
+        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<String, String> p = new HashMap<String, String>();
+        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<String, String> p = new HashMap<String, String>();
+        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<String, String> p = new HashMap<String, String>();
+        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<String, String> p = new HashMap<String, String>();
+        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<String, String> p = new HashMap<String, String>();
+        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<String, String> p = new HashMap<String, String>();
+        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<String, String> p = new HashMap<String, String>();
+        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);
+    }
 }