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
50 changes: 31 additions & 19 deletions runnerconfiguration/compat/actions_runner_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"math/big"
"strconv"
"strings"

"github.com/ChristopherHX/github-act-runner/common"
"github.com/ChristopherHX/github-act-runner/protocol"
Expand All @@ -24,19 +25,30 @@ type DotnetRsaParameters struct {
Q []byte `json:"q"`
}

type DotnetBoolean bool

func (b *DotnetBoolean) UnmarshalJSON(data []byte) error {
var v bool
if err := json.Unmarshal([]byte(strings.Trim(strings.ToLower(string(data)), "\"")), &v); err != nil {
return err
}
*b = DotnetBoolean(v)
return nil
}

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"`
UseRunnerAdminFlow bool `json:"UseRunnerAdminFlow,omitempty"`
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 DotnetBoolean `json:"UseV2Flow"`
ServerURLV2 string `json:"ServerUrlV2"`
UseRunnerAdminFlow DotnetBoolean `json:"UseRunnerAdminFlow,omitempty"`
}

type DotnetCredentials struct {
Expand All @@ -45,9 +57,9 @@ type DotnetCredentials struct {
}

type DotnetCredentialsData struct {
ClientID string `json:"ClientId"`
AuthorizationURL string `json:"AuthorizationUrl"`
RequireFipsCryptography bool `json:"RequireFipsCryptography"`
ClientID string `json:"ClientId"`
AuthorizationURL string `json:"AuthorizationUrl"`
RequireFipsCryptography DotnetBoolean `json:"RequireFipsCryptography"`
}

func BytesToBigInt(bytes []byte) *big.Int {
Expand Down Expand Up @@ -171,7 +183,7 @@ func ToRunnerInstance(fileAccess ConfigFileAccess) (*runnerconfiguration.RunnerI
PoolID: poolID,
Auth: &protocol.GitHubAuthResult{
TenantURL: agent.ServerURL,
UseV2FLow: agent.UseRunnerAdminFlow,
UseV2FLow: bool(agent.UseRunnerAdminFlow),
},
PKey: FromRsaParameters(rsaParameters),
Agent: &protocol.TaskAgent{
Expand Down Expand Up @@ -208,9 +220,9 @@ func FromRunnerInstance(instance *runnerconfiguration.RunnerInstance, fileAccess
ServerURL: serverURL,
WorkFolder: instance.WorkFolder,
GitHubURL: instance.RegistrationURL,
UseV2Flow: useV2Flow,
UseV2Flow: DotnetBoolean(useV2Flow),
ServerURLV2: serverV2URL,
UseRunnerAdminFlow: instance.Auth.UseV2FLow,
UseRunnerAdminFlow: DotnetBoolean(instance.Auth.UseV2FLow),
}
if agent.WorkFolder == "" {
agent.WorkFolder = "_work"
Expand All @@ -223,7 +235,7 @@ func FromRunnerInstance(instance *runnerconfiguration.RunnerInstance, fileAccess
AuthorizationURL: instance.Agent.Authorization.AuthorizationURL,
// serverV2URL != "" means recent GitHub Server that requires recent actions/runner
// that has received this bugfix https://github.com/actions/runner/pull/3789
RequireFipsCryptography: requireFipsCryptography && hasRequireFipsCryptography || serverV2URL != "",
RequireFipsCryptography: DotnetBoolean(requireFipsCryptography && hasRequireFipsCryptography || serverV2URL != ""),
},
}
if err := fileAccess.Write(".runner", agent); err != nil {
Expand Down
92 changes: 92 additions & 0 deletions runnerconfiguration/compat/actions_runner_compat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package compat

import (
"encoding/json"
"testing"
)

func TestUnmarshalConfig(t *testing.T) {
table := []struct {
name string
jsonData string
expected DotnetAgent
}{
{
name: "Boolean true without quotes",
jsonData: `{"UseV2Flow": true}`,
expected: DotnetAgent{UseV2Flow: DotnetBoolean(true)},
},
{
name: "Boolean false without quotes",
jsonData: `{"UseV2Flow": false}`,
expected: DotnetAgent{UseV2Flow: DotnetBoolean(false)},
},
{
name: "Boolean false without quotes no space",
jsonData: `{"UseV2Flow":false}`,
expected: DotnetAgent{UseV2Flow: DotnetBoolean(false)},
},
{
name: "Boolean True with quotes",
jsonData: `{"UseV2Flow": "True"}`,
expected: DotnetAgent{UseV2Flow: DotnetBoolean(true)},
},
{
name: "Boolean False with quotes",
jsonData: `{"UseV2Flow": "False"}`,
expected: DotnetAgent{UseV2Flow: DotnetBoolean(false)},
},
{
name: "Boolean true with quotes",
jsonData: `{"UseV2Flow": "true"}`,
expected: DotnetAgent{UseV2Flow: DotnetBoolean(true)},
},
{
name: "Boolean false with quotes",
jsonData: `{"UseV2Flow": "false"}`,
expected: DotnetAgent{UseV2Flow: DotnetBoolean(false)},
},
}
for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
var agent DotnetAgent
err := json.Unmarshal([]byte(tt.jsonData), &agent)
if err != nil {
t.Fatalf("Unexpected error during unmarshal: %v", err)
}
if agent.UseV2Flow != tt.expected.UseV2Flow {
t.Errorf("Expected UseV2Flow to be %v, got %v", tt.expected.UseV2Flow, agent.UseV2Flow)
}
})
}
}

func TestMarshalDotnetBoolean(t *testing.T) {
table := []struct {
name string
agent DotnetBoolean
expectedJSON string
}{
{
name: "Boolean true",
agent: DotnetBoolean(true),
expectedJSON: "true",
},
{
name: "Boolean false",
agent: DotnetBoolean(false),
expectedJSON: "false",
},
}
for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.agent)
if err != nil {
t.Fatalf("Unexpected error during marshal: %v", err)
}
if string(data) != tt.expectedJSON {
t.Errorf("Expected JSON to be %s, got %s", tt.expectedJSON, string(data))
}
})
}
}