@GET @Path("authenticate") @Produces(MediaType.APPLICATION_JSON) public List<AuthorizationNode> getAuthentication(@QueryParam("participantAddress") String participantAddress) { AuthorizationInfo authorizationInfo; try { authorizationInfo = getAccountabilityManager() .getAuthorization(provenanceId) .exceptionally(error -> null) .get(); } catch (InterruptedException | ExecutionException | AccountabilityException e) { LOGGER.error("Cannot authenticate participant {}. Reason: {}", participantAddress, e.getMessage()); throw createException(e); } if (Objects.nonNull(authorizationInfo)) { return authorizationInfo .getAuthorizationLineage(participantAddress) .orElseGet(ArrayList::new); } return new ArrayList<>(); }
@GET @Path("modelHistory") @Produces(MediaType.APPLICATION_JSON) public List<ModelProvenanceElement> getModelHistory() { ServiceTemplateId serviceTemplateId = new ServiceTemplateId(new QName(provenanceId)); String qNameWithComponentVersionOnly = VersionUtils.getQNameWithComponentVersionOnly(serviceTemplateId); try { return getAccountabilityManager() .getHistory(qNameWithComponentVersionOnly) .exceptionally(error -> null) .get(); } catch (InterruptedException | ExecutionException | AccountabilityException e) { LOGGER.error("Cannot get history of model. Reason: {}", e.getMessage()); throw createException(e); } }
@GET @Path("retrieveFile") @Produces(MediaType.TEXT_PLAIN) public Response retrieveFileFromImmutableStorage(@QueryParam("address") String address) { try (InputStream data = getAccountabilityManager().retrieveState(address).get()) { String fileAsString = IOUtils.toString(data, Charset.defaultCharset().toString()); return Response .ok(fileAsString, MediaType.TEXT_PLAIN) .build(); } catch (IOException | InterruptedException | ExecutionException | AccountabilityException e) { LOGGER.error("Cannot retrieve file ({}) from immutable storage. Reason: {}", address, e.getMessage()); throw createException(e); } } }
private boolean isValid(ImportMetaInformation metaInformation, Map<String, File> fileMap) throws ExecutionException, InterruptedException, AccountabilityException { ServiceTemplateId entryServiceTemplate = metaInformation.entryServiceTemplate; metaInformation.verificationMap = new HashMap<>(); if (Objects.nonNull(entryServiceTemplate)) { Properties props = RepositoryFactory.getRepository().getAccountabilityConfigurationManager().properties; AccountabilityManager accountabilityManager = AccountabilityManagerFactory.getAccountabilityManager(props); String provenanceIdentifier = VersionUtils.getQNameWithComponentVersionOnly(entryServiceTemplate); metaInformation.verificationMap = accountabilityManager .verify(provenanceIdentifier, "TOSCA-Metadata/TOSCA.meta", fileMap) .exceptionally(e -> { LOGGER.debug("accountabilityManager.verify completed exceptionally", e); return null; }) .get(); return metaInformation.verificationMap.values().stream().allMatch(v -> v == ProvenanceVerification.VERIFIED); } return false; }
@POST @Path("authorize") @Consumes(MediaType.APPLICATION_JSON) public String addParticipant(AuthorizationNode participant) { try { return getAccountabilityManager() .authorize(Util.URLdecode(Util.URLdecode(provenanceId)), participant.getAddress(), participant.getIdentity()) .exceptionally(error -> null) .get(); } catch (InterruptedException | ExecutionException | AccountabilityException e) { LOGGER.error("Cannot authorize participant {}. Reason: {}", participant.getAddress(), e.getMessage()); throw createException(e); } }
public CompletableFuture<String> writeCsarAndSaveManifestInProvenanceLayer(IRepository repository, DefinitionsChildId entryId, OutputStream out) throws IOException, RepositoryCorruptException, AccountabilityException, InterruptedException, ExecutionException { LocalDateTime start = LocalDateTime.now(); Properties props = repository.getAccountabilityConfigurationManager().properties; AccountabilityManager accountabilityManager = AccountabilityManagerFactory.getAccountabilityManager(props); Map<String, Object> exportConfiguration = new HashMap<>(); exportConfiguration.put(CsarExportConfiguration.INCLUDE_HASHES.name(), null); exportConfiguration.put(CsarExportConfiguration.STORE_IMMUTABLY.name(), null); String manifestString = this.writeCsar(repository, entryId, out, exportConfiguration); String qNameWithComponentVersionOnly = VersionUtils.getQNameWithComponentVersionOnly(entryId); LOGGER.debug("Preparing CSAR export (provenance) lasted {}", Duration.between(LocalDateTime.now(), start).toString()); return accountabilityManager.storeFingerprint(qNameWithComponentVersionOnly, manifestString); }
/** * Stores all files listed in the map in the immutable file storage, and updates the CsarContentProperties of each * file to contain its address in the aforementioned storage. * * @param filesToStore a map of the CsarContentProperties of all files to be stored in the CSAR and their contents. */ private void immutablyStoreRefFiles(Map<CsarContentProperties, CsarEntry> filesToStore, IRepository repository) throws AccountabilityException, ExecutionException, InterruptedException, IOException { Properties props = repository.getAccountabilityConfigurationManager().properties; AccountabilityManager manager = AccountabilityManagerFactory.getAccountabilityManager(props); Map<String, InputStream> filesMap = new HashMap<>(); for (Map.Entry<CsarContentProperties, CsarEntry> entry : filesToStore.entrySet()) { filesMap.put(entry.getKey().getPathInsideCsar(), entry.getValue().getInputStream()); } // store all files in immutable storage (already stored files will get their same old address) Map<String, String> addressMap = manager .storeState(filesMap) .get(); filesToStore.keySet().forEach((CsarContentProperties properties) -> { properties.setImmutableAddress(addressMap.get(properties.getPathInsideCsar())); }); }
public static AccountabilityManager getAccountabilityManager(Properties accountabilityConfiguration) throws AccountabilityException { boolean requiresRecreation = false; // if any of the relevant configurations is changed, we need a new instance! if (activeProperties != null) { requiresRecreation = checkIfChanged(accountabilityConfiguration); } if (Objects.isNull(accountabilityManager) || requiresRecreation) { BlockchainFactory.reset(); ImmutableStorageProviderFactory.reset(); // if there is an older accountability manager, we should shut it down if (!Objects.isNull(accountabilityManager)) { accountabilityManager.close(); } try { BlockchainAccess blockchain = BlockchainFactory.getBlockchainAccess(BlockchainFactory.AvailableBlockchains.ETHEREUM, accountabilityConfiguration); ImmutableStorageProvider storageProvider = ImmutableStorageProviderFactory.getStorageProvider(ImmutableStorageProviderFactory.AvailableImmutableStorages.SWARM, accountabilityConfiguration); accountabilityManager = new AccountabilityManagerImpl(blockchain, storageProvider); copyProperties(accountabilityConfiguration); } catch (BlockchainException e) { String msg = "Could not instantiate accountability layer: " + e.getMessage(); LOGGER.error(msg, e); throw new AccountabilityException(msg, e); } } return accountabilityManager; }
@GET @Path("downloadFile") @Produces(MediaType.APPLICATION_OCTET_STREAM) public Response downloadFileFromImmutableStorage(@QueryParam("address") String address, @QueryParam("filename") String filename) { StreamingOutput fileStream = output -> { try (InputStream data = getAccountabilityManager().retrieveState(address).get()) { IOUtils.copy(data, output); output.flush(); } catch (Exception e) { LOGGER.error("Cannot download file ({}) from immutable storage. Reason: {}", address, e.getMessage()); throw createException(e); } }; return Response .ok(fileStream, MediaType.APPLICATION_OCTET_STREAM) .header("content-disposition", "attachment; filename = " + filename) .build(); }
@GET @Path("fileHistory") @Produces(MediaType.APPLICATION_JSON) public List<FileProvenanceElement> getFileHistory(@QueryParam("fileId") String fileId) { ServiceTemplateId serviceTemplateId = new ServiceTemplateId(new QName(provenanceId)); String qNameWithComponentVersionOnly = VersionUtils.getQNameWithComponentVersionOnly(serviceTemplateId); Objects.requireNonNull(fileId); String fileIdDecoded = Util.URLdecode(fileId); try { return getAccountabilityManager() .getHistory(qNameWithComponentVersionOnly, fileIdDecoded) .exceptionally(error -> null) .get(); } catch (InterruptedException | ExecutionException | AccountabilityException e) { LOGGER.error("Cannot history of file {}. Reason: {}", fileId, e.getMessage()); throw createException(e); } }