use of org.craftercms.studio.api.v2.service.security.internal.UserServiceInternal in project studio by craftercms.
the class GitContentRepository method getContent.
@Override
public InputStream getContent(String site, String path) throws ContentNotFoundException, CryptoException {
InputStream toReturn = null;
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
if (repo == null) {
throw new ContentNotFoundException("Repository not found for site " + site);
}
try {
RevTree tree = helper.getTreeForLastCommit(repo);
try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(path), tree)) {
// pick the first item in the list
if (tw != null && tw.getObjectId(0) != null) {
ObjectId id = tw.getObjectId(0);
ObjectLoader objectLoader = repo.open(id);
toReturn = objectLoader.openStream();
tw.close();
}
} catch (IOException e) {
logger.error("Error while getting content for file at site: " + site + " path: " + path, e);
}
} catch (IOException e) {
logger.error("Failed to create RevTree for site: " + site + " path: " + path, e);
}
return toReturn;
}
use of org.craftercms.studio.api.v2.service.security.internal.UserServiceInternal in project studio by craftercms.
the class GitContentRepository method copyContent.
@Override
public String copyContent(String site, String fromPath, String toPath) {
String commitId = null;
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
String gitFromPath = helper.getGitPath(fromPath);
String gitToPath = helper.getGitPath(toPath);
try (Git git = new Git(repo)) {
Path sourcePath = Paths.get(repo.getDirectory().getParent(), fromPath);
File sourceFile = sourcePath.toFile();
Path targetPath = Paths.get(repo.getDirectory().getParent(), toPath);
File targetFile = targetPath.toFile();
// Check if we're copying a single file or whole subtree
FileUtils.copyDirectory(sourceFile, targetFile);
// The operation is done on disk, now it's time to commit
git.add().addFilepattern(gitToPath).call();
CommitCommand commitCommand = git.commit().setOnly(gitFromPath).setOnly(gitToPath).setAuthor(helper.getCurrentUserIdent()).setCommitter(helper.getCurrentUserIdent()).setMessage(helper.getCommitMessage(REPO_COPY_CONTENT_COMMIT_MESSAGE).replaceAll(PATTERN_FROM_PATH, fromPath).replaceAll(PATTERN_TO_PATH, toPath));
RevCommit commit = retryingRepositoryOperationFacade.call(commitCommand);
commitId = commit.getName();
}
}
} catch (IOException | GitAPIException | ServiceLayerException | UserNotFoundException | CryptoException e) {
logger.error("Error while copying content for site: " + site + " fromPath: " + fromPath + " toPath: " + toPath + " newName: ");
} finally {
generalLockService.unlock(gitLockKey);
}
return commitId;
}
use of org.craftercms.studio.api.v2.service.security.internal.UserServiceInternal in project studio by craftercms.
the class GitContentRepository method deleteParentFolder.
private String deleteParentFolder(Git git, Path parentFolder, boolean wasPage) throws GitAPIException, CryptoException, IOException {
String parent = parentFolder.toString();
String toRet = parent;
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
String folderToDelete = helper.getGitPath(parent);
Path toDelete = Paths.get(git.getRepository().getDirectory().getParent(), parent);
if (toDelete.toFile().exists()) {
List<String> dirs = Files.walk(toDelete).filter(x -> !x.equals(toDelete)).filter(Files::isDirectory).map(y -> y.getFileName().toString()).collect(Collectors.toList());
List<String> files = Files.walk(toDelete, 1).filter(x -> !x.equals(toDelete)).filter(Files::isRegularFile).map(y -> y.getFileName().toString()).collect(Collectors.toList());
if (wasPage || (CollectionUtils.isEmpty(dirs) && (CollectionUtils.isEmpty(files) || files.size() < 2 && files.get(0).equals(EMPTY_FILE)))) {
if (CollectionUtils.isNotEmpty(dirs)) {
for (String child : dirs) {
Path childToDelete = Paths.get(folderToDelete, child);
deleteParentFolder(git, childToDelete, false);
RmCommand rmCommand = git.rm().addFilepattern(folderToDelete + FILE_SEPARATOR + child + FILE_SEPARATOR + "*").setCached(false);
retryingRepositoryOperationFacade.call(rmCommand);
}
}
if (CollectionUtils.isNotEmpty(files)) {
for (String child : files) {
git.rm().addFilepattern(folderToDelete + FILE_SEPARATOR + child).setCached(false).call();
}
}
}
}
return toRet;
}
use of org.craftercms.studio.api.v2.service.security.internal.UserServiceInternal in project studio by craftercms.
the class GitContentRepository method getContentSize.
@Override
public long getContentSize(final String site, final String path) {
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
RevTree tree = helper.getTreeForLastCommit(repo);
try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(path), tree)) {
if (tw != null && tw.getObjectId(0) != null) {
ObjectId id = tw.getObjectId(0);
ObjectLoader objectLoader = repo.open(id);
return objectLoader.getSize();
}
}
} catch (IOException | CryptoException e) {
logger.error("Error while getting content for file at site: " + site + " path: " + path, e);
}
return -1L;
}
use of org.craftercms.studio.api.v2.service.security.internal.UserServiceInternal in project studio by craftercms.
the class GitContentRepository method pullFromRemote.
@Override
public boolean pullFromRemote(String siteId, String remoteName, String remoteBranch) throws ServiceLayerException, InvalidRemoteUrlException, CryptoException {
logger.debug("Get remote data from database for remote " + remoteName + " and site " + siteId);
Map<String, String> params = new HashMap<String, String>();
params.put("siteId", siteId);
params.put("remoteName", remoteName);
RemoteRepository remoteRepository = remoteRepositoryDAO.getRemoteRepository(params);
logger.debug("Prepare pull command");
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(siteId, SANDBOX);
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
generalLockService.lock(gitLockKey);
try (Git git = new Git(repo)) {
PullResult pullResult = null;
PullCommand pullCommand = git.pull();
logger.debug("Set remote " + remoteName);
pullCommand.setRemote(remoteRepository.getRemoteName());
logger.debug("Set branch to be " + remoteBranch);
pullCommand.setRemoteBranchName(remoteBranch);
switch(remoteRepository.getAuthenticationType()) {
case NONE:
logger.debug("No authentication");
pullResult = pullCommand.call();
break;
case BASIC:
logger.debug("Basic authentication");
String hashedPassword = remoteRepository.getRemotePassword();
String password = encryptor.decrypt(hashedPassword);
pullCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteRepository.getRemoteUsername(), password));
pullResult = pullCommand.call();
break;
case TOKEN:
logger.debug("Token based authentication");
String hashedToken = remoteRepository.getRemoteToken();
String token = encryptor.decrypt(hashedToken);
pullCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(token, EMPTY));
pullResult = pullCommand.call();
break;
case PRIVATE_KEY:
logger.debug("Private key authentication");
final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
String hashedPrivateKey = remoteRepository.getRemotePrivateKey();
String privateKey = encryptor.decrypt(hashedPrivateKey);
tempKey.toFile().deleteOnExit();
pullCommand.setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(getSshSessionFactory(privateKey, tempKey));
}
});
pullResult = retryingRepositoryOperationFacade.call(pullCommand);
Files.delete(tempKey);
break;
default:
throw new ServiceLayerException("Unsupported authentication type " + remoteRepository.getAuthenticationType());
}
return pullResult != null ? pullResult.isSuccessful() : false;
} catch (InvalidRemoteException e) {
logger.error("Remote is invalid " + remoteName, e);
throw new InvalidRemoteUrlException();
} catch (GitAPIException e) {
logger.error("Error while pulling from remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
throw new ServiceLayerException("Error while pulling from remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
} catch (CryptoException | IOException e) {
throw new ServiceLayerException(e);
} finally {
generalLockService.unlock(gitLockKey);
}
}
Aggregations