- Notifications
You must be signed in to change notification settings - Fork23
feat: reuse agent tokens when a prebuilt agent reinitializes#374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
5418ed7
af25037
56d1ab7
c8c5101
0a50b31
50bda99
e46f69a
0f5842a
f0e699a
5a2fd97
e51bf1c
eff062b
7b1d8e6
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2,13 +2,16 @@ package provider | ||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"path/filepath" | ||
"reflect" | ||
"strings" | ||
"github.com/google/uuid" | ||
"github.com/hashicorp/go-cty/cty" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
@@ -22,10 +25,12 @@ func agentResource() *schema.Resource { | ||
SchemaVersion: 1, | ||
Description: "Use this resource to associate an agent.", | ||
CreateContext: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
agentID := uuid.NewString() | ||
resourceData.SetId(agentID) | ||
token := agentAuthToken(ctx, "") | ||
err := resourceData.Set("token", token) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
@@ -48,10 +53,12 @@ func agentResource() *schema.Resource { | ||
return updateInitScript(resourceData, i) | ||
}, | ||
ReadWithoutTimeout: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
token := agentAuthToken(ctx, "") | ||
err := resourceData.Set("token", token) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if _, ok := resourceData.GetOk("display_apps"); !ok { | ||
err = resourceData.Set("display_apps", []interface{}{ | ||
map[string]bool{ | ||
@@ -469,3 +476,37 @@ func updateInitScript(resourceData *schema.ResourceData, i interface{}) diag.Dia | ||
} | ||
return nil | ||
} | ||
func agentAuthToken(ctx context.Context, agentID string) string { | ||
existingToken := helpers.OptionalEnv(RunningAgentTokenEnvironmentVariable(agentID)) | ||
if existingToken == "" { | ||
// Most of the time, we will generate a new token for the agent. | ||
// In the case of a prebuilt workspace being claimed, we will override with | ||
// an existing token provided below. | ||
token := uuid.NewString() | ||
return token | ||
} | ||
// An existing token was provided for this agent. That means that this | ||
// is a prebuilt workspace in the process of being claimed. | ||
// We should reuse the token. | ||
tflog.Info(ctx, "using provided agent token for prebuild", map[string]interface{}{ | ||
"agent_id": agentID, | ||
}) | ||
return existingToken | ||
} | ||
// RunningAgentTokenEnvironmentVariable returns the name of an environment variable | ||
// that contains the token to use for the running agent. This is used for prebuilds, | ||
// where we want to reuse the same token for the next iteration of a workspace agent | ||
// before and after the workspace was claimed by a user. | ||
// | ||
// By reusing an existing token, we can avoid the need to change a value that may have been | ||
// used immutably. Thus, allowing us to avoid reprovisioning resources that may take a long time | ||
// to replace. | ||
// | ||
// agentID is unused for now, but will be used as soon as we support multiple agents. | ||
func RunningAgentTokenEnvironmentVariable(agentID string) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. How will the ID be injected? How will we maintain a persistent identity across | ||
sum := sha256.Sum256([]byte(agentID)) | ||
return "CODER_RUNNING_WORKSPACE_AGENT_TOKEN_" + hex.EncodeToString(sum[:]) | ||
} |
Uh oh!
There was an error while loading.Please reload this page.