Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GetCALeaf endpoint, added tests for the endpoint in AgentTest.cs, modified IAgentEndpoint.cs to have GetCALeaf #339

Merged
merged 4 commits into from
Jun 20, 2024
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
27 changes: 27 additions & 0 deletions Consul.Test/AgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,33 @@ public async Task Agent_CARoots()
}
}

[Fact]
public async Task Agent_CALeaf()
{
var service = new AgentServiceRegistration
{
Name = "test_leaf",
Tags = new[]
{
"bar",
"baz"
},
Port = 8000,
};
await _client.Agent.ServiceRegister(service);
var leaf = await _client.Agent.GetCALeaf("test_leaf");
Assert.True(leaf.LastIndex > 0);
Assert.NotNull(leaf.Response.SerialNumber);
Assert.NotNull(leaf.Response.CertPEM);
Assert.NotNull(leaf.Response.PrivateKeyPEM);
Assert.Equal("test_leaf", leaf.Response.Service);
Assert.Contains("/svc/test_leaf", leaf.Response.ServiceURI);
Assert.True(leaf.Response.ModifyIndex > 0);
Assert.NotEqual(0, leaf.Response.CreateIndex);
Assert.True(leaf.Response.ValidBefore > DateTime.Now);
Assert.True(leaf.Response.ValidAfter < DateTime.Now);
}

[SkippableFact]
public async Task Agent_Reload()
{
Expand Down
36 changes: 36 additions & 0 deletions Consul/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,19 @@ public class Root
public long ModifyIndex { get; set; }
}

public class CALeaf
{
public string SerialNumber { get; set; }
public string CertPEM { get; set; }
public string PrivateKeyPEM { get; set; }
public string Service { get; set; }
public string ServiceURI { get; set; }
public DateTime ValidAfter { get; set; }
public DateTime ValidBefore { get; set; }
public long CreateIndex { get; set; }
public long ModifyIndex { get; set; }
}

/// <summary>
/// Agent can be used to query the Agent endpoints
/// </summary>
Expand Down Expand Up @@ -1185,6 +1198,29 @@ public async Task<QueryResult<CARoots>> GetCARoots(QueryOptions q, CancellationT
return await _client.Get<CARoots>("v1/agent/connect/ca/roots", q).Execute(ct).ConfigureAwait(false);
}

/// <summary>
/// GetCALeaf, returns the leaf certificate representing a single service
/// </summary>
/// <param name="serviceId">Id of service to fetch</param>
/// <param name="ct">Cancellation Token</param>
/// <returns>Leaf certificate</returns>
public async Task<QueryResult<CALeaf>> GetCALeaf(string serviceId, CancellationToken ct = default)
{
return await GetCALeaf(serviceId, QueryOptions.Default, ct).ConfigureAwait(false);
}

/// <summary>
/// GetCALeaf, returns the leaf certificate representing a single service
/// </summary>
/// <param name="serviceId">Id of service to fetch</param>
/// <param name="q">Query Options</param>
/// <param name="ct">Cancellation Token</param>
/// <returns>Leaf certificate</returns>
public async Task<QueryResult<CALeaf>> GetCALeaf(string serviceId, QueryOptions q, CancellationToken ct = default)
{
return await _client.Get<CALeaf>($"v1/agent/connect/ca/leaf/{serviceId}", q).Execute(ct).ConfigureAwait(false);
}

/// <summary>
/// Log streamer
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Consul/Interfaces/IAgentEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public interface IAgentEndpoint
Task<QueryResult<Metrics>> GetAgentMetrics(CancellationToken ct = default);
Task<QueryResult<CARoots>> GetCARoots(CancellationToken ct = default);
Task<QueryResult<CARoots>> GetCARoots(QueryOptions q, CancellationToken ct = default);
Task<QueryResult<CALeaf>> GetCALeaf(string serviceId, CancellationToken ct = default);
Task<QueryResult<CALeaf>> GetCALeaf(string serviceId, QueryOptions q, CancellationToken ct = default);
Task<QueryResult<AgentVersion>> GetAgentVersion(CancellationToken ct = default);
Task<WriteResult> Reload(CancellationToken ct = default);
[Obsolete]
Expand Down