use of org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException in project studio by craftercms.
the class GitRepositoryHelper method createSiteCloneRemoteGitRepo.
public boolean createSiteCloneRemoteGitRepo(String siteId, String sandboxBranch, String remoteName, String remoteUrl, String remoteBranch, boolean singleBranch, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey, boolean createAsOrphan) throws InvalidRemoteRepositoryException, InvalidRemoteRepositoryCredentialsException, RemoteRepositoryNotFoundException, ServiceLayerException {
boolean toRet = true;
// prepare a new folder for the cloned repository
Path siteSandboxPath = buildRepoPath(SANDBOX, siteId);
File localPath = siteSandboxPath.toFile();
localPath.delete();
logger.debug("Add user credentials if provided");
// then clone
logger.debug("Cloning from " + remoteUrl + " to " + localPath);
CloneCommand cloneCommand = Git.cloneRepository();
Git cloneResult = null;
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
generalLockService.lock(gitLockKey);
try {
final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
switch(authenticationType) {
case RemoteRepository.AuthenticationType.NONE:
logger.debug("No authentication");
break;
case RemoteRepository.AuthenticationType.BASIC:
logger.debug("Basic authentication");
cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteUsername, remotePassword));
break;
case RemoteRepository.AuthenticationType.TOKEN:
logger.debug("Token based authentication");
cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteToken, StringUtils.EMPTY));
break;
case RemoteRepository.AuthenticationType.PRIVATE_KEY:
logger.debug("Private key authentication");
tempKey.toFile().deleteOnExit();
cloneCommand.setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(getSshSessionFactory(remotePrivateKey, tempKey));
}
});
break;
default:
throw new ServiceLayerException("Unsupported authentication type " + authenticationType);
}
if (StringUtils.isNotEmpty(remoteBranch)) {
cloneCommand.setBranch(remoteBranch);
}
cloneResult = cloneCommand.setURI(remoteUrl).setDirectory(localPath).setRemote(remoteName).setCloneAllBranches(!singleBranch).call();
Files.deleteIfExists(tempKey);
Repository sandboxRepo = checkIfCloneWasOk(cloneResult, remoteName, remoteUrl);
sandboxRepo = optimizeRepository(sandboxRepo);
// Make repository orphan if needed
if (createAsOrphan) {
makeRepoOrphan(sandboxRepo, siteId);
}
sandboxes.put(siteId, sandboxRepo);
} catch (InvalidRemoteException e) {
logger.error("Invalid remote repository: " + remoteName + " (" + remoteUrl + ")", e);
throw new InvalidRemoteRepositoryException("Invalid remote repository: " + remoteName + " (" + remoteUrl + ")");
} catch (TransportException e) {
if (StringUtils.endsWithIgnoreCase(e.getMessage(), "not authorized")) {
logger.error("Bad credentials or read only repository: " + remoteName + " (" + remoteUrl + ")", e);
throw new InvalidRemoteRepositoryCredentialsException("Bad credentials or read only repository: " + remoteName + " (" + remoteUrl + ") for username " + remoteUsername, e);
} else {
logger.error("Remote repository not found: " + remoteName + " (" + remoteUrl + ")", e);
throw new RemoteRepositoryNotFoundException("Remote repository not found: " + remoteName + " (" + remoteUrl + ")");
}
} catch (GitAPIException | IOException | UserNotFoundException e) {
logger.error("Error while creating repository for site with path" + siteSandboxPath.toString(), e);
toRet = false;
} finally {
generalLockService.unlock(gitLockKey);
if (cloneResult != null) {
cloneResult.close();
}
}
return toRet;
}
Aggregations