Commit cdd90aae authored by michael.simon's avatar michael.simon
Browse files

Add key status

Keep old keys instead of deleting them.
parent 70e9901c
......@@ -13,9 +13,12 @@ package edu.kit.scc.webreg.dao;
import java.util.List;
import edu.kit.scc.webreg.entity.SshPubKeyEntity;
import edu.kit.scc.webreg.entity.SshPubKeyStatus;
public interface SshPubKeyDao extends BaseDao<SshPubKeyEntity, Long> {
List<SshPubKeyEntity> findByUser(Long userId);
List<SshPubKeyEntity> findByUserAndStatus(Long userId, SshPubKeyStatus keyStatus);
}
......@@ -17,6 +17,7 @@ import javax.inject.Named;
import edu.kit.scc.webreg.dao.SshPubKeyDao;
import edu.kit.scc.webreg.entity.SshPubKeyEntity;
import edu.kit.scc.webreg.entity.SshPubKeyStatus;
@Named
@ApplicationScoped
......@@ -29,6 +30,15 @@ public class JpaSshPubKeyDao extends JpaBaseDao<SshPubKeyEntity, Long> implement
.setParameter("userId", userId).getResultList();
}
@Override
@SuppressWarnings("unchecked")
public List<SshPubKeyEntity> findByUserAndStatus(Long userId, SshPubKeyStatus keyStatus) {
return em.createQuery("select e from SshPubKeyEntity e where e.user.id = :userId and e.keyStatus = :keyStatus")
.setParameter("userId", userId)
.setParameter("keyStatus", keyStatus)
.getResultList();
}
@Override
public Class<SshPubKeyEntity> getEntityClass() {
return SshPubKeyEntity.class;
......
package edu.kit.scc.webreg.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
......@@ -19,6 +21,9 @@ public class SshPubKeyEntity extends AbstractBaseEntity {
@Enumerated(EnumType.STRING)
private SshPubKeyUsageType usageType;
@Enumerated(EnumType.STRING)
private SshPubKeyStatus keyStatus;
@Column(name = "name", length = 128)
private String name;
......@@ -37,6 +42,9 @@ public class SshPubKeyEntity extends AbstractBaseEntity {
@Column(name = "comment", length = 1024)
private String comment;
@Column(name = "expires_at")
private Date expiresAt;
public UserEntity getUser() {
return user;
}
......@@ -100,4 +108,20 @@ public class SshPubKeyEntity extends AbstractBaseEntity {
public void setKeyType(String keyType) {
this.keyType = keyType;
}
public SshPubKeyStatus getKeyStatus() {
return keyStatus;
}
public void setKeyStatus(SshPubKeyStatus keyStatus) {
this.keyStatus = keyStatus;
}
public Date getExpiresAt() {
return expiresAt;
}
public void setExpiresAt(Date expiresAt) {
this.expiresAt = expiresAt;
}
}
/*******************************************************************************
* Copyright (c) 2014 Michael Simon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Michael Simon - initial
******************************************************************************/
package edu.kit.scc.webreg.entity;
public enum SshPubKeyStatus {
ACTIVE,
DELETED,
}
package edu.kit.scc.webreg.dto.entity;
import java.util.Date;
import edu.kit.scc.webreg.entity.SshPubKeyStatus;
import edu.kit.scc.webreg.entity.SshPubKeyUsageType;
public class SshPubKeyEntityDto extends AbstractBaseEntityDto {
......@@ -18,7 +21,11 @@ public class SshPubKeyEntityDto extends AbstractBaseEntityDto {
private String comment;
private Date expiresAt;
private SshPubKeyUsageType usageType;
private SshPubKeyStatus keyStatus;
public String getCommand() {
return command;
......@@ -76,4 +83,20 @@ public class SshPubKeyEntityDto extends AbstractBaseEntityDto {
this.usageType = usageType;
}
public Date getExpiresAt() {
return expiresAt;
}
public void setExpiresAt(Date expiresAt) {
this.expiresAt = expiresAt;
}
public SshPubKeyStatus getKeyStatus() {
return keyStatus;
}
public void setKeyStatus(SshPubKeyStatus keyStatus) {
this.keyStatus = keyStatus;
}
}
......@@ -24,7 +24,7 @@ public class SshPubKeyEntityMapper extends AbstractBaseEntityMapper<SshPubKeyEnt
@Override
protected String[] getPropertiesToCopy() {
return new String[] { "name", "command",
"from", "comment", "encodedKey", "keyType"};
"from", "comment", "encodedKey", "keyType", "expiresAt", "keyStatus"};
}
}
......@@ -4,10 +4,14 @@ import java.util.List;
import edu.kit.scc.webreg.dto.entity.SshPubKeyEntityDto;
import edu.kit.scc.webreg.entity.SshPubKeyEntity;
import edu.kit.scc.webreg.entity.SshPubKeyStatus;
import edu.kit.scc.webreg.exc.RestInterfaceException;
public interface SshPubKeyDtoService extends BaseDtoService<SshPubKeyEntity, SshPubKeyEntityDto, Long> {
List<SshPubKeyEntityDto> findByUidNumber(Long uidNumber) throws RestInterfaceException;
List<SshPubKeyEntityDto> findByUidNumberAndStatus(Long uidNumber, SshPubKeyStatus keyStatus)
throws RestInterfaceException;
}
......@@ -15,6 +15,7 @@ import edu.kit.scc.webreg.dto.entity.SshPubKeyEntityDto;
import edu.kit.scc.webreg.dto.mapper.BaseEntityMapper;
import edu.kit.scc.webreg.dto.mapper.SshPubKeyEntityMapper;
import edu.kit.scc.webreg.entity.SshPubKeyEntity;
import edu.kit.scc.webreg.entity.SshPubKeyStatus;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.exc.RestInterfaceException;
......@@ -40,6 +41,20 @@ public class SshPubKeyDtoServiceImpl extends BaseDtoServiceImpl<SshPubKeyEntity,
UserEntity user = userDao.findByUidNumber(uidNumber);
List<SshPubKeyEntity> list = dao.findByUser(user.getId());
return convertList(list);
}
@Override
public List<SshPubKeyEntityDto> findByUidNumberAndStatus(Long uidNumber, SshPubKeyStatus keyStatus) throws RestInterfaceException {
UserEntity user = userDao.findByUidNumber(uidNumber);
List<SshPubKeyEntity> list = dao.findByUserAndStatus(user.getId(), keyStatus);
return convertList(list);
}
protected List<SshPubKeyEntityDto> convertList(List<SshPubKeyEntity> list) {
List<SshPubKeyEntityDto> dtoList = new ArrayList<SshPubKeyEntityDto>(list.size());
for (SshPubKeyEntity key : list) {
......@@ -49,7 +64,7 @@ public class SshPubKeyDtoServiceImpl extends BaseDtoServiceImpl<SshPubKeyEntity,
}
return dtoList;
}
}
@Override
protected BaseEntityMapper<SshPubKeyEntity, SshPubKeyEntityDto, Long> getMapper() {
......
......@@ -13,10 +13,13 @@ package edu.kit.scc.webreg.service.ssh;
import java.util.List;
import edu.kit.scc.webreg.entity.SshPubKeyEntity;
import edu.kit.scc.webreg.entity.SshPubKeyStatus;
import edu.kit.scc.webreg.service.BaseService;
public interface SshPubKeyService extends BaseService<SshPubKeyEntity, Long> {
List<SshPubKeyEntity> findByUser(Long userId);
List<SshPubKeyEntity> findByUserAndStatus(Long userId, SshPubKeyStatus keyStatus);
}
......@@ -18,6 +18,7 @@ import javax.inject.Inject;
import edu.kit.scc.webreg.dao.BaseDao;
import edu.kit.scc.webreg.dao.SshPubKeyDao;
import edu.kit.scc.webreg.entity.SshPubKeyEntity;
import edu.kit.scc.webreg.entity.SshPubKeyStatus;
import edu.kit.scc.webreg.service.impl.BaseServiceImpl;
@Stateless
......@@ -33,6 +34,11 @@ public class SshPubKeyServiceImpl extends BaseServiceImpl<SshPubKeyEntity, Long>
return dao.findByUser(userId);
}
@Override
public List<SshPubKeyEntity> findByUserAndStatus(Long userId, SshPubKeyStatus keyStatus) {
return dao.findByUserAndStatus(userId, keyStatus);
}
@Override
protected BaseDao<SshPubKeyEntity, Long> getDao() {
return dao;
......
......@@ -12,6 +12,7 @@ package edu.kit.scc.webreg.bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
......@@ -21,7 +22,9 @@ import javax.inject.Inject;
import org.slf4j.Logger;
import edu.kit.scc.webreg.bootstrap.ApplicationConfig;
import edu.kit.scc.webreg.entity.SshPubKeyEntity;
import edu.kit.scc.webreg.entity.SshPubKeyStatus;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.service.UserService;
import edu.kit.scc.webreg.service.ssh.SshPubKeyService;
......@@ -57,6 +60,9 @@ public class UserSshKeyManagementBean implements Serializable {
@Inject
private SshPubKeyService sshPubKeyService;
@Inject
private ApplicationConfig appConfig;
private List<OpenSshPublicKey> keyList;
private String newKey;
private String newName;
......@@ -65,7 +71,7 @@ public class UserSshKeyManagementBean implements Serializable {
public void preRenderView(ComponentSystemEvent ev) {
if (user == null) {
user = userService.findById(sessionManager.getUserId());
List<SshPubKeyEntity> sshPubKeyList = sshPubKeyService.findByUser(user.getId());
List<SshPubKeyEntity> sshPubKeyList = sshPubKeyService.findByUserAndStatus(user.getId(), SshPubKeyStatus.ACTIVE);
keyList = new ArrayList<>();
for (SshPubKeyEntity sshKey : sshPubKeyList) {
......@@ -93,7 +99,8 @@ public class UserSshKeyManagementBean implements Serializable {
if (removeIndex != -1) {
keyList.remove(removeIndex);
sshPubKeyService.delete(removeEntity);
removeEntity.setKeyStatus(SshPubKeyStatus.DELETED);
removeEntity = sshPubKeyService.save(removeEntity);
}
messageGenerator.addResolvedInfoMessage("info", "ssh_key_deleted", false);
......@@ -102,10 +109,20 @@ public class UserSshKeyManagementBean implements Serializable {
public void deployKey() {
OpenSshPublicKey key;
Long expireTime = 90 * 24 * 60 * 60 * 1000L; // 90 days standard expiry time for ssh keys. -1 for never expire
if (appConfig.getConfigValue("sshpubkey_expire_time") != null) {
expireTime = Long.parseLong(appConfig.getConfigValue("sshpubkey_expire_time"));
}
SshPubKeyEntity sshPubKeyEntity = sshPubKeyService.createNew();
sshPubKeyEntity.setName(newName);
sshPubKeyEntity.setEncodedKey(newKey);
sshPubKeyEntity.setUser(user);
sshPubKeyEntity.setKeyStatus(SshPubKeyStatus.ACTIVE);
if (expireTime != -1) {
sshPubKeyEntity.setExpiresAt(new Date(System.currentTimeMillis() + expireTime));
}
try {
key = keyDecoder.decode(sshPubKeyEntity);
......
......@@ -19,6 +19,7 @@ import edu.kit.scc.webreg.dto.service.SshPubKeyDtoService;
import edu.kit.scc.webreg.entity.RegistryEntity;
import edu.kit.scc.webreg.entity.RegistryStatus;
import edu.kit.scc.webreg.entity.ServiceEntity;
import edu.kit.scc.webreg.entity.SshPubKeyStatus;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.exc.NoItemFoundException;
import edu.kit.scc.webreg.exc.NoRegistryFoundException;
......@@ -52,7 +53,7 @@ public class SshKeyController {
@Inject
private SshPubKeyDtoService dtoService;
@Path(value = "/list/uidnumber/{uidNumber}")
@Path(value = "/list/uidnumber/{uidNumber}/all")
@Produces({MediaType.APPLICATION_JSON})
@GET
public List<SshPubKeyEntityDto> listKeysForUser(@PathParam("uidNumber") Long uidNumber, @Context HttpServletRequest request)
......@@ -60,6 +61,15 @@ public class SshKeyController {
return dtoService.findByUidNumber(uidNumber);
}
@Path(value = "/list/uidnumber/{uidNumber}/key-status/{status}")
@Produces({MediaType.APPLICATION_JSON})
@GET
public List<SshPubKeyEntityDto> listKeysForUserAndStatus(@PathParam("uidNumber") Long uidNumber,
@PathParam("status") SshPubKeyStatus keyStatus, @Context HttpServletRequest request)
throws IOException, RestInterfaceException {
return dtoService.findByUidNumberAndStatus(uidNumber, keyStatus);
}
@Path(value = "/auth/{ssn}/uidnumber/{uidNumber}")
@Produces({MediaType.TEXT_PLAIN})
@GET
......
......@@ -40,6 +40,10 @@
<p:column>
<h:outputText value="#{key.publicKey}" rendered="#{not empty key.publicKey}" />
<h:outputText value="#{messages.ssh_key_type_unknown}" rendered="#{empty key.publicKey}" />
</p:column>
<p:column>
<f:facet name="header"><h:outputText value="#{messages.expires}:"/></f:facet>
<h:outputText value="#{of:formatDate(key.pubKeyEntity.expiresAt, 'dd.MM.yyyy HH:mm')}"/>
</p:column>
<p:column>
<p:commandLink id="delete" action="#{userSshKeyManagementBean.deleteKey(key.pubKeyEntity.name)}" value="#{messages.delete}" immediate="true" update="@form">
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment