use of com.bluenimble.platform.security.KeyPair in project serverless by bluenimble.
the class FileSystemKeyStoreManager method read.
@Override
public SpaceKeyStore read(ApiSpace space) throws IOException {
SpaceKeyStoreImpl ks = new SpaceKeyStoreImpl(this, space);
// load default keys
JsonArray aKeys = space.keys();
if (aKeys != null && !aKeys.isEmpty()) {
for (int i = 0; i < aKeys.count(); i++) {
KeyPair skp = toKeyPair((String) aKeys.get(i));
ks._put(skp);
}
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(keyStoreFile(space, true)), Encodings.UTF8));
String line = reader.readLine();
while (line != null) {
KeyPair skp = toKeyPair(line);
ks._put(skp);
line = reader.readLine();
}
} finally {
IOUtils.closeQuietly(reader);
}
return ks;
}
use of com.bluenimble.platform.security.KeyPair in project serverless by bluenimble.
the class FileSystemKeyStoreManager method write.
@Override
public void write(ApiSpace space) throws IOException {
SpaceKeyStoreImpl iKeystore = (SpaceKeyStoreImpl) ((AbstractApiSpace) space).keystore();
Map<String, KeyPair> all = iKeystore.all();
if (all == null) {
return;
}
StringBuilder sb = new StringBuilder();
Writer wr = null;
try {
wr = new OutputStreamWriter(new FileOutputStream(keyStoreFile(space, true)));
int counter = 0;
Collection<KeyPair> values = all.values();
for (KeyPair skp : values) {
counter++;
wr.write(skp.accessKey());
wr.write(Lang.SEMICOLON);
wr.write(skp.secretKey());
wr.write(Lang.SEMICOLON);
if (skp.expiryDate() != null) {
wr.write(Lang.toString(skp.expiryDate(), Lang.DEFAULT_DATE_FORMAT));
}
Iterator<String> props = skp.properties();
if (props != null) {
wr.write(Lang.SEMICOLON);
int pCounter = 0;
while (props.hasNext()) {
pCounter++;
if (pCounter > 1) {
sb.append(Lang.COMMA);
}
String name = props.next();
sb.append(name).append(Lang.EQUALS).append(skp.property(name));
}
wr.write(sb.toString());
sb.setLength(0);
}
if (counter < values.size()) {
wr.write(Lang.ENDLN);
}
if (counter % flushRate == 0) {
wr.flush();
}
}
wr.flush();
sb.setLength(0);
} finally {
IOUtils.closeQuietly(wr);
}
}
use of com.bluenimble.platform.security.KeyPair in project serverless by bluenimble.
the class SpaceKeyStoreImpl method list.
@Override
public List<KeyPair> list(int offset, int length, ListFilter... filters) {
if (keys.isEmpty()) {
return null;
}
List<KeyPair> list = new ArrayList<KeyPair>();
int counter = 0;
Collection<KeyPair> kps = keys.values();
for (KeyPair kp : kps) {
if (counter < offset) {
continue;
}
counter++;
if (check(kp, filters)) {
list.add(kp);
}
if (list.size() == length) {
break;
}
}
return list;
}
use of com.bluenimble.platform.security.KeyPair in project serverless by bluenimble.
the class SpaceKeyStoreImpl method create.
@Override
public List<KeyPair> create(int pack, final Date expiryDate, final Map<String, Object> properties) throws SpaceKeyStoreException {
if (pack < 1) {
return null;
}
List<KeyPair> keys = null;
try {
keys = new ArrayList<KeyPair>();
for (int i = 0; i < pack; i++) {
final String[] aKeys = Lang.keys();
KeyPair skp = new KeyPair() {
private static final long serialVersionUID = -1855450550265796892L;
@Override
public String accessKey() {
return aKeys[0];
}
@Override
public String secretKey() {
return aKeys[1];
}
@Override
public Date expiryDate() {
return expiryDate;
}
@Override
public Iterator<String> properties() {
if (properties == null) {
return null;
}
return properties.keySet().iterator();
}
@Override
public Object property(String name) {
if (properties == null) {
return null;
}
return properties.get(name);
}
@Override
public JsonObject toJson() {
JsonObject out = new JsonObject();
out.set(KeyPair.Fields.AccessKey, accessKey()).set(KeyPair.Fields.SecretKey, secretKey());
if (expiryDate() != null) {
out.set(KeyPair.Fields.ExpiryDate, Lang.toUTC(expiryDate()));
}
if (properties != null) {
out.set(KeyPair.Fields.Properties, properties);
}
return out;
}
@Override
public String toString() {
return toJson().toString();
}
};
put(skp);
keys.add(skp);
}
} catch (Exception ex) {
throw new SpaceKeyStoreException(ex.getMessage(), ex);
}
return keys;
}
use of com.bluenimble.platform.security.KeyPair in project serverless by bluenimble.
the class DownloadRootKeysSpi method execute.
@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String paraphrase = (String) request.get(Spec.Paraphrase);
try {
KeyPair kp = api.space().getRootKeys();
JsonObject oKeys = new JsonObject();
oKeys.set(Output.Name, Json.getString(request.getNode(), ApiRequest.Fields.Node.Id) + " " + Json.getString(request.getNode(), ApiRequest.Fields.Node.Version));
oKeys.set(Output.Endpoint, request.getScheme() + "://" + request.getEndpoint() + Lang.SLASH + api.space().getNamespace() + Lang.SLASH + api.getNamespace());
oKeys.set(KeyPair.Fields.AccessKey, kp.accessKey());
oKeys.set(KeyPair.Fields.SecretKey, kp.secretKey());
oKeys.set(CommonSpec.Role, "SUPER");
ByteArrayOutputStream out = new ByteArrayOutputStream();
Json.encrypt(oKeys, paraphrase, out);
return new ApiByteArrayOutput(Output.KeysName + Lang.DOT + Output.KeysExt, Base64.encodeBase64(out.toByteArray()), ApiContentTypes.Stream, Output.KeysExt).set(ApiOutput.Defaults.Disposition, "attachment");
} catch (Exception e) {
throw new ApiServiceExecutionException(e.getMessage()).status(ApiResponse.FORBIDDEN);
}
}
Aggregations