Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 17 additions & 1 deletion protocol/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,16 +357,32 @@ func (vssConnection *VssConnection) CreateSessionExt(ctx context.Context, server

con := &AgentMessageConnection{VssConnection: vssConnection, TaskAgentSession: session}
con.Status = statusOnline
con.ServerV2URL = serverV2URL
return con, nil
}

func (vssConnection *VssConnection) CreateSession(ctx context.Context) (*AgentMessageConnection, error) {
return vssConnection.CreateSessionExt(ctx, vssConnection.TaskAgent.ServerV2URL)
useV2Flow, hasUseV2Flow := vssConnection.TaskAgent.Properties.LookupBool("UseV2Flow")
serverV2URL, hasServerV2URL := vssConnection.TaskAgent.Properties.LookupString("ServerUrlV2")
if !useV2Flow || !hasUseV2Flow || !hasServerV2URL {
serverV2URL = ""
} else {
serverV2URL = strings.TrimSuffix(serverV2URL, "/")
}
return vssConnection.CreateSessionExt(ctx, serverV2URL)
}

func (vssConnection *VssConnection) LoadSession(ctx context.Context, session *TaskAgentSession) (*AgentMessageConnection, error) {
con := &AgentMessageConnection{VssConnection: vssConnection, TaskAgentSession: session}
con.Status = statusOnline
useV2Flow, hasUseV2Flow := vssConnection.TaskAgent.Properties.LookupBool("UseV2Flow")
serverV2URL, hasServerV2URL := vssConnection.TaskAgent.Properties.LookupString("ServerUrlV2")
if !useV2Flow || !hasUseV2Flow || !hasServerV2URL {
serverV2URL = ""
} else {
serverV2URL = strings.TrimSuffix(serverV2URL, "/")
}
con.ServerV2URL = serverV2URL
return con, nil
}

Expand Down
55 changes: 53 additions & 2 deletions protocol/task_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/url"
"strings"
"time"

"github.com/golang-jwt/jwt"
Expand Down Expand Up @@ -39,6 +40,57 @@ type AgentLabel struct {
Type string
}

type PropertyValue struct {
Type string `json:"$type"`
Value interface{} `json:"$value"`
}

func (v *PropertyValue) UnmarshalJSON(data []byte) error {
var b bool
if json.Unmarshal(data, &b) == nil {
v.Type = "System.Boolean"
v.Value = b
return nil
}
var raw string
if json.Unmarshal(data, &raw) == nil {
v.Type = "System.String"
v.Value = raw
return nil
}
type PropertyValueRaw PropertyValue
// Best Effort, drop errors
_ = json.Unmarshal(data, (*PropertyValueRaw)(v))
return nil
}

type PropertiesCollection map[string]PropertyValue

func (c *PropertiesCollection) Lookup(name, ty string) (interface{}, bool) {
for k, v := range *c {
if strings.EqualFold(k, name) && strings.EqualFold(v.Type, ty) {
return v.Value, true
}
}
return nil, false
}

func (c *PropertiesCollection) LookupBool(name string) (value, ok bool) {
if v, ok := c.Lookup(name, "System.Boolean"); ok && v != nil {
b, isBool := v.(bool)
return b, isBool
}
return false, false
}

func (c *PropertiesCollection) LookupString(name string) (string, bool) {
if v, ok := c.Lookup(name, "System.String"); ok && v != nil {
b, isString := v.(string)
return b, isString
}
return "", false
}

type TaskAgent struct {
Authorization TaskAgentAuthorization
Labels []AgentLabel
Expand All @@ -53,8 +105,7 @@ type TaskAgent struct {
CreatedOn string
Ephemeral bool `json:",omitempty"`
DisableUpdate bool `json:",omitempty"`
// Just a convenient way to store the URL, not part of the spec
ServerV2URL string `json:",omitempty"`
Properties PropertiesCollection
}

type TaskAgents struct {
Expand Down
3 changes: 1 addition & 2 deletions runnerconfiguration/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (config *ConfigureRunner) Configure(
}
if res.UseV2FLow {
vssConnection = &protocol.VssConnection{
AuthHeader: "RemoteAuth " + config.Token,
AuthHeader: "Bearer " + res.Token,
Trace: config.Trace,
Client: c,
}
Expand Down Expand Up @@ -309,7 +309,6 @@ func registerOrReplaceRunnerV2(taskAgent *protocol.TaskAgent, config *ConfigureR
taskAgent.Name = runnerResp.Name
taskAgent.Authorization.AuthorizationURL = runnerResp.Authorization.AuthorizationURL
taskAgent.Authorization.ClientID = runnerResp.Authorization.ClientId
taskAgent.ServerV2URL = runnerResp.Authorization.ServerURL
return nil
}

Expand Down
74 changes: 52 additions & 22 deletions runnerconfiguration/compat/actions_runner_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ type DotnetRsaParameters struct {
}

type DotnetAgent struct {
AgentID string `json:"AgentId"`
AgentName string `json:"AgentName"`
DisableUpdate string `json:"DisableUpdate"`
Ephemeral string `json:"Ephemeral"`
PoolID string `json:"PoolId"`
PoolName string `json:"PoolName,omitempty"`
ServerURL string `json:"ServerUrl"`
WorkFolder string `json:"WorkFolder"`
GitHubURL string `json:"GitHubUrl"`
UseV2Flow bool `json:"UseV2Flow"`
ServerURLV2 string `json:"ServerUrlV2"`
AgentID string `json:"AgentId"`
AgentName string `json:"AgentName"`
DisableUpdate string `json:"DisableUpdate"`
Ephemeral string `json:"Ephemeral"`
PoolID string `json:"PoolId"`
PoolName string `json:"PoolName,omitempty"`
ServerURL string `json:"ServerUrl"`
WorkFolder string `json:"WorkFolder"`
GitHubURL string `json:"GitHubUrl"`
UseV2Flow bool `json:"UseV2Flow"`
ServerURLV2 string `json:"ServerUrlV2"`
UseRunnerAdminFlow bool `json:"UseRunnerAdminFlow,omitempty"`
}

type DotnetCredentials struct {
Expand Down Expand Up @@ -138,10 +139,32 @@ func ToRunnerInstance(fileAccess ConfigFileAccess) (*runnerconfiguration.RunnerI
}
ephemeral, _ := strconv.ParseBool(agent.Ephemeral)
disableUpdate, _ := strconv.ParseBool(agent.DisableUpdate)

props := protocol.PropertiesCollection{}
if agent.ServerURL != "" {
props["ServerUrl"] = protocol.PropertyValue{
Type: "System.String",
Value: agent.ServerURL,
}
}
if agent.ServerURLV2 != "" {
props["ServerUrlV2"] = protocol.PropertyValue{
Type: "System.String",
Value: agent.ServerURLV2,
}
}
if agent.UseV2Flow {
props["UseV2Flow"] = protocol.PropertyValue{
Type: "System.Boolean",
Value: agent.UseV2Flow,
}
}

return &runnerconfiguration.RunnerInstance{
PoolID: poolID,
Auth: &protocol.GitHubAuthResult{
TenantURL: agent.ServerURL,
UseV2FLow: agent.UseRunnerAdminFlow,
},
PKey: FromRsaParameters(rsaParameters),
Agent: &protocol.TaskAgent{
Expand All @@ -155,25 +178,32 @@ func ToRunnerInstance(fileAccess ConfigFileAccess) (*runnerconfiguration.RunnerI
},
DisableUpdate: disableUpdate,
Version: "3.0.0",
ServerV2URL: agent.ServerURLV2,
Properties: props,
},
WorkFolder: agent.WorkFolder,
RegistrationURL: agent.GitHubURL,
}, nil
}

func FromRunnerInstance(instance *runnerconfiguration.RunnerInstance, fileAccess ConfigFileAccess) error {
useV2Flow, _ := instance.Agent.Properties.LookupBool("UseV2Flow")
serverURL, ok := instance.Agent.Properties.LookupString("ServerUrl")
if serverURL == "" || !ok {
serverURL = instance.Auth.TenantURL
}
serverV2URL, _ := instance.Agent.Properties.LookupString("ServerUrlV2")
agent := &DotnetAgent{
AgentID: fmt.Sprint(instance.Agent.ID),
AgentName: instance.Agent.Name,
Ephemeral: fmt.Sprint(instance.Agent.Ephemeral),
DisableUpdate: fmt.Sprint(instance.Agent.DisableUpdate),
PoolID: fmt.Sprint(instance.PoolID),
ServerURL: instance.Auth.TenantURL,
WorkFolder: instance.WorkFolder,
GitHubURL: instance.RegistrationURL,
UseV2Flow: instance.Auth.UseV2FLow,
ServerURLV2: instance.Agent.ServerV2URL,
AgentID: fmt.Sprint(instance.Agent.ID),
AgentName: instance.Agent.Name,
Ephemeral: fmt.Sprint(instance.Agent.Ephemeral),
DisableUpdate: fmt.Sprint(instance.Agent.DisableUpdate),
PoolID: fmt.Sprint(instance.PoolID),
ServerURL: serverURL,
WorkFolder: instance.WorkFolder,
GitHubURL: instance.RegistrationURL,
UseV2Flow: useV2Flow,
ServerURLV2: serverV2URL,
UseRunnerAdminFlow: instance.Auth.UseV2FLow,
}
if agent.WorkFolder == "" {
agent.WorkFolder = "_work"
Expand Down
Loading