Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUBMARINE-646. Create rest api to authenticate user from LDAP #419

Closed
wants to merge 14 commits into from
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.submarine.server.api.ldap;

public class Ldap {
private String username;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change username to userName.

private String password;

public String getUsername() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change getUsername to getUserName.

return username;
}

public void setUsername(String username) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change setUsername(String username) to setUserName(String userName).

this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.submarine.server.api.spec;

public class LdapSpec {

/**
* Name of the user
*/
private String user_name;

/**
* password of the user
*/
private String password;

public String getUserName(){
return user_name;
}

public String getPassword(){
return password;
}

public void setUserName(String user_name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function setUserName param is user_name, this.userName = userName; corrent??

this.user_name = user_name;
}

public void setPassword(String password) {
this.password = password;
}
}
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.submarine.server.ldap;

import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LdapManager {

private static final Logger LOG = LoggerFactory.getLogger(LdapManager.class);

private static volatile LdapManager manager;

private final AtomicInteger experimentTemplateIdCounter = new AtomicInteger(0);


/**
* Get the singleton instance
* @return object
*/
public static LdapManager getInstance() {
if (manager == null) {
synchronized (LdapManager.class) {
if (manager == null) {
manager = new LdapManager();
}
}
}
return manager;
}
}
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.submarine.server.rest;

import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.submarine.server.ldap.LdapManager;

import java.util.Hashtable;


@Path(RestConstants.V1 + "/" + RestConstants.LDAP)
@Produces({MediaType.APPLICATION_JSON + "; " + RestConstants.CHARSET_UTF8})

public class LdapAuthenticateRestApi {
private final LdapManager ldapManager = LdapManager.getInstance();

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response authenticateUser(@FormParam("username") String username,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change param name and value nameusername to userName.

@FormParam("password") String password) {
try {
//Authenticate the user using the credentials provided
authenticate(username, password);

return Response.ok().build();
}
catch (Exception e) {
return Response.status(Response.Status.FORBIDDEN).build();
}
}

private void authenticate(String username, String password) throws Exception {
DirContext ctx = null;
Hashtable<String, String> HashEnv = new Hashtable<>();

String loginId = "uid=" + username + ",dc=example,dc=com";

HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
HashEnv.put(Context.SECURITY_PRINCIPAL, loginId);
HashEnv.put(Context.SECURITY_CREDENTIALS, password);
HashEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
HashEnv.put("com.sun.jndi.ldap.connect.timeout", "3000");
HashEnv.put(Context.PROVIDER_URL, "ldap://ldap.forumsys.com:389");

try {
ctx = new InitialDirContext(HashEnv);
}
catch (AuthenticationException e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code style

Suggested change
}
catch (AuthenticationException e) {
} catch (AuthenticationException e) {

e.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use LOG.error(xxx) to print exception.

}

if (ctx != null) {
try {
ctx.close();
}
catch (NamingException e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
catch (NamingException e) {
} catch (NamingException e) {

e.printStackTrace();
}
}
}

}
Expand Up @@ -58,7 +58,7 @@ public class RestConstants {
* Experimect template
*/
public static final String EXPERIMENT_TEMPLATES = "template";

public static final String EXPERIMENT_TEMPLATE_ID = "id";

/**
Expand All @@ -68,4 +68,9 @@ public class RestConstants {

public static final String NOTEBOOK_ID = "id";

/**
* Ldap
*/
public static final String LDAP = "ldap";

}