You are viewing a plain text version of this content. The canonical link for it is here.
Posted to codereview@trafodion.apache.org by rucore <gi...@git.apache.org> on 2017/01/11 09:04:07 UTC
[GitHub] incubator-trafodion pull request #911: add oauth2 ldap for restful api
GitHub user rucore opened a pull request:
https://github.com/apache/incubator-trafodion/pull/911
add oauth2 ldap for restful api
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/rucore/incubator-trafodion release2.0
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/incubator-trafodion/pull/911.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #911
----
commit b464988d1a0e13d3bc4733cb1906be2828a4d184
Author: rucore <li...@mac.local>
Date: 2017-01-11T06:57:34Z
add oauth2 ldap for restful api
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-trafodion pull request #911: add oauth2 ldap for restful api
Posted by svarnau <gi...@git.apache.org>.
Github user svarnau commented on a diff in the pull request:
https://github.com/apache/incubator-trafodion/pull/911#discussion_r95625541
--- Diff: core/rest/src/main/java/org/trafodion/rest/util/Token.java ---
@@ -0,0 +1,194 @@
+package org.trafodion.rest.util;
+
+import java.util.Date;
+
+import java.security.Key;
+
+import javax.crypto.Cipher;
+
+public class Token {
+
+ public static String error_invalid_request = "invalid_request";
+ public static String error_invalid_client = "invalid_client";
+ public static String error_invalid_grant = "invalid_grant";
+ public static String error_unauthorized_client = "unauthorized_client";
+ public static String error_unsupported_grant_type = "unsupported_grant_type";
+ public static String error_invalid_scope = "invalid_scope";
+
+ public static String error = "error";
+ public static String error_description = "error_description";
+ public static String error_uri = "error_uri";
+
+ public static String access_token = "access_token";
+ public static String token_type = "token_type";
+ public static String expires_in = "expires_in";
+
+ public static String error_invalid_token = "error_invalid_token";
+
+ private String passwd = "fuck-trafodion!";
+ private String token = "";
+ private String interval = "7200000";
+
+ public String getInterval() {
+ return interval;
+ }
+
+ public void setInterval(String interval) {
+ this.interval = interval;
+ }
+
+ public String getPasswd() {
+ return passwd;
+ }
+
+ public void setPasswd(String passwd) {
+ this.passwd = passwd;
+ }
+
+ public String getToken() {
+ this.token = encode();
+ return this.token;
+ }
+
+ public void setToken(String token) {
+ this.token = token;
+ }
+
+ public boolean isTimeOut() {
+ if (token == null)
+ return true;
+ long timeout = Long.parseLong(getTimeOut(token));
+ Date date = new Date();
+ long timenow = date.getTime();
+ return (timenow > timeout);
+ }
+
+ private String deconde(String t) {
+
+ byte[] decryptFrom = parseHexStr2Byte(t);
+
+ if (decryptFrom == null)
+ return null;
+ byte[] decryptResult = decrypt(decryptFrom, passwd);
+
+ return new String(decryptResult);
+ }
+
+ private String encode() {
+
+ Date date = new Date();
+ long timeout = date.getTime() + Long.parseLong(interval);
+
+ String content = String.valueOf(timeout) + "=" + "continue";
+
+ byte[] encryptResult = encrypt(content, passwd);
+ String encryptResultStr = parseByte2HexStr(encryptResult);
+ return encryptResultStr;
+ }
+
+ private String getTimeOut(String t) {
+ String d = deconde(t);
+ if (d == null)
+ return "0";
+ return d.split("=")[0];
+ }
+
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ Token t = new Token();
+ // t.setPasswd("12345678");
+ String content = "test";
+ // String password = "12345678";
+
+ System.out.println("before:" + content);
+ byte[] encryptResult = t.encrypt(content, "fuck-trafodion!");
--- End diff --
That is not appropriate language, even in test code.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-trafodion pull request #911: add oauth2 ldap for restful api
Posted by venkat1m <gi...@git.apache.org>.
Github user venkat1m commented on a diff in the pull request:
https://github.com/apache/incubator-trafodion/pull/911#discussion_r95642534
--- Diff: core/rest/src/main/java/org/trafodion/rest/TransactionsResource.java ---
@@ -57,176 +57,197 @@
import org.trafodion.rest.zookeeper.ZkClient;
import org.trafodion.rest.RestConstants;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.core.HttpHeaders;
+
public class TransactionsResource extends ResourceBase {
- private static final Log LOG =
- LogFactory.getLog(TransactionsResource.class);
-
- static CacheControl cacheControl;
- static {
- cacheControl = new CacheControl();
- cacheControl.setNoCache(true);
- cacheControl.setNoTransform(false);
- }
-
-
- public TransactionsResource() throws IOException {
- super();
- }
-
- private String stats() throws IOException {
- ScriptContext scriptContext = new ScriptContext();
- scriptContext.setScriptName(Constants.SYS_SHELL_SCRIPT_NAME);
- scriptContext.setCommand("dtmci stats -j");
-
- try {
- ScriptManager.getInstance().runScript(scriptContext);//This will block while script is running
- } catch (Exception e) {
- e.printStackTrace();
- throw new IOException(e);
- }
-
- if(LOG.isDebugEnabled()) {
+ private static final Log LOG = LogFactory.getLog(TransactionsResource.class);
+
+ static CacheControl cacheControl;
+ static {
+ cacheControl = new CacheControl();
+ cacheControl.setNoCache(true);
+ cacheControl.setNoTransform(false);
+ }
+
+ public TransactionsResource() throws IOException {
+ super();
+ }
+
+ private String stats() throws IOException {
+ ScriptContext scriptContext = new ScriptContext();
+ scriptContext.setScriptName(Constants.SYS_SHELL_SCRIPT_NAME);
+ scriptContext.setCommand("dtmci stats -j");
+
+ try {
+ ScriptManager.getInstance().runScript(scriptContext);// This will
+ // block
+ // while
+ // script is
+ // running
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new IOException(e);
+ }
+
+ if (LOG.isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
sb.append("exit code [" + scriptContext.getExitCode() + "]");
- if(! scriptContext.getStdOut().toString().isEmpty())
+ if (!scriptContext.getStdOut().toString().isEmpty())
sb.append(", stdout [" + scriptContext.getStdOut().toString() + "]");
- if(! scriptContext.getStdErr().toString().isEmpty())
+ if (!scriptContext.getStdErr().toString().isEmpty())
sb.append(", stderr [" + scriptContext.getStdErr().toString() + "]");
LOG.debug(sb.toString());
}
return scriptContext.getStdOut().toString();
- }
-
- private String tm() throws IOException {
- ScriptContext scriptContext = new ScriptContext();
- scriptContext.setScriptName(Constants.SYS_SHELL_SCRIPT_NAME);
- scriptContext.setCommand("dtmci status tm -j");
-
- try {
- ScriptManager.getInstance().runScript(scriptContext);//This will block while script is running
- } catch (Exception e) {
- e.printStackTrace();
- throw new IOException(e);
- }
-
- if(LOG.isDebugEnabled()) {
- StringBuilder sb = new StringBuilder();
- sb.append("exit code [" + scriptContext.getExitCode() + "]");
- if(! scriptContext.getStdOut().toString().isEmpty())
- sb.append(", stdout [" + scriptContext.getStdOut().toString() + "]");
- if(! scriptContext.getStdErr().toString().isEmpty())
- sb.append(", stderr [" + scriptContext.getStdErr().toString() + "]");
- LOG.debug(sb.toString());
- }
-
- return scriptContext.getStdOut().toString();
- }
-
- @GET
- @Produces({MIMETYPE_JSON})
- public Response getAll(final @Context UriInfo uriInfo,@Context Request request) {
- try {
- if (LOG.isDebugEnabled()) {
- LOG.debug("GET " + uriInfo.getAbsolutePath());
-
- MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
- String output = " Query Parameters :\n";
- for (String key : queryParams.keySet()) {
- output += key + " : " + queryParams.getFirst(key) +"\n";
- }
- LOG.debug(output);
-
- MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();
- output = " Path Parameters :\n";
- for (String key : pathParams.keySet()) {
- output += key + " : " + pathParams.getFirst(key) +"\n";
- }
- LOG.debug(output);
- }
-
- String result = tm();
-
- ResponseBuilder response = Response.ok(result);
- response.cacheControl(cacheControl);
- return response.build();
- } catch (IOException e) {
- e.printStackTrace();
- return Response.status(Response.Status.SERVICE_UNAVAILABLE)
- .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
- .build();
- }
- }
-
- @GET
+ }
+
+ private String tm() throws IOException {
+ ScriptContext scriptContext = new ScriptContext();
+ scriptContext.setScriptName(Constants.SYS_SHELL_SCRIPT_NAME);
+ scriptContext.setCommand("dtmci status tm -j");
+
+ try {
+ ScriptManager.getInstance().runScript(scriptContext);// This will
+ // block
+ // while
+ // script is
+ // running
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new IOException(e);
+ }
+
+ if (LOG.isDebugEnabled()) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("exit code [" + scriptContext.getExitCode() + "]");
+ if (!scriptContext.getStdOut().toString().isEmpty())
+ sb.append(", stdout [" + scriptContext.getStdOut().toString() + "]");
+ if (!scriptContext.getStdErr().toString().isEmpty())
+ sb.append(", stderr [" + scriptContext.getStdErr().toString() + "]");
+ LOG.debug(sb.toString());
+ }
+
+ return scriptContext.getStdOut().toString();
+ }
+
+ @GET
+ @Produces({ MIMETYPE_JSON })
+ public Response getAll(final @Context UriInfo uriInfo, @Context Request request,
+ @HeaderParam(HttpHeaders.AUTHORIZATION) final String auth) {
+ try {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("GET " + uriInfo.getAbsolutePath());
+
+ MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
+ String output = " Query Parameters :\n";
+ for (String key : queryParams.keySet()) {
+ output += key + " : " + queryParams.getFirst(key) + "\n";
+ }
+ LOG.debug(output);
+
+ MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();
+ output = " Path Parameters :\n";
+ for (String key : pathParams.keySet()) {
+ output += key + " : " + pathParams.getFirst(key) + "\n";
+ }
+ LOG.debug(output);
+ }
+
+ Response rs = TokenTool.getResponse(auth);
--- End diff --
Instead of having to call TokenTool.getResponse(auth) on every REST method, it would be better to implement a servlet filter (javax.servlet.Filter) and have this done in one place. Plus it will guarantee that no REST request can come through without passing the authentication enforced by the filter.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-trafodion pull request #911: add oauth2 ldap for restful api
Posted by svarnau <gi...@git.apache.org>.
Github user svarnau commented on a diff in the pull request:
https://github.com/apache/incubator-trafodion/pull/911#discussion_r95625263
--- Diff: core/rest/src/main/java/org/trafodion/rest/util/TrafLdap.java ---
@@ -0,0 +1,94 @@
+package org.trafodion.rest.util;
+
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+
+import javax.naming.ldap.InitialLdapContext;
+import javax.naming.ldap.LdapContext;
+
+public class TrafLdap {
+
+ private String ip = "10.10.10.3";
+ private String port = "389";
+ private String basedn = "cn=users,cn=accounts,dc=esgyncn,dc=local";
+ private String username="trafodion";
+ private String password="traf123";
+
--- End diff --
Even for test code, it seems this type of info should be in a config file, not hard-coded.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-trafodion pull request #911: add oauth2 ldap for restful api
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/incubator-trafodion/pull/911
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] incubator-trafodion pull request #911: add oauth2 ldap for restful api
Posted by venkat1m <gi...@git.apache.org>.
Github user venkat1m commented on a diff in the pull request:
https://github.com/apache/incubator-trafodion/pull/911#discussion_r95634484
--- Diff: core/rest/src/main/java/org/trafodion/rest/util/Token.java ---
@@ -0,0 +1,194 @@
+package org.trafodion.rest.util;
+
+import java.util.Date;
+
+import java.security.Key;
+
+import javax.crypto.Cipher;
+
+public class Token {
+
+ public static String error_invalid_request = "invalid_request";
+ public static String error_invalid_client = "invalid_client";
+ public static String error_invalid_grant = "invalid_grant";
+ public static String error_unauthorized_client = "unauthorized_client";
+ public static String error_unsupported_grant_type = "unsupported_grant_type";
+ public static String error_invalid_scope = "invalid_scope";
+
+ public static String error = "error";
+ public static String error_description = "error_description";
+ public static String error_uri = "error_uri";
+
+ public static String access_token = "access_token";
+ public static String token_type = "token_type";
+ public static String expires_in = "expires_in";
+
+ public static String error_invalid_token = "error_invalid_token";
+
+ private String passwd = "fuck-trafodion!";
+ private String token = "";
+ private String interval = "7200000";
+
+ public String getInterval() {
+ return interval;
+ }
+
+ public void setInterval(String interval) {
+ this.interval = interval;
+ }
+
+ public String getPasswd() {
+ return passwd;
+ }
+
+ public void setPasswd(String passwd) {
+ this.passwd = passwd;
+ }
+
+ public String getToken() {
+ this.token = encode();
+ return this.token;
+ }
+
+ public void setToken(String token) {
+ this.token = token;
+ }
+
+ public boolean isTimeOut() {
+ if (token == null)
+ return true;
+ long timeout = Long.parseLong(getTimeOut(token));
+ Date date = new Date();
+ long timenow = date.getTime();
+ return (timenow > timeout);
+ }
+
+ private String deconde(String t) {
+
+ byte[] decryptFrom = parseHexStr2Byte(t);
+
+ if (decryptFrom == null)
+ return null;
+ byte[] decryptResult = decrypt(decryptFrom, passwd);
+
+ return new String(decryptResult);
+ }
+
+ private String encode() {
+
+ Date date = new Date();
+ long timeout = date.getTime() + Long.parseLong(interval);
+
+ String content = String.valueOf(timeout) + "=" + "continue";
+
+ byte[] encryptResult = encrypt(content, passwd);
+ String encryptResultStr = parseByte2HexStr(encryptResult);
+ return encryptResultStr;
+ }
+
+ private String getTimeOut(String t) {
+ String d = deconde(t);
+ if (d == null)
+ return "0";
+ return d.split("=")[0];
+ }
+
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ Token t = new Token();
+ // t.setPasswd("12345678");
+ String content = "test";
+ // String password = "12345678";
+
+ System.out.println("before:" + content);
+ byte[] encryptResult = t.encrypt(content, "fuck-trafodion!");
--- End diff --
Agree. This is totally inappropriate for any code.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---