Commit a583d6cc authored by benjamin.ertl's avatar benjamin.ertl
Browse files

init test + license

parent d4169efa
......@@ -85,5 +85,9 @@
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc;
import org.slf4j.Logger;
......
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc;
import java.text.ParseException;
......
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.dao;
import java.util.List;
import edu.kit.scc.dto.GroupDTO;
public interface GroupDAO {
public List<GroupDTO> getAllGroups();
public List<GroupDTO> getGroupDetails(String commonName);
public void insertGroup(GroupDTO groupDTO);
public void updateGroup(GroupDTO groupDTO);
public void deleteGroup(GroupDTO groupDTO);
}
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.dao;
import java.util.List;
......@@ -5,9 +13,9 @@ import java.util.List;
import edu.kit.scc.dto.UserDTO;
public interface UserDAO {
public List<UserDTO> getAllUserNames();
public List<UserDTO> getAllUsers();
public List<UserDTO> getUserDetails(String commonName, String lastName);
public List<UserDTO> getUserDetails(String uid);
public void insertUser(UserDTO userDTO);
......
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.dto;
public class GroupDTO {
String commonName;
int gidNumber;
public String getCommonName() {
return commonName;
}
public void setCommonName(String commonName) {
this.commonName = commonName;
}
public int getGidNumber() {
return gidNumber;
}
public void setGidNumber(int gidNumber) {
this.gidNumber = gidNumber;
}
@Override
public String toString() {
return "GroupDTO [" + (commonName != null ? "commonName=" + commonName + ", " : "") + "gidNumber=" + gidNumber
+ "]";
}
}
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.dto;
public class UserDTO {
......@@ -5,6 +13,15 @@ public class UserDTO {
String commonName;
String lastName;
String description;
String uid;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getCommonName() {
return commonName;
......@@ -29,4 +46,12 @@ public class UserDTO {
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "UserDTO [" + (commonName != null ? "commonName=" + commonName + ", " : "")
+ (lastName != null ? "lastName=" + lastName + ", " : "")
+ (description != null ? "description=" + description + ", " : "") + (uid != null ? "uid=" + uid : "")
+ "]";
}
}
\ No newline at end of file
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.http;
import java.io.ByteArrayInputStream;
......
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.http;
import java.io.IOException;
......
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.http;
import java.io.IOException;
......
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.http;
public class HttpResponse {
......
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.http;
import javax.net.ssl.HostnameVerifier;
......
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.ldap;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import org.springframework.ldap.core.AttributesMapper;
import edu.kit.scc.dto.GroupDTO;
public class GroupAttributeMapper implements AttributesMapper<GroupDTO> {
@Override
public GroupDTO mapFromAttributes(Attributes attributes) throws NamingException {
GroupDTO groupDTO = new GroupDTO();
String commonName = (String) attributes.get("cn").get();
if (commonName != null)
groupDTO.setCommonName(commonName);
Attribute gidNumber = attributes.get("gidNumber");
if (gidNumber != null)
groupDTO.setGidNumber(Integer.valueOf((String) gidNumber.get()));
return groupDTO;
}
}
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.ldap;
import java.util.List;
......@@ -11,6 +19,7 @@ import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.stereotype.Component;
import edu.kit.scc.dto.GroupDTO;
import edu.kit.scc.dto.UserDTO;
@Component
......@@ -22,10 +31,16 @@ public class LdapClient {
private String url;
@Value("${ldap.searchBase}")
private String base;
private String searchBase;
@Value("${ldap.userBase}")
private String userBase;
@Value("${ldap.groupBase}")
private String groupBase;
@Value("${ldap.bindDn}")
private String dn;
private String bindDn;
@Value("${ldap.bindPassword}")
private String password;
......@@ -34,8 +49,8 @@ public class LdapClient {
LdapContextSource contextSource() {
LdapContextSource ldapContextSource = new LdapContextSource();
ldapContextSource.setUrl(url);
ldapContextSource.setBase(base);
ldapContextSource.setUserDn(dn);
ldapContextSource.setBase(searchBase);
ldapContextSource.setUserDn(bindDn);
ldapContextSource.setPassword(password);
return ldapContextSource;
}
......@@ -46,26 +61,42 @@ public class LdapClient {
}
@Bean
LDAPUserDAO ldapUser(LdapTemplate ldapTemplate) {
LDAPUserDAO ldapUserDAO = new LDAPUserDAO();
LdapUserDAO ldapUser(LdapTemplate ldapTemplate) {
LdapUserDAO ldapUserDAO = new LdapUserDAO();
ldapUserDAO.setLdapTemplate(ldapTemplate);
ldapUserDAO.setUserBase(userBase);
return ldapUserDAO;
}
@Bean
LdapGroupDAO ldapGroup(LdapTemplate ldapTemplate) {
LdapGroupDAO ldapGroupDAO = new LdapGroupDAO();
ldapGroupDAO.setLdapTemplate(ldapTemplate);
ldapGroupDAO.setGroupBase(groupBase);
return ldapGroupDAO;
}
@Autowired
private LDAPUserDAO ldapUser;
private LdapUserDAO ldapUser;
public void getLdapUser() {
List<UserDTO> userList = ldapUser.getAllUserNames();
@Autowired
private LdapGroupDAO ldapGroup;
public void getLdapUsers() {
List<UserDTO> userList = ldapUser.getAllUsers();
for (int i = 0; i < userList.size(); i++)
log.info("User name {}", ((UserDTO) userList.get(i)).getCommonName());
List<UserDTO> userDetails = ldapUser.getUserDetails("John Smith", "Smith");
List<UserDTO> userDetails = ldapUser.getUserDetails("jsmith1");
for (int i = 0; i < userDetails.size(); i++)
log.info("Description {}", ((UserDTO) userDetails.get(i)).getDescription());
}
UserDTO newUser = new UserDTO();
newUser.setCommonName("me");
newUser.setLastName("too");
ldapUser.insertUser(newUser);
public void getLdapGroups() {
List<GroupDTO> groupList = ldapGroup.getAllGroups();
for (int i = 0; i < groupList.size(); i++)
log.info("Gropu name {}", ((GroupDTO) groupList.get(i)).getCommonName());
List<GroupDTO> groupDetails = ldapGroup.getGroupDetails("user");
for (int i = 0; i < groupDetails.size(); i++)
log.info("GidNumber {}", ((GroupDTO) groupDetails.get(i)).getGidNumber());
}
}
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.ldap;
import java.util.List;
import javax.naming.InvalidNameException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.ldap.LdapName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.support.LdapUtils;
import edu.kit.scc.dao.GroupDAO;
import edu.kit.scc.dto.GroupDTO;
public class LdapGroupDAO implements GroupDAO {
private static final Logger log = LoggerFactory.getLogger(LdapGroupDAO.class);
private LdapTemplate ldapTemplate;
private String groupBase;
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
public void setGroupBase(String groupBase) {
this.groupBase = groupBase;
}
@Override
public List<GroupDTO> getAllGroups() {
return ldapTemplate.search(groupBase, "(objectclass=posixGroup)", new GroupAttributeMapper());
}
@Override
public List<GroupDTO> getGroupDetails(String commonName) {
AndFilter andFilter = new AndFilter();
andFilter.and(new EqualsFilter("objectclass", "posixGroup")).and(new EqualsFilter("cn", commonName));
log.debug("LDAP query {}", andFilter.encode());
return ldapTemplate.search(groupBase, andFilter.encode(), new GroupAttributeMapper());
}
@Override
public void insertGroup(GroupDTO groupDTO) {
BasicAttribute posixGroupBasicAttribute = new BasicAttribute("objectclass");
posixGroupBasicAttribute.add("posixGroup");
Attributes posixGroupAttributes = new BasicAttributes();
posixGroupAttributes.put(posixGroupBasicAttribute);
posixGroupAttributes.put("cn", groupDTO.getCommonName());
posixGroupAttributes.put("gidNumber", groupDTO.getGidNumber());
LdapName newGroupDN = LdapUtils.emptyLdapName();
try {
newGroupDN = new LdapName(groupBase);
newGroupDN.add("cn=" + groupDTO.getCommonName());
log.debug(newGroupDN.toString());
// ldapTemplate.bind(newUserDN, null, personAttributes);
} catch (InvalidNameException e) {
e.printStackTrace();
}
}
@Override
public void updateGroup(GroupDTO groupDTO) {
BasicAttribute posixGroupBasicAttribute = new BasicAttribute("objectclass");
posixGroupBasicAttribute.add("posixGroup");
Attributes posixGroupAttributes = new BasicAttributes();
posixGroupAttributes.put(posixGroupBasicAttribute);
posixGroupAttributes.put("cn", groupDTO.getCommonName());
posixGroupAttributes.put("gidNumber", groupDTO.getGidNumber());
LdapName newGroupDN = LdapUtils.emptyLdapName();
try {
newGroupDN = new LdapName(groupBase);
newGroupDN.add("cn=" + groupDTO.getCommonName());
log.debug(newGroupDN.toString());
// ldapTemplate.bind(newUserDN, null, personAttributes);
} catch (InvalidNameException e) {
e.printStackTrace();
}
}
@Override
public void deleteGroup(GroupDTO groupDTO) {
LdapName newGroupDN = LdapUtils.emptyLdapName();
try {
newGroupDN = new LdapName(groupBase);
newGroupDN.add("cn=" + groupDTO.getCommonName());
log.debug(newGroupDN.toString());
// ldapTemplate.unbind(newUserDN);
} catch (InvalidNameException e) {
e.printStackTrace();
}
}
}
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.ldap;
import java.util.List;
......@@ -18,26 +26,31 @@ import org.springframework.ldap.support.LdapUtils;
import edu.kit.scc.dao.UserDAO;
import edu.kit.scc.dto.UserDTO;
public class LDAPUserDAO implements UserDAO {
public class LdapUserDAO implements UserDAO {
private static final Logger log = LoggerFactory.getLogger(LDAPUserDAO.class);
private static final Logger log = LoggerFactory.getLogger(LdapUserDAO.class);
private LdapTemplate ldapTemplate;
private String userBase;
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
public void setUserBase(String userBase) {
this.userBase = userBase;
}
@Override
public List<UserDTO> getAllUserNames() {
return ldapTemplate.search("", "(objectclass=person)", new UserAttributeMapper());
public List<UserDTO> getAllUsers() {
return ldapTemplate.search(userBase, "(objectclass=person)", new UserAttributeMapper());
}
@Override
public List<UserDTO> getUserDetails(String commonName, String lastName) {
public List<UserDTO> getUserDetails(String uid) {
AndFilter andFilter = new AndFilter();
andFilter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("cn", commonName))
.and(new EqualsFilter("sn", lastName));
andFilter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("uid", uid));
log.debug("LDAP query {}", andFilter.encode());
return ldapTemplate.search("", andFilter.encode(), new UserAttributeMapper());
......@@ -45,7 +58,6 @@ public class LDAPUserDAO implements UserDAO {
@Override
public void insertUser(UserDTO userDTO) {
BasicAttribute personBasicAttribute = new BasicAttribute("objectclass");
personBasicAttribute.add("person");
......@@ -54,11 +66,12 @@ public class LDAPUserDAO implements UserDAO {
personAttributes.put("cn", userDTO.getCommonName());
personAttributes.put("sn", userDTO.getLastName());
personAttributes.put("description", userDTO.getDescription());
personAttributes.put("uid", userDTO.getUid());
LdapName newUserDN = LdapUtils.emptyLdapName();
try {
newUserDN = new LdapName("o=sshService");
newUserDN.add("uid=" + userDTO.getCommonName());
newUserDN = new LdapName(userBase);
newUserDN.add("uid=" + userDTO.getUid());
log.debug(newUserDN.toString());
// ldapTemplate.bind(newUserDN, null, personAttributes);
} catch (InvalidNameException e) {
......@@ -80,7 +93,7 @@ public class LDAPUserDAO implements UserDAO {
LdapName newUserDN = LdapUtils.emptyLdapName();
try {
newUserDN = new LdapName("o=sshService");
newUserDN = new LdapName(userBase);
newUserDN.add("uid=" + userDTO.getCommonName());
log.debug(newUserDN.toString());
// ldapTemplate.rebind(newUserDN, null, personAttributes);
......@@ -94,7 +107,7 @@ public class LDAPUserDAO implements UserDAO {
public void deleteUser(UserDTO userDTO) {
LdapName newUserDN = LdapUtils.emptyLdapName();
try {
newUserDN = new LdapName("o=sshService");
newUserDN = new LdapName(userBase);
newUserDN.add("uid=" + userDTO.getCommonName());
log.debug(newUserDN.toString());
// ldapTemplate.unbind(newUserDN);
......@@ -103,5 +116,4 @@ public class LDAPUserDAO implements UserDAO {
e.printStackTrace();
}
}
}
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
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
*/
package edu.kit.scc.ldap;
import javax.naming.NamingException;
......@@ -13,6 +21,9 @@ public class UserAttributeMapper implements AttributesMapper<UserDTO> {
@Override
public UserDTO mapFromAttributes(Attributes attributes) throws NamingException {
UserDTO userDTO = new UserDTO();
String uid = (String) attributes.get("uid").get();
if (uid != null)
userDTO.setUid(uid);
String commonName = (String) attributes.get("cn").get();
if (commonName != null)
userDTO.setCommonName(commonName);
......
/* Copyright 2016 Karlsruhe Institute of Technology (KIT)
Licensed under the Apache License, Version 2.0 (the "License");