|
| 1 | +namespace Weaviate.Client.Tests.Integration; |
| 2 | + |
| 3 | +using System.Linq; |
| 4 | +using Weaviate.Client; |
| 5 | +using Weaviate.Client.Models; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// RBAC Groups integration tests (Rest:8092 gRPC:50063). Authorization checks for various operations. |
| 10 | +/// </summary> |
| 11 | +public class TestRbacAuthorization : IntegrationTests |
| 12 | +{ |
| 13 | + public override ushort RestPort => 8092; |
| 14 | + public override ushort GrpcPort => 50063; |
| 15 | + private const string ADMIN_API_KEY = "admin-key"; |
| 16 | + |
| 17 | + public override async ValueTask InitializeAsync() |
| 18 | + { |
| 19 | + await base.InitializeAsync(); |
| 20 | + |
| 21 | + RequireVersion("1.32.0"); |
| 22 | + } |
| 23 | + |
| 24 | + public override ICredentials? Credentials => Auth.ApiKey(ADMIN_API_KEY); |
| 25 | + |
| 26 | + [Fact, Trait("Category", "RBAC")] |
| 27 | + public async Task TestAuthorizationFailure() |
| 28 | + { |
| 29 | + // Generate random names for collection and user |
| 30 | + var collectionName = $"AuthorizationTest"; |
| 31 | + var userId = Helpers.GenerateUniqueIdentifier("user"); |
| 32 | + |
| 33 | + // Create collection |
| 34 | + var collectionConfig = new CollectionConfig |
| 35 | + { |
| 36 | + Name = collectionName, |
| 37 | + Properties = new[] |
| 38 | + { |
| 39 | + new Property { Name = "name", DataType = new[] { "string" } }, |
| 40 | + }, |
| 41 | + }; |
| 42 | + |
| 43 | + var client = await CollectionFactory<object>(collectionConfig); |
| 44 | + |
| 45 | + // Create a role with only read permission for this collection |
| 46 | + var readOnlyRole = await _weaviate.Roles.Create( |
| 47 | + "read-only-role", |
| 48 | + [ |
| 49 | + new Permissions.Collections(collectionName) { Read = true }, |
| 50 | + new Permissions.Data(collectionName, null, null) { Read = true }, |
| 51 | + ], |
| 52 | + TestContext.Current.CancellationToken |
| 53 | + ); |
| 54 | + |
| 55 | + // Create a user and assign the read-only role |
| 56 | + var apiKey = await _weaviate.Users.Db.Create( |
| 57 | + userId, |
| 58 | + cancellationToken: TestContext.Current.CancellationToken |
| 59 | + ); |
| 60 | + await _weaviate.Users.Db.AssignRoles( |
| 61 | + userId, |
| 62 | + new[] { readOnlyRole.Name }, |
| 63 | + cancellationToken: TestContext.Current.CancellationToken |
| 64 | + ); |
| 65 | + |
| 66 | + // Create a new client with the user's API key |
| 67 | + var userClient = await new WeaviateClientBuilder() |
| 68 | + .WithRestEndpoint("localhost") |
| 69 | + .WithRestPort(RestPort) |
| 70 | + .WithGrpcEndpoint("localhost") |
| 71 | + .WithGrpcPort(GrpcPort) |
| 72 | + .WithCredentials(Auth.ApiKey(apiKey)) |
| 73 | + .BuildAsync(); |
| 74 | + |
| 75 | + var userCollection = userClient.Collections.Use(collectionName); |
| 76 | + |
| 77 | + // Try to insert data and assert that authorization exception is thrown |
| 78 | + await Assert.ThrowsAsync<WeaviateAuthorizationException>(async () => |
| 79 | + { |
| 80 | + await userCollection.Data.Insert( |
| 81 | + new { name = "should fail" }, |
| 82 | + cancellationToken: TestContext.Current.CancellationToken |
| 83 | + ); |
| 84 | + }); |
| 85 | + } |
| 86 | +} |
0 commit comments