Exemple #1
0
 protected PSTemplateSpecTemplateArtifact(
     TemplateSpecTemplateArtifact artifact
     ) : base(artifact)
 {
     // Note: Cast is redundant, but present for clarity reasons:
     this.Template = ((JToken)artifact.Template).ToString();
 }
Exemple #2
0
 internal static PSTemplateSpecTemplateArtifact FromAzureSDKTemplateSpecTemplateArtifact(
     TemplateSpecTemplateArtifact artifact)
 {
     return(artifact != null
         ? new PSTemplateSpecTemplateArtifact(artifact)
         : null);
 }
        /// <summary>
        /// Recursively packs the specified template and its referenced artifacts and
        /// adds the artifacts to the current packing context.
        /// </summary>
        /// <param name="templateAbsoluteFilePath">
        /// The path to the ARM Template .json file to pack
        /// </param>
        /// <param name="context">
        /// The packing context of the current packing operation
        /// </param>
        /// <param name="artifactableTemplateObj">
        /// The packagable template object
        /// </param>
        private static void PackArtifacts(
            string templateAbsoluteFilePath,
            PackingContext context,
            out JObject artifactableTemplateObj)
        {
            string originalDirectory = context.CurrentDirectory;

            try
            {
                context.CurrentDirectory = Path.GetDirectoryName(
                    templateAbsoluteFilePath
                    );

                string  templateJson = FileUtilities.DataStore.ReadFileAsText(templateAbsoluteFilePath);
                JObject templateObj  = artifactableTemplateObj = JObject.Parse(templateJson);

                JObject[] templateLinkToArtifactObjs = GetTemplateLinksToArtifacts(
                    templateObj, includeNested: true);

                foreach (JObject templateLinkObj in templateLinkToArtifactObjs)
                {
                    string relativePath = ((string)templateLinkObj["relativePath"])?
                                          .TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);

                    if (string.IsNullOrWhiteSpace(relativePath))
                    {
                        continue; // Throw here?
                    }

                    // This is a templateLink to a local template... Get the absolute path of the
                    // template based on its relative path from the current template directory and
                    // make sure it exists:

                    string absoluteLocalPath = Path.Combine(context.CurrentDirectory, relativePath);
                    if (!File.Exists(absoluteLocalPath))
                    {
                        throw new FileNotFoundException(absoluteLocalPath);
                    }

                    // Let's make sure we're not referencing a file outside of our root directory
                    // heirarchy. We won't allow such references for security purposes:

                    if (!absoluteLocalPath.StartsWith(
                            context.RootTemplateDirectory + Path.DirectorySeparatorChar,
                            StringComparison.OrdinalIgnoreCase))
                    {
                        throw new InvalidOperationException(
                                  $"Unable to handle the reference to file '{absoluteLocalPath}' from " +
                                  $"'{templateAbsoluteFilePath}' because it exists outside of the root template " +
                                  $"directory of '{context.RootTemplateDirectory}'");
                    }

                    // Convert the template relative path to one that is relative to our root
                    // directory path, and then if we haven't already processed that template into
                    // an artifact elsewhere, we'll do so here...

                    string asRelativePath = AbsoluteToRelativePath(context.RootTemplateDirectory, absoluteLocalPath);
                    if (context.Artifacts.Any(prevAddedArtifact => prevAddedArtifact.Path.Equals(
                                                  asRelativePath, StringComparison.OrdinalIgnoreCase)))
                    {
                        continue; // We've already packed this artifact from elsewhere
                    }

                    PackArtifacts(absoluteLocalPath, context, out JObject templateObjForArtifact);

                    TemplateSpecArtifact artifact = new TemplateSpecTemplateArtifact
                    {
                        Path     = asRelativePath,
                        Template = templateObjForArtifact
                    };

                    context.Artifacts.Add(artifact);
                }
            }
            finally
            {
                context.CurrentDirectory = originalDirectory;
            }
        }