private void ValidateConfiguration(ApplicationTypeContext context) { Dictionary <string, IEnumerable <ConfigPackage> > configurationPackageDictionary = new Dictionary <string, IEnumerable <ConfigPackage> >(); foreach (var serviceManifest in context.ServiceManifests) { configurationPackageDictionary.Add(serviceManifest.ServiceManifestType.Name, serviceManifest.ConfigPackages); } ApplicationManifestTypeServiceManifestImport[] serviceManifestImports = context.ApplicationManifest.ServiceManifestImport; bool hasEncryptedParameter = false; foreach (KeyValuePair <string, IEnumerable <ConfigPackage> > configKeyValuePair in configurationPackageDictionary) { if (configKeyValuePair.Value != null) { ApplicationManifestTypeServiceManifestImport matchingServiceManifestImport = serviceManifestImports.FirstOrDefault <ApplicationManifestTypeServiceManifestImport>( serviceManifestImport => ImageBuilderUtility.Equals(serviceManifestImport.ServiceManifestRef.ServiceManifestName, configKeyValuePair.Key)); ReleaseAssert.AssertIf( matchingServiceManifestImport == null, "Could not find matching ServiceManifest with Name {0}", configKeyValuePair.Key); foreach (ConfigPackage configurationPackage in configKeyValuePair.Value) { ImageBuilder.TraceSource.WriteInfo( TraceType, "Validating the configuration. ApplicationTypeName:{0}, ServiceManifestName:{1}, ConfigPackageName:{2}", context.ApplicationManifest.ApplicationTypeName, matchingServiceManifestImport.ServiceManifestRef.ServiceManifestName, configurationPackage.ConfigPackageType.Name); if (configurationPackage.SettingsType != null && configurationPackage.SettingsType.Section != null) { string configPackageDirectory = context.BuildLayoutSpecification.GetConfigPackageFolder(configKeyValuePair.Key, configurationPackage.ConfigPackageType.Name); string configPackageArchive = context.BuildLayoutSpecification.GetSubPackageArchiveFile(configPackageDirectory); if (!FabricDirectory.Exists(configPackageDirectory) && !FabricFile.Exists(configPackageArchive)) { continue; } ConfigOverrideType matchingConfigOverride = null; if (matchingServiceManifestImport.ConfigOverrides != null) { matchingConfigOverride = matchingServiceManifestImport.ConfigOverrides.FirstOrDefault <ConfigOverrideType>( configOverride => ImageBuilderUtility.Equals(configOverride.Name, configurationPackage.ConfigPackageType.Name)); } string settingsFileName = context.BuildLayoutSpecification.GetSettingsFile(configPackageDirectory); DuplicateDetector sectionDuplicateDetector = new DuplicateDetector("Section", "Name", settingsFileName); foreach (SettingsTypeSection section in configurationPackage.SettingsType.Section) { sectionDuplicateDetector.Add(section.Name); SettingsOverridesTypeSection matchingConfigOverrideSection = null; if (matchingConfigOverride != null && matchingConfigOverride.Settings != null) { matchingConfigOverrideSection = matchingConfigOverride.Settings.FirstOrDefault <SettingsOverridesTypeSection>( configOverrideSection => ImageBuilderUtility.Equals(configOverrideSection.Name, section.Name)); } if (section.Parameter != null) { DuplicateDetector settingDuplicateDetector = new DuplicateDetector("Parameter", "Name", settingsFileName); foreach (SettingsTypeSectionParameter parameter in section.Parameter) { settingDuplicateDetector.Add(parameter.Name); if (!hasEncryptedParameter) { hasEncryptedParameter = parameter.IsEncrypted; } if (parameter.MustOverride) { SettingsOverridesTypeSectionParameter matchingOverrideParameter = null; if (matchingConfigOverrideSection != null && matchingConfigOverrideSection.Parameter != null) { matchingOverrideParameter = matchingConfigOverrideSection.Parameter.FirstOrDefault <SettingsOverridesTypeSectionParameter>( configOverrideSetting => ImageBuilderUtility.Equals(configOverrideSetting.Name, parameter.Name)); } if (matchingOverrideParameter == null) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, settingsFileName, StringResources.ImageBuilderError_ParameterNotOverriden, parameter.Name); } } if (!String.IsNullOrEmpty(parameter.Type)) { this.VerifySourceLocation(parameter.Name, "Settings.xml", parameter.Type); } } } } } } } } if (hasEncryptedParameter && RequireCertACLingForUsers(context.ApplicationManifest.Principals) && (context.ApplicationManifest.Certificates == null || context.ApplicationManifest.Certificates.SecretsCertificate == null)) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, context.GetApplicationManifestFileName(), StringResources.ImageBuilderError_EncryptedSettingsNoCertInAppManifest); } }
private void ValidateServiceManifests(ApplicationTypeContext context) { IEnumerable <ServiceManifestType> serviceManifestTypes = context.GetServiceManifestTypes(); // Validates the ServiceManifest var duplicateServiceTypeDetector = new DuplicateDetector("ServiceType", "ServiceTypeName"); foreach (ServiceManifestType serviceManifestType in serviceManifestTypes) { ImageBuilder.TraceSource.WriteInfo( TraceType, "Validating ServiceManifest. ApplicationTypeName:{0}, ServiceManifestName:{1}, ServiceManifestVersion:{2}", context.ApplicationManifest.ApplicationTypeName, serviceManifestType.Name, serviceManifestType.Version); string serviceManifestFileName = context.GetServiceManifestFileName(serviceManifestType.Name); if (!ManifestValidatorUtility.IsValidName(serviceManifestType.Name)) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, serviceManifestFileName, StringResources.ImageBuilderError_InvalidName_arg2, "ServiceManifest", serviceManifestType.Name, Path.DirectorySeparatorChar, StringConstants.DoubleDot); } if (!ManifestValidatorUtility.IsValidVersion(serviceManifestType.Version)) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, serviceManifestFileName, StringResources.ImageBuilderError_InvalidVersion_arg2, "ServiceManifestVersion", serviceManifestType.Version, Path.DirectorySeparatorChar, StringConstants.DoubleDot); } var implicitServiceTypeCount = 0; var normalServiceTypeCount = 0; var hasServiceGroupTypes = false; foreach (object manifestServiceTypeType in serviceManifestType.ServiceTypes) { string serviceTypeName = null; string placementConstraints = null; if (manifestServiceTypeType is ServiceTypeType serviceTypeType) { serviceTypeName = serviceTypeType.ServiceTypeName; placementConstraints = serviceTypeType.PlacementConstraints; if (IsImplicitServiceType(serviceTypeType)) { ++implicitServiceTypeCount; } else { ++normalServiceTypeCount; } } else { hasServiceGroupTypes = true; ServiceGroupTypeType serviceGroupTypeType = (ServiceGroupTypeType)manifestServiceTypeType; serviceTypeName = serviceGroupTypeType.ServiceGroupTypeName; placementConstraints = serviceGroupTypeType.PlacementConstraints; } this.ValidatePlacementConstraints(serviceManifestFileName, placementConstraints); duplicateServiceTypeDetector.Add(serviceTypeName); } this.ValidateServiceTypeCombination( serviceManifestFileName, implicitServiceTypeCount, normalServiceTypeCount, hasServiceGroupTypes); var activatorCodePackageCount = 0; // Validates the CodePackage in the ServiceManifest var codePackageDuplicateDetector = new DuplicateDetector("CodePackage", "Name", serviceManifestFileName); foreach (CodePackageType codePackageType in serviceManifestType.CodePackage) { if (!ManifestValidatorUtility.IsValidName(codePackageType.Name)) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, serviceManifestFileName, StringResources.ImageBuilderError_InvalidName_arg2, "CodePackage", codePackageType.Name, Path.DirectorySeparatorChar, StringConstants.DoubleDot); } if (!ManifestValidatorUtility.IsValidVersion(codePackageType.Version)) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, serviceManifestFileName, StringResources.ImageBuilderError_InvalidVersion_arg2, "CodePackageVersion", codePackageType.Version, Path.DirectorySeparatorChar, StringConstants.DoubleDot); } codePackageDuplicateDetector.Add(codePackageType.Name); if (codePackageType.IsActivator) { if (implicitServiceTypeCount > 0) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, serviceManifestFileName, StringResources.ImageBuilderError_GuestAppWithActivatorCodePackage); } activatorCodePackageCount++; if (activatorCodePackageCount > 1) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, serviceManifestFileName, StringResources.ImageBuilderError_MultipleActivatorCodePackage); } } var codePackageFolderName = context.BuildLayoutSpecification.GetCodePackageFolder(serviceManifestType.Name, codePackageType.Name); var codePackageArchiveName = context.BuildLayoutSpecification.GetSubPackageArchiveFile(codePackageFolderName); FileLocator fileLocator = null; if (FabricFile.Exists(codePackageArchiveName)) { fileLocator = new FileLocator(codePackageArchiveName, true); } else if (FabricDirectory.Exists(codePackageFolderName)) { fileLocator = new FileLocator(codePackageFolderName, false); } else { // If the code package doesn't exist, it means it was already provisioned; // validation must have passed at the time of first provisioning continue; } if (codePackageType.SetupEntryPoint != null) { this.ValidateEntryPointPath(serviceManifestFileName, fileLocator, codePackageType.SetupEntryPoint.ExeHost.Program); } if (codePackageType.EntryPoint != null && codePackageType.EntryPoint.Item != null) { if (codePackageType.EntryPoint.Item is DllHostEntryPointType) { this.ValidateEntryPoint(serviceManifestFileName, fileLocator, (DllHostEntryPointType)codePackageType.EntryPoint.Item); } else if (codePackageType.EntryPoint.Item is ExeHostEntryPointType) { this.ValidateEntryPoint(serviceManifestFileName, fileLocator, (ExeHostEntryPointType)codePackageType.EntryPoint.Item); } } } // Validates the ConfigPackage in the ServiceManifest if (serviceManifestType.ConfigPackage != null) { DuplicateDetector configPackageDuplicateDetector = new DuplicateDetector("ConfigPackage", "Name", serviceManifestFileName); foreach (ConfigPackageType configPackageType in serviceManifestType.ConfigPackage) { if (!ManifestValidatorUtility.IsValidName(configPackageType.Name)) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, serviceManifestFileName, StringResources.ImageBuilderError_InvalidName_arg2, "ConfigPackage", configPackageType.Name, Path.DirectorySeparatorChar, StringConstants.DoubleDot); } if (!ManifestValidatorUtility.IsValidVersion(configPackageType.Version)) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, serviceManifestFileName, StringResources.ImageBuilderError_InvalidVersion_arg2, "ConfigPackageVersion", configPackageType.Version, Path.DirectorySeparatorChar, StringConstants.DoubleDot); } configPackageDuplicateDetector.Add(configPackageType.Name); } } // Validates the DataPackage in the ServiceManifest if (serviceManifestType.DataPackage != null) { DuplicateDetector dataPackageDuplicateDetector = new DuplicateDetector("DataPackage", "Name", serviceManifestFileName); foreach (DataPackageType dataPackageType in serviceManifestType.DataPackage) { if (!ManifestValidatorUtility.IsValidName(dataPackageType.Name)) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, serviceManifestFileName, StringResources.ImageBuilderError_InvalidName_arg2, "DataPackage", dataPackageType.Name, Path.DirectorySeparatorChar, StringConstants.DoubleDot); } if (!ManifestValidatorUtility.IsValidVersion(dataPackageType.Version)) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, serviceManifestFileName, StringResources.ImageBuilderError_InvalidVersion_arg2, "DataPackageVersion", dataPackageType.Version, Path.DirectorySeparatorChar, StringConstants.DoubleDot); } dataPackageDuplicateDetector.Add(dataPackageType.Name); } } // Validates the Resources in the ServiceManifest if (serviceManifestType.Resources != null && serviceManifestType.Resources.Endpoints != null) { DuplicateDetector resourceNameDuplicateDetector = new DuplicateDetector("Endpoint", "Name", serviceManifestFileName); foreach (var endpoint in serviceManifestType.Resources.Endpoints) { if (endpoint.Name.Contains(';') || endpoint.Name.Contains(',')) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, serviceManifestFileName, StringResources.ImageBuilderError_InvalidName_arg2, "EndppointResource", endpoint.Name, ",", ";"); } if (!string.IsNullOrEmpty(endpoint.CodePackageRef)) { var matchingCodePackage = serviceManifestType.CodePackage.FirstOrDefault( codePackageType => ImageBuilderUtility.Equals(codePackageType.Name, endpoint.CodePackageRef)); if (matchingCodePackage == null) { ImageBuilderUtility.TraceAndThrowValidationErrorWithFileName( TraceType, serviceManifestFileName, StringResources.ImageBuilderError_InvalidRefSameFile, "CodePackageRef", endpoint.CodePackageRef, "EndppointResource", endpoint.Name, "CodePackage", "ServiceManifest"); } } resourceNameDuplicateDetector.Add(endpoint.Name); } } } }