Example #1
0
        public async Task <CiCdDto> UpdateAsync(CiCdDto dto)
        {
            encryptionService.Encrypt(dto);

            var cicd = context.CicdSources.Find(dto.CiCdId);

            cicd.Endpoint       = dto.Endpoint;
            cicd.Kind           = dto.Kind;
            cicd.IsEnabled      = dto.IsEnabled;
            cicd.IsGuestEnabled = dto.IsGuestEnabled;
            cicd.Password       = dto.Password;
            cicd.Username       = dto.Username;
            cicd.ApiKey         = dto.ApiKey;

            context.CicdSources.Update(cicd);
            await context.SaveChangesAsync();

            return(new CiCdDto
            {
                ApiKey = cicd.ApiKey,
                CiCdId = cicd.Id,
                Endpoint = cicd.Endpoint,
                Kind = cicd.Kind,
                Username = cicd.Username,
                Password = cicd.Password
            });
        }
Example #2
0
        public async Task <RepositoryDto> ToggleIgnoreAsync(Guid repoId)
        {
            VcsRepository repository = await context.VcsRepositories.FindAsync(repoId);

            repository.IsIgnored = !repository.IsIgnored;
            await context.SaveChangesAsync();

            if (repository.IsIgnored)
            {
                context.RemoveRange(context.Assets.Where(asset => asset.RepositoryId == repoId));
            }
            else
            {
                context.Tasks.Add(new RefreshTask {
                    Scope = TaskScopeKind.Repository, TargetId = repoId
                });
            }

            await context.SaveChangesAsync();

            return(new RepositoryDto
            {
                VcsId = repository.VcsId,
                Assets = context.Assets.Count(asset => asset.RepositoryId == repoId),
                Url = repository.Url,
                WebUrl = repository.WebUrl,
                RepositoryId = repository.Id,
                IsIgnored = repository.IsIgnored
            });
        }
Example #3
0
        private async Task HandleRefreshTasks(IRefreshService taskService, VisionDbContext context)
        {
            foreach (RefreshTask task in await context.Tasks.OrderByDescending(t => t.Created).Where(t => t.Completed == null).ToListAsync())
            {
                task.Started = DateTime.Now;
                context.Tasks.Update(task);
                await context.SaveChangesAsync();

                try
                {
                    logger.LogInformation($"Starting refresh task {task.Scope.ToString()}:{task.Id}");

                    Task refreshTask = task.Scope switch
                    {
                        TaskScopeKind.Asset => taskService.RefreshAssetByIdAsync(task.TargetId),
                        TaskScopeKind.Dependency => taskService.RefreshDependencyByIdAsync(task.TargetId),
                        TaskScopeKind.Repository => taskService.RefreshRepositoryByIdAsync(task.TargetId),
                        TaskScopeKind.VersionControl => taskService.RefreshVersionControlByIdAsync(task.TargetId),
                        _ => Task.Delay(0)
                    };

                    await refreshTask;

                    task.Completed = DateTime.Now;
                    context.Tasks.Update(task);
                    await context.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    logger.LogError(e, $"Error performing refresh task {task.Scope.ToString()}:{task.Id}.");
                }
            }
        }
Example #4
0
        public async Task <RefreshTask> UpdateAssetAsync(Guid id)
        {
            RefreshTask task = new RefreshTask {
                Scope = TaskScopeKind.Asset, TargetId = id
            };

            context.Tasks.Add(task);
            await context.SaveChangesAsync();

            return(task);
        }
Example #5
0
        public async Task <VersionControlDto> CreateVersionControl(VersionControlDto dto)
        {
            encryptionService.Encrypt(dto);

            Vcs vcs = new Vcs
            {
                Id        = Guid.NewGuid(),
                ApiKey    = dto.ApiKey,
                Endpoint  = dto.Endpoint,
                Kind      = dto.Kind,
                IsEnabled = dto.IsEnabled
            };

            context.VcsSources.Add(vcs);
            await context.SaveChangesAsync();

            return(new VersionControlDto {
                Endpoint = vcs.Endpoint, ApiKey = vcs.ApiKey, Kind = vcs.Kind, VcsId = vcs.Id
            });
        }
Example #6
0
 private async Task HandleRepositoryCleaning(VisionDbContext context)
 {
     foreach (var repository in await context.VcsRepositories.Where(repository => repository.IsIgnored).ToListAsync())
     {
         if (await context.Assets.AnyAsync(a => a.RepositoryId == repository.Id))
         {
             logger.LogInformation($"Removing repository assocated from repository {repository.Id}");
             context.AssetEcoSystems.RemoveRange(context.AssetEcoSystems.Where(af => af.Asset.Repository == repository));
             context.Assets.RemoveRange(context.Assets.Where(asset => asset.RepositoryId == repository.Id));
             await context.SaveChangesAsync();
         }
     }
 }
Example #7
0
        public async Task <RegistryDto> CreateAsync(RegistryDto dto)
        {
            encryptionService.Encrypt(dto);

            EcoRegistry registry = new EcoRegistry
            {
                Endpoint  = dto.Endpoint,
                Kind      = dto.Kind,
                IsPublic  = dto.IsPublic,
                IsEnabled = dto.IsEnabled,
                ApiKey    = dto.ApiKey,
                Username  = dto.Username,
                Password  = dto.Password
            };

            context.EcoRegistrySources.Add(registry);
            await context.SaveChangesAsync();

            logger.LogInformation("Created registry {0}", registry.Id);

            return(new RegistryDto {
                Endpoint = registry.Endpoint, ApiKey = registry.ApiKey, Kind = registry.Kind, RegistryId = registry.Id, IsPublic = dto.IsPublic, IsEnabled = dto.IsEnabled
            });
        }
Example #8
0
        private async Task PopulateDependencyVersionVulnerabilities()
        {
            foreach (Dependency dependency in context.Dependencies)
            {
                UniqueRandomPicker <DependencyVersion> picker = Pick <DependencyVersion> .UniqueRandomList(With.Between(0).And(dependency.Versions.Count).Elements);

                foreach (DependencyVersion version in picker.From(dependency.Versions.ToList()))
                {
                    context.Vulnerabilities.Add(new Vulnerability
                    {
                        DependencyVersion   = version,
                        DependencyVersionId = version.Id,
                        Id           = Guid.NewGuid(),
                        CheckTime    = DateTime.Now,
                        Kind         = ReporterKind.OSSIndex,
                        ResponseData = FakeResponseData,
                        Link         = $"https://ossindex.sonatype.org/component/pkg:nuget/[email protected]"
                    });
                }
            }

            await context.SaveChangesAsync();
        }
Example #9
0
        public async Task RefreshRepositoryByIdAsync(Guid repositoryId)
        {
            VcsRepository repository = await context.VcsRepositories.FindAsync(repositoryId);

            Vcs versionControl = await context.VcsSources.FindAsync(repository.VcsId);

            logger.LogInformation($"Refreshing repository: {repository.Url}");
            await context.Entry(repository).Collection(r => r.Assets).LoadAsync();

            context.Assets.RemoveRange(repository.Assets);
            await context.SaveChangesAsync();

            var versionControlDto = new VersionControlDto {
                ApiKey = versionControl.ApiKey, Endpoint = versionControl.Endpoint, VcsId = versionControl.Id, Kind = versionControl.Kind, IsEnabled = versionControl.IsEnabled
            };
            var repositoryDto = new RepositoryDto {
                VcsId = repository.VcsId, Url = repository.Url, WebUrl = repository.WebUrl
            };

            encryptionService.Decrypt(versionControlDto);
            IEnumerable <Asset> items = await versionControlProvider.GetAssetsAsync(versionControlDto, repositoryDto);

            foreach (Asset asset in items)
            {
                context.Assets.Add(asset);
                await context.SaveChangesAsync();
                await RefreshAssetByIdAsync(asset.Id);
            }
        }