Skip to content

feature: extract coder_workspace_tags from template, detect dangerous data source usage #15087

Closed

Description

@johnstcn

Relates to #15047

Motivation

Provisioner daemons can expose a set of tags [1] that allow Coder administrators to control the context in which a workspace or template job are evaluated. When creating a template version import job or workspace build job, users can manually specify a set of tags to be applied to the job as part of the request.

These tags are normally evaluated in coderd/wsbuilder.

The coder_workspace_tags [2] data source allows users to specify provisioner tags for workspaces built from a given template.

Example:

data "coder_workspace_tags" "custom_workspace_tags" {
  tags = {
    "zone"        = "developers"
    "runtime"     = data.coder_parameter.runtime_selector.value
    "project_id"  = "PROJECT_${data.coder_parameter.project_name.value}"
    "cache"       = data.coder_parameter.feature_cache_enabled.value == "true" ? "with-cache" : "no-cache"
  }
}

While investigating #15047 we noticed an issue whereby a template import job would appear to 'hang'. This turned out to be due to the template specifying a set of tags in the HCL but not having any suitably tagged provisioners available. There is an inherent chicken and egg problem here, as Coder would need to run the job to determine the tag values, but needs the tag values to schedule the job.

Additionally, we also noted an issue where users could specify potentially mutable / variable data sources as a value source for coder_workspace_tags.
Example:

data "coder_workspace_tags" "custom_workspace_tags" {
  tags = {
    "runtime"     = data.env_var.runtime.value # using some provider that reads environment variables
  }
}

The value of that environment variable is not guaranteed to resolve to the same value, depending on what provisioner is running the job.

Proposed Solution

  1. Add a pre-flight check when importing a template. This will involve parsing the Terraform files provided, checking for any declarations of coder_workspace_tags, and validating that the values for each key resolve to either:
  • A static variable ("developers")
  • A template-level variable (var.az)
  • A coder parameter (data.coder_parameter.foo.value)

Any other data sources for workspace tags should cause a large warning on the potential ramifications of this block the template import job as the required data sources will not be available. We could also block a template version import but that runs the risk of blocking a critical path for a hypothetical problem.

As a side-effect, the pre-flight check can also automatically set the provisioner tags it discovers on the provisioner job.

[1] https://coder.com/docs/admin/templates/extending-templates/workspace-tags
[2] https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/workspace_tags

Activity

  1. added
    needs-rfc
    s3
    on Oct 15, 2024
  2. removed
    needs-rfc
    on Oct 15, 2024
  3. self-assigned this
    on Oct 15, 2024
  4. ammario commented on Oct 15, 2024

    @ammario
    Member

    Is it really important to support dynamic tags in the template? Does it really make sense for the tags to specified in the template when the evaluation of such tags depends on the provisioner? There may be some way we can jank it by first routing the request to the last known provisioner and then re-routing it if the tags suggest a different provisioner. That would be pretty hard to reason about and has surprising operations / security ramifications.

    At first glance it makes more sense to put tags in coderd_template.

  5. johnstcn commented on Oct 15, 2024

    @johnstcn
    MemberAuthor

    Is it really important to support dynamic tags in the template?

    @ammario from my read of the background motivation for #5979 it appears it was a customer-requested feature.

  6. bpmct commented on Oct 16, 2024

    @bpmct
    Member

    Yes, dynamic tags in the template is important

  7. ammario commented on Oct 16, 2024

    @ammario
    Member

    I see a single user requested for provisioning local workspaces. If it's just that it's pretty niche and potentially solvable outside dynamic tags.

    What are the other supporting requests or use cases? #5979 is detailed on implementation but not user value.

    Maybe @kylecarbs could shed some light here.

  8. spikecurtis commented on Oct 16, 2024

    @spikecurtis
    Collaborator

    @ammario it's to allow a single template to target multiple clusters, where the provisionerd is specific to the cluster. Making provisionerds specific to a cluster is important to security isolation, so each can have credentials on just one cluster. Often you want to run the provisionerd within the cluster itself to take advantage of cluster-provided IAM (AWS role, Kubernetes service account, etc).

    Without this, you'd need to have a different template per cluster, or have a single provisionerd have credentials on multiple clusters.

  9. spikecurtis commented on Oct 16, 2024

    @spikecurtis
    Collaborator

    Any other data sources for workspace tags should cause a large warning on the potential ramifications of this. We could also block a template version import but that runs the risk of blocking a critical path for a hypothetical problem.

    This needs to be a hard block, not a warning. Other data sources are not available at the time we evaluate the tags (before the provisionerd is even chosen), so using them will be broken, plain and simple.

  10. mtojek commented on Oct 16, 2024

    @mtojek
    Member

    Can we mention/document use case when the proposed solution may not work and could be exploited?

  11. johnstcn commented on Oct 16, 2024

    @johnstcn
    MemberAuthor

    Can we mention/document use case when the proposed solution may not work and could be exploited?

    Yes, documentation will be an important component of this too. (Context: the workspace_tags data source could be defined outside of the scope of the template, such as a Terraform module.)

  12. johnstcn commented on Oct 16, 2024

    @johnstcn
    MemberAuthor

    Implementation note: there is existing logic in provisioner/terraform that should probably be re-used here for consistency.

  13. added
    must-do
    s2
    and removed
    s3
    on Nov 7, 2024
  14. bpmct commented on Nov 18, 2024

    @bpmct
    Member

    @johnstcn can you be sure to work with @EdwardAngert to get this in our documentation for the release?

  15. johnstcn commented on Nov 25, 2024

    @johnstcn
    MemberAuthor

    #15578 adds logic to extract and validate tags from uploaded templates.
    Docs are updated in #15620.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

must-dos2

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions