public ServiceFabricApplicationProject Parse(
            ServiceFabricApplicationProject sfProject,
            DirectoryInfo srcBasePath)
        {
            var basePath = Path.GetDirectoryName(sfProject.ProjectFileFullPath);

            using (var fileStream = new FileStream(sfProject.ProjectFileFullPath, FileMode.Open))
                using (var reader = XmlReader.Create(fileStream))
                {
                    var document = new XmlDocument();
                    document.Load(reader);
                    var manager = new XmlNamespaceManager(document.NameTable);
                    manager.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003");

                    sfProject.ApplicationManifestPath = ExtractApplicationManifest(basePath, document, manager);
                    sfProject = _appManifestHandler.ReadXml(sfProject);

                    sfProject.Services = ExtractProjectReferences(basePath, sfProject.BuildOutputPathSuffix, document, manager);

                    var guestExecutables = DiscoverAndReadGuestExecutables(sfProject);
                    foreach (var guest in guestExecutables)
                    {
                        if (!sfProject.Services.ContainsKey(guest.Key))
                        {
                            sfProject.Services.Add(guest.Key, guest.Value);
                        }
                    }

                    return(sfProject);
                }
        }
        public ServiceFabricApplicationProject ReadXml(ServiceFabricApplicationProject appProject)
        {
            var document    = new XmlDocument();
            var manifestXml = File.ReadAllText(appProject.ApplicationManifestFileFullPath);

            document.LoadXml(manifestXml);
            var nsManager = new XmlNamespaceManager(document.NameTable);

            nsManager.AddNamespace("x", "http://schemas.microsoft.com/2011/01/fabric");

            appProject.ApplicationTypeName    = document.GetSingleValue("//x:ApplicationManifest/@ApplicationTypeName", nsManager);
            appProject.ApplicationTypeVersion =
                document.GetSingleValue("//x:ApplicationManifest/@ApplicationTypeVersion", nsManager);

            return(appProject);
        }
        private async Task CopyServicesToPackage(
            IEnumerable <KeyValuePair <string, GlobalVersion> > services,
            Dictionary <string, GlobalVersion> thingsToPackage,
            ServiceFabricApplicationProject appData,
            DirectoryInfo basePackagePath,
            Dictionary <string, byte[]> hackedFiles)
        {
            foreach (var service in services)
            {
                var serviceKey  = service.Key.Split('-').Last();
                var serviceData = appData.Services[serviceKey];

                CopyServiceManifest(serviceData, basePackagePath);

                var subPackages = thingsToPackage
                                  .Where(x => x.Value.VersionType == VersionType.ServicePackage)
                                  .Where(x => x.Value.IncludeInPackage)
                                  .Where(x => x.Value.ParentRef.Equals(service.Key));

                foreach (var subPackage in subPackages)
                {
                    if (serviceData.IsAspNetCore && subPackage.Value.PackageType == PackageType.Code)
                    {
                        _hack.RecreateHackFiles(hackedFiles);

                        var package = serviceData.SubPackages
                                      .First(x => x.PackageType == PackageType.Code);
                        var servicePackageFolder = Path.Combine(basePackagePath.FullName, serviceData.ServiceName, package.Name);
                        var resultCode           = _aspNetCorePackager.Package(serviceData.ProjectFolder.FullName, servicePackageFolder, _baseConfig.BuildConfiguration, _baseConfig.DotNetPublishExtraArgs);
                        if (resultCode != 0)
                        {
                            throw new InvalidOperationException("Something went wrong packaging ASP.Net Core stuff");
                        }
                    }
                    else
                    {
                        await PackageFiles(appData, serviceData, subPackage).ConfigureAwait(false);
                    }
                }
            }
        }
        public Dictionary <string, byte[]> FindHackableThings(ServiceFabricApplicationProject randomProject)
        {
            var projectFolderToFind = "Common";
            var filePattern         = "Common.resources.dll";

            var root = Directory.GetParent(randomProject.ProjectFolder);

            var projectRoot   = Path.Combine(root.FullName, projectFolderToFind);
            var searchSubPath = $"obj\\{_appConfig.BuildConfiguration}";

            var searchPath = Path.Combine(projectRoot, searchSubPath);

            var allFiles = Directory.EnumerateFiles(searchPath, filePattern, SearchOption.AllDirectories);

            var results = new Dictionary <string, byte[]>();

            foreach (var foundFile in allFiles)
            {
                var data = File.ReadAllBytes(foundFile);
                results.Add(foundFile, data);
            }

            return(results);
        }
        private Dictionary <string, ServiceFabricServiceProject> DiscoverAndReadGuestExecutables(ServiceFabricApplicationProject sfProject)
        {
            var result = new Dictionary <string, ServiceFabricServiceProject>();

            var guests = _packageConfig.GuestExecutables.Where(x => x.ApplicationTypeName.Equals(sfProject.ApplicationTypeName, StringComparison.CurrentCultureIgnoreCase));

            foreach (var guest in guests)
            {
                var serviceProject = new ServiceFabricServiceProject
                {
                    IsAspNetCore  = false,
                    ProjectFolder = new DirectoryInfo(Path.Combine(_baseConfig.SourcePath.FullName, guest.PackageName)),
                    ProjectFile   = null,
                    PackageRoot   = new DirectoryInfo(Path.Combine(_baseConfig.SourcePath.FullName, guest.PackageName)),
                };

                var finalProject = _appManifestHandler.ReadXml(serviceProject, Path.Combine(serviceProject.ProjectFolder.FullName, "Code"));
                finalProject.IsGuestExecutable = true;

                result.Add(guest.PackageName, finalProject);
            }

            return(result);
        }
Exemplo n.º 6
0
        public async Task <Dictionary <string, GlobalVersion> > Calculate(
            ServiceFabricApplicationProject project,
            VersionNumber currentVersion)
        {
            var projectHashes = new Dictionary <string, GlobalVersion>();

            foreach (var service in project.Services)
            {
                foreach (var subPackage in service.Value.SubPackages)
                {
                    _log.WriteLine($"Computing hash for Service: {service.Key} - Package: {subPackage.Name}");
                    var hasher    = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
                    var directory = new DirectoryInfo(subPackage.Path);
                    IOrderedEnumerable <string> files;

                    if (subPackage.PackageType == PackageType.Code)
                    {
                        if (!service.Value.IsGuestExecutable)
                        {
                            files = directory
                                    .GetFiles("*", SearchOption.AllDirectories)
                                    .Where(
                                x =>
                                _packageConfig.HashIncludeExtensions.Any(
                                    include =>
                                    x.FullName.EndsWith(include,
                                                        StringComparison.CurrentCultureIgnoreCase)))
                                    .Where(
                                x =>
                                _packageConfig.HashSpecificExludes.All(
                                    exclude => !x.FullName.ToLowerInvariant()
                                    .Contains(exclude.ToLowerInvariant())))
                                    .Select(x => x.FullName)
                                    .OrderBy(x => x);
                        }
                        else
                        {
                            files = directory
                                    .GetFiles("*", SearchOption.AllDirectories)
                                    .Where(
                                x =>
                                _packageConfig.HashSpecificExludes.All(
                                    exclude => !x.FullName.ToLowerInvariant()
                                    .Contains(exclude.ToLowerInvariant())))
                                    .Select(x => x.FullName)
                                    .OrderBy(x => x);
                        }
                    }
                    else
                    {
                        files = directory
                                .GetFiles("*", SearchOption.AllDirectories)
                                .Select(x => x.FullName)
                                .OrderBy(x => x);
                    }

                    foreach (var data in files.Select(File.ReadAllBytes))
                    {
                        hasher.AppendData(data);
                    }

                    var externalIncludes = _packageConfig
                                           .ExternalIncludes
                                           .Where(x => x
                                                  .ApplicationTypeName.Equals(project.ApplicationTypeName,
                                                                              StringComparison.CurrentCultureIgnoreCase))
                                           .Where(x => x
                                                  .ServiceManifestName.Equals(service.Value.ServiceName,
                                                                              StringComparison.CurrentCultureIgnoreCase))
                                           .Where(x => x
                                                  .PackageName.Equals(subPackage.Name,
                                                                      StringComparison.CurrentCultureIgnoreCase))
                                           .OrderBy(x => x.SourceFileName);

                    foreach (var externalFile in externalIncludes)
                    {
                        var file = await _fileHandler
                                   .GetFileAsBytesAsync(externalFile.SourceFileName)
                                   .ConfigureAwait(false);

                        if (!file.IsSuccessful)
                        {
                            throw new IOException("Failed to get external file from storage");
                        }

                        hasher.AppendData(file.ResponseContent);
                    }

                    var finalHash = hasher.GetHashAndReset();
                    var hash      = BitConverter.ToString(finalHash).Replace("-", "").ToLowerInvariant();

                    var packageVersion = new GlobalVersion
                    {
                        Hash        = hash,
                        VersionType = VersionType.ServicePackage,
                        ParentRef   = $"{project.ApplicationTypeName}-{service.Key}",
                        PackageType = subPackage.PackageType
                    };

                    projectHashes.Add($"{project.ApplicationTypeName}-{service.Key}-{subPackage.Name}", packageVersion);
                }

                var serviceManifest = _serviceManifestLoader.Load(service.Value.SourceServiceManifestPath);
                _manifestHandler.SetServiceEndpoints(serviceManifest, project.ApplicationTypeName, service.Value.ServiceName);

                using (var serviceManifestStream = new MemoryStream())
                {
                    _serviceManifestLoader.Save(serviceManifest, serviceManifestStream);

                    var serviceVersion = new GlobalVersion
                    {
                        VersionType = VersionType.Service,
                        ParentRef   = project.ApplicationTypeName,
                        Hash        = HashStream(serviceManifestStream)
                    };

                    projectHashes.Add($"{project.ApplicationTypeName}-{service.Key}", serviceVersion);
                }
            }

            var appManifest = _appManifestLoader.Load(project.ApplicationManifestFileFullPath);

            _manifestHandler.CleanAppManifest(appManifest);
            _handleEndpointCert.SetEndpointCerts(_packageConfig, appManifest, project.ApplicationTypeName);
            _handleEnciphermentCert.SetEnciphermentCerts(_packageConfig, appManifest, project.ApplicationTypeName);

            var guests = _packageConfig.GuestExecutables.Where(x =>
                                                               x.ApplicationTypeName.Equals(project.ApplicationTypeName, StringComparison.CurrentCultureIgnoreCase));

            foreach (var guest in guests)
            {
                var policies = new Policies();

                if (guest.GuestRunAs != null)
                {
                    var runAs = new RunAsPolicy
                    {
                        UserRef        = guest.GuestRunAs.UserName,
                        CodePackageRef = "Code"
                    };

                    var runAsPolicies = new List <RunAsPolicy> {
                        runAs
                    };
                    policies.RunAsPolicy = runAsPolicies;

                    if (appManifest.Principals == null)
                    {
                        appManifest.Principals = new Principals();
                    }
                    if (appManifest.Principals.Users == null)
                    {
                        appManifest.Principals.Users = new Users();
                    }
                    if (appManifest.Principals.Users.User == null)
                    {
                        appManifest.Principals.Users.User = new List <User>();
                    }

                    if (!appManifest.Principals.Users.User.Any(x =>
                                                               x.Name.Equals(guest.GuestRunAs.UserName, StringComparison.CurrentCultureIgnoreCase)))
                    {
                        var user = new User
                        {
                            Name        = guest.GuestRunAs.UserName,
                            AccountType = guest.GuestRunAs.AccountType
                        };
                        appManifest.Principals.Users.User.Add(user);
                    }
                }

                var serviceManifestRef = new ServiceManifestRef
                {
                    ServiceManifestName    = guest.PackageName,
                    ServiceManifestVersion = "1.0.0"
                };
                var serviceImport = new ServiceManifestImport
                {
                    ServiceManifestRef = serviceManifestRef,
                    ConfigOverrides    = new ConfigOverrides(),
                    Policies           = policies
                };

                appManifest.ServiceManifestImports.Add(serviceImport);
            }

            using (var appManifestStream = new MemoryStream())
            {
                _appManifestLoader.Save(appManifest, appManifestStream);
                projectHashes.Add(project.ApplicationTypeName, new GlobalVersion
                {
                    VersionType = VersionType.Application,
                    Version     = currentVersion,
                    Hash        = HashStream(appManifestStream)
                });
            }

            return(projectHashes);
        }
 private static void CopyApplicationManifestToPackage(ServiceFabricApplicationProject appData, DirectoryInfo applicationPackagePath)
 {
     File.Copy(appData.ApplicationManifestFileFullPath, appData.GetAppManifestTargetFile(applicationPackagePath).FullName, true);
 }
        private async Task PackageFiles(
            ServiceFabricApplicationProject appData,
            ServiceFabricServiceProject serviceProject,
            KeyValuePair <string, GlobalVersion> service)
        {
            var                    appPackagePath     = appData.GetPackagePath(_baseConfig.PackageOutputPath);
            var                    servicePackagePath = serviceProject.GetServicePackageFolder(appPackagePath);
            DirectoryInfo          directory;
            IEnumerable <FileInfo> files;
            var                    package = serviceProject.SubPackages
                                             .First(x => x.PackageType == service.Value.PackageType);

            var extraFiles = _packageConfig
                             .ExternalIncludes
                             .Where(x => x
                                    .ApplicationTypeName.Equals(appData.ApplicationTypeName,
                                                                StringComparison.CurrentCultureIgnoreCase))
                             .Where(x => x
                                    .ServiceManifestName.Equals(serviceProject.ServiceName,
                                                                StringComparison.CurrentCultureIgnoreCase))
                             .Where(x => x
                                    .PackageName.Equals(package.Name,
                                                        StringComparison.CurrentCultureIgnoreCase));

            if (service.Value.PackageType == PackageType.Code)
            {
                if (!serviceProject.IsGuestExecutable)
                {
                    directory = new DirectoryInfo(Path.Combine(serviceProject.ProjectFolder.FullName,
                                                               appData.BuildOutputPathSuffix));

                    files = directory
                            .GetFiles("*", SearchOption.AllDirectories)
                            .Where(x => _packageConfig.HashIncludeExtensions.Any(include =>
                                                                                 x.FullName.ToLowerInvariant().EndsWith(include.ToLowerInvariant())))
                            .Select(x => x.FullName)
                            .OrderBy(x => x)
                            .Select(x => new FileInfo(x));
                }
                else
                {
                    directory = new DirectoryInfo(Path.Combine(serviceProject.ProjectFolder.FullName, "Code"));

                    files = directory
                            .GetFiles("*", SearchOption.AllDirectories)
                            .Select(x => x.FullName)
                            .OrderBy(x => x)
                            .Select(x => new FileInfo(x));
                }
            }
            else
            {
                directory = new DirectoryInfo(Path.Combine(serviceProject.PackageRoot.FullName, package.Name));
                files     = directory
                            .GetFiles("*", SearchOption.AllDirectories)
                            .Select(x => x.FullName)
                            .OrderBy(x => x)
                            .Select(x => new FileInfo(x));
            }

            var basePathLength   = directory.FullName.Length;
            var subPackageFolder = package.GetSubPackageTargetPath(servicePackagePath);

            if (!subPackageFolder.Exists)
            {
                subPackageFolder.Create();
            }

            foreach (var file in files)
            {
                var relPath    = file.FullName.Remove(0, basePathLength + 1);
                var targetFile = new FileInfo(Path.Combine(subPackageFolder.FullName, relPath));
                if (!Directory.Exists(targetFile.DirectoryName))
                {
                    Directory.CreateDirectory(targetFile.DirectoryName);
                }

                File.Copy(file.FullName, targetFile.FullName);
            }

            foreach (var externalFile in extraFiles)
            {
                var file = await _fileHandler
                           .GetFileAsBytesAsync(externalFile.SourceFileName)
                           .ConfigureAwait(false);

                if (!file.IsSuccessful)
                {
                    throw new IOException("Failed to get external file from storage");
                }

                File.WriteAllBytes(Path.Combine(subPackageFolder.FullName, externalFile.TargetFileName), file.ResponseContent);
            }
        }