Search in sources :

Example 1 with KeyPair

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;
}
Also used : JsonArray(com.bluenimble.platform.json.JsonArray) KeyPair(com.bluenimble.platform.security.KeyPair) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileInputStream(java.io.FileInputStream)

Example 2 with KeyPair

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);
    }
}
Also used : KeyPair(com.bluenimble.platform.security.KeyPair) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Example 3 with KeyPair

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;
}
Also used : KeyPair(com.bluenimble.platform.security.KeyPair) ArrayList(java.util.ArrayList)

Example 4 with KeyPair

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;
}
Also used : KeyPair(com.bluenimble.platform.security.KeyPair) SpaceKeyStoreException(com.bluenimble.platform.security.SpaceKeyStoreException) JsonObject(com.bluenimble.platform.json.JsonObject) SpaceKeyStoreException(com.bluenimble.platform.security.SpaceKeyStoreException)

Example 5 with KeyPair

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);
    }
}
Also used : KeyPair(com.bluenimble.platform.security.KeyPair) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ApiByteArrayOutput(com.bluenimble.platform.api.impls.ApiByteArrayOutput) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException)

Aggregations

KeyPair (com.bluenimble.platform.security.KeyPair)12 JsonObject (com.bluenimble.platform.json.JsonObject)6 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)5 ApiSpace (com.bluenimble.platform.api.ApiSpace)5 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)4 SpaceKeyStoreException (com.bluenimble.platform.security.SpaceKeyStoreException)4 JsonArray (com.bluenimble.platform.json.JsonArray)3 HashMap (java.util.HashMap)3 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)2 Role (com.bluenimble.platform.apis.mgm.Role)2 EncryptionProviderException (com.bluenimble.platform.security.EncryptionProviderException)2 Api (com.bluenimble.platform.api.Api)1 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)1 ApiResourcesManagerException (com.bluenimble.platform.api.ApiResourcesManagerException)1 DescribeOption (com.bluenimble.platform.api.DescribeOption)1 ApiByteArrayOutput (com.bluenimble.platform.api.impls.ApiByteArrayOutput)1 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)1 CommonSpec (com.bluenimble.platform.apis.mgm.CommonSpec)1 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1