/** * Returns the data stored in the secure store. * @param namespace The namespace this key belongs to. * @param name Name of the data element. * @return An object representing the securely stored data associated with the name. * @throws NamespaceNotFoundException If the specified namespace does not exist. * @throws NotFoundException If the key is not found in the store. * @throws IOException If there was a problem reading from the store. */ @Override public SecureStoreData get(String namespace, String name) throws Exception { checkNamespaceExists(namespace); String keyName = getKeyName(namespace, name); readLock.lock(); try { if (!keyStore.containsAlias(keyName)) { throw new NotFoundException(name + " not found in the secure store."); } Key key = keyStore.getKey(keyName, password); return deserialize(key.getEncoded()); } catch (NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException e) { throw new IOException("Unable to retrieve the key " + name, e); } finally { readLock.unlock(); } }
/** * Returns the metadata for the element identified by the given name. * The name must be of the format namespace + NAME_SEPARATOR + key name. * @param keyName Name of the element * @return An object representing the metadata associated with the element * @throws NotFoundException If the key was not found in the store. * @throws IOException If there was a problem in getting the key from the store */ private SecureStoreMetadata getSecureStoreMetadata(String keyName) throws Exception { String[] namespaceAndName = keyName.split(NAME_SEPARATOR); Preconditions.checkArgument(namespaceAndName.length == 2); String namespace = namespaceAndName[0]; String name = namespaceAndName[1]; readLock.lock(); try { if (!keyStore.containsAlias(keyName)) { throw new NotFoundException(new SecureKeyId(namespace, name)); } Key key = keyStore.getKey(keyName, password); return deserialize(key.getEncoded()).getMetadata(); } catch (NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException e) { throw new IOException("Unable to retrieve the metadata for " + name + " in namespace " + namespace, e); } finally { readLock.unlock(); } }