Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: refactor instance identity to be a SessionTokenProvider#19566

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

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletionsagent/agent.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -74,7 +74,6 @@ type Options struct {
LogDirstring
TempDirstring
ScriptDataDirstring
ExchangeTokenfunc(ctx context.Context) (string,error)
ClientClient
ReconnectingPTYTimeout time.Duration
EnvironmentVariablesmap[string]string
Expand All@@ -99,6 +98,7 @@ type Client interface {
proto.DRPCAgentClient26, tailnetproto.DRPCTailnetClient26,error,
)
tailnet.DERPMapRewriter
agentsdk.RefreshableSessionTokenProvider
}

typeAgentinterface {
Expand DownExpand Up@@ -131,11 +131,6 @@ func New(options Options) Agent {
}
options.ScriptDataDir=options.TempDir
}
ifoptions.ExchangeToken==nil {
options.ExchangeToken=func(_ context.Context) (string,error) {
return"",nil
}
}
ifoptions.ReportMetadataInterval==0 {
options.ReportMetadataInterval=time.Second
}
Expand DownExpand Up@@ -172,7 +167,6 @@ func New(options Options) Agent {
coordDisconnected:make(chanstruct{}),
environmentVariables:options.EnvironmentVariables,
client:options.Client,
exchangeToken:options.ExchangeToken,
filesystem:options.Filesystem,
logDir:options.LogDir,
tempDir:options.TempDir,
Expand DownExpand Up@@ -203,7 +197,6 @@ func New(options Options) Agent {
// coordinator during shut down.
close(a.coordDisconnected)
a.announcementBanners.Store(new([]codersdk.BannerConfig))
a.sessionToken.Store(new(string))
a.init()
returna
}
Expand All@@ -212,7 +205,6 @@ type agent struct {
clock quartz.Clock
logger slog.Logger
clientClient
exchangeTokenfunc(ctx context.Context) (string,error)
tailnetListenPortuint16
filesystem afero.Fs
logDirstring
Expand DownExpand Up@@ -254,7 +246,6 @@ type agent struct {
scriptRunner*agentscripts.Runner
announcementBanners atomic.Pointer[[]codersdk.BannerConfig]// announcementBanners is atomic because it is periodically updated.
announcementBannersRefreshInterval time.Duration
sessionToken atomic.Pointer[string]
sshServer*agentssh.Server
sshMaxTimeout time.Duration
blockFileTransferbool
Expand DownExpand Up@@ -916,11 +907,10 @@ func (a *agent) run() (retErr error) {
// This allows the agent to refresh its token if necessary.
// For instance identity this is required, since the instance
// may not have re-provisioned, but a new agent ID was created.
sessionToken,err:=a.exchangeToken(a.hardCtx)
err:=a.client.RefreshToken(a.hardCtx)
iferr!=nil {
returnxerrors.Errorf("exchange token: %w",err)
returnxerrors.Errorf("refresh token: %w",err)
}
a.sessionToken.Store(&sessionToken)

// ConnectRPC returns the dRPC connection we use for the Agent and Tailnet v2+ APIs
aAPI,tAPI,err:=a.client.ConnectRPC26(a.hardCtx)
Expand DownExpand Up@@ -1359,7 +1349,7 @@ func (a *agent) updateCommandEnv(current []string) (updated []string, err error)
"CODER_WORKSPACE_OWNER_NAME":manifest.OwnerName,

// Specific Coder subcommands require the agent token exposed!
"CODER_AGENT_TOKEN":*a.sessionToken.Load(),
"CODER_AGENT_TOKEN":a.client.GetSessionToken(),

// Git on Windows resolves with UNIX-style paths.
// If using backslashes, it's unable to find the executable.
Expand Down
34 changes: 12 additions & 22 deletionsagent/agent_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,7 +22,6 @@ import (
"slices"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"

Expand DownExpand Up@@ -2926,11 +2925,11 @@ func TestAgent_Speedtest(t *testing.T) {

funcTestAgent_Reconnect(t*testing.T) {
t.Parallel()
ctx:=testutil.Context(t,testutil.WaitShort)
logger:=testutil.Logger(t)
// After the agent is disconnected from a coordinator, it's supposed
// to reconnect!
coordinator:=tailnet.NewCoordinator(logger)
defercoordinator.Close()
fCoordinator:=tailnettest.NewFakeCoordinator()

agentID:=uuid.New()
statsCh:=make(chan*proto.Stats,50)
Expand All@@ -2942,27 +2941,24 @@ func TestAgent_Reconnect(t *testing.T) {
DERPMap:derpMap,
},
statsCh,
coordinator,
fCoordinator,
)
deferclient.Close()
initialized:= atomic.Int32{}

closer:=agent.New(agent.Options{
ExchangeToken:func(ctx context.Context) (string,error) {
initialized.Add(1)
return"",nil
},
Client:client,
Logger:logger.Named("agent"),
})
defercloser.Close()

require.Eventually(t,func()bool {
returncoordinator.Node(agentID)!=nil
},testutil.WaitShort,testutil.IntervalFast)
client.LastWorkspaceAgent()
require.Eventually(t,func()bool {
returninitialized.Load()==2
},testutil.WaitShort,testutil.IntervalFast)
call1:=testutil.RequireReceive(ctx,t,fCoordinator.CoordinateCalls)
require.Equal(t,client.GetNumRefreshTokenCalls(),1)
close(call1.Resps)// hang up
// expect reconnect
testutil.RequireReceive(ctx,t,fCoordinator.CoordinateCalls)
// Check that the agent refreshes the token when it reconnects.
require.Equal(t,client.GetNumRefreshTokenCalls(),2)
closer.Close()
}

funcTestAgent_WriteVSCodeConfigs(t*testing.T) {
Expand All@@ -2984,9 +2980,6 @@ func TestAgent_WriteVSCodeConfigs(t *testing.T) {
deferclient.Close()
filesystem:=afero.NewMemMapFs()
closer:=agent.New(agent.Options{
ExchangeToken:func(ctx context.Context) (string,error) {
return"",nil
},
Client:client,
Logger:logger.Named("agent"),
Filesystem:filesystem,
Expand DownExpand Up@@ -3015,9 +3008,6 @@ func TestAgent_DebugServer(t *testing.T) {
conn,_,_,_,agnt:=setupAgent(t, agentsdk.Manifest{
DERPMap:derpMap,
},0,func(c*agenttest.Client,o*agent.Options) {
o.ExchangeToken=func(context.Context) (string,error) {
return"token",nil
}
o.LogDir=logDir
})

Expand Down
10 changes: 1 addition & 9 deletionsagent/agenttest/agent.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
package agenttest

import (
"context"
"net/url"
"testing"

Expand DownExpand Up@@ -31,18 +30,11 @@ func New(t testing.TB, coderURL *url.URL, agentToken string, opts ...func(*agent
}

ifo.Client==nil {
agentClient:=agentsdk.New(coderURL)
agentClient.SetSessionToken(agentToken)
agentClient:=agentsdk.New(coderURL,agentsdk.WithFixedToken(agentToken))
agentClient.SDK.SetLogger(log)
o.Client=agentClient
}

ifo.ExchangeToken==nil {
o.ExchangeToken=func(_ context.Context) (string,error) {
returnagentToken,nil
}
}

ifo.LogDir=="" {
o.LogDir=t.TempDir()
}
Expand Down
34 changes: 30 additions & 4 deletionsagent/agenttest/client.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@ package agenttest
import (
"context"
"io"
"net/http"
"slices"
"sync"
"sync/atomic"
Expand All@@ -28,6 +29,7 @@ import (
"github.com/coder/coder/v2/tailnet"
"github.com/coder/coder/v2/tailnet/proto"
"github.com/coder/coder/v2/testutil"
"github.com/coder/websocket"
)

conststatsInterval=500*time.Millisecond
Expand DownExpand Up@@ -86,10 +88,34 @@ type Client struct {
fakeAgentAPI*FakeAgentAPI
LastWorkspaceAgentfunc()

mu sync.Mutex// Protects following.
logs []agentsdk.Log
derpMapUpdateschan*tailcfg.DERPMap
derpMapOnce sync.Once
mu sync.Mutex// Protects following.
logs []agentsdk.Log
derpMapUpdateschan*tailcfg.DERPMap
derpMapOnce sync.Once
refreshTokenCallsint
}

func (*Client)AsRequestOption() codersdk.RequestOption {
returnfunc(_*http.Request) {}
}

func (*Client)SetDialOption(*websocket.DialOptions) {}

func (*Client)GetSessionToken()string {
return"agenttest-token"
}

func (c*Client)RefreshToken(context.Context)error {
c.mu.Lock()
deferc.mu.Unlock()
c.refreshTokenCalls++
returnnil
}

func (c*Client)GetNumRefreshTokenCalls()int {
c.mu.Lock()
deferc.mu.Unlock()
returnc.refreshTokenCalls
}

func (*Client)RewriteDERPMap(*tailcfg.DERPMap) {}
Expand Down
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp