Skip to content

Commit 40a798b

Browse files
committed
Say which way a session echo missed, not always that Basecamp predates sessions
A server that echoed this session's id but a different label, or none, was still told it predates agent session attribution, which its own echo contradicts. Only a missing id says that now; an id for another session and a label that came back wrong or not at all each say so.
1 parent c56bff3 commit 40a798b

2 files changed

Lines changed: 35 additions & 11 deletions

File tree

‎internal/auth/agent_launch.go‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,26 @@ func (l *agentLaunch) requireEcho(status int, body []byte) error {
135135
return nil
136136
}
137137

138+
// Say which way the echo missed. Only a server that echoed no id at
139+
// all is one that predates sessions; one that echoed this id records
140+
// them, and what it got wrong is the label.
138141
msg := fmt.Sprintf("Basecamp minted a token but did not bind it to session %s, so it was discarded", l.id)
139-
if echoed.ID == nil {
142+
hint := "A session token needs a Basecamp that records agent sessions; without one, leave out --session-id and --session-label"
143+
switch {
144+
case echoed.ID == nil:
140145
msg += ": this Basecamp predates agent session attribution"
141-
} else {
142-
msg += ": this Basecamp predates agent session attribution, or answered for a different session"
146+
case !idMatches:
147+
msg += ": Basecamp answered for a different session"
148+
hint = "Run it again; if it keeps answering for another session, report it"
149+
case echoed.Label == nil:
150+
msg += ": Basecamp recorded the session but did not record the label"
151+
hint = "Leave out --session-label to mint for the session alone"
152+
default:
153+
msg += ": Basecamp recorded a different label for the session"
154+
hint = "Leave out --session-label to mint for the session alone"
143155
}
144156
e := output.ErrAPI(status, msg)
145-
e.Hint = "A session token needs a Basecamp that records agent sessions; without one, leave out --session-id and --session-label"
157+
e.Hint = hint
146158
e.Cause = errLaunchNotRecorded
147159
return e
148160
}

‎internal/auth/agent_launch_test.go‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,25 @@ func TestSessionTokenWithoutLabelSendsNoLabel(t *testing.T) {
9090

9191
// TestSessionTokenFromAServerThatIgnoresTheSessionFailsClosed: a Basecamp
9292
// that predates session attribution answers with an ordinary token. That
93-
// token is not bound to the session, so it is discarded, not printed.
93+
// token is not bound to the session, so it is discarded, not printed. The
94+
// message says which way the echo missed: a server that echoed the id
95+
// does record sessions, so it is not told it predates them.
9496
func TestSessionTokenFromAServerThatIgnoresTheSessionFailsClosed(t *testing.T) {
95-
for name, body := range map[string]string{
96-
"no echo": `{"access_token":"unbound","token_type":"Bearer","expires_in":3600}`,
97-
"other id": `{"access_token":"unbound","token_type":"Bearer","expires_in":3600,"launch_id":"ffffffffffffffffffffffffffffffff","launch_label":"coworker@box"}`,
98-
"other label": `{"access_token":"unbound","token_type":"Bearer","expires_in":3600,"launch_id":"` + testSessionID + `","launch_label":"someone-else"}`,
99-
"label not given": `{"access_token":"unbound","token_type":"Bearer","expires_in":3600,"launch_id":"` + testSessionID + `"}`,
97+
for name, tc := range map[string]struct{ body, says, never string }{
98+
"no echo": {
99+
`{"access_token":"unbound","token_type":"Bearer","expires_in":3600}`,
100+
"predates agent session attribution", "different"},
101+
"other id": {
102+
`{"access_token":"unbound","token_type":"Bearer","expires_in":3600,"launch_id":"ffffffffffffffffffffffffffffffff","launch_label":"coworker@box"}`,
103+
"answered for a different session", "predates"},
104+
"other label": {
105+
`{"access_token":"unbound","token_type":"Bearer","expires_in":3600,"launch_id":"` + testSessionID + `","launch_label":"someone-else"}`,
106+
"recorded a different label", "predates"},
107+
"label not given": {
108+
`{"access_token":"unbound","token_type":"Bearer","expires_in":3600,"launch_id":"` + testSessionID + `"}`,
109+
"did not record the label", "predates"},
100110
} {
111+
body := tc.body
101112
t.Run(name, func(t *testing.T) {
102113
as := startDeviceAS(t)
103114
as.token = func(int) (int, string) { return http.StatusOK, body }
@@ -109,7 +120,8 @@ func TestSessionTokenFromAServerThatIgnoresTheSessionFailsClosed(t *testing.T) {
109120
assert.Empty(t, token)
110121
assert.NotContains(t, err.Error(), "unbound", "the unbound token must not leak into the error")
111122
assert.Contains(t, err.Error(), testSessionID)
112-
assert.Contains(t, err.Error(), "predates agent session attribution")
123+
assert.Contains(t, err.Error(), tc.says)
124+
assert.NotContains(t, err.Error(), tc.never)
113125

114126
stored, loadErr := m.store.Load(key)
115127
require.NoError(t, loadErr)

0 commit comments

Comments
 (0)