Esempio n. 1
0
        public async Task BaseUri_VerifyBaseUriIsSetForAllFilesAsync()
        {
            // Arrange
            using (var packagesFolder = new TestFolder())
                using (var target = new TestFolder())
                    using (var cache = new LocalCache())
                    {
                        var outputRoot = Path.Combine(target.Root, "output");
                        var baseUri    = UriUtility.CreateUri("https://localhost:8080/testFeed/");

                        var log = new TestLogger();

                        var testPackage = new TestNupkg("packageA", "1.0.0");

                        var sleetConfig = TestUtility.CreateConfigWithLocal("local", outputRoot, baseUri.AbsoluteUri);

                        var sleetConfigPath = Path.Combine(target.Root, "sleet.config");
                        await JsonUtility.SaveJsonAsync(new FileInfo(sleetConfigPath), sleetConfig);

                        var zipFile = testPackage.Save(packagesFolder.Root);

                        var settings   = LocalSettings.Load(sleetConfigPath);
                        var fileSystem = FileSystemFactory.CreateFileSystem(settings, cache, "local");

                        // Act
                        var initSuccess = await InitCommand.RunAsync(settings, fileSystem, log);

                        var pushSuccess = await PushCommand.RunAsync(settings, fileSystem, new List <string>() { zipFile.FullName }, false, false, log);


                        // Assert
                        Assert.True(initSuccess, log.ToString());
                        Assert.True(pushSuccess, log.ToString());

                        var files = Directory.GetFiles(outputRoot, "*.json", SearchOption.AllDirectories);
                        await BaseURITestUtil.VerifyBaseUris(files, baseUri);
                    }
        }
Esempio n. 2
0
        public void UriUtility_ThrowsIfGetAbsolutePathWithNoSettingsFile()
        {
            Exception ex = null;

            try
            {
                using (var cache = new LocalCache())
                {
                    var baseUri = UriUtility.CreateUri("https://localhost:8080/testFeed/");

                    var sleetConfig = TestUtility.CreateConfigWithLocal("local", "relativePath", baseUri.AbsoluteUri);

                    var settings = LocalSettings.Load(sleetConfig, null);
                    FileSystemFactory.CreateFileSystem(settings, cache, "local");
                }
            }
            catch (Exception e)
            {
                ex = e;
            }

            ex.Should().NotBeNull();
            ex.Message.Should().Be("Cannot use a relative 'path' without a settings.json file.");
        }
Esempio n. 3
0
        public async Task NuGetReader_DependencyInfoResource_DependencyGroupsAsync()
        {
            // Arrange
            using (var packagesFolder = new TestFolder())
                using (var target = new TestFolder())
                    using (var cache = new LocalCache())
                    {
                        var outputRoot = Path.Combine(target.Root, "output");
                        var baseUri    = UriUtility.CreateUri("https://localhost:8080/testFeed/");

                        var log = new TestLogger();

                        var testPackage = new TestNupkg()
                        {
                            Nuspec = new TestNuspec()
                            {
                                Id           = "packageA",
                                Version      = "1.0.0",
                                Dependencies = new List <PackageDependencyGroup>()
                                {
                                    new PackageDependencyGroup(NuGetFramework.Parse("net46"), new List <PackageDependency>()
                                    {
                                    }),
                                    new PackageDependencyGroup(NuGetFramework.Parse("net45"), new[] { new PackageDependency("packageB", VersionRange.Parse("1.0.0")), new PackageDependency("packageC", VersionRange.Parse("2.0.0")) }),
                                    new PackageDependencyGroup(NuGetFramework.Parse("any"), new List <PackageDependency>()
                                    {
                                        new PackageDependency("packageB", VersionRange.Parse("1.0.0"))
                                    })
                                }
                            }
                        };

                        var sleetConfig = TestUtility.CreateConfigWithLocal("local", outputRoot, baseUri.AbsoluteUri);

                        var sleetConfigPath = Path.Combine(target.Root, "sleet.config");
                        await JsonUtility.SaveJsonAsync(new FileInfo(sleetConfigPath), sleetConfig);

                        var zipFile = testPackage.Save(packagesFolder.Root);

                        var settings   = LocalSettings.Load(sleetConfigPath);
                        var fileSystem = FileSystemFactory.CreateFileSystem(settings, cache, "local");
                        var success    = await InitCommand.RunAsync(settings, fileSystem, log);

                        // Act
                        // Run sleet
                        success &= await PushCommand.RunAsync(settings, fileSystem, new List <string>() { zipFile.FullName }, false, false, log);

                        // Create a repository abstraction for nuget
                        var nugetFileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(outputRoot), baseUri);
                        var localSource     = GetSource(outputRoot, baseUri, nugetFileSystem);
                        var cacheContext    = new SourceCacheContext();

                        var dependencyInfoResource = await localSource.GetResourceAsync <DependencyInfoResource>();

                        var dependencyPackagesNet46 = await dependencyInfoResource.ResolvePackages("packageA", NuGetFramework.Parse("net46"), cacheContext, log, CancellationToken.None);

                        var dependencyPackageNet46 = dependencyPackagesNet46.Single();
                        var depString46            = string.Join("|", dependencyPackageNet46.Dependencies.Select(d => d.Id + " " + d.VersionRange.ToNormalizedString()));

                        var dependencyPackagesNet45 = await dependencyInfoResource.ResolvePackages("packageA", NuGetFramework.Parse("net45"), cacheContext, log, CancellationToken.None);

                        var dependencyPackageNet45 = dependencyPackagesNet45.Single();
                        var depString45            = string.Join("|", dependencyPackageNet45.Dependencies.Select(d => d.Id + " " + d.VersionRange.ToNormalizedString()));

                        var dependencyPackagesNet40 = await dependencyInfoResource.ResolvePackages("packageA", NuGetFramework.Parse("net40"), cacheContext, log, CancellationToken.None);

                        var dependencyPackageNet40 = dependencyPackagesNet40.Single();
                        var depString40            = string.Join("|", dependencyPackageNet40.Dependencies.Select(d => d.Id + " " + d.VersionRange.ToNormalizedString()));

                        // Assert
                        Assert.True(success, log.ToString());

                        Assert.Equal("https://localhost:8080/testFeed/flatcontainer/packagea/1.0.0/packagea.1.0.0.nupkg", dependencyPackageNet46.DownloadUri.AbsoluteUri);
                        Assert.True(dependencyPackageNet46.Listed);
                        Assert.Equal("packageA", dependencyPackageNet46.Id);
                        Assert.Equal("1.0.0", dependencyPackageNet46.Version.ToNormalizedString());
                        Assert.Equal("", depString46);
                        Assert.Equal("packageB [1.0.0, )|packageC [2.0.0, )", depString45);
                        Assert.Equal("packageB [1.0.0, )", depString40);
                    }
        }
Esempio n. 4
0
        public async Task NuGetReader_PackageSearchResourceAsync()
        {
            // Arrange
            using (var packagesFolder = new TestFolder())
                using (var target = new TestFolder())
                    using (var cache = new LocalCache())
                    {
                        var outputRoot = Path.Combine(target.Root, "output");
                        var baseUri    = UriUtility.CreateUri("https://localhost:8080/testFeed/");

                        var log = new TestLogger();

                        var testPackage = new TestNupkg()
                        {
                            Nuspec = new TestNuspec()
                            {
                                Id                       = "packageA",
                                Version                  = "1.0.0",
                                Authors                  = "author",
                                Description              = "desc",
                                IconUrl                  = "http://www.tempuri.org",
                                Language                 = "en-us",
                                MinClientVersion         = "1.0.0",
                                Title                    = "title",
                                Tags                     = "a b d",
                                Summary                  = "summary",
                                LicenseUrl               = "http://www.tempuri.org/lic",
                                ProjectUrl               = "http://www.tempuri.org/proj",
                                ReleaseNotes             = "notes",
                                Owners                   = "owners",
                                Copyright                = "copyright",
                                RequireLicenseAcceptance = "true"
                            }
                        };

                        var testPackage2 = new TestNupkg()
                        {
                            Nuspec = new TestNuspec()
                            {
                                Id                       = "packageA",
                                Version                  = "2.0.0",
                                Authors                  = "author2",
                                Description              = "desc2",
                                IconUrl                  = "http://www.tempuri2.org/",
                                Language                 = "en-us",
                                MinClientVersion         = "1.0.0",
                                Title                    = "title2",
                                Tags                     = "a b c",
                                Summary                  = "summary2",
                                LicenseUrl               = "http://www.tempuri.org/lic2",
                                ProjectUrl               = "http://www.tempuri.org/proj2",
                                ReleaseNotes             = "notes2",
                                Owners                   = "owners2",
                                Copyright                = "copyright2",
                                RequireLicenseAcceptance = "true"
                            }
                        };

                        var sleetConfig = TestUtility.CreateConfigWithLocal("local", outputRoot, baseUri.AbsoluteUri);

                        var sleetConfigPath = Path.Combine(target.Root, "sleet.config");
                        await JsonUtility.SaveJsonAsync(new FileInfo(sleetConfigPath), sleetConfig);

                        var zipFile  = testPackage.Save(packagesFolder.Root);
                        var zipFile2 = testPackage2.Save(packagesFolder.Root);

                        var settings   = LocalSettings.Load(sleetConfigPath);
                        var fileSystem = FileSystemFactory.CreateFileSystem(settings, cache, "local");
                        var success    = await InitCommand.RunAsync(settings, fileSystem, log);

                        // Act
                        // Run sleet
                        success &= await PushCommand.RunAsync(settings, fileSystem, new List <string>() { zipFile2.FullName }, false, false, log);

                        success &= await PushCommand.RunAsync(settings, fileSystem, new List <string>() { zipFile.FullName }, false, false, log);

                        // Create a repository abstraction for nuget
                        var nugetFileSystem = new PhysicalFileSystem(cache, UriUtility.CreateUri(outputRoot), baseUri);
                        var localSource     = GetSource(outputRoot, baseUri, nugetFileSystem);

                        var resource = await localSource.GetResourceAsync <PackageSearchResource>();

                        var results = await resource.SearchAsync(string.Empty, new SearchFilter(includePrerelease : true), 0, 10, log, CancellationToken.None);

                        var result = results.Single();

                        var versions = await result.GetVersionsAsync();

                        // Assert
                        Assert.True(success, log.ToString());

                        Assert.Equal(testPackage2.Nuspec.Authors, result.Authors);
                        Assert.Equal(testPackage2.Nuspec.Description, result.Description);
                        Assert.Equal(0, result.DownloadCount);
                        Assert.Equal(testPackage2.Nuspec.IconUrl, result.IconUrl.AbsoluteUri);
                        Assert.Equal(testPackage2.Nuspec.Id, result.Identity.Id);
                        Assert.Equal(testPackage2.Nuspec.Version.ToString(), result.Identity.Version.ToString());
                        Assert.Equal(testPackage2.Nuspec.LicenseUrl, result.LicenseUrl.AbsoluteUri);
                        Assert.Equal(testPackage2.Nuspec.Owners, result.Owners);
                        Assert.Equal(testPackage2.Nuspec.ProjectUrl, result.ProjectUrl.AbsoluteUri);
                        Assert.Equal(testPackage2.Nuspec.Summary, result.Summary);
                        Assert.Equal("a, b, c", result.Tags);
                        Assert.Equal(testPackage2.Nuspec.Title, result.Title);

                        Assert.Equal(2, versions.Count());
                        Assert.Equal("1.0.0", versions.First().Version.ToString());
                        Assert.Equal("2.0.0", versions.Skip(1).First().Version.ToString());
                    }
        }
Esempio n. 5
0
        private void FsUser_Authentication(object sender, AuthenticationEventArgs e)
        {
            /*
             * https://www.rebex.net/file-server/features/events.aspx#authentication
             */
            try
            {
                var callPath = $"{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}";

                using (var scope = _factory.CreateScope())
                {
                    var conf = scope.ServiceProvider.GetRequiredService <IConfiguration>();
                    var log  = scope.ServiceProvider.GetRequiredService <ILogger>();
                    var uow  = scope.ServiceProvider.GetRequiredService <IUnitOfWork>();
                    var user = uow.Users.Get(QueryExpressionFactory.GetQueryExpression <tbl_User>()
                                             .Where(x => x.IdentityAlias == e.UserName).ToLambda(),
                                             new List <Expression <Func <tbl_User, object> > >()
                    {
                        x => x.tbl_PublicKey,
                        x => x.tbl_UserMount,
                    }).SingleOrDefault();

                    var admin = scope.ServiceProvider.GetRequiredService <IAdminService>();
                    var sts   = scope.ServiceProvider.GetRequiredService <IStsService>();

                    if (e.Key != null)
                    {
                        Log.Information($"'{callPath}' '{e.UserName}' in-progress with public key");

                        if (UserHelper.ValidatePubKey(user.tbl_PublicKey.Where(x => x.Enabled).ToList(), e.Key) &&
                            admin.User_VerifyV1(user.IdentityId).Result)
                        {
                            Log.Information($"'{callPath}' '{e.UserName}' success with public key");

                            if (e.PartiallyAccepted ||
                                !user.RequirePassword)
                            {
                                /*
                                 * an smb mount will not succeed without a user password or ambassador credential.
                                 */
                                if (user.FileSystemType == FileSystemTypes.SMB.ToString() &&
                                    !user.tbl_UserMount.CredentialId.HasValue)
                                {
                                    Log.Warning($"'{callPath}' '{e.UserName}' failure no credential to create {FileSystemTypes.SMB} filesystem");

                                    e.Reject();
                                    return;
                                }

                                var fs     = FileSystemFactory.CreateFileSystem(_factory, log, user, e.UserName, e.Password);
                                var fsUser = new FileServerUser(e.UserName, e.Password);
                                fsUser.SetFileSystem(fs);

                                var fsNotify = fs.GetFileSystemNotifier();
                                fsNotify.CreatePreview   += FsNotify_CreatePreview;
                                fsNotify.CreateCompleted += FsNotify_CreateCompleted;
                                fsNotify.DeletePreview   += FsNotify_DeletePreview;
                                fsNotify.DeleteCompleted += FsNotify_DeleteCompleted;

                                e.Accept(fsUser);
                                return;
                            }
                            else
                            {
                                /*
                                 * authenticate partially if another kind of credential has not been provided yet.
                                 */
                                e.AcceptPartially();
                                return;
                            }
                        }
                        else
                        {
                            Log.Warning($"'{callPath}' '{e.UserName}' failure with public key");

                            e.Reject();
                            return;
                        }
                    }

                    if (e.Password != null)
                    {
                        Log.Information($"'{callPath}' '{e.UserName}' in-progress with password");

                        try
                        {
                            var identity = admin.User_GetV1(user.IdentityId.ToString()).Result;

                            var auth = sts.ResourceOwner_GrantV2(
                                new ResourceOwnerV2()
                            {
                                issuer     = conf["IdentityCredentials:IssuerName"],
                                client     = conf["IdentityCredentials:AudienceName"],
                                grant_type = "password",
                                user       = identity.UserName,
                                password   = e.Password,
                            }).Result;

                            Log.Information($"'{callPath}' '{e.UserName}' success with password");

                            if (e.PartiallyAccepted ||
                                !user.RequirePublicKey)
                            {
                                var fs     = FileSystemFactory.CreateFileSystem(_factory, log, user, e.UserName, e.Password);
                                var fsUser = new FileServerUser(e.UserName, e.Password);
                                fsUser.SetFileSystem(fs);

                                var fsNotify = fs.GetFileSystemNotifier();
                                fsNotify.CreatePreview   += FsNotify_CreatePreview;
                                fsNotify.CreateCompleted += FsNotify_CreateCompleted;
                                fsNotify.DeletePreview   += FsNotify_DeletePreview;
                                fsNotify.DeleteCompleted += FsNotify_DeleteCompleted;

                                e.Accept(fsUser);
                                return;
                            }
                            else
                            {
                                /*
                                 * authenticate partially if another kind of credential has not been provided yet.
                                 */
                                e.AcceptPartially();
                                return;
                            }
                        }
                        catch (HttpRequestException)
                        {
                            Log.Warning($"'{callPath}' '{e.UserName}' failure with password");

                            e.Reject();
                            return;
                        }
                    }

                    Log.Warning($"'{callPath}' '{e.UserName}' denied");

                    e.Reject();
                    return;
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
            }
        }