示例#1
0
        /// <summary>
        /// Получить изменения репозитория
        /// </summary>
        private void FetchRepository(string repoPath, UsernamePasswordCredentials credentials)
        {
            try
            {
                using var repo = new Repository(repoPath);

                var fetchOptions = new FetchOptions
                {
                    Prune = true,
                    CredentialsProvider = (_url, _user, _cred) => credentials
                };

                _logger.LogInformation($"Fetching started: \"{repoPath}\"");

                string logMessage = "";
                foreach (var remote in repo.Network.Remotes)
                {
                    var refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
                    Commands.Fetch(repo, remote.Name, refSpecs, fetchOptions, logMessage);
                }

                _logger.LogInformation($"Fetching ended: \"{repoPath}\". Message: \"{logMessage}\"");
            }
            catch (Exception ex)
            {
                _logger.LogError($"Fetching error! Repository: \"{repoPath}\". Message: {ex.Message}");
            }
        }
 protected internal virtual void PreemptivelySetAuthCredentials(HttpClient httpClient
                                                                )
 {
     // if the URL contains user info AND if this a DefaultHttpClient
     // then preemptively set the auth credentials
     if (url.GetUserInfo() != null)
     {
         if (url.GetUserInfo().Contains(":") && !url.GetUserInfo().Trim().Equals(":"))
         {
             string[]    userInfoSplit = url.GetUserInfo().Split(":");
             Credentials creds         = new UsernamePasswordCredentials(URIUtils.Decode(userInfoSplit
                                                                                         [0]), URIUtils.Decode(userInfoSplit[1]));
             if (httpClient is DefaultHttpClient)
             {
                 DefaultHttpClient        dhc            = (DefaultHttpClient)httpClient;
                 MessageProcessingHandler preemptiveAuth = new _MessageProcessingHandler_185(creds
                                                                                             );
                 dhc.AddRequestInterceptor(preemptiveAuth, 0);
             }
         }
         else
         {
             Log.W(Database.Tag, "RemoteRequest Unable to parse user info, not setting credentials"
                   );
         }
     }
 }
示例#3
0
        public static AuthenticationHeaderValue CreateBasicAuthHeaderValue(
            this UsernamePasswordCredentials credentials)
        {
            var encodedCredentials = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(credentials.User + ":" + credentials.Password));

            return(new AuthenticationHeaderValue("Basic", encodedCredentials));
        }
                private HttpWebResponse GetResponseWithRedirects()
                {
                    HttpWebRequest  request  = CreateWebRequest(EndpointUrl, IsPost, ContentType);
                    HttpWebResponse response = null;

                    for (int i = 0; i < MAX_REDIRECTS; i++)
                    {
                        if (IsPost && postBuffer.Length > 0)
                        {
                            postBuffer.Seek(0, SeekOrigin.Begin);

                            using (Stream requestStream = request.GetRequestStream())
                            {
                                postBuffer.WriteTo(requestStream);
                            }
                        }

                        try
                        {
                            response = (HttpWebResponse)request.GetResponse();
                        }
                        catch (WebException ex)
                        {
                            response = ex.Response as HttpWebResponse;
                            if (response.StatusCode == HttpStatusCode.Unauthorized)
                            {
                                Credentials cred;
                                int         ret = SmartTransport.AcquireCredentials(out cred, null, typeof(UsernamePasswordCredentials));
                                if (ret != 0)
                                {
                                    throw new InvalidOperationException("dunno");
                                }

                                request = CreateWebRequest(EndpointUrl, IsPost, ContentType);
                                UsernamePasswordCredentials userpass = (UsernamePasswordCredentials)cred;
                                request.Credentials = new NetworkCredential(userpass.Username, userpass.Password);
                                continue;
                            }

                            // rethrow if it's not 401
                            throw ex;
                        }

                        if (response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect)
                        {
                            request = CreateWebRequest(response.Headers["Location"], IsPost, ContentType);
                            continue;
                        }


                        break;
                    }

                    if (response == null)
                    {
                        throw new Exception("Too many redirects");
                    }

                    return(response);
                }
示例#5
0
        /**
         * Upload the latest local changes to GitHub.com
         */
        private static void GitPush(LibGit2Sharp.Repository repo)
        {
            // Credentials are needed to push up to the server
            // I am using a token I generates
            LibGit2Sharp.Credentials credentials = new UsernamePasswordCredentials()
            {
                Username = accessToken,
                Password = string.Empty
            };

            // Push this to remote using token authentication
            Remote remote  = repo.Network.Remotes["origin"];
            var    options = new PushOptions();

            // Token Auth works by the username being a token and the password being blank.
            options.CredentialsProvider = (_url, _user, _cred) =>
                                          new UsernamePasswordCredentials {
                Username = accessToken, Password = string.Empty
            };
            try
            {
                repo.Network.Push(remote, @"refs/heads/master", options);
            }
            catch (LibGit2Sharp.NonFastForwardException nff)
            {
                IEnumerable <string> refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
                Commands.Fetch(repo, remote.Name, refSpecs, null, "");
                GitPush(repo);
            }
        }
        private static IGitRepositoryContextFactory GetRepositoryFactory(bool isRemote, string workingDir, Context context)
        {
            IGitRepositoryContextFactory gitRepoFactory = null;

            if (isRemote)
            {
                // clone repo from the remote url
                var cloneRepoArgs = new GitRemoteRepositoryContextFactory.RemoteRepoArgs();
                cloneRepoArgs.Url = context.Repository.Url;
                var credentials = new UsernamePasswordCredentials();

                credentials.Username = context.Repository.Username;
                credentials.Password = context.Repository.Password;

                cloneRepoArgs.Credentials     = credentials;
                cloneRepoArgs.DestinationPath = workingDir;

                Log.WriteLine("Cloning a git repo from {0}", cloneRepoArgs.Url);
                gitRepoFactory = new GitRemoteRepositoryContextFactory(cloneRepoArgs);
            }
            else
            {
                gitRepoFactory = new GitLocalRepositoryContextFactory(workingDir);
            }

            return(gitRepoFactory);
        }
        /// <summary>
        /// Клонировать репозиторий
        /// </summary>
        private void CloneRepository(string repoUrl, string repoPath, UsernamePasswordCredentials credentials)
        {
            try
            {
                if (!Directory.Exists(repoPath))
                {
                    Directory.CreateDirectory(repoPath);
                }

                var cloneOptions = new CloneOptions
                {
                    IsBare = false,
                    CredentialsProvider = (_url, _user, _cred) => credentials
                };

                _logger.LogInformation($"Cloning started: \"{repoUrl}\"");

                Repository.Clone(repoUrl, repoPath, cloneOptions);

                _logger.LogInformation($"Cloning ended: \"{repoUrl}\"");
            }
            catch (Exception ex)
            {
                _logger.LogError($"Clonning error! Repository: \"{repoUrl}\". Message: {ex.Message}");
            }
        }
示例#8
0
        public static void Commit(string folder, string path, string content, string username, string name, string password)
        {
            using (var repo = new Repository(folder))
            {
                var paths = new List <string>();
                paths.Add(folder);
                paths.AddRange(path.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries));
                var path2 = Path.Combine(paths.ToArray());
                Directory.CreateDirectory(Path.GetDirectoryName(path2));
                File.WriteAllText(path2, content);
                repo.Index.Add(path.Substring(1));
                repo.Commit($"Updated {Path.GetFileName(path2)}",
                            new Signature(username, username, DateTimeOffset.UtcNow),
                            new Signature(username, username, DateTimeOffset.UtcNow));

                if (repo.Network.Remotes["origin"] != null)
                {
                    PushOptions co = new PushOptions()
                    {
                    };

                    if (name != null && password != null)
                    {
                        Credentials ca = new UsernamePasswordCredentials()
                        {
                            Username = name, Password = password
                        };
                        co.CredentialsProvider = (_url, _user, _cred) => ca;
                    }

                    repo.Network.Push(repo.Network.Remotes["origin"], "refs/heads/master:refs/heads/master", co);
                }
            }
        }
        private string CloneRepo(string repoUrl)
        {
            var credentials = new UsernamePasswordCredentials
            {
                Username = _configuration.GetValue <string>("Gitlab:Username"),
                Password = _configuration.GetValue <string>("Gitlab:Password")
            };

            var folderName = repoUrl.Replace("http://", "").Replace("https://", "").Replace(" ", "").Remove(0, 1)
                             .Replace("gitlab.com", "");
            var path = Path.Combine(Directory.GetCurrentDirectory(), "gitToJarTemp", folderName);

            if (Directory.Exists(path))
            {
                var directory = new DirectoryInfo(path);
                SetAttributesNormal(directory);
                directory.Delete(true);
            }

            var cloneOptions = new CloneOptions
            {
                BranchName          = "master",
                Checkout            = true,
                CredentialsProvider = (url, user, cred) => credentials,
                RecurseSubmodules   = true
            };

            Repository.Clone(repoUrl, path, cloneOptions);
            return(path);
        }
        /// <summary>
        /// Pull изменений репозитория
        /// </summary>
        private void PullRepository(string repoPath, UsernamePasswordCredentials credentials)
        {
            try
            {
                using var repo = new Repository(repoPath);

                var pullOptions = new PullOptions
                {
                    FetchOptions = new FetchOptions
                    {
                        Prune = true,
                        CredentialsProvider = (_url, _user, _cred) => credentials
                    }
                };

                var signature = new Signature(
                    new Identity(_repositoriesConfig.MergeUserName, _repositoriesConfig.MergeUserEmail),
                    DateTimeOffset.Now);

                _logger.LogInformation($"Pulling started: \"{repoPath}\"");

                Commands.Pull(repo, signature, pullOptions);

                _logger.LogInformation($"Pulling ended: \"{repoPath}\"");
            }
            catch (Exception ex)
            {
                _logger.LogError($"Pulling error! Repository: \"{repoPath}\". Message: {ex.Message}");
            }
        }
示例#11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GitRepositoryManager" /> class.
        /// </summary>
        /// <param name="username">The Git credentials username.</param>
        /// <param name="password">The Git credentials password.</param>
        /// <param name="gitRepoUrl">The Git repo URL.</param>
        /// <param name="localFolder">The full path to local folder.</param>
        public GitRepositoryManager(string username, string password, string gitRepoUrl, string localFolder,
                                    string authorname,
                                    string authoremail,
                                    string committername,
                                    string committeremail)
        {
            var folder = new DirectoryInfo(localFolder);

            if (!folder.Exists)
            {
                throw new Exception(string.Format("Source folder '{0}' does not exist.", _localFolder));
            }

            _localFolder = folder;

            _credentials = new UsernamePasswordCredentials
            {
                Username = username,
                Password = password
            };

            _repoSource = gitRepoUrl;

            _authorName     = authorname;
            _authorEmail    = authoremail;
            _committerName  = committername;
            _committerEmail = committeremail;
        }
示例#12
0
        public T CreateProxy <T>(ExecutionIdentity ident) where T : IDisposable
        {
            Credentials creds = null;

            if (ident == ExecutionIdentity.CurrentUser)
            {
                creds = new UsernamePasswordCredentials(_username, _password);
            }
            else if (ident == ExecutionIdentity.System)
            {
                var username = SharedTestHelpers.ConfigurationHelper.ADMIN_USERNAME;
                var password = SharedTestHelpers.ConfigurationHelper.DEFAULT_PASSWORD;
                creds = new UsernamePasswordCredentials(username, password);
            }

            if (creds == null)
            {
                throw new NotSupportedException($"{ident} is not supported in the Test Service Mangager.");
            }

            var serviceFactorySettings = new ServiceFactorySettings(GetServicesURL(), this.GetKeplerUrl(), creds);
            var serviceFactory         = new Relativity.Services.ServiceProxy.ServiceFactory(serviceFactorySettings);
            T   proxy = serviceFactory.CreateProxy <T>();

            return(proxy);
        }
示例#13
0
        public void CloneRepo()
        {
            var          scd         = BuildSelfCleaningDirectory();
            CloneOptions options     = new CloneOptions();
            var          credentials = new UsernamePasswordCredentials();

            credentials.Username         = Constants.Identity.Name;
            credentials.Password         = "******";
            options.CredentialsProvider += (url, fromUrl, types) => credentials;

            string clonedRepoPath = Repository.Clone(testUrl, scd.DirectoryPath, options);
            string file           = Path.Combine(scd.DirectoryPath, "testpush.txt");
            var    rbg            = new RandomBufferGenerator(30000);

            using (var repo = new Repository(clonedRepoPath)) {
                for (int i = 0; i < 1; i++)
                {
                    var          network      = repo.Network.Remotes.First();
                    FetchOptions fetchOptions = new FetchOptions();
                    fetchOptions.CredentialsProvider += (url, fromUrl, types) => credentials;
                    repo.Fetch(network.Name, fetchOptions);

                    File.WriteAllBytes(file, rbg.GenerateBufferFromSeed(30000));
                    repo.Stage(file);
                    Signature author = Constants.Signature;

                    Commit      commit      = repo.Commit($"Here's a commit {i + 1} i made!", author, author);
                    PushOptions pushOptions = new PushOptions();
                    pushOptions.CredentialsProvider += (url, fromUrl, types) => credentials;
                    repo.Network.Push(repo.Branches["master"], pushOptions);
                }
            }
        }
示例#14
0
        public void Pull(UsernamePasswordCredentials gitCredentials, Signature signature)
        {
            Repository repo = new Repository(RepositoryPath);
            // Get git credentials
            CredentialsHandler credentialsHandler = new CredentialsHandler(
                (url, usernameFromUrl, types) => gitCredentials);
            // Pull repository
            PullOptions pullOptions = new PullOptions
            {
                FetchOptions = new FetchOptions()
            };

            pullOptions.FetchOptions.CredentialsProvider = credentialsHandler;
            Signature merger = signature;

            Commands.Pull(repo, merger, pullOptions);

            // Compress changes to zip file
            File.Delete(ZipFilePath);
            using (ZipArchive zipArchive = ZipFile.Open(ZipFilePath, ZipArchiveMode.Create))
            {
                foreach (string f in DirSearch(RepositoryPath))
                {
                    zipArchive.CreateEntryFromFile(f, f.Replace(RepositoryPath + @"\", ""));
                }
            }
        }
        public async Task <int> Run(RepositorySettings repository,
                                    UsernamePasswordCredentials gitCreds,
                                    Identity userIdentity,
                                    SettingsContainer settings)
        {
            try
            {
                var repo = await BuildGitRepositorySpec(repository, gitCreds.Username);

                if (repo == null)
                {
                    return(0);
                }

                if (!await _repositoryFilter.ContainsDotNetProjects(repository))
                {
                    return(0);
                }

                var tempFolder = _folderFactory.UniqueTemporaryFolder();
                var git        = new LibGit2SharpDriver(_logger, tempFolder, gitCreds, userIdentity);

                var updatesDone = await _repositoryUpdater.Run(git, repo, settings);

                tempFolder.TryDelete();
                return(updatesDone);
            }
            catch (Exception ex)
            {
                _logger.Error($"Failed on repo {repository.RepositoryName}", ex);
                return(0);
            }
        }
示例#16
0
        public async Task <int> Run(RepositorySettings repository,
                                    UsernamePasswordCredentials gitCreds,
                                    Identity userIdentity,
                                    SettingsContainer settings)
        {
            try
            {
                var repo = await BuildGitRepositorySpec(repository, gitCreds.Username);

                if (repo == null)
                {
                    return(0);
                }

                // should perform the remote check for "is this a .NET repo"
                // (and also not a github fork)
                // only when we have multiple remote repos
                // otherwise it's ok to work locally, and check there
                if (!(settings.SourceControlServerSettings.Scope == ServerScope.Repository || repository.IsLocalRepo))
                {
                    var remoteRepoContainsDotNet = await _repositoryFilter.ContainsDotNetProjects(repository);

                    if (!remoteRepoContainsDotNet)
                    {
                        return(0);
                    }
                }

                IFolder folder;
                if (repository.IsLocalRepo)
                {
                    folder = new Folder(_logger, new DirectoryInfo(repository.RemoteInfo.LocalRepositoryUri.AbsolutePath));
                    settings.WorkingFolder = new Folder(_logger, new DirectoryInfo(repository.RemoteInfo.WorkingFolder.AbsolutePath));
                    repo.DefaultBranch     = repository.RemoteInfo.BranchName;
                    repo.Remote            = repository.RemoteInfo.RemoteName;
                }
                else
                {
                    folder = _folderFactory.UniqueTemporaryFolder();
                    settings.WorkingFolder = folder;
                }

                var git = new LibGit2SharpDriver(_logger, folder, gitCreds, userIdentity);

                var updatesDone = await _repositoryUpdater.Run(git, repo, settings);

                if (!repository.IsLocalRepo)
                {
                    folder.TryDelete();
                }

                return(updatesDone);
            }
            catch (Exception ex)
            {
                _logger.Error($"Failed on repo {repository.RepositoryName}", ex);
                return(1);
            }
        }
示例#17
0
        public void Static()
        {
            var credentials = new UsernamePasswordCredentials();

            using var factory = new StaticCredentialsFactory(credentials);

            Assert.That(factory.NewCredentials(), Is.SameAs(credentials));
        }
示例#18
0
 public LocalGitRepositoryUpdater(string gitHubPersonalAccessToken)
 {
     _usernamePasswordCredentials = new UsernamePasswordCredentials
     {
         Username = gitHubPersonalAccessToken,
         Password = string.Empty,
     };
 }
示例#19
0
 public E2ETestBase(GitBucketFixture fixture, ITestOutputHelper output)
 {
     GitBucketFixture = fixture;
     Output           = output;
     Credentials      = new UsernamePasswordCredentials {
         Username = GitBucketDefaults.Owner, Password = GitBucketDefaults.Password
     };
     WorkingDir = Path.Combine(Path.GetDirectoryName(Assembly.GetAssembly(typeof(E2ETestBase)) !.Location) !, Guid.NewGuid().ToString());
 }
示例#20
0
 public SvnClient(UsernamePasswordCredentials credentials, Agent agent, string svnExePath, ILogSink log, CancellationToken?cancellationToken = null)
 {
     this.execOps           = new Lazy <IRemoteProcessExecuter>(() => agent.GetService <IRemoteProcessExecuter>());
     this.userName          = credentials?.UserName;
     this.password          = credentials?.Password;
     this.svnExePath        = AH.CoalesceString(svnExePath, RemoteMethods.GetEmbeddedSvnExePath(agent));
     this.log               = log ?? (ILogSink)Logger.Null;
     this.cancellationToken = cancellationToken ?? CancellationToken.None;
 }
示例#21
0
 public GITOperations()
 {
     Credentials = new UsernamePasswordCredentials
     {
         Username = this.UserName,
         Password = this.Word_pAss
     };
     FileFullPath = new DirectoryInfo(RepoFolder);
 }
示例#22
0
        /// <summary>
        /// Provide the credentials from this object.
        /// </summary>
        /// <param name="url">The url to provide credentials for.</param>
        /// <param name="usernameFromUrl">The user to provide credentials for.</param>
        /// <param name="types">The type of credentials to provide.</param>
        /// <returns>The credentials to use.</returns>
        private Credentials ProvideCredentials(string url, string usernameFromUrl, SupportedCredentialTypes types)
        {
            UsernamePasswordCredentials c = new UsernamePasswordCredentials();

            c.Username = Username;
            c.Password = Password;

            return(c);
        }
示例#23
0
 internal SvnClient(UsernamePasswordCredentials credentials, IRemoteProcessExecuter execOps, string svnExePath, ILogSink log, CancellationToken?cancellationToken = null)
 {
     this.execOps           = new Lazy <IRemoteProcessExecuter>(() => execOps);
     this.userName          = credentials?.UserName;
     this.password          = credentials?.Password;
     this.svnExePath        = svnExePath;
     this.log               = log ?? (ILogSink)Logger.Null;
     this.cancellationToken = cancellationToken ?? CancellationToken.None;
 }
示例#24
0
        public GitSourceControl(ILogger logger, UserInfo userInfo)
        {
            _logger   = logger;
            _userInfo = userInfo;

            _credentials = new UsernamePasswordCredentials
            {
                Username = _userInfo.Username, Password = _userInfo.Password
            };
        }
示例#25
0
        string GetGitInfoFromUrl()
        {
            var gitDirectory = Path.Combine(arguments.TargetPath, "_dynamicrepository", ".git");

            if (Directory.Exists(gitDirectory))
            {
                Logger.WriteInfo(string.Format("Deleting existing .git folder from '{0}' to force new checkout from url", gitDirectory));

                DeleteHelper.DeleteGitRepository(gitDirectory);
            }

            Credentials credentials    = null;
            var         authentication = arguments.Authentication;

            if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password))
            {
                Logger.WriteInfo(string.Format("Setting up credentials using name '{0}'", authentication.Username));

                credentials = new UsernamePasswordCredentials
                {
                    Username = authentication.Username,
                    Password = authentication.Password
                };
            }

            Logger.WriteInfo(string.Format("Retrieving git info from url '{0}'", arguments.TargetUrl));

            Repository.Clone(arguments.TargetUrl, gitDirectory,
                             new CloneOptions {
                IsBare = true, Checkout = false, Credentials = credentials
            });

            if (!string.IsNullOrWhiteSpace(arguments.TargetBranch))
            {
                // Normalize (download branches) before using the branch
                GitHelper.NormalizeGitDirectory(gitDirectory, arguments.Authentication);

                using (var repository = new Repository(gitDirectory))
                {
                    var targetBranchName = string.Format("refs/heads/{0}", arguments.TargetBranch);
                    if (!string.Equals(repository.Head.CanonicalName, targetBranchName))
                    {
                        Logger.WriteInfo(string.Format("Switching to branch '{0}'", arguments.TargetBranch));

                        repository.Refs.UpdateTarget("HEAD", targetBranchName);
                    }

                    repository.CheckoutFilesIfExist("NextVersion.txt");
                }
            }

            DynamicGitRepositoryPath = gitDirectory;

            return(gitDirectory);
        }
示例#26
0
        private CredentialsHandler GetSourceCredentialsHandler()
        {
            var credentials = new UsernamePasswordCredentials()
            {
                Username = SourceControlUser,
                Password = SourceControlPass,
            };
            CredentialsHandler credentialHandler = (_url, _user, _cred) => credentials;

            return(credentialHandler);
        }
示例#27
0
        public async Task <int> Run(RepositorySettings repository,
                                    UsernamePasswordCredentials gitCreds,
                                    Identity userIdentity,
                                    SettingsContainer settings)
        {
            try
            {
                var repo = await BuildGitRepositorySpec(repository, gitCreds.Username);

                if (repo == null)
                {
                    return(0);
                }

                if (!repository.IsLocalRepo) // The updaters will do the check for the local files, and they know what file types they can handle.
                {
                    if (!await _repositoryFilter.ContainsDotNetProjects(repository))
                    {
                        return(0);
                    }
                }

                IFolder folder;
                if (repository.IsLocalRepo)
                {
                    folder = new Folder(_logger, new DirectoryInfo(repository.RemoteInfo.LocalRepositoryUri.AbsolutePath));
                    settings.WorkingFolder = new Folder(_logger, new DirectoryInfo(repository.RemoteInfo.WorkingFolder.AbsolutePath));
                    repo.DefaultBranch     = repository.RemoteInfo.BranchName;
                    repo.Remote            = repository.RemoteInfo.RemoteName;
                }
                else
                {
                    folder = _folderFactory.UniqueTemporaryFolder();
                    settings.WorkingFolder = folder;
                }

                var git = new LibGit2SharpDriver(_logger, folder, gitCreds, userIdentity);

                var updatesDone = await _repositoryUpdater.Run(git, repo, settings);

                if (!repository.IsLocalRepo)
                {
                    folder.TryDelete();
                }

                return(updatesDone);
            }
            catch (Exception ex)
            {
                _logger.Error($"Failed on repo {repository.RepositoryName}", ex);
                return(1);
            }
        }
示例#28
0
        /// <summary>
        /// Обновление всех репозиториев
        /// </summary>
        public async Task UpdateAllRepositories()
        {
            _logger.LogInformation($"Updating repositories started");

            // TODO: Если токена нет то использовать логин/пароль Credentials = new UsernamePasswordCredentials { Username = info.Username, Password = info.Password }
            var defaultCreds = new UsernamePasswordCredentials
            {
                Username = "******",
                Password = _repositoriesConfig.GitlabAuthToken
            };

            var reposInfo = _repositoriesConfig.ReposInfo
                            .Select(info => new
            {
                RepoUrl     = info.Url,
                RepoPath    = @$ "{_repositoriesConfig.ReposFolder}/{info.LocalPath}",
                Credentials = defaultCreds
            }).ToList();

            await Task.Run(() =>
            {
                Parallel.ForEach(reposInfo, new ParallelOptions {
                    MaxDegreeOfParallelism = MAX_PARALLEL
                }, info =>
                {
                    if (!Repository.IsValid(info.RepoPath))
                    {
                        try
                        {
                            CloneRepository(info.RepoUrl, info.RepoPath, info.Credentials);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError($"Clonning error! Repository: \"{info.RepoUrl}\". Message: {ex.Message}");
                        }
                    }
                    else
                    {
                        try
                        {
                            PullRepository(info.RepoPath, info.Credentials);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError($"Pulling error! Repository: \"{info.RepoPath}\". Message: {ex.Message}");
                        }
                    }
                });
            });

            _logger.LogInformation($"Updating repositories ended");
        }
示例#29
0
        static void CloneRepository(string repositoryUrl, string gitDirectory, AuthenticationInfo authentication)
        {
            Credentials credentials = null;

            if (authentication != null)
            {
                if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password))
                {
                    Logger.WriteInfo($"Setting up credentials using name '{authentication.Username}'");

                    credentials = new UsernamePasswordCredentials
                    {
                        Username = authentication.Username,
                        Password = authentication.Password
                    };
                }
            }


            try
            {
                using (Logger.IndentLog($"Cloning repository from url '{repositoryUrl}'"))
                {
                    var cloneOptions = new CloneOptions
                    {
                        Checkout            = false,
                        CredentialsProvider = (url, usernameFromUrl, types) => credentials
                    };

                    var returnedPath = Repository.Clone(repositoryUrl, gitDirectory, cloneOptions);
                    Logger.WriteInfo($"Returned path after repository clone: {returnedPath}");
                }
            }
            catch (LibGit2SharpException ex)
            {
                var message = ex.Message;
                if (message.Contains("401"))
                {
                    throw new Exception("Unauthorised: Incorrect username/password");
                }
                if (message.Contains("403"))
                {
                    throw new Exception("Forbidden: Possbily Incorrect username/password");
                }
                if (message.Contains("404"))
                {
                    throw new Exception("Not found: The repository was not found");
                }

                throw new Exception("There was an unknown problem with the Git repository you provided", ex);
            }
        }
示例#30
0
        /// <summary>
        /// Обновление репозитория
        /// </summary>
        public async Task UpdateRepository(string repoPath)
        {
            // TODO: Если токена нет то использовать логин/пароль Credentials = new UsernamePasswordCredentials { Username = info.Username, Password = info.Password }
            var defaultCreds = new UsernamePasswordCredentials
            {
                Username = "******",
                Password = _repositoriesConfig.GitlabAuthToken
            };

            var allRepos = await GetAllRepositories();

            var repo = allRepos
                       .Where(rp => rp.Url == HttpUtility.UrlDecode(repoPath))
                       .Select(rp => new
            {
                Name        = GenerateRepoNameByWebUI(rp.WebUI),
                RepoUrl     = rp.Url,
                RepoPath    = @$ "{_repositoriesConfig.ReposFolder}/{GenerateRepoNameByWebUI(rp.WebUI)}",
                Credentials = defaultCreds
            })
                       .First();

            _logger.LogInformation($"Updating repository {repo.Name} started");

            await Task.Run(() =>
            {
                if (!Repository.IsValid(repo.RepoPath))
                {
                    try
                    {
                        CloneRepository(repo.RepoUrl, repo.RepoPath, repo.Credentials);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception($"Clonning error! Repository: \"{repo.RepoUrl}\". Message: {ex.Message}");
                    }
                }
                else
                {
                    try
                    {
                        PullRepository(repo.RepoPath, repo.Credentials);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception($"Pulling error! Repository: \"{repo.RepoPath}\". Message: {ex.Message}");
                    }
                }
            });

            _logger.LogInformation($"Updating repository {repo.Name} ended");
        }
        public async Task AuthenticateWithUsernamePasswordAsyncTest()
        {
            // Create a new user
            var user = await UserHelper.CreateNewUserAsync();

            // Authenticate the user
            var creds = new UsernamePasswordCredentials(user.Username, user.Password);
            var userSession = await creds.AuthenticateAsync();

            // Asserts
            Assert.IsNotNull(userSession, "User session is null.");
            Assert.IsFalse( string.IsNullOrWhiteSpace(userSession.UserToken), "User token is null or whitespace.");
            Console.WriteLine("user token: {0}", userSession.UserToken);
            Assert.IsNotNull(userSession.LoggedInUser, "Logged in user is null.");
            Assert.AreEqual(userSession.LoggedInUser.Id, user.Id, "Logged in user ids do not match as expected.");
        }
        public async Task GetLoggedInUserTest()
        {
            // Create a new user
            var newUser = await UserHelper.CreateNewUserAsync();
            // Authenticate
            var creds = new UsernamePasswordCredentials(newUser.Username, newUser.Password);
            var userSession = await App.LoginAsync(creds);
            Assert.IsNotNull(userSession);
            Assert.IsFalse(string.IsNullOrWhiteSpace(userSession.UserToken));
            Assert.IsNotNull(userSession.LoggedInUser);

            
            var loggedInUser = await Users.GetLoggedInUserAsync();
            Assert.IsNotNull(loggedInUser);
            Assert.IsTrue(loggedInUser.Id == userSession.LoggedInUser.Id);
            
        }
 public virtual void Run()
 {
     running = true;
     HttpClient httpClient;
     if (client == null)
     {
         // This is a race condition that can be reproduced by calling cbpuller.start() and cbpuller.stop()
         // directly afterwards.  What happens is that by the time the Changetracker thread fires up,
         // the cbpuller has already set this.client to null.  See issue #109
         Log.W(Log.TagChangeTracker, "%s: ChangeTracker run() loop aborting because client == null"
             , this);
         return;
     }
     if (mode == ChangeTracker.ChangeTrackerMode.Continuous)
     {
         // there is a failing unit test for this, and from looking at the code the Replication
         // object will never use Continuous mode anyway.  Explicitly prevent its use until
         // it is demonstrated to actually work.
         throw new RuntimeException("ChangeTracker does not correctly support continuous mode"
             );
     }
     httpClient = client.GetHttpClient();
     backoff = new ChangeTrackerBackoff();
     while (running)
     {
         Uri url = GetChangesFeedURL();
         if (usePOST)
         {
             HttpPost postRequest = new HttpPost(url.ToString());
             postRequest.SetHeader("Content-Type", "application/json");
             StringEntity entity;
             try
             {
                 entity = new StringEntity(ChangesFeedPOSTBody());
             }
             catch (UnsupportedEncodingException e)
             {
                 throw new RuntimeException(e);
             }
             postRequest.SetEntity(entity);
             request = postRequest;
         }
         else
         {
             request = new HttpGet(url.ToString());
         }
         AddRequestHeaders(request);
         // Perform BASIC Authentication if needed
         bool isUrlBasedUserInfo = false;
         // If the URL contains user info AND if this a DefaultHttpClient then preemptively set the auth credentials
         string userInfo = url.GetUserInfo();
         if (userInfo != null)
         {
             isUrlBasedUserInfo = true;
         }
         else
         {
             if (authenticator != null)
             {
                 AuthenticatorImpl auth = (AuthenticatorImpl)authenticator;
                 userInfo = auth.AuthUserInfo();
             }
         }
         if (userInfo != null)
         {
             if (userInfo.Contains(":") && !userInfo.Trim().Equals(":"))
             {
                 string[] userInfoElements = userInfo.Split(":");
                 string username = isUrlBasedUserInfo ? URIUtils.Decode(userInfoElements[0]) : userInfoElements
                     [0];
                 string password = isUrlBasedUserInfo ? URIUtils.Decode(userInfoElements[1]) : userInfoElements
                     [1];
                 Credentials credentials = new UsernamePasswordCredentials(username, password);
                 if (httpClient is DefaultHttpClient)
                 {
                     DefaultHttpClient dhc = (DefaultHttpClient)httpClient;
                     MessageProcessingHandler preemptiveAuth = new _MessageProcessingHandler_285(credentials
                         );
                     dhc.AddRequestInterceptor(preemptiveAuth, 0);
                 }
             }
             else
             {
                 Log.W(Log.TagChangeTracker, "RemoteRequest Unable to parse user info, not setting credentials"
                     );
             }
         }
         try
         {
             string maskedRemoteWithoutCredentials = GetChangesFeedURL().ToString();
             maskedRemoteWithoutCredentials = maskedRemoteWithoutCredentials.ReplaceAll("://.*:.*@"
                 , "://---:---@");
             Log.V(Log.TagChangeTracker, "%s: Making request to %s", this, maskedRemoteWithoutCredentials
                 );
             HttpResponse response = httpClient.Execute(request);
             StatusLine status = response.GetStatusLine();
             if (status.GetStatusCode() >= 300 && !Utils.IsTransientError(status))
             {
                 Log.E(Log.TagChangeTracker, "%s: Change tracker got error %d", this, status.GetStatusCode
                     ());
                 this.error = new HttpResponseException(status.GetStatusCode(), status.GetReasonPhrase
                     ());
                 Stop();
             }
             HttpEntity entity = response.GetEntity();
             InputStream input = null;
             if (entity != null)
             {
                 try
                 {
                     input = entity.GetContent();
                     if (mode == ChangeTracker.ChangeTrackerMode.LongPoll)
                     {
                         // continuous replications
                         IDictionary<string, object> fullBody = Manager.GetObjectMapper().ReadValue<IDictionary
                             >(input);
                         bool responseOK = ReceivedPollResponse(fullBody);
                         if (mode == ChangeTracker.ChangeTrackerMode.LongPoll && responseOK)
                         {
                             Log.V(Log.TagChangeTracker, "%s: Starting new longpoll", this);
                             backoff.ResetBackoff();
                             continue;
                         }
                         else
                         {
                             Log.W(Log.TagChangeTracker, "%s: Change tracker calling stop (LongPoll)", this);
                             Stop();
                         }
                     }
                     else
                     {
                         // one-shot replications
                         JsonFactory jsonFactory = Manager.GetObjectMapper().GetJsonFactory();
                         JsonParser jp = jsonFactory.CreateJsonParser(input);
                         while (jp.NextToken() != JsonToken.StartArray)
                         {
                         }
                         // ignore these tokens
                         while (jp.NextToken() == JsonToken.StartObject)
                         {
                             IDictionary<string, object> change = (IDictionary)Manager.GetObjectMapper().ReadValue
                                 <IDictionary>(jp);
                             if (!ReceivedChange(change))
                             {
                                 Log.W(Log.TagChangeTracker, "Received unparseable change line from server: %s", change
                                     );
                             }
                         }
                         Log.W(Log.TagChangeTracker, "%s: Change tracker calling stop (OneShot)", this);
                         Stop();
                         break;
                     }
                     backoff.ResetBackoff();
                 }
                 finally
                 {
                     try
                     {
                         entity.ConsumeContent();
                     }
                     catch (IOException)
                     {
                     }
                 }
             }
         }
         catch (Exception e)
         {
             if (!running && e is IOException)
             {
             }
             else
             {
                 // in this case, just silently absorb the exception because it
                 // frequently happens when we're shutting down and have to
                 // close the socket underneath our read.
                 Log.E(Log.TagChangeTracker, this + ": Exception in change tracker", e);
             }
             backoff.SleepAppropriateAmountOfTime();
         }
     }
     Log.V(Log.TagChangeTracker, "%s: Change tracker run loop exiting", this);
 }
        public async Task ValidateSessionTest()
        {
            // Create a new user
            var newUser = await UserHelper.CreateNewUserAsync();
            // Authenticate
            var creds = new UsernamePasswordCredentials(newUser.Username, newUser.Password);
            var userSession = await creds.AuthenticateAsync();
            Assert.IsNotNull(userSession);
            Assert.IsFalse(string.IsNullOrWhiteSpace(userSession.UserToken));
            Assert.IsNotNull(userSession.LoggedInUser);

            var isValid = await UserSession.IsValidAsync(userSession.UserToken);
            Assert.IsTrue(isValid);
        }
示例#35
0
		public virtual void Run()
		{
			running = true;
			HttpClient httpClient;
			if (client == null)
			{
				// This is a race condition that can be reproduced by calling cbpuller.start() and cbpuller.stop()
				// directly afterwards.  What happens is that by the time the Changetracker thread fires up,
				// the cbpuller has already set this.client to null.  See issue #109
				Log.W(Database.Tag, this + ": ChangeTracker run() loop aborting because client == null"
					);
				return;
			}
			if (mode == ChangeTracker.ChangeTrackerMode.Continuous)
			{
				// there is a failing unit test for this, and from looking at the code the Replication
				// object will never use Continuous mode anyway.  Explicitly prevent its use until
				// it is demonstrated to actually work.
				throw new RuntimeException("ChangeTracker does not correctly support continuous mode"
					);
			}
			httpClient = client.GetHttpClient();
			backoff = new ChangeTrackerBackoff();
			while (running)
			{
				Uri url = GetChangesFeedURL();
				request = new HttpGet(url.ToString());
				AddRequestHeaders(request);
				// if the URL contains user info AND if this a DefaultHttpClient
				// then preemptively set the auth credentials
				if (url.GetUserInfo() != null)
				{
					Log.V(Database.Tag, this + ": url.getUserInfo(): " + url.GetUserInfo());
					if (url.GetUserInfo().Contains(":") && !url.GetUserInfo().Trim().Equals(":"))
					{
						string[] userInfoSplit = url.GetUserInfo().Split(":");
						Credentials creds = new UsernamePasswordCredentials(URIUtils.Decode(userInfoSplit
							[0]), URIUtils.Decode(userInfoSplit[1]));
						if (httpClient is DefaultHttpClient)
						{
							DefaultHttpClient dhc = (DefaultHttpClient)httpClient;
							MessageProcessingHandler preemptiveAuth = new _MessageProcessingHandler_221(creds
								);
							dhc.AddRequestInterceptor(preemptiveAuth, 0);
						}
					}
					else
					{
						Log.W(Database.Tag, this + ": ChangeTracker Unable to parse user info, not setting credentials"
							);
					}
				}
				try
				{
					string maskedRemoteWithoutCredentials = GetChangesFeedURL().ToString();
					maskedRemoteWithoutCredentials = maskedRemoteWithoutCredentials.ReplaceAll("://.*:.*@"
						, "://---:---@");
					Log.V(Database.Tag, this + ": Making request to " + maskedRemoteWithoutCredentials
						);
					HttpResponse response = httpClient.Execute(request);
					StatusLine status = response.GetStatusLine();
					if (status.GetStatusCode() >= 300)
					{
						Log.E(Database.Tag, this + ": Change tracker got error " + Sharpen.Extensions.ToString
							(status.GetStatusCode()));
						string msg = string.Format(status.ToString());
						this.error = new CouchbaseLiteException(msg, new Status(status.GetStatusCode()));
						Stop();
					}
					HttpEntity entity = response.GetEntity();
					InputStream input = null;
					if (entity != null)
					{
						try
						{
							input = entity.GetContent();
							if (mode == ChangeTracker.ChangeTrackerMode.LongPoll)
							{
								// continuous replications
								IDictionary<string, object> fullBody = Manager.GetObjectMapper().ReadValue<IDictionary
									>(input);
								bool responseOK = ReceivedPollResponse(fullBody);
								if (mode == ChangeTracker.ChangeTrackerMode.LongPoll && responseOK)
								{
									Log.V(Database.Tag, this + ": Starting new longpoll");
									backoff.ResetBackoff();
									continue;
								}
								else
								{
									Log.W(Database.Tag, this + ": Change tracker calling stop (LongPoll)");
									Stop();
								}
							}
							else
							{
								// one-shot replications
								JsonFactory jsonFactory = Manager.GetObjectMapper().GetJsonFactory();
								JsonParser jp = jsonFactory.CreateJsonParser(input);
								while (jp.NextToken() != JsonToken.StartArray)
								{
								}
								// ignore these tokens
								while (jp.NextToken() == JsonToken.StartObject)
								{
									IDictionary<string, object> change = (IDictionary)Manager.GetObjectMapper().ReadValue
										<IDictionary>(jp);
									if (!ReceivedChange(change))
									{
										Log.W(Database.Tag, string.Format("Received unparseable change line from server: %s"
											, change));
									}
								}
								Log.W(Database.Tag, this + ": Change tracker calling stop (OneShot)");
								Stop();
								break;
							}
							backoff.ResetBackoff();
						}
						finally
						{
							try
							{
								entity.ConsumeContent();
							}
							catch (IOException)
							{
							}
						}
					}
				}
				catch (Exception e)
				{
					if (!running && e is IOException)
					{
					}
					else
					{
						// in this case, just silently absorb the exception because it
						// frequently happens when we're shutting down and have to
						// close the socket underneath our read.
						Log.E(Database.Tag, this + ": Exception in change tracker", e);
					}
					backoff.SleepAppropriateAmountOfTime();
				}
			}
			Log.V(Database.Tag, this + ": Change tracker run loop exiting");
		}
示例#36
0
		protected internal virtual void PreemptivelySetAuthCredentials(HttpClient httpClient
			)
		{
			// if the URL contains user info AND if this a DefaultHttpClient
			// then preemptively set the auth credentials
			if (url.GetUserInfo() != null)
			{
				if (url.GetUserInfo().Contains(":") && !url.GetUserInfo().Trim().Equals(":"))
				{
					string[] userInfoSplit = url.GetUserInfo().Split(":");
					Credentials creds = new UsernamePasswordCredentials(URIUtils.Decode(userInfoSplit
						[0]), URIUtils.Decode(userInfoSplit[1]));
					if (httpClient is DefaultHttpClient)
					{
						DefaultHttpClient dhc = (DefaultHttpClient)httpClient;
						MessageProcessingHandler preemptiveAuth = new _MessageProcessingHandler_185(creds
							);
						dhc.AddRequestInterceptor(preemptiveAuth, 0);
					}
				}
				else
				{
					Log.W(Database.Tag, "RemoteRequest Unable to parse user info, not setting credentials"
						);
				}
			}
		}
 protected internal virtual void PreemptivelySetAuthCredentials(HttpClient httpClient
     )
 {
     bool isUrlBasedUserInfo = false;
     string userInfo = url.GetUserInfo();
     if (userInfo != null)
     {
         isUrlBasedUserInfo = true;
     }
     else
     {
         if (authenticator != null)
         {
             AuthenticatorImpl auth = (AuthenticatorImpl)authenticator;
             userInfo = auth.AuthUserInfo();
         }
     }
     if (userInfo != null)
     {
         if (userInfo.Contains(":") && !userInfo.Trim().Equals(":"))
         {
             string[] userInfoElements = userInfo.Split(":");
             string username = isUrlBasedUserInfo ? URIUtils.Decode(userInfoElements[0]) : userInfoElements
                 [0];
             string password = isUrlBasedUserInfo ? URIUtils.Decode(userInfoElements[1]) : userInfoElements
                 [1];
             Credentials credentials = new UsernamePasswordCredentials(username, password);
             if (httpClient is DefaultHttpClient)
             {
                 DefaultHttpClient dhc = (DefaultHttpClient)httpClient;
                 MessageProcessingHandler preemptiveAuth = new _MessageProcessingHandler_286(credentials
                     );
                 dhc.AddRequestInterceptor(preemptiveAuth, 0);
             }
         }
         else
         {
             Log.W(Log.TagRemoteRequest, "RemoteRequest Unable to parse user info, not setting credentials"
                 );
         }
     }
 }