-
Notifications
You must be signed in to change notification settings - Fork 22
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+70
−11
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5418ed7
feat: allow presets to define prebuilds
SasSwart af25037
document prebuild parameters
SasSwart 56d1ab7
remove todo
SasSwart c8c5101
make gen
SasSwart 0a50b31
feat: reuse agent tokens when a prebuilt agent reinitializes
SasSwart 50bda99
Merge remote-tracking branch 'origin/main' into jjs/364
SasSwart e46f69a
WIP: get agent.go ready to be merged with support for prebuilds
SasSwart 0f5842a
fix: ensure the agent token is reused for prebuilds
SasSwart f0e699a
lint and make gen
SasSwart 5a2fd97
Merge remote-tracking branch 'origin/main' into jjs/364
SasSwart e51bf1c
simplify function
SasSwart eff062b
test: rbac role test assertion to handle site wide roles
Emyrk 7b1d8e6
Merge remote-tracking branch 'origin/main' into jjs/364
SasSwart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,38 @@ func agentResource() *schema.Resource { | |
SchemaVersion: 1, | ||
|
||
Description: "Use this resource to associate an agent.", | ||
CreateContext: func(_ context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
// This should be a real authentication token! | ||
resourceData.SetId(uuid.NewString()) | ||
err := resourceData.Set("token", uuid.NewString()) | ||
CreateContext: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
agentID := uuid.NewString() | ||
resourceData.SetId(agentID) | ||
|
||
// 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() | ||
|
||
// If isPrebuild is true, then this workspace was built by the prebuilds system. | ||
// This does not determine whether the workspace has been claimed by a user. | ||
// At this point, it may or may not have been claimed. | ||
isPrebuild := helpers.OptionalEnv(IsPrebuildEnvironmentVariable()) == "true" | ||
// existingToken should only have been set if isPrebuild is true, because we only | ||
// reuse the token when a prebuilt workspace is being claimed. | ||
existingToken := helpers.OptionalEnv(RunningAgentTokenEnvironmentVariable(agentID)) | ||
logFields := map[string]interface{}{ | ||
"agent_id": agentID, | ||
"is_prebuild": isPrebuild, | ||
"token_provided": existingToken != "", | ||
} | ||
if isPrebuild && existingToken != "" { | ||
// check if a token was already generated for this agent. | ||
// If so, this workspace is in the process of being claimed | ||
// and we should reuse the token. If not, we use a new token as usual. | ||
tflog.Info(ctx, "using provided agent token for prebuild", logFields) | ||
token = existingToken | ||
} else { | ||
tflog.Info(ctx, "using a new agent token", logFields) | ||
} | ||
SasSwart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
err := resourceData.Set("token", token) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
@@ -469,3 +500,16 @@ func updateInitScript(resourceData *schema.ResourceData, i interface{}) diag.Dia | |
} | ||
return nil | ||
} | ||
|
||
// RunningAgentTokenEnvironmentVariable returns the name of the environment variable | ||
// that contains the token 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. | ||
SasSwart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// 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 |
||
agentID = "" // remove this once we need to support multiple agents per prebuilt workspace. | ||
|
||
sum := sha256.Sum256([]byte(agentID)) | ||
return "CODER_RUNNING_WORKSPACE_AGENT_TOKEN_" + hex.EncodeToString(sum[:]) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.