@SuppressWarnings("unchecked") private String getVersion(String version, Bundle bundle) { if (null != version && version.trim().length() > 0) { return version; } BundleVersion latestBundleVersion = null; Query q = entityManager.createNamedQuery(BundleVersion.QUERY_FIND_LATEST_BY_BUNDLE_ID); q.setParameter("bundleId", bundle.getId()); List<BundleVersion> list = q.getResultList(); if (list.size() > 0) { if (list.size() == 1) { latestBundleVersion = list.get(0); } else { throw new RuntimeException("Bundle [" + bundle.getName() + "] (id=" + bundle.getId() + ") has more than 1 'latest' version. This should not happen - aborting"); } } // note - this is the same algo used by ResourceClientProxy in updatebackingContent (for a resource) String latestVersion = latestBundleVersion != null ? latestBundleVersion.getVersion() : null; String newVersion = NumberUtil.autoIncrementVersion(latestVersion); return newVersion; }
@Override public void deleteBundleDestination(Subject subject, int destinationId) throws Exception { BundleDestination doomed = this.entityManager.find(BundleDestination.class, destinationId); if (null == doomed) { return; } checkDeployBundleAuthz(subject, doomed.getBundle().getId(), doomed.getGroup().getId()); // deployments replace other deployments and have a self-referring FK. The deployments // need to be removed in a way that will ensure that a replaced deployment is not removed // prior to the replacer. To do this we'll just blanket update all the doomed deployments // to break the FK dependency with nulls. Query q = entityManager.createNamedQuery(BundleDeployment.QUERY_UPDATE_FOR_DESTINATION_REMOVE); q.setParameter("destinationId", destinationId); q.executeUpdate(); entityManager.flush(); entityManager.remove(doomed); }
int bundleId = bundleVersion.getBundle().getId();
@Override public BundleFile addBundleFileViaPackageVersion(Subject subject, int bundleVersionId, String name, int packageVersionId) throws Exception { if (null == name || "".equals(name.trim())) { throw new IllegalArgumentException("Invalid bundleFileName: " + name); } BundleVersion bundleVersion = entityManager.find(BundleVersion.class, bundleVersionId); if (null == bundleVersion) { throw new IllegalArgumentException("Invalid bundleVersionId: " + bundleVersionId); } PackageVersion packageVersion = entityManager.find(PackageVersion.class, packageVersionId); if (null == packageVersion) { throw new IllegalArgumentException("Invalid packageVersionId: " + packageVersionId); } // Check authorization checkCreateBundleVersionAuthz(subject, bundleVersion.getBundle().getId()); // With all the plumbing in place, create and persist the BundleFile. Tie it to the Package if the caller // wants this BundleFile pinned to the most recent version. BundleFile bundleFile = new BundleFile(); bundleFile.setBundleVersion(bundleVersion); bundleFile.setPackageVersion(packageVersion); entityManager.persist(bundleFile); return bundleFile; }
@Override public String getBundleDeploymentName(Subject subject, int bundleDestinationId, int bundleVersionId, int prevDeploymentId) { BundleDestination bundleDestination = entityManager.find(BundleDestination.class, bundleDestinationId); if (null == bundleDestination) { throw new IllegalArgumentException("Invalid bundleDestinationId: " + bundleDestinationId); } BundleVersion bundleVersion = null; BundleDeployment prevDeployment = null; if (bundleVersionId > 0) { bundleVersion = entityManager.find(BundleVersion.class, bundleVersionId); if (null == bundleVersion) { throw new IllegalArgumentException("Invalid bundleVersionId: " + bundleVersionId); } } else if (prevDeploymentId > 0) { prevDeployment = entityManager.find(BundleDeployment.class, prevDeploymentId); if (null == prevDeployment) { throw new IllegalArgumentException("Invalid prevDeploymentId: " + prevDeploymentId); } } else { throw new IllegalArgumentException("Must specify either a valid bundleVersionId [" + bundleVersionId + "] or prevDeploymentId [" + prevDeploymentId + "]"); } if (bundleVersion != null) { checkDeployBundleAuthz(subject, bundleVersion.getBundle().getId(), bundleDestination.getGroup().getId()); } return getBundleDeploymentNameImpl(subject, bundleDestination, bundleVersion, prevDeployment); }
@Override public BundleDeployment createBundleDeployment(Subject subject, int bundleVersionId, int bundleDestinationId, String description, Configuration configuration) throws Exception { BundleVersion bundleVersion = entityManager.find(BundleVersion.class, bundleVersionId); if (null == bundleVersion) { throw new IllegalArgumentException("Invalid bundleVersionId: " + bundleVersionId); } BundleDestination bundleDestination = entityManager.find(BundleDestination.class, bundleDestinationId); if (null == bundleDestination) { throw new IllegalArgumentException("Invalid bundleDestinationId: " + bundleDestinationId); } checkDeployBundleAuthz(subject, bundleVersion.getBundle().getId(), bundleDestination.getGroup().getId()); String name = getBundleDeploymentNameImpl(subject, bundleDestination, bundleVersion, null); return this.createBundleDeploymentImpl(subject, bundleVersion, bundleDestination, name, description, configuration); }
q.setParameter("bundleId", bundle.getId()); @SuppressWarnings("unchecked") List<Object[]> list = (List<Object[]>) q.getResultList(); entityManager.flush(); q = entityManager.createNamedQuery(BundleVersion.UPDATE_VERSION_ORDER_BY_BUNDLE_ID); q.setParameter("bundleId", bundle.getId()); q.setParameter("versionOrder", versionOrder); q.executeUpdate();
@Override @TransactionAttribute(TransactionAttributeType.NEVER) public BundleFile addBundleFileViaURL(Subject subject, int bundleVersionId, String name, String version, Architecture architecture, String bundleFileUrl, String userName, String password) throws Exception { // Check authorization prior to performing any file download BundleVersion bundleVersion = entityManager.find(BundleVersion.class, bundleVersionId); if (null == bundleVersion) { throw new IllegalArgumentException("Invalid bundleVersionId: " + bundleVersionId); } checkCreateBundleVersionAuthz(subject, bundleVersion.getBundle().getId()); File file = null; FileInputStream fis = null; try { file = downloadFile(bundleFileUrl, userName, password); fis = new FileInputStream(file); return addBundleFile(subject, bundleVersionId, name, version, architecture, fis); } finally { if (fis != null) { safeClose(fis); } if (file != null) { file.delete(); } } }
@Override public void deleteBundleDeployment(Subject subject, int bundleDeploymentId) throws Exception { BundleDeployment doomed = this.entityManager.find(BundleDeployment.class, bundleDeploymentId); if (null == doomed) { return; } checkDeployBundleAuthz(subject, doomed.getBundleVersion().getBundle().getId(), doomed.getDestination() .getGroup().getId()); // only allow deployments to be deleted if they are not started or finished if (BundleDeploymentStatus.PENDING == doomed.getStatus() || BundleDeploymentStatus.SUCCESS == doomed.getStatus() || BundleDeploymentStatus.FAILURE == doomed.getStatus() || BundleDeploymentStatus.MIXED == doomed.getStatus()) { // change the pointer like in the linked list (ie. when removing B from A -> B -> C, it should result // in following situation: A -> C) Query q = entityManager.createNamedQuery(BundleDeployment.QUERY_UPDATE_FOR_DEPLOYMENT_REMOVE); q.setParameter("bundleId", doomed.getId()); q.executeUpdate(); entityManager.flush(); entityManager.remove(doomed); } else { throw new IllegalArgumentException("Can not delete deployment with status [" + doomed.getStatus() + "]"); } }
@Override @RequiredPermission(Permission.MANAGE_BUNDLE_GROUPS) public BundleGroup createBundleGroup(Subject subject, BundleGroup bundleGroup) throws Exception { String name = bundleGroup.getName(); if (null == name || "".equals(name.trim())) { throw new IllegalArgumentException("Invalid bundleGroupName: " + name); } BundleGroupCriteria c = new BundleGroupCriteria(); c.addFilterName(name); c.setStrict(true); if (!bundleManager.findBundleGroupsByCriteria(subject, c).isEmpty()) { throw new IllegalArgumentException("Invalid bundleGroupName, bundle group already exists with name: " + name); } entityManager.persist(bundleGroup); Set<Bundle> bundles = bundleGroup.getBundles(); if (null != bundles) { int[] bundleIds = new int[bundles.size()]; int i = 0; for (Bundle b : bundles) { bundleIds[i++] = b.getId(); } assignBundlesToBundleGroups(subject, new int[] { bundleGroup.getId() }, bundleIds); } return bundleGroup; }
checkCreateBundleVersionAuthz(subject, bundleVersion.getBundle().getId());
@Override public BundleGroup updateBundleGroup(Subject subject, BundleGroup bundleGroup) throws Exception { BundleGroup attachedBundleGroup = entityManager.find(BundleGroup.class, bundleGroup.getId()); if (attachedBundleGroup == null) { throw new IllegalArgumentException("Cannot update " + bundleGroup + ", because no bundle group exists with id [" + bundleGroup.getId() + "]."); } // First update the simple fields and the permissions. attachedBundleGroup.setName(bundleGroup.getName()); attachedBundleGroup.setDescription(bundleGroup.getDescription()); Set<Bundle> newBundles = bundleGroup.getBundles(); if (newBundles != null) { // wrap in new HashSet to avoid ConcurrentModificationExceptions. Set<Bundle> currentBundles = attachedBundleGroup.getBundles(); Set<Bundle> BundlesToRemove = new HashSet<Bundle>(currentBundles); for (Bundle bg : newBundles) { BundlesToRemove.remove(bg); } for (Bundle bg : BundlesToRemove) { attachedBundleGroup.removeBundle(bg); } for (Bundle bg : newBundles) { Bundle attachedBundle = entityManager.find(Bundle.class, bg.getId()); attachedBundleGroup.addBundle(attachedBundle); } } // Fetch the lazy Set for the return attachedBundleGroup.getBundles().size(); return attachedBundleGroup; }
checkCreateBundleVersionAuthz(subject, bundleVersion.getBundle().getId());
checkCreateBundleVersionAuthz(subject, bundleVersion.getBundle().getId());
checkDeployBundleAuthz(subject, bundle.getId(), groupId);
private void deleteBundles(final Subject subject, ResourceType resourceType) throws Exception { BundleType bundleType = resourceType.getBundleType(); if (bundleType == null) { return; } BundleCriteria criteria = new BundleCriteria(); criteria.addFilterBundleTypeId(bundleType.getId()); //Use CriteriaQuery to automatically chunk/page through criteria query results CriteriaQueryExecutor<Bundle, BundleCriteria> queryExecutor = new CriteriaQueryExecutor<Bundle, BundleCriteria>() { @Override public PageList<Bundle> execute(BundleCriteria criteria) { return bundleMgr.findBundlesByCriteria(subject, criteria); } }; CriteriaQuery<Bundle, BundleCriteria> bundles = new CriteriaQuery<Bundle, BundleCriteria>(criteria, queryExecutor); for (Bundle bundle : bundles) { bundleMgr.deleteBundle(subject, bundle.getId()); } }
checkCreateBundleVersionAuthz(subject, bundle.getId());
checkDeployBundleAuthz(subject, liveDeployment.getBundleVersion().getBundle().getId(), liveDeployment .getDestination().getGroup().getId());
BundleDestinationCriteria destinationCriteria = new BundleDestinationCriteria(); destinationCriteria.addFilterBundleId(bundle.getId());
@Override public PageList<Bundle> findBundlesByCriteria(Subject subject, BundleCriteria criteria) { CriteriaQueryGenerator generator = new CriteriaQueryGenerator(subject, criteria); if (!authorizationManager.hasGlobalPermission(subject, Permission.VIEW_BUNDLES)) { generator.setAuthorizationBundleFragment(CriteriaQueryGenerator.AuthorizationTokenType.BUNDLE, subject.getId(), null); } CriteriaQueryRunner<Bundle> queryRunner = new CriteriaQueryRunner<Bundle>(criteria, generator, entityManager); PageList<Bundle> result = queryRunner.execute(); // If asking for optional data that the subject may not be able to see then ensure that the optional // data is filtered appropriately. In this case only destinations viewable by the subject can be returned. // The result currently holds bundles viewable by the caller, but the bundle may have been deployed to // destinations for which the user does not have access to the destination's resource group. (BZ 694741) if (!result.isEmpty() && criteria.isInventoryManagerRequired() && !authorizationManager.isInventoryManager(subject)) { // this works because findBundleDestinationsByCriteria() authorizes against resource group associations for (Bundle bundle : result) { if (bundle.getDestinations().isEmpty()) { continue; } BundleDestinationCriteria destinationCriteria = new BundleDestinationCriteria(); destinationCriteria.clearPaging(); //disable paging as the code assumes all the results will be returned. destinationCriteria.addFilterBundleId(bundle.getId()); List<BundleDestination> destinations = findBundleDestinationsByCriteria(subject, destinationCriteria); entityManager.detach(bundle); // make sure the narrowed set of destinations does not get persisted bundle.setDestinations(destinations); } } return result; }