Commit ad376797 authored by ls1947's avatar ls1947
Browse files

add bulk import for blacklisted ssh keys

parent 4d37fdff
......@@ -25,4 +25,6 @@ public interface SshPubKeyDao extends BaseDao<SshPubKeyEntity, Long> {
List<SshPubKeyEntity> findByUserAndStatusWithRegs(Long userId, SshPubKeyStatus keyStatus);
List<SshPubKeyEntity> findByKey(String encodedKey);
}
......@@ -59,6 +59,14 @@ public class JpaSshPubKeyDao extends JpaBaseDao<SshPubKeyEntity, Long> implement
.getResultList();
}
@Override
@SuppressWarnings("unchecked")
public List<SshPubKeyEntity> findByKey(String encodedKey) {
return em.createQuery("select e from SshPubKeyEntity e where e.encodedKey = :encodedKey")
.setParameter("encodedKey", encodedKey)
.getResultList();
}
@Override
public Class<SshPubKeyEntity> getEntityClass() {
return SshPubKeyEntity.class;
......
......@@ -30,4 +30,6 @@ public interface SshPubKeyService extends BaseService<SshPubKeyEntity, Long> {
SshPubKeyEntity deleteKey(SshPubKeyEntity entity, String executor);
List<SshPubKeyEntity> findByKey(String encodedKey);
}
......@@ -52,6 +52,11 @@ public class SshPubKeyServiceImpl extends BaseServiceImpl<SshPubKeyEntity, Long>
public List<SshPubKeyEntity> findByUser(Long userId) {
return dao.findByUser(userId);
}
@Override
public List<SshPubKeyEntity> findByKey(String encodedKey) {
return dao.findByKey(encodedKey);
}
@Override
public List<SshPubKeyEntity> findByUserAndStatus(Long userId, SshPubKeyStatus keyStatus) {
......@@ -91,7 +96,7 @@ public class SshPubKeyServiceImpl extends BaseServiceImpl<SshPubKeyEntity, Long>
@Override
public SshPubKeyEntity deployKey(Long userId, SshPubKeyEntity entity, String executor)
throws SshPubKeyBlacklistedException {
List<SshPubKeyEntity> keyList = dao.findByUserAndKey(userId, entity.getEncodedKey());
List<SshPubKeyEntity> keyList = dao.findByKey(entity.getEncodedKey());
if (keyList != null && keyList.size() > 0) {
logger.warn("User {} tried to re-add blacklisted key", userId);
throw new SshPubKeyBlacklistedException("Key already used by user");
......
/*******************************************************************************
* 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.bean.admin.bulk;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import org.slf4j.Logger;
import edu.kit.scc.webreg.entity.SshPubKeyEntity;
import edu.kit.scc.webreg.entity.SshPubKeyStatus;
import edu.kit.scc.webreg.service.ssh.SshPubKeyService;
import edu.kit.scc.webreg.ssh.OpenSshKeyDecoder;
import edu.kit.scc.webreg.ssh.OpenSshPublicKey;
import edu.kit.scc.webreg.ssh.UnsupportedKeyTypeException;
@ManagedBean
@ViewScoped
public class BulkSshKeyBlacklistImportBean implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private Logger logger;
@Inject
private SshPubKeyService sshPubKeyService;
@Inject
private OpenSshKeyDecoder keyDecoder;
private String keyField;
public void importKeys() {
logger.info("Starting import");
long importTime = System.currentTimeMillis();
int importCount = 0;
StringBuffer sb = new StringBuffer();
String[] lines = keyField.split(System.getProperty("line.separator"));
for (String line : lines) {
importCount++;
OpenSshPublicKey key = new OpenSshPublicKey();
SshPubKeyEntity entity = sshPubKeyService.createNew();
entity.setEncodedKey(line);
entity.setKeyStatus(SshPubKeyStatus.DELETED);
entity.setName("imported-" + importTime + "-" + importCount);
key.setPubKeyEntity(entity);
logger.info("Importing line {}", importCount);
try {
keyDecoder.decode(key);
entity.setEncodedKey(key.getBaseDate());
List<SshPubKeyEntity> blackList = sshPubKeyService.findByKey(entity.getEncodedKey());
if (blackList.size() == 0) {
entity = sshPubKeyService.save(entity);
}
else {
logger.info("Key {} already blacklisted", importCount);
}
} catch (UnsupportedKeyTypeException e) {
logger.info("Key not supported: " + e.getMessage());
sb.append(line);
sb.append(System.getProperty("line.separator"));
}
}
logger.info("Done import");
keyField = sb.toString();
}
public String getKeyField() {
return keyField;
}
public void setKeyField(String keyField) {
this.keyField = keyField;
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:bw="http://www.scc.kit.edu/bwfacelets"
xmlns:p="http://primefaces.org/ui"
xmlns:of="http://omnifaces.org/functions">
<head>
<title></title>
</head>
<body>
<f:view>
<ui:composition template="/template/default-admin.xhtml">
<ui:param name="title" value="#{messages.title}"/>
<ui:define name="content">
<h:form id="form">
<div class="panel">
<p:panel id="uidPanel" header="#{messages.bulk_import}">
<p:panelGrid id="baseData" columns="2">
<p:outputLabel for="@next" value="Keys"/>
<p:inputTextarea id="keyField" rows="12" cols="80" autoResize="false"
value="#{bulkSshKeyBlacklistImportBean.keyField}" required="true"/>
</p:panelGrid>
<p:commandButton id="importBtn" action="#{bulkSshKeyBlacklistImportBean.importKeys()}" value="Importieren"
update=":form:uidPanel"/>
</p:panel>
<p:blockUI block="uidPanel" trigger="importBtn">
<p:graphicImage value="#{resource['/img/ajax-loader.gif']}" alt="#{messages.loading}" />
</p:blockUI>
</div>
</h:form>
</ui:define>
</ui:composition>
</f:view>
</body>
</html>
Markdown is supported
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