Commit 24f81ee7 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Added user key service

Added post key with json
parent aece94f7
......@@ -42,15 +42,17 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import nl.uva.sne.drip.api.dao.ToscaDao;
import nl.uva.sne.drip.api.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.api.rpc.DRIPCaller;
import nl.uva.sne.drip.api.rpc.ProvisionerCaller;
import nl.uva.sne.drip.api.service.ToscaService;
import nl.uva.sne.drip.api.service.UserKeyService;
import nl.uva.sne.drip.api.service.UserScriptService;
import nl.uva.sne.drip.api.service.UserService;
import nl.uva.sne.drip.commons.types.CloudCredentials;
import nl.uva.sne.drip.commons.types.LoginKey;
import nl.uva.sne.drip.commons.types.UserScript;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.RequestBody;
......@@ -69,6 +71,12 @@ public class ProvisionController {
@Autowired
private ToscaService toscaService;
@Autowired
private UserScriptService userScriptService;
@Autowired
private UserKeyService userKeysService;
@Autowired
private CloudCredentialsDao cloudCredentialsDao;
......@@ -120,7 +128,7 @@ public class ProvisionController {
parameters.addAll(userScripts);
List<Parameter> userKeys = buildKeysParams(pReq.getUserKeyID());
parameters.addAll(userScripts);
parameters.addAll(userKeys);
invokationMessage.setParameters(parameters);
invokationMessage.setCreationDate((System.currentTimeMillis()));
......@@ -208,11 +216,31 @@ public class ProvisionController {
}
private List<Parameter> buildScriptParams(String userScriptID) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
UserScript script = userScriptService.getDao().findOne(userScriptID);
if (script == null) {
throw new BadRequestException("User script: " + userScriptID + " was not found");
}
List<Parameter> parameters = new ArrayList();
Parameter scriptParameter = new Parameter();
scriptParameter.setName("guiscript");
scriptParameter.setValue(script.getContents());
scriptParameter.setEncoding("UTF-8");
parameters.add(scriptParameter);
return parameters;
}
private List<Parameter> buildKeysParams(String userKeyID) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
LoginKey key = userKeysService.get(userKeyID, LoginKey.Type.PUBLIC);
if (key == null) {
throw new BadRequestException("User key: " + userKeyID + " was not found");
}
List<Parameter> parameters = new ArrayList();
Parameter keyParameter = new Parameter();
keyParameter.setName("sshkey");
keyParameter.setValue(key.getKey());
keyParameter.setEncoding("UTF-8");
parameters.add(keyParameter);
return parameters;
}
}
......@@ -22,7 +22,6 @@ import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.security.RolesAllowed;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -32,9 +31,12 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import nl.uva.sne.drip.api.dao.UserKeyDao;
import nl.uva.sne.drip.api.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.api.service.UserKeyService;
import nl.uva.sne.drip.api.service.UserService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
/**
*
......@@ -46,7 +48,7 @@ import org.springframework.web.bind.annotation.PathVariable;
public class UserPublicKeysController {
@Autowired
private UserKeyDao dao;
private UserKeyService service;
// curl -v -X POST -F "file=@.ssh/id_dsa.pub" localhost:8080/drip-api/user_key/upload
@RequestMapping(value = "/upload", method = RequestMethod.POST)
......@@ -64,7 +66,8 @@ public class UserPublicKeysController {
upk.setKey(key);
upk.setName(name);
upk.setType(LoginKey.Type.PUBLIC);
dao.save(upk);
service.getDao().save(upk);
return upk.getId();
} catch (IOException | IllegalStateException ex) {
......@@ -75,24 +78,38 @@ public class UserPublicKeysController {
}
// curl -H "Content-Type: application/json" -X POST -d '{"key":"ssh-rsa AAAAB3NzaDWBqs75i849MytgwgQcRYMcsXIki0yeYTKABH6JqoiyFBHtYlyh/EV1t6cujb9LyNP4J5EN4fPbtwKYvxecd0LojSPxl4wjQlfrHyg6iKUYB7hVzGqACMvgYZHrtHPfrdEmOGPplPVPpoaX2j+u0BZ0yYhrWMKjzyYZKa68yy5N18+Gq+1p83HfUDwIU9wWaUYdgEvDujqF6b8p3z6LDx9Ob+RanSMZSt+b8eZRcd+F2Oy/gieJEJ8kc152VIOv8UY1xB3hVEwVnSRGgrAsa+9PChfF6efXUGWiKf8KBlWgBOYsSTsOY4ks9zkXMnbcTdC+o7xspOkyIcWjv us@u\n","name":"id_rsa.pub"}' localhost:8080/drip-api/user_key/
// @RequestMapping(method = RequestMethod.POST)
// @RolesAllowed({UserService.USER, UserService.ADMIN})
// public @ResponseBody
// String postConf(LoginKey uk) throws JSONException {
// String name = System.currentTimeMillis() + "_" + uk.getName();
// uk.setName(name);
// dao.save(uk);
// return uk.getId();
// }
@RequestMapping(method = RequestMethod.POST)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String postKey(@RequestBody LoginKey uk) {
LoginKey.Type type = uk.getType();
if (type != null && type.equals(LoginKey.Type.PRIVATE)) {
throw new BadRequestException("Key can't be private");
}
if (uk.getKey() == null) {
throw new BadRequestException("Key can't be empty");
}
String originalName = uk.getName();
if (originalName == null) {
originalName = "id.pub";
}
String name = System.currentTimeMillis() + "_" + originalName;
uk.setName(name);
uk.setType(LoginKey.Type.PUBLIC);
service.getDao().save(uk);
return uk.getId();
}
//curl localhost:8080/drip-api/user_key/58a20be263d4a5898835676e
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public LoginKey get(@PathVariable("id") String id) {
LoginKey key = dao.findOne(id);
if (key == null || !key.getType().equals(LoginKey.Type.PUBLIC)) {
public @ResponseBody
LoginKey get(@PathVariable("id") String id) {
LoginKey key = service.get(id, LoginKey.Type.PUBLIC);
if (key == null) {
throw new NotFoundException();
}
return key;
}
......@@ -101,14 +118,20 @@ public class UserPublicKeysController {
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
List<String> getIds() {
List<LoginKey> all = dao.findAll();
List<LoginKey> all = service.getAll(LoginKey.Type.PUBLIC);
List<String> ids = new ArrayList<>();
for (LoginKey tr : all) {
if (tr.getType().equals(LoginKey.Type.PUBLIC)) {
ids.add(tr.getId());
}
ids.add(tr.getId());
}
return ids;
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody
String delete(@PathVariable("id") String id) {
service.delete(id, LoginKey.Type.PUBLIC);
return "Deleteed: " + id;
}
}
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* 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
*
* 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 nl.uva.sne.drip.api.service;
import java.util.ArrayList;
import java.util.List;
import nl.uva.sne.drip.api.dao.UserKeyDao;
import nl.uva.sne.drip.commons.types.LoginKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* @author S. Koulouzis
*/
@Service
public class UserKeyService {
@Autowired
UserKeyDao dao;
public UserKeyDao getDao() {
return dao;
}
public List<LoginKey> getAll(LoginKey.Type type) {
List<LoginKey> all = getDao().findAll();
if (all != null) {
List<LoginKey> allPublic = new ArrayList<>();
for (LoginKey tr : all) {
if (tr.getType() != null && tr.getType().equals(LoginKey.Type.PUBLIC)) {
allPublic.add(tr);
}
}
return allPublic;
}
return null;
}
public LoginKey get(String id, LoginKey.Type type) {
LoginKey key = getDao().findOne(id);
if (key.getType().equals(type)) {
return key;
}
return null;
}
public void delete(String id, LoginKey.Type type) {
LoginKey k = get(id, type);
if (k != null) {
getDao().delete(k);
}
}
}
......@@ -15,18 +15,8 @@
*/
package nl.uva.sne.drip.api.service;
import java.util.Collection;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.uva.sne.drip.api.dao.UserDao;
import nl.uva.sne.drip.api.dao.UserScriptDao;
import nl.uva.sne.drip.commons.types.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
/**
......
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