diff --git a/Directory.Packages.props b/Directory.Packages.props index 7d8b68c..8b5e726 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -19,5 +19,10 @@ + + + + + \ No newline at end of file diff --git a/pgLabII.PgUtils.Tests/Acls/AclParserTests.cs b/pgLabII.PgUtils.Tests/Acls/AclParserTests.cs new file mode 100644 index 0000000..eb642ba --- /dev/null +++ b/pgLabII.PgUtils.Tests/Acls/AclParserTests.cs @@ -0,0 +1,17 @@ +namespace pgLabII.PgUtils.Tests; + +public class AclParserTests +{ + [Theory] + [InlineData("foo=rw/postgres", "foo", "rw", "postgres")] + [InlineData("\"Test\"\"=\"=rw/postgres", "Test\"=", "rw", "postgres")] + [InlineData("foo=rw/\"Test\"\"=\"", "foo", "rw", "Test\"=")] + public void Test1(string aclString, string expectedGrantee, string expectedRights, string expectedGrantor) + { + AclParser parser = new(); + Acl acl = parser.Parse(aclString); + Assert.Equal(expectedGrantee, acl.Grantee); + Assert.Equal(expectedRights, acl.Rights); + Assert.Equal(expectedGrantor, acl.Grantor); + } +} diff --git a/pgLabII.PgUtils.Tests/pgLabII.PgUtils.Tests.csproj b/pgLabII.PgUtils.Tests/pgLabII.PgUtils.Tests.csproj new file mode 100644 index 0000000..cf88fa9 --- /dev/null +++ b/pgLabII.PgUtils.Tests/pgLabII.PgUtils.Tests.csproj @@ -0,0 +1,25 @@ + + + + net9.0 + enable + enable + false + + + + + + + + + + + + + + + + + + diff --git a/pgLabII.PgUtils/Acls/AclParser.cs b/pgLabII.PgUtils/Acls/AclParser.cs new file mode 100644 index 0000000..9d2eb20 --- /dev/null +++ b/pgLabII.PgUtils/Acls/AclParser.cs @@ -0,0 +1,113 @@ +using System.Text; + +namespace pgLabII.PgUtils; + +public class Acl(string grantee, string rights, string grantor) +{ + public string Grantee { get; } = grantee; + public string Rights { get; } = rights; + public string Grantor { get; } = grantor; +} +/// +/// Parser for acl's. Acl's have a fairly simple structure grantee=right/grantor see also https://www.postgresql.org/docs/current/ddl-priv.html +/// When the grantee is empty it is "PUBLIC" which you can see as a wildcard that matches everybody that can authenticate. +/// Because the grantee and grantor name can contain special characters there is an escape mechinism in place. +/// When the name contains a conflicting character it is quoted. If the name contains a quote it is doubles. +/// +public class AclParser +{ + private int _position = 0; + private string _input = null!; + + public Acl Parse(string acl) + { + _input = acl; + _position = 0; + ConsumeWhitespace(); + string grantee = ConsumeRoleName(); + string rights = ConsumeRights(); + if (Current() != '/') + throw new ArgumentException("Unexpected symbol in acl string"); + _position++; + string grantor = ConsumeRoleName(); + return new Acl(grantee, rights, grantor); + } + + /// + /// There is a limited set of possible characters for the rights + /// + /// + /// + private string ConsumeRights() + { + if (Current() != '=') + throw new ArgumentException("Unexpected symbol in acl string"); + _position++; + int start = _position; + while (NotAtEnd() && Current() != '/') + _position++; + + return _input.Substring(start, _position - start); + } + + // Whitespace should have been consumed + // There should be either a " in which case the name is quoted + // a = in which case there is no name ie PUBLIC + // all other cases it is an unquoted name + private string ConsumeRoleName() => + _input[_position] switch + { + '=' => "", + '"' => ParseQuotedName(), + _ => ParseUnquotedName() + }; + + private string ParseQuotedName() + { + _position++; // consume opening quote + int start = _position; + StringBuilder sb = new(); + + while (true) + { + while (NotAtEnd() && Current() != '"') + _position++; + + char peek = _position + 1 < _input.Length ? _input[_position + 1] : '\0'; + if (peek == '"') + { + _position++; + sb.Append(_input.AsSpan(start, _position - start)); + _position++; + start = _position; + } + else + { + sb.Append(_input.AsSpan(start, _position - start)); + _position++; + break; + } + } + + return sb.ToString(); + } + + private string ParseUnquotedName() + { + int start = _position; + while (NotAtEnd() && Current() != '=') + _position++; + + return _input.Substring(start, _position - start); + } + + private void ConsumeWhitespace() + { + while (NotAtEnd() && char.IsWhiteSpace(Current())) + ++_position; + } + + private bool NotAtEnd() => _position < _input.Length; + + private char Current() => _input[_position]; +} diff --git a/pgLabII.PgUtils/pgLabII.PgUtils.csproj b/pgLabII.PgUtils/pgLabII.PgUtils.csproj new file mode 100644 index 0000000..17b910f --- /dev/null +++ b/pgLabII.PgUtils/pgLabII.PgUtils.csproj @@ -0,0 +1,9 @@ + + + + net9.0 + enable + enable + + + diff --git a/pgLabII.sln b/pgLabII.sln index f11383b..26bc6ef 100644 --- a/pgLabII.sln +++ b/pgLabII.sln @@ -18,6 +18,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UI", "UI", "{EBBC2081-5061-4843-B420-AB4C48A5C283}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "pgLabII.PgUtils", "pgLabII.PgUtils\pgLabII.PgUtils.csproj", "{6694EE99-3AEA-4823-B2C3-02F28F575D14}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "pgLabII.PgUtils.Tests", "pgLabII.PgUtils.Tests\pgLabII.PgUtils.Tests.csproj", "{915C5439-4CF5-4625-AB7B-24F0E34E5B5F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -41,6 +45,14 @@ Global {7AD1DAC8-7FBE-49D5-8614-7321233DB82E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU {7AD1DAC8-7FBE-49D5-8614-7321233DB82E}.Release|Any CPU.ActiveCfg = Release|Any CPU {7AD1DAC8-7FBE-49D5-8614-7321233DB82E}.Release|Any CPU.Build.0 = Release|Any CPU + {6694EE99-3AEA-4823-B2C3-02F28F575D14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6694EE99-3AEA-4823-B2C3-02F28F575D14}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6694EE99-3AEA-4823-B2C3-02F28F575D14}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6694EE99-3AEA-4823-B2C3-02F28F575D14}.Release|Any CPU.Build.0 = Release|Any CPU + {915C5439-4CF5-4625-AB7B-24F0E34E5B5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {915C5439-4CF5-4625-AB7B-24F0E34E5B5F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {915C5439-4CF5-4625-AB7B-24F0E34E5B5F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {915C5439-4CF5-4625-AB7B-24F0E34E5B5F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE