This commit is contained in:
Luke Tomlinson
2023-03-24 11:57:47 -07:00
parent 12584aa551
commit 78330c84a7
4 changed files with 60 additions and 39 deletions

View File

@@ -17,7 +17,7 @@ namespace GitHub.Runner.Common
{ {
Task<List<TaskAgent>> GetRunnersAsync(int runnerGroupId, string githubUrl, string githubToken, string agentName); Task<List<TaskAgent>> GetRunnersAsync(int runnerGroupId, string githubUrl, string githubToken, string agentName);
Task<TaskAgent> AddRunnerAsync(int runnerGroupId, TaskAgent agent, string githubUrl, string githubToken, string publicKey, string hostId); Task<DistributedTask.WebApi.Runner> AddRunnerAsync(int runnerGroupId, TaskAgent agent, string githubUrl, string githubToken, string publicKey);
Task<List<TaskAgentPool>> GetRunnerGroupsAsync(string githubUrl, string githubToken); Task<List<TaskAgentPool>> GetRunnerGroupsAsync(string githubUrl, string githubToken);
string GetGitHubRequestId(HttpResponseHeaders headers); string GetGitHubRequestId(HttpResponseHeaders headers);
@@ -136,7 +136,7 @@ namespace GitHub.Runner.Common
return agentPools?.ToAgentPoolList(); return agentPools?.ToAgentPoolList();
} }
public async Task<TaskAgent> AddRunnerAsync(int runnerGroupId, TaskAgent agent, string githubUrl, string githubToken, string publicKey, string hostId) public async Task<DistributedTask.WebApi.Runner> AddRunnerAsync(int runnerGroupId, TaskAgent agent, string githubUrl, string githubToken, string publicKey)
{ {
var gitHubUrlBuilder = new UriBuilder(githubUrl); var gitHubUrlBuilder = new UriBuilder(githubUrl);
var path = gitHubUrlBuilder.Path.Split('/', '\\', StringSplitOptions.RemoveEmptyEntries); var path = gitHubUrlBuilder.Path.Split('/', '\\', StringSplitOptions.RemoveEmptyEntries);
@@ -159,20 +159,12 @@ namespace GitHub.Runner.Common
{"updates_disabled", agent.DisableUpdate}, {"updates_disabled", agent.DisableUpdate},
{"ephemeral", agent.Ephemeral}, {"ephemeral", agent.Ephemeral},
{"labels", agent.Labels}, {"labels", agent.Labels},
{"public_key", publicKey}, {"public_key", publicKey}
{"host_id", hostId},
}; };
var body = new StringContent(StringUtil.ConvertToJson(bodyObject), null, "application/json"); var body = new StringContent(StringUtil.ConvertToJson(bodyObject), null, "application/json");
var runner = await RetryRequest<DistributedTask.WebApi.Runner>(githubApiUrl, githubToken, RequestType.Post, 3, "Failed to add agent", body); return await RetryRequest<DistributedTask.WebApi.Runner>(githubApiUrl, githubToken, RequestType.Post, 3, "Failed to add agent", body);
agent.Id = runner.Id;
agent.Authorization = new TaskAgentAuthorization()
{
AuthorizationUrl = runner.RunnerAuthorization.AuthorizationUrl,
ClientId = new Guid(runner.RunnerAuthorization.ClientId),
};
return agent;
} }
private async Task<T> RetryRequest<T>(string githubApiUrl, string githubToken, RequestType requestType, int maxRetryAttemptsCount = 5, string errorMessage = null, StringContent body = null) private async Task<T> RetryRequest<T>(string githubApiUrl, string githubToken, RequestType requestType, int maxRetryAttemptsCount = 5, string errorMessage = null, StringContent body = null)

View File

@@ -196,6 +196,11 @@ namespace GitHub.Runner.Listener
var configManager = HostContext.GetService<IConfigurationManager>(); var configManager = HostContext.GetService<IConfigurationManager>();
_settings = configManager.LoadSettings(); _settings = configManager.LoadSettings();
if (_settings.ServerUrlV2 == null)
{
throw new InvalidOperationException("ServerUrlV2 is not set");
}
var credMgr = HostContext.GetService<ICredentialManager>(); var credMgr = HostContext.GetService<ICredentialManager>();
VssCredentials creds = credMgr.LoadCredentials(); VssCredentials creds = credMgr.LoadCredentials();
await _brokerServer.ConnectAsync(new Uri(_settings.ServerUrlV2), creds); await _brokerServer.ConnectAsync(new Uri(_settings.ServerUrlV2), creds);

View File

@@ -117,7 +117,6 @@ namespace GitHub.Runner.Listener.Configuration
VssCredentials creds = null; VssCredentials creds = null;
_term.WriteSection("Authentication"); _term.WriteSection("Authentication");
string registerToken = string.Empty; string registerToken = string.Empty;
string hostId = string.Empty;
while (true) while (true)
{ {
// When testing against a dev deployment of Actions Service, set this environment variable // When testing against a dev deployment of Actions Service, set this environment variable
@@ -142,7 +141,6 @@ namespace GitHub.Runner.Listener.Configuration
_term.WriteLine($"Using V2 flow: {runnerSettings.UseV2Flow}"); _term.WriteLine($"Using V2 flow: {runnerSettings.UseV2Flow}");
creds = authResult.ToVssCredentials(); creds = authResult.ToVssCredentials();
Trace.Info("cred retrieved via GitHub auth"); Trace.Info("cred retrieved via GitHub auth");
hostId = GetHostId(authResult.Token);
} }
try try
@@ -302,7 +300,9 @@ namespace GitHub.Runner.Listener.Configuration
{ {
if (runnerSettings.UseV2Flow) if (runnerSettings.UseV2Flow)
{ {
agent = await _dotcomServer.AddRunnerAsync(runnerSettings.PoolId, agent, runnerSettings.GitHubUrl, registerToken, publicKeyXML, hostId); var runner = await _dotcomServer.AddRunnerAsync(runnerSettings.PoolId, agent, runnerSettings.GitHubUrl, registerToken, publicKeyXML);
runner.ApplyToTaskAgent(agent);
runnerSettings.ServerUrlV2 = runner.RunnerAuthorization.ServerUrl;
} }
else else
{ {
@@ -359,6 +359,9 @@ namespace GitHub.Runner.Listener.Configuration
} }
// Testing agent connection, detect any potential connection issue, like local clock skew that cause OAuth token expired. // Testing agent connection, detect any potential connection issue, like local clock skew that cause OAuth token expired.
if (!runnerSettings.UseV2Flow)
{
var credMgr = HostContext.GetService<ICredentialManager>(); var credMgr = HostContext.GetService<ICredentialManager>();
VssCredentials credential = credMgr.LoadCredentials(); VssCredentials credential = credMgr.LoadCredentials();
try try
@@ -378,6 +381,7 @@ namespace GitHub.Runner.Listener.Configuration
Trace.Error(ex); Trace.Error(ex);
throw new Exception("The local machine's clock may be out of sync with the server time by more than five minutes. Please sync your clock with your domain or internet time and try again."); throw new Exception("The local machine's clock may be out of sync with the server time by more than five minutes. Please sync your clock with your domain or internet time and try again.");
} }
}
_term.WriteSection("Runner settings"); _term.WriteSection("Runner settings");
@@ -778,12 +782,5 @@ namespace GitHub.Runner.Listener.Configuration
} }
return null; return null;
} }
// Temporary hack for sending legacy host id using v2 flow
private string GetHostId(string accessToken)
{
var claims = JsonWebToken.Create(accessToken).ExtractClaims();
return claims.FirstOrDefault(x => x.Type == "aud").Value.Split(':').LastOrDefault();
}
} }
} }

View File

@@ -8,6 +8,9 @@ namespace GitHub.DistributedTask.WebApi
public class Authorization public class Authorization
{ {
/// <summary>
/// The url to refresh tokens
/// </summary>
[JsonProperty("authorization_url")] [JsonProperty("authorization_url")]
public Uri AuthorizationUrl public Uri AuthorizationUrl
{ {
@@ -15,6 +18,19 @@ namespace GitHub.DistributedTask.WebApi
internal set; internal set;
} }
/// <summary>
/// The url to connect to to poll for messages
/// </summary>
[JsonProperty("server_url")]
public string ServerUrl
{
get;
internal set;
}
/// <summary>
/// The client id to use when connecting to the authorization_url
/// </summary>
[JsonProperty("client_id")] [JsonProperty("client_id")]
public string ClientId public string ClientId
{ {
@@ -43,5 +59,16 @@ namespace GitHub.DistributedTask.WebApi
get; get;
internal set; internal set;
} }
public TaskAgent ApplyToTaskAgent(TaskAgent agent)
{
agent.Id = this.Id;
agent.Authorization = new TaskAgentAuthorization()
{
AuthorizationUrl = this.RunnerAuthorization.AuthorizationUrl,
ClientId = new Guid(this.RunnerAuthorization.ClientId)
};
return agent;
}
} }
} }