Skip to content

Commit ebd9dcf

Browse files
Fix respect Server Decision to use v2 broker flow (#222)
1 parent 3d7fa31 commit ebd9dcf

4 files changed

Lines changed: 123 additions & 27 deletions

File tree

protocol/connection.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,16 +357,32 @@ func (vssConnection *VssConnection) CreateSessionExt(ctx context.Context, server
357357

358358
con := &AgentMessageConnection{VssConnection: vssConnection, TaskAgentSession: session}
359359
con.Status = statusOnline
360+
con.ServerV2URL = serverV2URL
360361
return con, nil
361362
}
362363

363364
func (vssConnection *VssConnection) CreateSession(ctx context.Context) (*AgentMessageConnection, error) {
364-
return vssConnection.CreateSessionExt(ctx, vssConnection.TaskAgent.ServerV2URL)
365+
useV2Flow, hasUseV2Flow := vssConnection.TaskAgent.Properties.LookupBool("UseV2Flow")
366+
serverV2URL, hasServerV2URL := vssConnection.TaskAgent.Properties.LookupString("ServerUrlV2")
367+
if !useV2Flow || !hasUseV2Flow || !hasServerV2URL {
368+
serverV2URL = ""
369+
} else {
370+
serverV2URL = strings.TrimSuffix(serverV2URL, "/")
371+
}
372+
return vssConnection.CreateSessionExt(ctx, serverV2URL)
365373
}
366374

367375
func (vssConnection *VssConnection) LoadSession(ctx context.Context, session *TaskAgentSession) (*AgentMessageConnection, error) {
368376
con := &AgentMessageConnection{VssConnection: vssConnection, TaskAgentSession: session}
369377
con.Status = statusOnline
378+
useV2Flow, hasUseV2Flow := vssConnection.TaskAgent.Properties.LookupBool("UseV2Flow")
379+
serverV2URL, hasServerV2URL := vssConnection.TaskAgent.Properties.LookupString("ServerUrlV2")
380+
if !useV2Flow || !hasUseV2Flow || !hasServerV2URL {
381+
serverV2URL = ""
382+
} else {
383+
serverV2URL = strings.TrimSuffix(serverV2URL, "/")
384+
}
385+
con.ServerV2URL = serverV2URL
370386
return con, nil
371387
}
372388

protocol/task_agent.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"net/http"
1010
"net/url"
11+
"strings"
1112
"time"
1213

1314
"github.com/golang-jwt/jwt"
@@ -39,6 +40,57 @@ type AgentLabel struct {
3940
Type string
4041
}
4142

43+
type PropertyValue struct {
44+
Type string `json:"$type"`
45+
Value interface{} `json:"$value"`
46+
}
47+
48+
func (v *PropertyValue) UnmarshalJSON(data []byte) error {
49+
var b bool
50+
if json.Unmarshal(data, &b) == nil {
51+
v.Type = "System.Boolean"
52+
v.Value = b
53+
return nil
54+
}
55+
var raw string
56+
if json.Unmarshal(data, &raw) == nil {
57+
v.Type = "System.String"
58+
v.Value = raw
59+
return nil
60+
}
61+
type PropertyValueRaw PropertyValue
62+
// Best Effort, drop errors
63+
_ = json.Unmarshal(data, (*PropertyValueRaw)(v))
64+
return nil
65+
}
66+
67+
type PropertiesCollection map[string]PropertyValue
68+
69+
func (c *PropertiesCollection) Lookup(name, ty string) (interface{}, bool) {
70+
for k, v := range *c {
71+
if strings.EqualFold(k, name) && strings.EqualFold(v.Type, ty) {
72+
return v.Value, true
73+
}
74+
}
75+
return nil, false
76+
}
77+
78+
func (c *PropertiesCollection) LookupBool(name string) (value, ok bool) {
79+
if v, ok := c.Lookup(name, "System.Boolean"); ok && v != nil {
80+
b, isBool := v.(bool)
81+
return b, isBool
82+
}
83+
return false, false
84+
}
85+
86+
func (c *PropertiesCollection) LookupString(name string) (string, bool) {
87+
if v, ok := c.Lookup(name, "System.String"); ok && v != nil {
88+
b, isString := v.(string)
89+
return b, isString
90+
}
91+
return "", false
92+
}
93+
4294
type TaskAgent struct {
4395
Authorization TaskAgentAuthorization
4496
Labels []AgentLabel
@@ -53,8 +105,7 @@ type TaskAgent struct {
53105
CreatedOn string
54106
Ephemeral bool `json:",omitempty"`
55107
DisableUpdate bool `json:",omitempty"`
56-
// Just a convenient way to store the URL, not part of the spec
57-
ServerV2URL string `json:",omitempty"`
108+
Properties PropertiesCollection
58109
}
59110

60111
type TaskAgents struct {

runnerconfiguration/add.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (config *ConfigureRunner) Configure(
8181
}
8282
if res.UseV2FLow {
8383
vssConnection = &protocol.VssConnection{
84-
AuthHeader: "RemoteAuth " + config.Token,
84+
AuthHeader: "Bearer " + res.Token,
8585
Trace: config.Trace,
8686
Client: c,
8787
}
@@ -309,7 +309,6 @@ func registerOrReplaceRunnerV2(taskAgent *protocol.TaskAgent, config *ConfigureR
309309
taskAgent.Name = runnerResp.Name
310310
taskAgent.Authorization.AuthorizationURL = runnerResp.Authorization.AuthorizationURL
311311
taskAgent.Authorization.ClientID = runnerResp.Authorization.ClientId
312-
taskAgent.ServerV2URL = runnerResp.Authorization.ServerURL
313312
return nil
314313
}
315314

runnerconfiguration/compat/actions_runner_compat.go

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ type DotnetRsaParameters struct {
2525
}
2626

2727
type DotnetAgent struct {
28-
AgentID string `json:"AgentId"`
29-
AgentName string `json:"AgentName"`
30-
DisableUpdate string `json:"DisableUpdate"`
31-
Ephemeral string `json:"Ephemeral"`
32-
PoolID string `json:"PoolId"`
33-
PoolName string `json:"PoolName,omitempty"`
34-
ServerURL string `json:"ServerUrl"`
35-
WorkFolder string `json:"WorkFolder"`
36-
GitHubURL string `json:"GitHubUrl"`
37-
UseV2Flow bool `json:"UseV2Flow"`
38-
ServerURLV2 string `json:"ServerUrlV2"`
28+
AgentID string `json:"AgentId"`
29+
AgentName string `json:"AgentName"`
30+
DisableUpdate string `json:"DisableUpdate"`
31+
Ephemeral string `json:"Ephemeral"`
32+
PoolID string `json:"PoolId"`
33+
PoolName string `json:"PoolName,omitempty"`
34+
ServerURL string `json:"ServerUrl"`
35+
WorkFolder string `json:"WorkFolder"`
36+
GitHubURL string `json:"GitHubUrl"`
37+
UseV2Flow bool `json:"UseV2Flow"`
38+
ServerURLV2 string `json:"ServerUrlV2"`
39+
UseRunnerAdminFlow bool `json:"UseRunnerAdminFlow,omitempty"`
3940
}
4041

4142
type DotnetCredentials struct {
@@ -138,10 +139,32 @@ func ToRunnerInstance(fileAccess ConfigFileAccess) (*runnerconfiguration.RunnerI
138139
}
139140
ephemeral, _ := strconv.ParseBool(agent.Ephemeral)
140141
disableUpdate, _ := strconv.ParseBool(agent.DisableUpdate)
142+
143+
props := protocol.PropertiesCollection{}
144+
if agent.ServerURL != "" {
145+
props["ServerUrl"] = protocol.PropertyValue{
146+
Type: "System.String",
147+
Value: agent.ServerURL,
148+
}
149+
}
150+
if agent.ServerURLV2 != "" {
151+
props["ServerUrlV2"] = protocol.PropertyValue{
152+
Type: "System.String",
153+
Value: agent.ServerURLV2,
154+
}
155+
}
156+
if agent.UseV2Flow {
157+
props["UseV2Flow"] = protocol.PropertyValue{
158+
Type: "System.Boolean",
159+
Value: agent.UseV2Flow,
160+
}
161+
}
162+
141163
return &runnerconfiguration.RunnerInstance{
142164
PoolID: poolID,
143165
Auth: &protocol.GitHubAuthResult{
144166
TenantURL: agent.ServerURL,
167+
UseV2FLow: agent.UseRunnerAdminFlow,
145168
},
146169
PKey: FromRsaParameters(rsaParameters),
147170
Agent: &protocol.TaskAgent{
@@ -155,25 +178,32 @@ func ToRunnerInstance(fileAccess ConfigFileAccess) (*runnerconfiguration.RunnerI
155178
},
156179
DisableUpdate: disableUpdate,
157180
Version: "3.0.0",
158-
ServerV2URL: agent.ServerURLV2,
181+
Properties: props,
159182
},
160183
WorkFolder: agent.WorkFolder,
161184
RegistrationURL: agent.GitHubURL,
162185
}, nil
163186
}
164187

165188
func FromRunnerInstance(instance *runnerconfiguration.RunnerInstance, fileAccess ConfigFileAccess) error {
189+
useV2Flow, _ := instance.Agent.Properties.LookupBool("UseV2Flow")
190+
serverURL, ok := instance.Agent.Properties.LookupString("ServerUrl")
191+
if serverURL == "" || !ok {
192+
serverURL = instance.Auth.TenantURL
193+
}
194+
serverV2URL, _ := instance.Agent.Properties.LookupString("ServerUrlV2")
166195
agent := &DotnetAgent{
167-
AgentID: fmt.Sprint(instance.Agent.ID),
168-
AgentName: instance.Agent.Name,
169-
Ephemeral: fmt.Sprint(instance.Agent.Ephemeral),
170-
DisableUpdate: fmt.Sprint(instance.Agent.DisableUpdate),
171-
PoolID: fmt.Sprint(instance.PoolID),
172-
ServerURL: instance.Auth.TenantURL,
173-
WorkFolder: instance.WorkFolder,
174-
GitHubURL: instance.RegistrationURL,
175-
UseV2Flow: instance.Auth.UseV2FLow,
176-
ServerURLV2: instance.Agent.ServerV2URL,
196+
AgentID: fmt.Sprint(instance.Agent.ID),
197+
AgentName: instance.Agent.Name,
198+
Ephemeral: fmt.Sprint(instance.Agent.Ephemeral),
199+
DisableUpdate: fmt.Sprint(instance.Agent.DisableUpdate),
200+
PoolID: fmt.Sprint(instance.PoolID),
201+
ServerURL: serverURL,
202+
WorkFolder: instance.WorkFolder,
203+
GitHubURL: instance.RegistrationURL,
204+
UseV2Flow: useV2Flow,
205+
ServerURLV2: serverV2URL,
206+
UseRunnerAdminFlow: instance.Auth.UseV2FLow,
177207
}
178208
if agent.WorkFolder == "" {
179209
agent.WorkFolder = "_work"

0 commit comments

Comments
 (0)