diff --git a/docs/3-create-a-zarf-package/4-zarf-schema.md b/docs/3-create-a-zarf-package/4-zarf-schema.md index d0f9027df6..31264a4589 100644 --- a/docs/3-create-a-zarf-package/4-zarf-schema.md +++ b/docs/3-create-a-zarf-package/4-zarf-schema.md @@ -2202,6 +2202,28 @@ Must be one of: +
+ + type + +  +
+ +**Description:** Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable - templated files should be kept below 1 MiB) + +| | | +| -------- | ------------------ | +| **Type** | `enum (of string)` | + +:::note +Must be one of: +* "raw" +* "file" +::: + +
+
+ @@ -2811,6 +2833,28 @@ Must be one of: +
+ + type + +  +
+ +**Description:** Changes the handling of a variable to load contents differently (i.e. from a file rather than as a raw variable - templated files should be kept below 1 MiB) + +| | | +| -------- | ------------------ | +| **Type** | `enum (of string)` | + +:::note +Must be one of: +* "raw" +* "file" +::: + +
+
+ diff --git a/examples/variables/README.md b/examples/variables/README.md index 993f4a31fd..03b16a9ea0 100644 --- a/examples/variables/README.md +++ b/examples/variables/README.md @@ -1,3 +1,4 @@ +import Properties from '@site/src/components/SchemaItemProperties'; import ExampleYAML from "@site/src/components/ExampleYAML"; # Variables @@ -23,7 +24,7 @@ To use variables and constants at deploy time you need to have two things: 1. a manifest that you want to template a value in 2. a defined variable in the `zarf.yaml` file from `variables` or `setVariable` -The manifest should have your desired variable name in ALL CAPS prefixed with `###ZARF_VAR` for `variables` or prefixed with `###ZARF_CONST` for `constants` and suffixed with `###`. For example in a configmap that took a variable named `DATABASE_USERNAME` you would provide the following: +The manifest should have your desired variable name in ALL CAPS prefixed with `###ZARF_VAR` for `variables` or prefixed with `###ZARF_CONST` for `constants` and suffixed with `###`. For example in a configmap that took a variable named `DATABASE_USERNAME` you would provide the following: ```yaml apiVersion: v1 @@ -34,7 +35,7 @@ data: username: ###ZARF_VAR_DATABASE_USERNAME### ``` -In the `zarf.yaml`, you would need to define the variable in the `variables` section or as output from an action with `setVariable` with the same `name` as above. Or for a constant you would use the `constants` section. For the same example as above, I would have the following for a variable defined by the deploy user: +In the `zarf.yaml`, you would need to define the variable in the `variables` section or as output from an action with `setVariable` with the same `name` as above. Or for a constant you would use the `constants` section. For the same example as above, you would have the following for a variable defined by the deploy user: ```yaml variables: @@ -55,6 +56,20 @@ components: - name: DATABASE_USERNAME ``` +Zarf `variables` can also have additional fields that describe how Zarf will handle them which are described below: + + + +:::note + +The fields `default`, `description` and `prompt` are not available on `setVariables` since they always take the standard output of an action command and will not be interacted with directly by a deploy user. + +::: + +Zarf `constants` are similar but have fewer options as they are static by the time `zarf package deploy` is run: + + + :::note All names must match the regex pattern `^[A-Z0-9_]+$` [Test](https://regex101.com/r/BG5ZqW/1)). @@ -63,7 +78,7 @@ All names must match the regex pattern `^[A-Z0-9_]+$` [Test](https://regex101.co :::tip -When not specifying `default`, `prompt`, `sensitive`, or `indent` Zarf will default to `default: ""`, `prompt: false`, `sensitive: false` and `indent: 0` +When not specifying `default`, `prompt`, `sensitive`, `autoIndent`, or `type` Zarf will default to `default: ""`, `prompt: false`, `sensitive: false`, `autoIndent: false`, and `type: "raw"` ::: diff --git a/examples/variables/nginx-configmap.yaml b/examples/variables/nginx-configmap.yaml index f0c1807859..7888c64d46 100644 --- a/examples/variables/nginx-configmap.yaml +++ b/examples/variables/nginx-configmap.yaml @@ -24,6 +24,7 @@ data:
           ###ZARF_VAR_MODIFIED_TERRAFORM###
         
+
File SHASUM: ###ZARF_VAR_MODIFIED_TERRAFORM_SHASUM###
###ZARF_VAR_OPTIONAL_FOOTER### diff --git a/examples/variables/zarf.yaml b/examples/variables/zarf.yaml index 62d6442d48..7310c39bdb 100644 --- a/examples/variables/zarf.yaml +++ b/examples/variables/zarf.yaml @@ -35,10 +35,16 @@ variables: - name: AWS_REGION default: us-east-1 sensitive: true + # MODIFIED_TERRAFORM sets a filepath for a terraform file to be used as the contents of a template + - name: MODIFIED_TERRAFORM + default: modified-terraform.tf + autoIndent: true + sensitive: true + type: file components: # The following component templates the provided .tf file with the defined AWS_REGION - # NOTE: this component does not actually execute this file in this example (see examples/terraform) + # NOTE: this component does not actually execute this file in this example - name: variables-with-terraform description: Change a value in a regular file with a Zarf variable. Set AWS_REGION variable to modify the file. required: true @@ -48,14 +54,13 @@ components: actions: onDeploy: after: - # This command `cat`s the modified terraform file on deploy for use later on (see examples/component-actions for more) - - cmd: cat modified-terraform.tf - # `mute` is set to exclude the command output from being shown (note this will include AWS_REGION which we marked sensitive above) + # This command uses Zarf to return the SHASUM of the terraform file (`type: file` variables will return the filepath instead of the contents when used in actions) + - cmd: ./zarf prepare sha256sum ${ZARF_VAR_MODIFIED_TERRAFORM} + # `mute` is set to exclude the command output from being shown (since we are treating it as sensitive below) mute: true setVariables: - - name: MODIFIED_TERRAFORM - autoIndent: true - # `sensitive` is set to exclude the command output from the logs (note this will include AWS_REGION which we marked sensitive above) + - name: MODIFIED_TERRAFORM_SHASUM + # `sensitive` is set to exclude the command output from the logs sensitive: true # The following component deploys nginx to the cluster using the defined variables diff --git a/src/internal/packager/template/template.go b/src/internal/packager/template/template.go index 3cc1f68075..8de80cd72e 100644 --- a/src/internal/packager/template/template.go +++ b/src/internal/packager/template/template.go @@ -139,6 +139,7 @@ func (values *Values) GetVariables(component types.ZarfComponent) (map[string]*u Value: variable.Value, Sensitive: variable.Sensitive, AutoIndent: variable.AutoIndent, + Type: variable.Type, } } diff --git a/src/pkg/packager/actions.go b/src/pkg/packager/actions.go index e3566bbdcc..59357bda12 100644 --- a/src/pkg/packager/actions.go +++ b/src/pkg/packager/actions.go @@ -111,7 +111,7 @@ func (p *Packager) runAction(defaultCfg types.ZarfComponentActionDefaults, actio // If an output variable is defined, set it. for _, v := range action.SetVariables { - p.setVariableInConfig(v.Name, out, v.Sensitive, v.AutoIndent) + p.setVariableInConfig(v.Name, out, v.Sensitive, v.AutoIndent, v.Type) } // If the action has a wait, change the spinner message to reflect that on success. diff --git a/src/pkg/packager/variables.go b/src/pkg/packager/variables.go index 5652059147..98ae8f005d 100644 --- a/src/pkg/packager/variables.go +++ b/src/pkg/packager/variables.go @@ -76,7 +76,7 @@ func (p *Packager) setVariableMapInConfig() error { // Ensure uppercase keys setVariableValues := helpers.TransformMapKeys(p.cfg.DeployOpts.SetVariables, strings.ToUpper) for name, value := range setVariableValues { - p.setVariableInConfig(name, value, false, false) + p.setVariableInConfig(name, value, false, false, "") } for _, variable := range p.cfg.Pkg.Variables { @@ -86,11 +86,12 @@ func (p *Packager) setVariableMapInConfig() error { if present { p.cfg.SetVariableMap[variable.Name].Sensitive = variable.Sensitive p.cfg.SetVariableMap[variable.Name].AutoIndent = variable.AutoIndent + p.cfg.SetVariableMap[variable.Name].Type = variable.Type continue } // First set default (may be overridden by prompt) - p.setVariableInConfig(variable.Name, variable.Default, variable.Sensitive, variable.AutoIndent) + p.setVariableInConfig(variable.Name, variable.Default, variable.Sensitive, variable.AutoIndent, variable.Type) // Variable is set to prompt the user if variable.Prompt && !config.CommonOptions.Confirm { @@ -101,19 +102,20 @@ func (p *Packager) setVariableMapInConfig() error { return err } - p.setVariableInConfig(variable.Name, val, variable.Sensitive, variable.AutoIndent) + p.setVariableInConfig(variable.Name, val, variable.Sensitive, variable.AutoIndent, variable.Type) } } return nil } -func (p *Packager) setVariableInConfig(name, value string, sensitive bool, autoIndent bool) { +func (p *Packager) setVariableInConfig(name, value string, sensitive bool, autoIndent bool, varType types.VariableType) { p.cfg.SetVariableMap[name] = &types.ZarfSetVariable{ Name: name, Value: value, Sensitive: sensitive, AutoIndent: autoIndent, + Type: varType, } } diff --git a/src/pkg/utils/io.go b/src/pkg/utils/io.go index 6bee5fe0fd..43c57539ca 100755 --- a/src/pkg/utils/io.go +++ b/src/pkg/utils/io.go @@ -21,6 +21,7 @@ import ( "github.com/defenseunicorns/zarf/src/pkg/message" "github.com/defenseunicorns/zarf/src/pkg/utils/helpers" + "github.com/defenseunicorns/zarf/src/types" "github.com/otiai10/copy" ) @@ -54,6 +55,7 @@ func GetCryptoHashFromFile(path string, hashName crypto.Hash) (string, error) { type TextTemplate struct { Sensitive bool AutoIndent bool + Type types.VariableType Value string } @@ -152,6 +154,14 @@ func ReplaceTextTemplate(path string, mappings map[string]*TextTemplate, depreca regexTemplateLine := regexp.MustCompile(fmt.Sprintf("(?P.*?)(?P