Skip to content

Commit

Permalink
Added GetCALeaf endpoint, added tests for the endpoint in AgentTest.c…
Browse files Browse the repository at this point in the history
…s, modified IAgentEndpoint.cs to have GetCALeaf (#339)

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

* style: fix whitespaces

---------

Co-authored-by: w1ano <83294629+w1ano@users.noreply.github.com>
Co-authored-by: octocat <octocat@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 20, 2024
1 parent 2b3b88e commit 25f2167
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
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

0 comments on commit 25f2167

Please sign in to comment.