feat: trust domain type string parsing and validation#5
Conversation
Signed-off-by: tjons <tylerschade99@gmail.com>
Signed-off-by: tjons <tylerschade99@gmail.com>
Signed-off-by: tjons <tylerschade99@gmail.com>
Coutlaw
left a comment
There was a problem hiding this comment.
great start for trust domains, already pulled this into my bundle code. Left 2 questions that don't need to be addressed now for this PR.
| const InvalidTrustDomain = error{ EmptyTrustDomain, TrustDomainContainsInvalidCharacters, TrustDomainContainsPercentEncodedCharacters, TrustDomainContainsUserPart, TrustDomainContainsPortPart }; | ||
|
|
||
| // validates a SPIFFE trust domain authority URI segment. | ||
| fn validateTrustDomain(idOrName: []const u8) InvalidTrustDomain!void { |
There was a problem hiding this comment.
❓ what would you say to something like this?
fn validateTrustDomain(idOrName: []const u8) InvalidTrustDomain!void {
if (idOrName.len == 0) return InvalidTrustDomain.EmptyTrustDomain;
for (idOrName, 0..) |character, index| {
// TODO(tjons): use this to provide an index number for the invalid character
_ = index;
var other_allowed_character: bool = false;
switch (character) {
'@' => {
return InvalidTrustDomain.TrustDomainContainsUserPart;
},
':' => {
return InvalidTrustDomain.TrustDomainContainsPortPart;
},
'%' => {
return InvalidTrustDomain.TrustDomainContainsPercentEncodedCharacters;
},
'_', '-', '.' => {
other_allowed_character = true;
},
}
const digit = std.ascii.isDigit(character);
const lowercase = std.ascii.isLower(character);
if (!digit and !lowercase and !other_allowed_character) {
return InvalidTrustDomain.TrustDomainContainsInvalidCharacters;
}
}
return;
}There was a problem hiding this comment.
100% up to you, I can't decide which is easier to read.
There was a problem hiding this comment.
lol. this is much better, I didn't see the switch syntax yet...
|
|
||
| pub fn idString(self: TrustDomain, allocator: std.mem.Allocator) ![]const u8 { | ||
| const result = try allocator.alloc(u8, "spiffe://".len + self.td.len); | ||
| @memcpy(result[0..9], "spiffe://"); |
There was a problem hiding this comment.
❓ should we have a global const for the uri base string length? since 9 is showing up a lot?
Not for this PR, but going forward maybe
There was a problem hiding this comment.
this is a good point, will fix in the next iteration. I had defined this here: https://github.com/tjons/ziffe/blob/main/src/spiffeid/id.zig#L3 but forgot to use it.
Implements the TrustDomain struct type from the go-spiffe API, and the following methods:
TrustDomainFromStringTrustDomain.name()TrustDomain.idString()TrustDomain.string()This PR intentionally does not implement the URI-based types, which will be handled in the next iteration.