[FEATURE] update ACL Token Read endpoint to add expanded parameter#601
[FEATURE] update ACL Token Read endpoint to add expanded parameter#601marcin-krystianc merged 16 commits intoG-Research:masterfrom
Read endpoint to add expanded parameter#601Conversation
marcin-krystianc
left a comment
There was a problem hiding this comment.
HI @MohamedM216,
look at the example of a response when the expanded=true (https://developer.hashicorp.com/consul/api-docs/acl/tokens#sample-response-1). See, there are two new fields, ExpandedPolicies and ExpandedRoles.
The test should assert that these fields are filled in, when expanded=true and null when expanded=false.
be4536f to
f6bda2c
Compare
|
|
| public TokenEntry(string accessorId, string description, PolicyLink[] policies, RoleLink[] roles, ServiceIdentity[] serviceIdentities, ExpandedPolicy[] expandedPolicies, ExpandedRole[] expandedRoles, string[] namespaceDefaultPolicyIDs, string[] namespaceDefaultRoleIDs, string agentACLDefaultPolicy, string agentACLDownPolicy, string resolvedByAgent) | ||
| { | ||
| AccessorID = accessorId; | ||
| Description = description; | ||
| Policies = policies; | ||
| Roles = roles; | ||
| ServiceIdentities = serviceIdentities; | ||
| ExpandedPolicies = expandedPolicies; | ||
| ExpandedRoles = expandedRoles; | ||
| NamespaceDefaultPolicyIDs = namespaceDefaultPolicyIDs; | ||
| NamespaceDefaultRoleIDs = namespaceDefaultRoleIDs; | ||
| AgentACLDefaultPolicy = agentACLDefaultPolicy; | ||
| AgentACLDownPolicy = agentACLDownPolicy; | ||
| ResolvedByAgent = resolvedByAgent; | ||
| } |
There was a problem hiding this comment.
I don't think we need to add this constructor? It just adds noise to the codebase.
| public ExpandedRole(string id, string name, string description, PolicyLink[] policies, ServiceIdentity[] serviceIdentities) | ||
| { | ||
| ID = id; | ||
| Name = name; | ||
| Description = description; | ||
| Policies = policies; | ||
| ServiceIdentities = serviceIdentities; | ||
| } | ||
| } |
There was a problem hiding this comment.
I don't think we need to add this constructor? It just adds noise to the codebase.
| public ExpandedPolicy(string id, string name, string description, string rules, string[] datacenters) | ||
| { | ||
| ID = id; | ||
| Name = name; | ||
| Description = description; | ||
| Rules = rules; | ||
| Datacenters = datacenters; | ||
| } |
There was a problem hiding this comment.
I don't think we need to add this constructor? It just adds noise to the codebase.
| // create test policy | ||
| var policyEntry = new PolicyEntry | ||
| { | ||
| Name = "TestExpandedPolicy-" + Guid.NewGuid().ToString().Substring(0, 8), |
There was a problem hiding this comment.
Please use KVTest.GenerateTestKeyName() for generating random names (we want to have a consistent way of generating random names).
992ff66 to
6fceff1
Compare
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| using System.Data; |
There was a problem hiding this comment.
it is not used, please remove:
| using System.Data; |
marcin-krystianc
left a comment
There was a problem hiding this comment.
Please improve assertions where applicable (e.g. Assert.NotEmpty instead of Assert.NotNull)
| // when expanded=false | ||
| var unexpandedRead = await _client.Token.Read(accessorId, false, QueryOptions.Default); | ||
| Assert.NotNull(unexpandedRead.Response.Policies); | ||
| Assert.Equal(unexpandedRead.Response.Policies.FirstOrDefault().Name, policyEntry.Name); |
There was a problem hiding this comment.
xUnit convention is Assert.Equal(expected, actual):
| Assert.Equal(unexpandedRead.Response.Policies.FirstOrDefault().Name, policyEntry.Name); | |
| Assert.Equal(policyEntry.Name, unexpandedRead.Response.Policies.FirstOrDefault().Name); |
| Assert.NotNull(expandedRead.Response.AgentACLDownPolicy); | ||
| Assert.NotNull(expandedRead.Response.ResolvedByAgent); | ||
| Assert.NotNull(expandedRead.Response.ExpandedPolicies); | ||
| Assert.Equal(expandedRead.Response.ExpandedPolicies.FirstOrDefault().Rules, policyEntry.Rules); |
There was a problem hiding this comment.
` xUnit convention is Assert.Equal(expected, actual):
- No need to use the
FirstOrDefault()form (it would throw confusing exception, itExpandedPolicieswas empty)
| Assert.Equal(expandedRead.Response.ExpandedPolicies.FirstOrDefault().Rules, policyEntry.Rules); | |
| Assert.Equal(policyEntry.Rules, expandedRead.Response.ExpandedPolicies.First().Rules); |
| Assert.NotNull(expandedRead.Response.ExpandedPolicies); | ||
| Assert.Equal(expandedRead.Response.ExpandedPolicies.FirstOrDefault().Rules, policyEntry.Rules); | ||
| Assert.NotNull(expandedRead.Response.ExpandedRoles); | ||
| Assert.Equal(expandedRead.Response.ExpandedRoles.FirstOrDefault().Name, roleEntry.Name); |
There was a problem hiding this comment.
xUnit convention is Assert.Equal(expected, actual):
| Assert.Equal(expandedRead.Response.ExpandedRoles.FirstOrDefault().Name, roleEntry.Name); | |
| Assert.Equal(roleEntry.Name, expandedRead.Response.ExpandedRoles.FirstOrDefault().Name); |
| Assert.NotNull(expandedRead.Response.AgentACLDefaultPolicy); | ||
| Assert.NotNull(expandedRead.Response.AgentACLDownPolicy); | ||
| Assert.NotNull(expandedRead.Response.ResolvedByAgent); | ||
| Assert.NotNull(expandedRead.Response.ExpandedPolicies); |
There was a problem hiding this comment.
Slightly better (stricter) check:
| Assert.NotNull(expandedRead.Response.ExpandedPolicies); | |
| Assert.NotEmpty(expandedRead.Response.ExpandedPolicies); |
| public async Task Token_ReadExpanded() | ||
| { | ||
| var cutOffVersion = SemanticVersion.Parse("1.12.0"); | ||
| Skip.If(AgentVersion < cutOffVersion, $"Current version is {AgentVersion}, but `Namespaces` is only supported from Consul {cutOffVersion}"); |
There was a problem hiding this comment.
Error message is not accurate, please update it.
marcin-krystianc
left a comment
There was a problem hiding this comment.
Looks good now, thank you!
| Task<WriteResult<bool>> Delete(string id, WriteOptions q, CancellationToken ct = default); | ||
| Task<QueryResult<TokenEntry>> Read(string id, CancellationToken ct); | ||
| Task<QueryResult<TokenEntry>> Read(string id); | ||
| Task<QueryResult<TokenEntry>> Read(string id, QueryOptions q, CancellationToken ct = default); |
There was a problem hiding this comment.
Due to losing this API, now during the building process, we get this:
/home/mohammad/programs/mlh-fellowship/projects/consuldotnet/consuldotnet/Consul/PublicAPI/PublicAPI.Shipped.txt(3013,1): error RS0017: Symbol 'Consul.ITokenEndpoint.Read(string id, Consul.QueryOptions q, System.Threading.CancellationToken ct = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<Consul.QueryResult<Consul.TokenEntry>>' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
/home/mohammad/programs/mlh-fellowship/projects/consuldotnet/consuldotnet/Consul/PublicAPI/PublicAPI.Shipped.txt(4099,1): error RS0017: Symbol 'Consul.Token.Read(string id, Consul.QueryOptions queryOptions, System.Threading.CancellationToken ct = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<Consul.QueryResult<Consul.TokenEntry>>' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
It's a breaking change and we should keep it in Shipped.txt. So the only way I see is to add the expanded parameter to QueryOptions, although QueryOptions is generic and expanded does only belong to ACL token Read endpoints, then we won't have any breaking changes and won't have to add any new overloads as well?
Also it's good to mention that we will face the same issue in #592
Please let me know your thoughts.
Thanks!
There was a problem hiding this comment.
See here how to handle this (dotnet/roslyn-analyzers#5058):
You should copy the entry for your change from PublicAPI.Shipped.txt and then put that entry in PublicAPI.Unshipped.txt with REMOVED prefix.
| var list = await _client.Token.List(); | ||
| var testToken = list.Response.FirstOrDefault(t => t.Description == "Expansion Test Token"); | ||
| if (testToken != null) | ||
| { | ||
| await _client.Token.Delete(testToken.AccessorID); | ||
| } |
There was a problem hiding this comment.
This is unecessarily complicated. Maybe let's get rid of try/finally and just assume test always completes normally, so no exception handling is needed (which is generally true). There is no need to complicate the test code to handle edge cases.
Once the try/finally is removed , then we don't need to look for a token to delete since we already have its AccessorID.
marcin-krystianc
left a comment
There was a problem hiding this comment.
I think this is a last remark, looks good otherwise!
| [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] | ||
| public string[] NamespaceDefaultPolicyIDs { get; set; } | ||
| [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] | ||
| public string[] NamespaceDefaultRoleIDs { get; set; } |
There was a problem hiding this comment.
I would rather not add properties for which we do not have proper testing, it is better not to add them blindly (I'm not even sure when and how these would be non null):
| [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] | |
| public string[] NamespaceDefaultPolicyIDs { get; set; } | |
| [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] | |
| public string[] NamespaceDefaultRoleIDs { get; set; } |
2a22818 to
90b3886
Compare
Read endpoint to add expnded parameterRead endpoint to add expanded parameter
Description
update ACL Token Read endpoint to add expnded parameter
Related Issues
#531
Additional Context
Checklist
Please make sure to check the following before submitting your pull request: