Skip to content

Make additional data from external auth provider available to resource coder_external_auth #12704

Closed

Description

@MaximeNoulin

While I'm setting up an instance of coder (by the way great work guys!), I wanted to auto-add the git author name and email to the workspace.

The module "Git config" (https://registry.coder.com/modules/git-config) works pretty good but the data could only come from the coder user.

I wanted to set up the variable using the external-auth account as it makes more sense to be sync with the Gitlab account then the Coder account.

For now, we can only get the access_token from the resource "coder_external_auth", but it could open a lot of feature cases to have access to data like:

  • the username of the user
  • the full name of the user (the one I would need in my example)

I've tried to check in the codebase how complicated it could be to add it, but I couldn't manage to find the way it has been implemented

Feel free to ask if I'm not clear on what I'm suggesting

Activity

  1. kylecarbs commented on Mar 21, 2024

    @kylecarbs
    Member

    I think you could use the Gitlab current user data source to accomplish this.

    We intentionally don't obtain extra data from the tokens because we could add so many fields that differ per provider.

  2. kylecarbs commented on Mar 21, 2024

    @kylecarbs
    Member

    @MaximeNoulin, it might look something like this:

    data "coder_git_auth" "gitlab" {
      id = "gitlab"
    }
    
    provider "gitlab" {
      token = data.coder_git_auth.gitlab.access_token
    }
    
    data "gitlab_current_user" "user" {}
    
    resource "kubernetes_pod" "dev" {
      name = data.gitlab_current_user.user.name
    }
  3. added
    waiting-for-info
    on Mar 21, 2024
  4. MaximeNoulin commented on Mar 21, 2024

    @MaximeNoulin
    Author

    @kylecarbs that would be awesome but it seems that we can't use datasource data to configure a provider as it's loaded by terraform at first (unless I'm mistaken)

  5. kylecarbs commented on Mar 21, 2024

    @kylecarbs
    Member

    @MaximeNoulin I believe you're mistaken. I've done this pattern before. You cannot use resources to configure a provider, but you can data sources.

  6. MaximeNoulin commented on Mar 21, 2024

    @MaximeNoulin
    Author

    Are you sure? I've tried it for the past few hours and I couldn't make it work

    And then I found this doc on terraform documentation: https://developer.hashicorp.com/terraform/language/providers/configuration

    "You can use expressions in the values of these configuration arguments, but can only reference values that are known before the configuration is applied. This means you can safely reference input variables, but not attributes exported by resources (with an exception for resource arguments that are specified directly in the configuration)."

  7. kylecarbs commented on Mar 21, 2024

    @kylecarbs
    Member

    @MaximeNoulin just tried it.

    image

    Exact code:

    terraform {
      required_providers {
        gitlab = {
          source = "gitlabhq/gitlab"
          version = "16.9.1"
        }
            coder = {
          source = "coder/coder"
          version = "0.18.0"
        }
      }
    }
    
    data "coder_external_auth" "test" {
        id = "gitlab"
    }
    
    provider "gitlab" {
        token = data.coder_external_auth.test.access_token
    }
    
    data "gitlab_current_user" "user" {}
  8. MaximeNoulin commented on Mar 21, 2024

    @MaximeNoulin
    Author

    Well I'm getting crazy here ahah

    I've tried:

    • to export the access_token in a coder_env to be absolutely sure there is a token (and I've got one)
    • I've created a new template with the button "Create Template" > "From scratch", add your code, and try to build it, but it refuse to build (as I guess there is no external auth token when building the template)

    To be able to build the template, I had to use some tricks so that it won't call gitlab unless he has a token

    provider "gitlab" {
      early_auth_check = false
      token = coalesce(data.coder_external_auth.gitlab.access_token, "-") 
    }
    
    data "gitlab_current_user" "me" {
      count = length(data.coder_external_auth.gitlab.access_token) > 0 ? 1 : 0
      depends_on = [ data.coder_external_auth.gitlab ]
    }

    I got the error

    Error: unable to extract iid from global id "". Was looking for an integer after the last slash (/).
    

    And the error seems to be the same if I force "-" as a token

  9. kylecarbs commented on Mar 21, 2024

    @kylecarbs
    Member

    Super weird. You could always just use the HTTP data provider and make the call yourself... but that's kinda weird.

    data "http" "example" {
      url = "https://gitlab.com/api/v4/user"
    
      request_headers = {
        Authorization = "Bearer ${data.coder_aexternal_auth.gitlab.access_token}"
        Accept = "application/json"
      }
    }
  10. MaximeNoulin commented on Apr 2, 2024

    @MaximeNoulin
    Author

    Thanks a lot @kylecarbs i'll do that then

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiwaiting-for-info

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions