Commit 58afb05a authored by michael.simon's avatar michael.simon
Browse files

deployment and storage of keys work now

parent dd8b4457
......@@ -25,10 +25,12 @@ public class OpenSshKeyDecoder implements Serializable {
private static final long serialVersionUID = 1L;
public OpenSshPublicKey decode(String opensshPublicKey) throws UnsupportedKeyTypeException {
public OpenSshPublicKey decode(String name, String opensshPublicKey) throws UnsupportedKeyTypeException {
OpenSshPublicKey key = new OpenSshPublicKey();
key.setName(name);
key.setValue(opensshPublicKey.trim());
getKeyBytes(key, opensshPublicKey);
getKeyBytes(key);
try {
String type = decodeType(key);
......@@ -63,11 +65,12 @@ public class OpenSshKeyDecoder implements Serializable {
}
}
private void getKeyBytes(OpenSshPublicKey key, String opensshPublicKey) throws UnsupportedKeyTypeException {
for (String part : opensshPublicKey.split(" ")) {
private void getKeyBytes(OpenSshPublicKey key) throws UnsupportedKeyTypeException {
for (String part : key.getValue().split(" ")) {
if (Base64.isBase64(part) && part.startsWith("AAAA")) {
key.setBaseDate(part);
key.setBytes(Base64.decodeBase64(part));
return;
}
}
throw new UnsupportedKeyTypeException("no Base64 part to decode");
......
......@@ -2,12 +2,26 @@ package edu.kit.scc.webreg.ssh;
import java.security.PublicKey;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class OpenSshPublicKey {
private String name;
private String value;
@JsonIgnore
private byte[] bytes;
@JsonIgnore
private int decoderPos;
@JsonIgnore
private PublicKey publicKey;
@JsonIgnore
private String baseDate;
@JsonIgnore
private String decoderResult;
public OpenSshPublicKey() {
......@@ -58,4 +72,20 @@ public class OpenSshPublicKey {
public void setDecoderResult(String decoderResult) {
this.decoderResult = decoderResult;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
......@@ -10,6 +10,7 @@
******************************************************************************/
package edu.kit.scc.webreg.bean;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
......@@ -19,12 +20,20 @@ import javax.faces.bean.ViewScoped;
import javax.faces.event.ComponentSystemEvent;
import javax.inject.Inject;
import org.slf4j.Logger;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.service.UserService;
import edu.kit.scc.webreg.session.SessionManager;
import edu.kit.scc.webreg.ssh.OpenSshKeyDecoder;
import edu.kit.scc.webreg.ssh.OpenSshPublicKey;
import edu.kit.scc.webreg.ssh.UnsupportedKeyTypeException;
import edu.kit.scc.webreg.util.FacesMessageGenerator;
@ManagedBean
@ViewScoped
......@@ -34,6 +43,9 @@ public class UserSshKeyManagementBean implements Serializable {
private UserEntity user;
@Inject
private Logger logger;
@Inject
private UserService userService;
......@@ -42,24 +54,51 @@ public class UserSshKeyManagementBean implements Serializable {
@Inject
private OpenSshKeyDecoder keyDecoder;
@Inject
private FacesMessageGenerator messageGenerator;
private List<OpenSshPublicKey> keyList;
private String newKey;
private String newName;
public void preRenderView(ComponentSystemEvent ev) {
if (user == null) {
user = userService.findByIdWithStore(sessionManager.getUserId());
keyList = new ArrayList<>();
if (user.getGenericStore().containsKey("ssh_key")) {
ObjectMapper om = new ObjectMapper();
try {
keyList = om.readValue(user.getGenericStore().get("ssh_key"),
new TypeReference<List<OpenSshPublicKey>>(){});
} catch (IOException e) {
logger.warn("Could not read SSH keys from user: " + e.getMessage());
messageGenerator.addResolvedErrorMessage("error_msg", "SSH Key not readable. Resetting keys.", false);
keyList = new ArrayList<>();
}
}
else {
keyList = new ArrayList<>();
}
}
}
public void deployKey() {
OpenSshPublicKey key;
try {
key = keyDecoder.decode(newKey);
key = keyDecoder.decode(newName, newKey);
keyList.add(key);
ObjectMapper om = new ObjectMapper();
ArrayNode array = om.createArrayNode();
for (OpenSshPublicKey sshKey : keyList) {
array.add(om.convertValue(sshKey, JsonNode.class));
}
user.getGenericStore().put("ssh_key", array.toString());
user = userService.save(user);
newKey = "";
newName = "";
} catch (UnsupportedKeyTypeException e) {
// happenes when there is not base64 part in key
logger.warn("An error occured whilst deploying key: " + e.getMessage());
messageGenerator.addResolvedErrorMessage("error_msg", e.toString(), false);
}
}
......@@ -82,4 +121,12 @@ public class UserSshKeyManagementBean implements Serializable {
public void setKeyList(List<OpenSshPublicKey> keyList) {
this.keyList = keyList;
}
public String getNewName() {
return newName;
}
public void setNewName(String newName) {
this.newName = newName;
}
}
......@@ -25,6 +25,7 @@
<ui:define name="content">
<h:form id="form" prependId="false">
<p:panel header="#{messages.ssh_key_management}">
<div><p:messages showDetail="true" /></div>
<p:panelGrid id="baseData" columns="2">
......@@ -32,11 +33,14 @@
<h:panelGroup>
<ul>
<ui:repeat var="key" value="#{userSshKeyManagementBean.keyList.toArray()}">
<li><h:outputText value="#{key.publicKey}"/></li>
<li><h:outputText value="#{key.name}"/></li>
</ui:repeat>
</ul>
</h:panelGroup>
<bw:inputText id="sshKeyName" label="#{messages.add_ssh_key_name}:"
value="#{userSshKeyManagementBean.newName}" required="true"/>
<h:outputText value="#{messages.add_ssh_key}:"/>
<p:inputTextarea value="#{userSshKeyManagementBean.newKey}" style="width: 600px; height: 100px;"
autoResize="false" />
......
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