Beispiel #1
0
        private Cache ReadCacheFile(string cacheFilePath)
        {
            Cache cache;

            using (var xmlReader = XmlReader.Create(cacheFilePath))
            {
                cache = (Cache)CacheXmlSerializer.Deserialize(xmlReader);
            }

            cache.Validate();

            if (cache.InstallationId != InstallationId.ToString())
            {
                return(null);
            }

            foreach (CachePluginInfo pluginInfo in cache.PluginInfos)
            {
                if (!System.IO.File.Exists(pluginInfo.PluginFile))
                {
                    return(null);
                }

                if (System.IO.File.GetLastWriteTimeUtc(pluginInfo.PluginFile) > pluginInfo.PluginFileModificationTime)
                {
                    return(null);
                }
            }

            return(cache);
        }
 public Installation(InstallationId id, AccountType targetType, AccountLogin targetAccount, IEnumerable <RepositoryFullName> repositories)
 {
     Id            = id;
     TargetType    = targetType;
     TargetAccount = targetAccount;
     Repositories  = repositories;
 }
 /// <inheritdoc />
 public TenantId GetTenantFor(InstallationId installationId)
 {
     if (_mapping.TryGetValue(installationId, out var tenantId))
     {
         return(tenantId);
     }
     return(TenantId.Unknown);
 }
        /// <inheritdoc />
        public void AssociateTenantWithInstallation(InstallationId installationId, TenantId tenantId)
        {
            _mapping[installationId] = tenantId;
            _mapping.AddOrUpdate(installationId, tenantId, (k, v) => tenantId);

            var content = _serializer.ToJson(_mapping);

            _fileSystem.WriteAllText(_filePath, content);
        }
        /// <inheritdoc />
        public void DisassociateTenantFromInstallation(InstallationId installationId)
        {
            Guid removed;

            if (_mapping.TryRemove(installationId, out removed))
            {
                var content = _serializer.ToJson(_mapping);
                _fileSystem.WriteAllText(_filePath, content);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Unregisteres an installation from this Applicaition
        /// </summary>
        /// <param name="id">The id of the <see cref="Installation" /> being unregistered</param>
        public void UnregisterInstallation(InstallationId id)
        {
            if (!_installations.ContainsKey(id))
            {
                throw new Exception("Installation is not registered!");
            }

            Apply(new InstallationUnregistered(
                      id,
                      _installations[id].Repositories.Select(_ => _.Value)
                      ));
        }
Beispiel #7
0
        /// <summary>
        /// Refreshes the list of repositories with this installation
        /// </summary>
        /// <param name="id">The id of the installation being refreshed</param>
        /// <param name="repositories">A collection of repositories that are associated with this <see cref="Installation" /></param>
        public void RefreshInstallationRepositories(InstallationId id, IEnumerable <RepositoryFullName> repositories)
        {
            if (!_installations.ContainsKey(id))
            {
                throw new Exception("Installation not registered on update!");
            }

            Apply(new InstallationRepositoriesRefreshed(
                      id,
                      repositories.Select(_ => (string)_)
                      ));
        }
Beispiel #8
0
        /// <summary>
        /// Updates the repositories that are associated with this <see cref="Installation" />
        /// </summary>
        /// <param name="id">The id of the <see cref="Installation" /> being updated</param>
        /// <param name="repositoriesAdded">A collection of repositories being added</param>
        /// <param name="repositoriesRemoved">A collection of repositories to be removed</param>
        public void UpdateInstallationRepositories(InstallationId id, IEnumerable <RepositoryFullName> repositoriesAdded, IEnumerable <RepositoryFullName> repositoriesRemoved)
        {
            if (!_installations.ContainsKey(id))
            {
                throw new Exception("Installation not registered on update!");
            }

            Apply(new InstallationRepositoriesUpdateReceived(
                      id,
                      repositoriesAdded.Select(fullName => (string)fullName),
                      repositoriesRemoved.Select(fullName => (string)fullName)
                      ));
        }
Beispiel #9
0
        InstallationRepositories GetOrCreateInstallationById(InstallationId id)
        {
            var installation = _repositoryForInstallationRepositories.GetById(id);

            if (installation == null)
            {
                installation = new InstallationRepositories {
                    Id = id, Repositories = new List <RepositoryFullName>()
                };
                _repositoryForInstallationRepositories.Insert(installation);
            }
            return(installation);
        }
Beispiel #10
0
        /// <summary>
        /// Registers an installation with this Application
        /// </summary>
        /// <param name="id">The id of the Installation</param>
        /// <param name="targetType">The type of the GitHub account</param>
        /// <param name="targetAccount">The login details of the GitHub account</param>
        /// <param name="repositories">A collection of repositories to be registered</param>
        public void RegisterInstallation(InstallationId id, AccountType targetType, AccountLogin targetAccount, IEnumerable <RepositoryFullName> repositories)
        {
            if (_installations.ContainsKey(id))
            {
                throw new Exception("Installation already registered!");
            }

            Apply(new InstallationRegistered(
                      id,
                      targetType,
                      targetAccount,
                      repositories.Select(fullName => (string)fullName)
                      ));
        }
        /// <inheritdoc/>
        protected override async Task <object> CallGitHubApi(DialogContext dc, Octokit.GitHubClient gitHubClient, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (InstallationId != null && Options != null)
            {
                var installationIdValue = InstallationId.GetValue(dc.State);
                var optionsValue        = Options.GetValue(dc.State);
                return(await gitHubClient.GitHubApps.Installation.GetAllRepositoriesForCurrentUser((Int64)installationIdValue, optionsValue).ConfigureAwait(false));
            }
            if (InstallationId != null)
            {
                var installationIdValue = InstallationId.GetValue(dc.State);
                return(await gitHubClient.GitHubApps.Installation.GetAllRepositoriesForCurrentUser((Int64)installationIdValue).ConfigureAwait(false));
            }

            throw new ArgumentNullException("Required [installationId] arguments missing for GitHubClient.GitHubApps.Installation.GetAllRepositoriesForCurrentUser");
        }
Beispiel #12
0
        private void Init(bool forInstall)
        {
            var dbConnectionString = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", DatabasePassword);

            _globalValues = new Dictionary <string, string>
            {
                ["globalSettings__baseServiceUri__vault"]               = Url,
                ["globalSettings__baseServiceUri__api"]                 = $"{Url}/api",
                ["globalSettings__baseServiceUri__identity"]            = $"{Url}/identity",
                ["globalSettings__baseServiceUri__admin"]               = $"{Url}/admin",
                ["globalSettings__baseServiceUri__notifications"]       = $"{Url}/notifications",
                ["globalSettings__sqlServer__connectionString"]         = $"\"{ dbConnectionString }\"",
                ["globalSettings__identityServer__certificatePassword"] = IdentityCertPassword,
                ["globalSettings__attachment__baseDirectory"]           = $"{OutputDirectory}/core/attachments",
                ["globalSettings__attachment__baseUrl"]                 = $"{Url}/attachments",
                ["globalSettings__dataProtection__directory"]           = $"{OutputDirectory}/core/aspnet-dataprotection",
                ["globalSettings__logDirectory"]         = $"{OutputDirectory}/logs",
                ["globalSettings__licenseDirectory"]     = $"{OutputDirectory}/core/licenses",
                ["globalSettings__internalIdentityKey"]  = Helpers.SecureRandomString(64, alpha: true, numeric: true),
                ["globalSettings__duo__aKey"]            = Helpers.SecureRandomString(64, alpha: true, numeric: true),
                ["globalSettings__installation__id"]     = InstallationId?.ToString(),
                ["globalSettings__installation__key"]    = InstallationKey,
                ["globalSettings__yubico__clientId"]     = "REPLACE",
                ["globalSettings__yubico__key"]          = "REPLACE",
                ["globalSettings__mail__replyToEmail"]   = $"no-reply@{Domain}",
                ["globalSettings__mail__smtp__host"]     = "REPLACE",
                ["globalSettings__mail__smtp__username"] = "******",
                ["globalSettings__mail__smtp__password"] = "******",
                ["globalSettings__mail__smtp__ssl"]      = "true",
                ["globalSettings__mail__smtp__port"]     = "587",
                ["globalSettings__mail__smtp__useDefaultCredentials"] = "false",
                ["globalSettings__disableUserRegistration"]           = "false",
                ["adminSettings__admins"] = string.Empty,
            };

            if (forInstall && !Push)
            {
                _globalValues.Add("globalSettings__pushRelayBaseUri", "REPLACE");
            }

            _mssqlValues = new Dictionary <string, string>
            {
                ["ACCEPT_EULA"] = "Y",
                ["MSSQL_PID"]   = "Express",
                ["SA_PASSWORD"] = DatabasePassword,
            };
        }
Beispiel #13
0
        /// <inheritdoc />
        protected override void LoadPlugins(PluginCallback pluginCallback, IProgressMonitor progressMonitor)
        {
            // Attempt to read the old cache.
            string cacheFilePath;

            try
            {
                Hash64 hash = new Hash64();
                foreach (string pluginPath in PluginPaths)
                {
                    hash = hash.Add(pluginPath);
                }
                foreach (string constant in InitialPreprocessorConstants)
                {
                    hash = hash.Add(constant);
                }
                hash = hash.Add(InstallationId.ToString());

                string cacheDirPath  = GetCurrentUserPluginCacheDir();
                string cacheFileName = hash + ".xml";
                cacheFilePath = Path.Combine(cacheDirPath, cacheFileName);

                if (Directory.Exists(cacheDirPath))
                {
                    if (File.Exists(cacheFilePath))
                    {
                        Cache oldCache = ReadCacheFile(cacheFilePath);
                        if (oldCache != null)
                        {
                            foreach (var pluginInfo in oldCache.PluginInfos)
                            {
                                pluginCallback(pluginInfo.Plugin, new DirectoryInfo(pluginInfo.BaseDirectory), pluginInfo.PluginFile);
                            }
                            return;
                        }
                    }
                }
                else
                {
                    Directory.CreateDirectory(cacheDirPath);
                }
            }
            catch (Exception)
            {
                // Fallback on any failure.
                // There can be all sorts of weird security exceptions that will prevent
                // us from manipulating the local application data directory.
                base.LoadPlugins(pluginCallback, progressMonitor);
                return;
            }

            // Load plugin metadata.
            var newCache = new Cache
            {
                InstallationId = InstallationId.ToString()
            };

            base.LoadPlugins((plugin, baseDirectory, pluginFile) =>
            {
                newCache.PluginInfos.Add(new CachePluginInfo
                {
                    Plugin        = plugin,
                    BaseDirectory = baseDirectory.FullName,
                    PluginFile    = pluginFile,
                    PluginFileModificationTime = File.GetLastWriteTimeUtc(pluginFile)
                });

                pluginCallback(plugin, baseDirectory, pluginFile);
            }, progressMonitor);

            // Attempt to store it in the cache.
            try
            {
                WriteCacheFile(cacheFilePath, newCache);
            }
            catch (Exception)
            {
                // Ignore any failure.
            }
        }
Beispiel #14
0
 private string GetUserAgent(InstallationId installation) => $"{GitHubClientFactory.UserAgent}-Installation{installation.Id}";
 public TenantId GetTenantFor(InstallationId installationId)
 {
     // TODO: Implement
     return(TenantId.Development);
 }