/** * Creates a secure key * * @param secureKeyId {@link SecureKeyId} secure key name * @param keyCreateRequest {@link SecureKeyCreateRequest} * @throws IOException if a network error occurred * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server * @throws SecureKeyAlreadyExistsException if the secure key already exists * @throws NamespaceNotFoundException if namespace is not found */ public void createKey(SecureKeyId secureKeyId, SecureKeyCreateRequest keyCreateRequest) throws IOException, UnauthenticatedException, AlreadyExistsException, NamespaceNotFoundException, UnauthorizedException { URL url = config.resolveNamespacedURLV3(secureKeyId.getParent(), getSecureKeyPath(secureKeyId)); HttpResponse response = restClient.execute(HttpMethod.PUT, url, GSON.toJson(keyCreateRequest), null, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_CONFLICT); if (response.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) { throw new SecureKeyAlreadyExistsException(secureKeyId); } if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new NamespaceNotFoundException(secureKeyId.getParent()); } }
/** * Get the metadata associated with the given secure key * * @param secureKeyId {@link SecureKeyId} secure key name * @return {@link SecureStoreMetadata} metadata associated with the key * @throws IOException if a network error occurred * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server * @throws SecureKeyNotFoundException if secure key or the namespace is not found */ public SecureStoreMetadata getKeyMetadata(SecureKeyId secureKeyId) throws IOException, UnauthenticatedException, SecureKeyNotFoundException, UnauthorizedException { URL url = config.resolveNamespacedURLV3(secureKeyId.getParent(), String.format("%s/metadata", getSecureKeyPath(secureKeyId))); HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND); if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new SecureKeyNotFoundException(secureKeyId); } return GSON.fromJson(response.getResponseBodyAsString(), SecureStoreMetadata.class); }
/** * Delete the secure key * @param secureKeyId {@link SecureKeyId} secure key name * @throws IOException if a network error occurred * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server * @throws SecureKeyNotFoundException if secure key or the namespace is not found */ public void deleteKey(SecureKeyId secureKeyId) throws IOException, UnauthenticatedException, SecureKeyNotFoundException, UnauthorizedException { URL url = config.resolveNamespacedURLV3(secureKeyId.getParent(), getSecureKeyPath(secureKeyId)); HttpResponse response = restClient.execute(HttpMethod.DELETE, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND); if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new SecureKeyNotFoundException(secureKeyId); } }
/** * Fetch the data associated with the given secure key * * @param secureKeyId {@link SecureKeyId} secure key name * @return the secure data in a utf-8 encoded byte array * @throws IOException if a network error occurred * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server * @throws SecureKeyNotFoundException if secure key or the namespace is not found */ public String getData(SecureKeyId secureKeyId) throws IOException, UnauthenticatedException, SecureKeyNotFoundException, UnauthorizedException { URL url = config.resolveNamespacedURLV3(secureKeyId.getParent(), getSecureKeyPath(secureKeyId)); HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND); if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { throw new SecureKeyNotFoundException(secureKeyId); } return response.getResponseBodyAsString(); }