2023-02-06 18:26:06 +01:00
|
|
|
|
using System.Net;
|
2023-01-20 00:31:19 +01:00
|
|
|
|
using Bit.Api.IntegrationTest.Factories;
|
2023-02-06 18:26:06 +01:00
|
|
|
|
using Bit.Api.IntegrationTest.SecretsManager.Enums;
|
2024-03-29 17:00:30 +01:00
|
|
|
|
using Bit.Api.IntegrationTest.SecretsManager.Helpers;
|
2023-02-06 18:26:06 +01:00
|
|
|
|
using Bit.Api.Models.Response;
|
2023-01-24 19:57:28 +01:00
|
|
|
|
using Bit.Api.SecretsManager.Models.Request;
|
|
|
|
|
using Bit.Api.SecretsManager.Models.Response;
|
2023-11-08 17:42:40 +01:00
|
|
|
|
using Bit.Core.AdminConsole.Entities;
|
|
|
|
|
using Bit.Core.AdminConsole.Repositories;
|
2023-10-27 18:13:52 +02:00
|
|
|
|
using Bit.Core.Entities;
|
2023-02-06 18:26:06 +01:00
|
|
|
|
using Bit.Core.Enums;
|
2023-01-24 19:57:28 +01:00
|
|
|
|
using Bit.Core.SecretsManager.Entities;
|
|
|
|
|
using Bit.Core.SecretsManager.Repositories;
|
2023-01-20 00:31:19 +01:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
2023-01-24 19:57:28 +01:00
|
|
|
|
namespace Bit.Api.IntegrationTest.SecretsManager.Controllers;
|
2023-01-20 00:31:19 +01:00
|
|
|
|
|
2023-03-22 16:04:50 +01:00
|
|
|
|
public class AccessPoliciesControllerTests : IClassFixture<ApiApplicationFactory>, IAsyncLifetime
|
2023-01-20 00:31:19 +01:00
|
|
|
|
{
|
2023-02-06 18:26:06 +01:00
|
|
|
|
private const string _mockEncryptedString =
|
|
|
|
|
"2.3Uk+WNBIoU5xzmVFNcoWzz==|1MsPIYuRfdOHfu/0uY6H2Q==|/98sp4wb6pHP1VTZ9JcNCYgQjEUMFPlqJgCwRk1YXKg=";
|
|
|
|
|
|
2023-01-20 00:31:19 +01:00
|
|
|
|
private readonly IAccessPolicyRepository _accessPolicyRepository;
|
|
|
|
|
|
|
|
|
|
private readonly HttpClient _client;
|
|
|
|
|
private readonly ApiApplicationFactory _factory;
|
2023-11-08 17:42:40 +01:00
|
|
|
|
private readonly IGroupRepository _groupRepository;
|
2024-03-29 17:00:30 +01:00
|
|
|
|
private readonly LoginHelper _loginHelper;
|
2024-05-06 21:56:58 +02:00
|
|
|
|
private readonly IProjectRepository _projectRepository;
|
2024-06-07 19:08:38 +02:00
|
|
|
|
private readonly ISecretRepository _secretRepository;
|
2024-05-06 21:56:58 +02:00
|
|
|
|
private readonly IServiceAccountRepository _serviceAccountRepository;
|
2023-02-06 18:26:06 +01:00
|
|
|
|
private string _email = null!;
|
|
|
|
|
private SecretsManagerOrganizationHelper _organizationHelper = null!;
|
2023-01-20 00:31:19 +01:00
|
|
|
|
|
2023-03-22 16:04:50 +01:00
|
|
|
|
public AccessPoliciesControllerTests(ApiApplicationFactory factory)
|
2023-01-20 00:31:19 +01:00
|
|
|
|
{
|
|
|
|
|
_factory = factory;
|
|
|
|
|
_client = _factory.CreateClient();
|
|
|
|
|
_accessPolicyRepository = _factory.GetService<IAccessPolicyRepository>();
|
|
|
|
|
_serviceAccountRepository = _factory.GetService<IServiceAccountRepository>();
|
2024-06-07 19:08:38 +02:00
|
|
|
|
_secretRepository = _factory.GetService<ISecretRepository>();
|
2023-01-20 00:31:19 +01:00
|
|
|
|
_projectRepository = _factory.GetService<IProjectRepository>();
|
2023-11-08 17:42:40 +01:00
|
|
|
|
_groupRepository = _factory.GetService<IGroupRepository>();
|
2024-03-29 17:00:30 +01:00
|
|
|
|
_loginHelper = new LoginHelper(_factory, _client);
|
2023-01-20 00:31:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task InitializeAsync()
|
|
|
|
|
{
|
2023-02-06 18:26:06 +01:00
|
|
|
|
_email = $"integration-test{Guid.NewGuid()}@bitwarden.com";
|
|
|
|
|
await _factory.LoginWithNewAccount(_email);
|
|
|
|
|
_organizationHelper = new SecretsManagerOrganizationHelper(_factory, _email);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task DisposeAsync()
|
|
|
|
|
{
|
|
|
|
|
_client.Dispose();
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
2023-10-16 16:29:02 +02:00
|
|
|
|
[InlineData(false, false, false)]
|
|
|
|
|
[InlineData(false, false, true)]
|
|
|
|
|
[InlineData(false, true, false)]
|
|
|
|
|
[InlineData(false, true, true)]
|
|
|
|
|
[InlineData(true, false, false)]
|
|
|
|
|
[InlineData(true, false, true)]
|
|
|
|
|
[InlineData(true, true, false)]
|
2024-05-06 21:56:58 +02:00
|
|
|
|
public async Task GetPeoplePotentialGrantees_SmAccessDenied_NotFound(bool useSecrets, bool accessSecrets,
|
|
|
|
|
bool organizationEnabled)
|
2023-02-06 18:26:06 +01:00
|
|
|
|
{
|
2023-10-16 16:29:02 +02:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(useSecrets, accessSecrets, organizationEnabled);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-02-06 18:26:06 +01:00
|
|
|
|
|
|
|
|
|
var response =
|
|
|
|
|
await _client.GetAsync(
|
|
|
|
|
$"/organizations/{org.Id}/access-policies/people/potential-grantees");
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task GetPeoplePotentialGrantees_Success(PermissionType permissionType)
|
|
|
|
|
{
|
2023-10-16 16:29:02 +02:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-02-06 18:26:06 +01:00
|
|
|
|
|
|
|
|
|
if (permissionType == PermissionType.RunAsUserWithPermission)
|
|
|
|
|
{
|
2023-02-16 16:47:34 +01:00
|
|
|
|
var (email, _) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(email);
|
2023-02-06 18:26:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var response =
|
|
|
|
|
await _client.GetAsync(
|
|
|
|
|
$"/organizations/{org.Id}/access-policies/people/potential-grantees");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ListResponseModel<PotentialGranteeResponseModel>>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result?.Data);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
Assert.NotEmpty(result.Data);
|
2023-02-06 18:26:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
2023-10-16 16:29:02 +02:00
|
|
|
|
[InlineData(false, false, false)]
|
|
|
|
|
[InlineData(false, false, true)]
|
|
|
|
|
[InlineData(false, true, false)]
|
|
|
|
|
[InlineData(false, true, true)]
|
|
|
|
|
[InlineData(true, false, false)]
|
|
|
|
|
[InlineData(true, false, true)]
|
|
|
|
|
[InlineData(true, true, false)]
|
2024-05-06 21:56:58 +02:00
|
|
|
|
public async Task GetServiceAccountPotentialGrantees_SmAccessDenied_NotFound(bool useSecrets, bool accessSecrets,
|
|
|
|
|
bool organizationEnabled)
|
2023-02-06 18:26:06 +01:00
|
|
|
|
{
|
2023-10-16 16:29:02 +02:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(useSecrets, accessSecrets, organizationEnabled);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-02-06 18:26:06 +01:00
|
|
|
|
|
|
|
|
|
var response =
|
|
|
|
|
await _client.GetAsync(
|
|
|
|
|
$"/organizations/{org.Id}/access-policies/service-accounts/potential-grantees");
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetServiceAccountPotentialGrantees_OnlyReturnsServiceAccountsWithWriteAccess()
|
|
|
|
|
{
|
|
|
|
|
// Create a new account as a user
|
2023-10-16 16:29:02 +02:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
2023-02-16 16:47:34 +01:00
|
|
|
|
var (email, _) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(email);
|
2023-02-06 18:26:06 +01:00
|
|
|
|
|
2023-02-16 16:47:34 +01:00
|
|
|
|
await _serviceAccountRepository.CreateAsync(new ServiceAccount
|
2023-02-06 18:26:06 +01:00
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Name = _mockEncryptedString
|
2023-02-06 18:26:06 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var response =
|
|
|
|
|
await _client.GetAsync(
|
|
|
|
|
$"/organizations/{org.Id}/access-policies/service-accounts/potential-grantees");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ListResponseModel<PotentialGranteeResponseModel>>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result?.Data);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
Assert.Empty(result.Data);
|
2023-02-06 18:26:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task GetServiceAccountsPotentialGrantees_Success(PermissionType permissionType)
|
|
|
|
|
{
|
2023-10-16 16:29:02 +02:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-02-06 18:26:06 +01:00
|
|
|
|
|
|
|
|
|
var serviceAccount = await _serviceAccountRepository.CreateAsync(new ServiceAccount
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Name = _mockEncryptedString
|
2023-02-06 18:26:06 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (permissionType == PermissionType.RunAsUserWithPermission)
|
|
|
|
|
{
|
|
|
|
|
var (email, orgUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(email);
|
2023-02-06 18:26:06 +01:00
|
|
|
|
|
|
|
|
|
await _accessPolicyRepository.CreateManyAsync(
|
2024-05-06 21:56:58 +02:00
|
|
|
|
[
|
|
|
|
|
new UserServiceAccountAccessPolicy
|
2023-02-06 18:26:06 +01:00
|
|
|
|
{
|
2024-05-06 21:56:58 +02:00
|
|
|
|
GrantedServiceAccountId = serviceAccount.Id,
|
|
|
|
|
OrganizationUserId = orgUser.Id,
|
|
|
|
|
Read = true,
|
|
|
|
|
Write = true
|
|
|
|
|
}
|
|
|
|
|
]);
|
2023-02-06 18:26:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var response =
|
|
|
|
|
await _client.GetAsync(
|
|
|
|
|
$"/organizations/{org.Id}/access-policies/service-accounts/potential-grantees");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ListResponseModel<PotentialGranteeResponseModel>>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result?.Data);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
Assert.NotEmpty(result.Data);
|
2023-07-14 17:18:26 +02:00
|
|
|
|
Assert.Equal(serviceAccount.Id, result.Data.First(x => x.Id == serviceAccount.Id).Id);
|
2023-02-16 16:47:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
2023-10-16 16:29:02 +02:00
|
|
|
|
[InlineData(false, false, false)]
|
|
|
|
|
[InlineData(false, false, true)]
|
|
|
|
|
[InlineData(false, true, false)]
|
|
|
|
|
[InlineData(false, true, true)]
|
|
|
|
|
[InlineData(true, false, false)]
|
|
|
|
|
[InlineData(true, false, true)]
|
|
|
|
|
[InlineData(true, true, false)]
|
2024-05-06 21:56:58 +02:00
|
|
|
|
public async Task GetProjectPotentialGrantees_SmAccessDenied_NotFound(bool useSecrets, bool accessSecrets,
|
|
|
|
|
bool organizationEnabled)
|
2023-02-16 16:47:34 +01:00
|
|
|
|
{
|
2023-10-16 16:29:02 +02:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(useSecrets, accessSecrets, organizationEnabled);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-02-16 16:47:34 +01:00
|
|
|
|
|
|
|
|
|
var response =
|
|
|
|
|
await _client.GetAsync(
|
|
|
|
|
$"/organizations/{org.Id}/access-policies/projects/potential-grantees");
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetProjectPotentialGrantees_OnlyReturnsProjectsWithWriteAccess()
|
|
|
|
|
{
|
|
|
|
|
// Create a new account as a user
|
2023-10-16 16:29:02 +02:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
2023-02-16 16:47:34 +01:00
|
|
|
|
var (email, _) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(email);
|
2023-02-16 16:47:34 +01:00
|
|
|
|
|
|
|
|
|
await _projectRepository.CreateAsync(new Project { OrganizationId = org.Id, Name = _mockEncryptedString });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var response =
|
|
|
|
|
await _client.GetAsync(
|
|
|
|
|
$"/organizations/{org.Id}/access-policies/projects/potential-grantees");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ListResponseModel<PotentialGranteeResponseModel>>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result?.Data);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
Assert.Empty(result.Data);
|
2023-02-16 16:47:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task GetProjectPotentialGrantees_Success(PermissionType permissionType)
|
|
|
|
|
{
|
2023-10-16 16:29:02 +02:00
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-02-16 16:47:34 +01:00
|
|
|
|
|
|
|
|
|
var project = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Name = _mockEncryptedString
|
2023-02-16 16:47:34 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (permissionType == PermissionType.RunAsUserWithPermission)
|
|
|
|
|
{
|
|
|
|
|
var (email, orgUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(email);
|
2023-02-16 16:47:34 +01:00
|
|
|
|
|
|
|
|
|
await _accessPolicyRepository.CreateManyAsync(
|
2024-05-06 21:56:58 +02:00
|
|
|
|
[
|
|
|
|
|
new UserProjectAccessPolicy
|
2023-02-16 16:47:34 +01:00
|
|
|
|
{
|
2024-05-06 21:56:58 +02:00
|
|
|
|
GrantedProjectId = project.Id,
|
|
|
|
|
OrganizationUserId = orgUser.Id,
|
|
|
|
|
Read = true,
|
|
|
|
|
Write = true
|
|
|
|
|
}
|
|
|
|
|
]);
|
2023-02-16 16:47:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var response =
|
|
|
|
|
await _client.GetAsync(
|
|
|
|
|
$"/organizations/{org.Id}/access-policies/projects/potential-grantees");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ListResponseModel<PotentialGranteeResponseModel>>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result?.Data);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
Assert.NotEmpty(result.Data);
|
2023-07-14 17:18:26 +02:00
|
|
|
|
Assert.Equal(project.Id, result.Data.First(x => x.Id == project.Id).Id);
|
2023-02-06 18:26:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 17:42:40 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false, false, false)]
|
|
|
|
|
[InlineData(false, false, true)]
|
|
|
|
|
[InlineData(false, true, false)]
|
|
|
|
|
[InlineData(false, true, true)]
|
|
|
|
|
[InlineData(true, false, false)]
|
|
|
|
|
[InlineData(true, false, true)]
|
|
|
|
|
[InlineData(true, true, false)]
|
2024-05-06 21:56:58 +02:00
|
|
|
|
public async Task GetProjectPeopleAccessPolicies_SmAccessDenied_NotFound(bool useSecrets, bool accessSecrets,
|
|
|
|
|
bool organizationEnabled)
|
2023-11-08 17:42:40 +01:00
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(useSecrets, accessSecrets, organizationEnabled);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-11-08 17:42:40 +01:00
|
|
|
|
|
2023-12-07 22:35:16 +01:00
|
|
|
|
var project = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
|
|
|
|
Name = _mockEncryptedString
|
|
|
|
|
});
|
2023-11-08 17:42:40 +01:00
|
|
|
|
|
2023-12-07 22:35:16 +01:00
|
|
|
|
var response = await _client.GetAsync($"/projects/{project.Id}/access-policies/people");
|
2023-11-08 17:42:40 +01:00
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetProjectPeopleAccessPolicies_ReturnsEmpty()
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-11-08 17:42:40 +01:00
|
|
|
|
|
|
|
|
|
var project = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
|
|
|
|
Name = _mockEncryptedString
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/projects/{project.Id}/access-policies/people");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ProjectPeopleAccessPoliciesResponseModel>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
Assert.Empty(result.UserAccessPolicies);
|
2023-11-08 17:42:40 +01:00
|
|
|
|
Assert.Empty(result.GroupAccessPolicies);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetProjectPeopleAccessPolicies_NoPermission_NotFound()
|
|
|
|
|
{
|
|
|
|
|
await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
var (email, orgUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(email);
|
2023-11-08 17:42:40 +01:00
|
|
|
|
|
|
|
|
|
var project = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = orgUser.OrganizationId,
|
|
|
|
|
Name = _mockEncryptedString
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/projects/{project.Id}/access-policies/people");
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task GetProjectPeopleAccessPolicies_Success(PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var (_, organizationUser) = await _organizationHelper.Initialize(true, true, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-11-08 17:42:40 +01:00
|
|
|
|
|
|
|
|
|
var (project, _) = await SetupProjectPeoplePermissionAsync(permissionType, organizationUser);
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/projects/{project.Id}/access-policies/people");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ProjectPeopleAccessPoliciesResponseModel>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result?.UserAccessPolicies);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
Assert.Single(result.UserAccessPolicies);
|
2023-11-08 17:42:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false, false, false)]
|
|
|
|
|
[InlineData(false, false, true)]
|
|
|
|
|
[InlineData(false, true, false)]
|
|
|
|
|
[InlineData(false, true, true)]
|
|
|
|
|
[InlineData(true, false, false)]
|
|
|
|
|
[InlineData(true, false, true)]
|
|
|
|
|
[InlineData(true, true, false)]
|
2024-05-06 21:56:58 +02:00
|
|
|
|
public async Task PutProjectPeopleAccessPolicies_SmAccessDenied_NotFound(bool useSecrets, bool accessSecrets,
|
|
|
|
|
bool organizationEnabled)
|
2023-11-08 17:42:40 +01:00
|
|
|
|
{
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var (_, organizationUser) =
|
|
|
|
|
await _organizationHelper.Initialize(useSecrets, accessSecrets, organizationEnabled);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-11-08 17:42:40 +01:00
|
|
|
|
|
|
|
|
|
var (project, request) = await SetupProjectPeopleRequestAsync(PermissionType.RunAsAdmin, organizationUser);
|
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync($"/projects/{project.Id}/access-policies/people", request);
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task PutProjectPeopleAccessPolicies_NoPermission()
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
var (email, organizationUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(email);
|
2023-11-08 17:42:40 +01:00
|
|
|
|
|
|
|
|
|
var project = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
|
|
|
|
Name = _mockEncryptedString
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var request = new PeopleAccessPoliciesRequestModel
|
|
|
|
|
{
|
|
|
|
|
UserAccessPolicyRequests = new List<AccessPolicyRequest>
|
|
|
|
|
{
|
|
|
|
|
new() { GranteeId = organizationUser.Id, Read = true, Write = true }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync($"/projects/{project.Id}/access-policies/people", request);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task PutProjectPeopleAccessPolicies_MismatchedOrgIds_NotFound(PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var (_, organizationUser) = await _organizationHelper.Initialize(true, true, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-11-08 17:42:40 +01:00
|
|
|
|
|
|
|
|
|
var (project, request) = await SetupProjectPeopleRequestAsync(permissionType, organizationUser);
|
|
|
|
|
var newOrg = await _organizationHelper.CreateSmOrganizationAsync();
|
|
|
|
|
var group = await _groupRepository.CreateAsync(new Group
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = newOrg.Id,
|
|
|
|
|
Name = _mockEncryptedString
|
|
|
|
|
});
|
|
|
|
|
request.GroupAccessPolicyRequests = new List<AccessPolicyRequest>
|
|
|
|
|
{
|
|
|
|
|
new() { GranteeId = group.Id, Read = true, Write = true }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync($"/projects/{project.Id}/access-policies/people", request);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task PutProjectPeopleAccessPolicies_Success(PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var (_, organizationUser) = await _organizationHelper.Initialize(true, true, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-11-08 17:42:40 +01:00
|
|
|
|
|
|
|
|
|
var (project, request) = await SetupProjectPeopleRequestAsync(permissionType, organizationUser);
|
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync($"/projects/{project.Id}/access-policies/people", request);
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ProjectPeopleAccessPoliciesResponseModel>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.Equal(request.UserAccessPolicyRequests.First().GranteeId,
|
2024-03-29 17:00:30 +01:00
|
|
|
|
result.UserAccessPolicies.First().OrganizationUserId);
|
2023-11-08 17:42:40 +01:00
|
|
|
|
Assert.True(result.UserAccessPolicies.First().Read);
|
|
|
|
|
Assert.True(result.UserAccessPolicies.First().Write);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-07 22:35:16 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false, false, false)]
|
|
|
|
|
[InlineData(false, false, true)]
|
|
|
|
|
[InlineData(false, true, false)]
|
|
|
|
|
[InlineData(false, true, true)]
|
|
|
|
|
[InlineData(true, false, false)]
|
|
|
|
|
[InlineData(true, false, true)]
|
|
|
|
|
[InlineData(true, true, false)]
|
2024-05-06 21:56:58 +02:00
|
|
|
|
public async Task GetServiceAccountPeopleAccessPolicies_SmAccessDenied_NotFound(bool useSecrets, bool accessSecrets,
|
|
|
|
|
bool organizationEnabled)
|
2023-12-07 22:35:16 +01:00
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(useSecrets, accessSecrets, organizationEnabled);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
var serviceAccount = await _serviceAccountRepository.CreateAsync(new ServiceAccount
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Name = _mockEncryptedString
|
2023-12-07 22:35:16 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/service-accounts/{serviceAccount.Id}/access-policies/people");
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetServiceAccountPeopleAccessPolicies_ReturnsEmpty()
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
|
|
|
|
|
var serviceAccount = await _serviceAccountRepository.CreateAsync(new ServiceAccount
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Name = _mockEncryptedString
|
2023-12-07 22:35:16 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/service-accounts/{serviceAccount.Id}/access-policies/people");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ServiceAccountPeopleAccessPoliciesResponseModel>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
Assert.Empty(result.UserAccessPolicies);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
Assert.Empty(result.GroupAccessPolicies);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetServiceAccountPeopleAccessPolicies_NoPermission()
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
var (email, _) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(email);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
|
|
|
|
|
var serviceAccount = await _serviceAccountRepository.CreateAsync(new ServiceAccount
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Name = _mockEncryptedString
|
2023-12-07 22:35:16 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/service-accounts/{serviceAccount.Id}/access-policies/people");
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task GetServiceAccountPeopleAccessPolicies_Success(PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var (_, organizationUser) = await _organizationHelper.Initialize(true, true, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
|
|
|
|
|
var (serviceAccount, _) = await SetupServiceAccountPeoplePermissionAsync(permissionType, organizationUser);
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/service-accounts/{serviceAccount.Id}/access-policies/people");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ServiceAccountPeopleAccessPoliciesResponseModel>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result?.UserAccessPolicies);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
Assert.Single(result.UserAccessPolicies);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
2024-05-01 18:47:11 +02:00
|
|
|
|
[InlineData(false, false, false)]
|
|
|
|
|
[InlineData(false, false, true)]
|
|
|
|
|
[InlineData(false, true, false)]
|
|
|
|
|
[InlineData(false, true, true)]
|
|
|
|
|
[InlineData(true, false, false)]
|
|
|
|
|
[InlineData(true, false, true)]
|
|
|
|
|
[InlineData(true, true, false)]
|
2024-05-06 21:56:58 +02:00
|
|
|
|
public async Task PutServiceAccountPeopleAccessPolicies_SmNotEnabled_NotFound(bool useSecrets, bool accessSecrets,
|
|
|
|
|
bool organizationEnabled)
|
2023-12-07 22:35:16 +01:00
|
|
|
|
{
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var (_, organizationUser) =
|
|
|
|
|
await _organizationHelper.Initialize(useSecrets, accessSecrets, organizationEnabled);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var (serviceAccount, request) =
|
|
|
|
|
await SetupServiceAccountPeopleRequestAsync(PermissionType.RunAsAdmin, organizationUser);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var response =
|
|
|
|
|
await _client.PutAsJsonAsync($"/service-accounts/{serviceAccount.Id}/access-policies/people", request);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task PutServiceAccountPeopleAccessPolicies_NoPermission()
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
var (email, organizationUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(email);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
|
|
|
|
|
var serviceAccount = await _serviceAccountRepository.CreateAsync(new ServiceAccount
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Name = _mockEncryptedString
|
2023-12-07 22:35:16 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var request = new PeopleAccessPoliciesRequestModel
|
|
|
|
|
{
|
|
|
|
|
UserAccessPolicyRequests = new List<AccessPolicyRequest>
|
|
|
|
|
{
|
|
|
|
|
new() { GranteeId = organizationUser.Id, Read = true, Write = true }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var response =
|
|
|
|
|
await _client.PutAsJsonAsync($"/service-accounts/{serviceAccount.Id}/access-policies/people", request);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task PutServiceAccountPeopleAccessPolicies_MismatchedOrgIds_NotFound(PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var (_, organizationUser) = await _organizationHelper.Initialize(true, true, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
|
|
|
|
|
var (serviceAccount, request) = await SetupServiceAccountPeopleRequestAsync(permissionType, organizationUser);
|
|
|
|
|
var newOrg = await _organizationHelper.CreateSmOrganizationAsync();
|
|
|
|
|
var group = await _groupRepository.CreateAsync(new Group
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = newOrg.Id,
|
|
|
|
|
Name = _mockEncryptedString
|
|
|
|
|
});
|
|
|
|
|
request.GroupAccessPolicyRequests = new List<AccessPolicyRequest>
|
|
|
|
|
{
|
|
|
|
|
new() { GranteeId = group.Id, Read = true, Write = true }
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var response =
|
|
|
|
|
await _client.PutAsJsonAsync($"/service-accounts/{serviceAccount.Id}/access-policies/people", request);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task PutServiceAccountPeopleAccessPolicies_Success(PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var (_, organizationUser) = await _organizationHelper.Initialize(true, true, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
|
|
|
|
|
var (serviceAccount, request) = await SetupServiceAccountPeopleRequestAsync(permissionType, organizationUser);
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var response =
|
|
|
|
|
await _client.PutAsJsonAsync($"/service-accounts/{serviceAccount.Id}/access-policies/people", request);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ServiceAccountPeopleAccessPoliciesResponseModel>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.Equal(request.UserAccessPolicyRequests.First().GranteeId,
|
2024-03-29 17:00:30 +01:00
|
|
|
|
result.UserAccessPolicies.First().OrganizationUserId);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
Assert.True(result.UserAccessPolicies.First().Read);
|
|
|
|
|
Assert.True(result.UserAccessPolicies.First().Write);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 18:47:11 +02:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false, false, false)]
|
|
|
|
|
[InlineData(false, false, true)]
|
|
|
|
|
[InlineData(false, true, false)]
|
|
|
|
|
[InlineData(false, true, true)]
|
|
|
|
|
[InlineData(true, false, false)]
|
|
|
|
|
[InlineData(true, false, true)]
|
|
|
|
|
[InlineData(true, true, false)]
|
2024-05-06 21:56:58 +02:00
|
|
|
|
public async Task GetServiceAccountGrantedPoliciesAsync_SmAccessDenied_ReturnsNotFound(bool useSecrets,
|
|
|
|
|
bool accessSecrets, bool organizationEnabled)
|
2024-05-01 18:47:11 +02:00
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(useSecrets, accessSecrets, organizationEnabled);
|
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var initData = await CreateServiceAccountProjectAccessPolicyAsync(org.Id);
|
2024-05-01 18:47:11 +02:00
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/service-accounts/{initData.ServiceAccountId}/granted-policies");
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetServiceAccountGrantedPoliciesAsync_NoAccessPolicies_ReturnsEmpty()
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
|
|
|
|
|
|
|
|
|
var serviceAccount = await _serviceAccountRepository.CreateAsync(new ServiceAccount
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Name = _mockEncryptedString
|
2024-05-01 18:47:11 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/service-accounts/{serviceAccount.Id}/granted-policies");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var result = await response.Content
|
|
|
|
|
.ReadFromJsonAsync<ServiceAccountGrantedPoliciesPermissionDetailsResponseModel>();
|
2024-05-01 18:47:11 +02:00
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.Empty(result.GrantedProjectPolicies);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetServiceAccountGrantedPoliciesAsync_UserDoesntHavePermission_ReturnsNotFound()
|
|
|
|
|
{
|
|
|
|
|
// Create a new account as a user
|
|
|
|
|
await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
var (email, orgUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await _loginHelper.LoginAsync(email);
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var initData = await CreateServiceAccountProjectAccessPolicyAsync(orgUser.OrganizationId);
|
2024-05-01 18:47:11 +02:00
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/service-accounts/{initData.ServiceAccountId}/granted-policies");
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task GetServiceAccountGrantedPoliciesAsync_Success(PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var initData = await CreateServiceAccountProjectAccessPolicyAsync(org.Id);
|
2024-05-01 18:47:11 +02:00
|
|
|
|
|
|
|
|
|
if (permissionType == PermissionType.RunAsUserWithPermission)
|
|
|
|
|
{
|
|
|
|
|
var (email, orgUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await _loginHelper.LoginAsync(email);
|
|
|
|
|
var accessPolicies = new List<BaseAccessPolicy>
|
|
|
|
|
{
|
|
|
|
|
new UserServiceAccountAccessPolicy
|
|
|
|
|
{
|
2024-05-06 21:56:58 +02:00
|
|
|
|
GrantedServiceAccountId = initData.ServiceAccountId,
|
|
|
|
|
OrganizationUserId = orgUser.Id,
|
|
|
|
|
Read = true,
|
|
|
|
|
Write = true
|
2024-05-01 18:47:11 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
await _accessPolicyRepository.CreateManyAsync(accessPolicies);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/service-accounts/{initData.ServiceAccountId}/granted-policies");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content
|
|
|
|
|
.ReadFromJsonAsync<ServiceAccountGrantedPoliciesPermissionDetailsResponseModel>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.NotEmpty(result.GrantedProjectPolicies);
|
|
|
|
|
Assert.NotNull(result.GrantedProjectPolicies.First().AccessPolicy.GrantedProjectName);
|
2024-06-07 19:08:38 +02:00
|
|
|
|
Assert.NotNull(result.GrantedProjectPolicies.First().AccessPolicy.GrantedProjectId);
|
2024-05-01 18:47:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false, false, false)]
|
|
|
|
|
[InlineData(false, false, true)]
|
|
|
|
|
[InlineData(false, true, false)]
|
|
|
|
|
[InlineData(false, true, true)]
|
|
|
|
|
[InlineData(true, false, false)]
|
|
|
|
|
[InlineData(true, false, true)]
|
|
|
|
|
[InlineData(true, true, false)]
|
2024-05-06 21:56:58 +02:00
|
|
|
|
public async Task PutServiceAccountGrantedPoliciesAsync_SmNotEnabled_NotFound(bool useSecrets, bool accessSecrets,
|
|
|
|
|
bool organizationEnabled)
|
2024-05-01 18:47:11 +02:00
|
|
|
|
{
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var (_, organizationUser) =
|
|
|
|
|
await _organizationHelper.Initialize(useSecrets, accessSecrets, organizationEnabled);
|
2024-05-01 18:47:11 +02:00
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var (serviceAccount, request) =
|
|
|
|
|
await SetupServiceAccountGrantedPoliciesRequestAsync(PermissionType.RunAsAdmin, organizationUser, false);
|
2024-05-01 18:47:11 +02:00
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync($"/service-accounts/{serviceAccount.Id}/granted-policies", request);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task PutServiceAccountGrantedPoliciesAsync_UserHasNoPermission_ReturnsNotFound()
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
var (email, _) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await _loginHelper.LoginAsync(email);
|
|
|
|
|
|
|
|
|
|
var (projectId, serviceAccountId) = await CreateProjectAndServiceAccountAsync(org.Id);
|
|
|
|
|
|
|
|
|
|
var request = new ServiceAccountGrantedPoliciesRequestModel
|
|
|
|
|
{
|
|
|
|
|
ProjectGrantedPolicyRequests = new List<GrantedAccessPolicyRequest>
|
|
|
|
|
{
|
|
|
|
|
new() { GrantedId = projectId, Read = true, Write = true }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync($"/service-accounts/{serviceAccountId}/granted-policies", request);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
2024-05-06 21:56:58 +02:00
|
|
|
|
public async Task PutServiceAccountGrantedPoliciesAsync_MismatchedOrgIds_ReturnsNotFound(
|
|
|
|
|
PermissionType permissionType)
|
2024-05-01 18:47:11 +02:00
|
|
|
|
{
|
|
|
|
|
var (_, organizationUser) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var (serviceAccount, request) =
|
|
|
|
|
await SetupServiceAccountGrantedPoliciesRequestAsync(permissionType, organizationUser, false);
|
2024-05-01 18:47:11 +02:00
|
|
|
|
var newOrg = await _organizationHelper.CreateSmOrganizationAsync();
|
|
|
|
|
|
|
|
|
|
var project = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
Name = _mockEncryptedString,
|
|
|
|
|
OrganizationId = newOrg.Id
|
|
|
|
|
});
|
|
|
|
|
request.ProjectGrantedPolicyRequests = new List<GrantedAccessPolicyRequest>
|
|
|
|
|
{
|
|
|
|
|
new() { GrantedId = project.Id, Read = true, Write = true }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync($"/service-accounts/{serviceAccount.Id}/granted-policies", request);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin, false)]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin, true)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission, false)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission, true)]
|
2024-05-06 21:56:58 +02:00
|
|
|
|
public async Task PutServiceAccountGrantedPoliciesAsync_Success(PermissionType permissionType,
|
|
|
|
|
bool createPreviousAccessPolicy)
|
2024-05-01 18:47:11 +02:00
|
|
|
|
{
|
|
|
|
|
var (_, organizationUser) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var (serviceAccount, request) =
|
|
|
|
|
await SetupServiceAccountGrantedPoliciesRequestAsync(permissionType, organizationUser,
|
|
|
|
|
createPreviousAccessPolicy);
|
2024-05-01 18:47:11 +02:00
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync($"/service-accounts/{serviceAccount.Id}/granted-policies", request);
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var result = await response.Content
|
|
|
|
|
.ReadFromJsonAsync<ServiceAccountGrantedPoliciesPermissionDetailsResponseModel>();
|
2024-05-01 18:47:11 +02:00
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.Equal(request.ProjectGrantedPolicyRequests.First().GrantedId,
|
|
|
|
|
result.GrantedProjectPolicies.First().AccessPolicy.GrantedProjectId);
|
|
|
|
|
Assert.True(result.GrantedProjectPolicies.First().AccessPolicy.Read);
|
|
|
|
|
Assert.True(result.GrantedProjectPolicies.First().AccessPolicy.Write);
|
|
|
|
|
Assert.True(result.GrantedProjectPolicies.First().HasPermission);
|
|
|
|
|
Assert.Single(result.GrantedProjectPolicies);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-02 18:06:20 +02:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false, false, false)]
|
|
|
|
|
[InlineData(false, false, true)]
|
|
|
|
|
[InlineData(false, true, false)]
|
|
|
|
|
[InlineData(false, true, true)]
|
|
|
|
|
[InlineData(true, false, false)]
|
|
|
|
|
[InlineData(true, false, true)]
|
|
|
|
|
[InlineData(true, true, false)]
|
2024-05-06 21:56:58 +02:00
|
|
|
|
public async Task GetProjectServiceAccountsAccessPoliciesAsync_SmAccessDenied_ReturnsNotFound(bool useSecrets,
|
|
|
|
|
bool accessSecrets, bool organizationEnabled)
|
2024-05-02 18:06:20 +02:00
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(useSecrets, accessSecrets, organizationEnabled);
|
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var initData = await CreateServiceAccountProjectAccessPolicyAsync(org.Id);
|
2024-05-02 18:06:20 +02:00
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/projects/{initData.ProjectId}/access-policies/service-accounts");
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetProjectServiceAccountsAccessPoliciesAsync_NoAccessPolicies_ReturnsEmpty()
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
|
|
|
|
|
|
|
|
|
var project = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Name = _mockEncryptedString
|
2024-05-02 18:06:20 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/projects/{project.Id}/access-policies/service-accounts");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<ProjectServiceAccountsAccessPoliciesResponseModel>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.Empty(result.ServiceAccountAccessPolicies);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetProjectServiceAccountsAccessPoliciesAsync_UserDoesntHavePermission_ReturnsNotFound()
|
|
|
|
|
{
|
|
|
|
|
// Create a new account as a user
|
|
|
|
|
await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
var (email, orgUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await _loginHelper.LoginAsync(email);
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var initData = await CreateServiceAccountProjectAccessPolicyAsync(orgUser.OrganizationId);
|
2024-05-02 18:06:20 +02:00
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/projects/{initData.ProjectId}/access-policies/service-accounts");
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task GetProjectServiceAccountsAccessPoliciesAsync_Success(PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var initData = await CreateServiceAccountProjectAccessPolicyAsync(org.Id);
|
2024-05-02 18:06:20 +02:00
|
|
|
|
|
|
|
|
|
if (permissionType == PermissionType.RunAsUserWithPermission)
|
|
|
|
|
{
|
|
|
|
|
var (email, orgUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await _loginHelper.LoginAsync(email);
|
|
|
|
|
var accessPolicies = new List<BaseAccessPolicy>
|
|
|
|
|
{
|
|
|
|
|
new UserProjectAccessPolicy
|
|
|
|
|
{
|
2024-05-06 21:56:58 +02:00
|
|
|
|
GrantedProjectId = initData.ProjectId,
|
|
|
|
|
OrganizationUserId = orgUser.Id,
|
|
|
|
|
Read = true,
|
|
|
|
|
Write = true
|
2024-05-02 18:06:20 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
await _accessPolicyRepository.CreateManyAsync(accessPolicies);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/projects/{initData.ProjectId}/access-policies/service-accounts");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content
|
|
|
|
|
.ReadFromJsonAsync<ProjectServiceAccountsAccessPoliciesResponseModel>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.NotEmpty(result.ServiceAccountAccessPolicies);
|
|
|
|
|
Assert.Equal(initData.ServiceAccountId, result.ServiceAccountAccessPolicies.First().ServiceAccountId);
|
|
|
|
|
Assert.NotNull(result.ServiceAccountAccessPolicies.First().ServiceAccountName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false, false, false)]
|
|
|
|
|
[InlineData(false, false, true)]
|
|
|
|
|
[InlineData(false, true, false)]
|
|
|
|
|
[InlineData(false, true, true)]
|
|
|
|
|
[InlineData(true, false, false)]
|
|
|
|
|
[InlineData(true, false, true)]
|
|
|
|
|
[InlineData(true, true, false)]
|
|
|
|
|
public async Task PutProjectServiceAccountsAccessPoliciesAsync_SmNotEnabled_NotFound(bool useSecrets,
|
|
|
|
|
bool accessSecrets, bool organizationEnabled)
|
|
|
|
|
{
|
|
|
|
|
var (_, organizationUser) =
|
|
|
|
|
await _organizationHelper.Initialize(useSecrets, accessSecrets, organizationEnabled);
|
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
|
|
|
|
|
|
|
|
|
var (projectId, serviceAccountId) = await CreateProjectAndServiceAccountAsync(organizationUser.OrganizationId);
|
|
|
|
|
|
|
|
|
|
var request = new ProjectServiceAccountsAccessPoliciesRequestModel
|
|
|
|
|
{
|
|
|
|
|
ServiceAccountAccessPolicyRequests =
|
|
|
|
|
[
|
|
|
|
|
new AccessPolicyRequest { GranteeId = serviceAccountId, Read = true, Write = true }
|
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync($"/projects/{projectId}/access-policies/service-accounts", request);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task PutProjectServiceAccountsAccessPoliciesAsync_UserHasNoPermission_ReturnsNotFound()
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
var (email, _) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await _loginHelper.LoginAsync(email);
|
|
|
|
|
|
|
|
|
|
var (projectId, serviceAccountId) = await CreateProjectAndServiceAccountAsync(org.Id);
|
|
|
|
|
|
|
|
|
|
var request = new ProjectServiceAccountsAccessPoliciesRequestModel
|
|
|
|
|
{
|
|
|
|
|
ServiceAccountAccessPolicyRequests =
|
|
|
|
|
[
|
|
|
|
|
new AccessPolicyRequest { GranteeId = serviceAccountId, Read = true, Write = true }
|
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await _client.PutAsJsonAsync($"/projects/{projectId}/access-policies/service-accounts", request);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission)]
|
|
|
|
|
public async Task PutProjectServiceAccountsAccessPoliciesAsync_MismatchedOrgIds_ReturnsNotFound(
|
|
|
|
|
PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var (_, organizationUser) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
|
|
|
|
|
|
|
|
|
var (project, request) =
|
|
|
|
|
await SetupProjectServiceAccountsAccessPoliciesRequestAsync(permissionType, organizationUser,
|
|
|
|
|
false);
|
|
|
|
|
|
|
|
|
|
var newOrg = await _organizationHelper.CreateSmOrganizationAsync();
|
|
|
|
|
|
|
|
|
|
var serviceAccount = await _serviceAccountRepository.CreateAsync(new ServiceAccount
|
|
|
|
|
{
|
|
|
|
|
Name = _mockEncryptedString,
|
|
|
|
|
OrganizationId = newOrg.Id
|
|
|
|
|
});
|
|
|
|
|
request.ServiceAccountAccessPolicyRequests =
|
|
|
|
|
[
|
|
|
|
|
new AccessPolicyRequest { GranteeId = serviceAccount.Id, Read = true, Write = true }
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
var response =
|
|
|
|
|
await _client.PutAsJsonAsync($"/projects/{project.Id}/access-policies/service-accounts", request);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin, false)]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin, true)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission, false)]
|
|
|
|
|
[InlineData(PermissionType.RunAsUserWithPermission, true)]
|
|
|
|
|
public async Task PutProjectServiceAccountsAccessPoliciesAsync_Success(PermissionType permissionType,
|
|
|
|
|
bool createPreviousAccessPolicy)
|
|
|
|
|
{
|
|
|
|
|
var (_, organizationUser) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
|
|
|
|
|
|
|
|
|
var (project, request) =
|
|
|
|
|
await SetupProjectServiceAccountsAccessPoliciesRequestAsync(permissionType, organizationUser,
|
|
|
|
|
createPreviousAccessPolicy);
|
|
|
|
|
|
|
|
|
|
var response =
|
|
|
|
|
await _client.PutAsJsonAsync($"/projects/{project.Id}/access-policies/service-accounts", request);
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content
|
|
|
|
|
.ReadFromJsonAsync<ProjectServiceAccountsAccessPoliciesResponseModel>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.Equal(request.ServiceAccountAccessPolicyRequests.First().GranteeId,
|
|
|
|
|
result.ServiceAccountAccessPolicies.First().ServiceAccountId);
|
|
|
|
|
Assert.True(result.ServiceAccountAccessPolicies.First().Read);
|
|
|
|
|
Assert.True(result.ServiceAccountAccessPolicies.First().Write);
|
|
|
|
|
Assert.Single(result.ServiceAccountAccessPolicies);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-07 19:08:38 +02:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(false, false, false)]
|
|
|
|
|
[InlineData(false, false, true)]
|
|
|
|
|
[InlineData(false, true, false)]
|
|
|
|
|
[InlineData(false, true, true)]
|
|
|
|
|
[InlineData(true, false, false)]
|
|
|
|
|
[InlineData(true, false, true)]
|
|
|
|
|
[InlineData(true, true, false)]
|
|
|
|
|
public async Task GetSecretAccessPoliciesAsync_SmAccessDenied_ReturnsNotFound(bool useSecrets,
|
|
|
|
|
bool accessSecrets, bool organizationEnabled)
|
|
|
|
|
{
|
|
|
|
|
var (org, _) = await _organizationHelper.Initialize(useSecrets, accessSecrets, organizationEnabled);
|
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
|
|
|
|
var secret = await _secretRepository.CreateAsync(new Secret
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
|
|
|
|
Key = _mockEncryptedString,
|
|
|
|
|
Value = _mockEncryptedString,
|
|
|
|
|
Note = _mockEncryptedString
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/secrets/{secret.Id}/access-policies");
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetSecretAccessPoliciesAsync_NoAccessPolicies_ReturnsEmpty()
|
|
|
|
|
{
|
|
|
|
|
var (secretId, _) = await SetupSecretAccessPoliciesTest(PermissionType.RunAsAdmin);
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/secrets/{secretId}/access-policies");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content
|
|
|
|
|
.ReadFromJsonAsync<SecretAccessPoliciesResponseModel>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.Empty(result.UserAccessPolicies);
|
|
|
|
|
Assert.Empty(result.GroupAccessPolicies);
|
|
|
|
|
Assert.Empty(result.ServiceAccountAccessPolicies);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetSecretAccessPoliciesAsync_UserDoesntHavePermission_ReturnsNotFound()
|
|
|
|
|
{
|
|
|
|
|
var (secretId, _) = await SetupSecretAccessPoliciesTest(PermissionType.RunAsUserWithPermission);
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/secrets/{secretId}/access-policies");
|
|
|
|
|
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(PermissionType.RunAsAdmin)]
|
|
|
|
|
public async Task GetSecretAccessPoliciesAsync_Success(PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var (secretId, currentOrgUser) = await SetupSecretAccessPoliciesTest(permissionType);
|
|
|
|
|
|
|
|
|
|
var accessPolicies = new List<BaseAccessPolicy>
|
|
|
|
|
{
|
|
|
|
|
new UserSecretAccessPolicy
|
|
|
|
|
{
|
|
|
|
|
GrantedSecretId = secretId, OrganizationUserId = currentOrgUser.Id, Read = true, Write = true
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
await _accessPolicyRepository.CreateManyAsync(accessPolicies);
|
|
|
|
|
|
|
|
|
|
var response = await _client.GetAsync($"/secrets/{secretId}/access-policies");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content
|
|
|
|
|
.ReadFromJsonAsync<SecretAccessPoliciesResponseModel>();
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
Assert.NotEmpty(result.UserAccessPolicies);
|
|
|
|
|
Assert.Empty(result.GroupAccessPolicies);
|
|
|
|
|
Assert.Empty(result.ServiceAccountAccessPolicies);
|
|
|
|
|
Assert.NotNull(result.UserAccessPolicies.First().OrganizationUserName);
|
|
|
|
|
Assert.NotNull(result.UserAccessPolicies.First().OrganizationUserId);
|
|
|
|
|
Assert.NotNull(result.UserAccessPolicies.First().CurrentUser);
|
|
|
|
|
Assert.Equal(currentOrgUser.Id, result.UserAccessPolicies.First().OrganizationUserId);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
private async Task<(Guid ProjectId, Guid ServiceAccountId)> CreateServiceAccountProjectAccessPolicyAsync(
|
|
|
|
|
Guid organizationId)
|
2023-01-20 00:31:19 +01:00
|
|
|
|
{
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var (projectId, serviceAccountId) = await CreateProjectAndServiceAccountAsync(organizationId);
|
2023-01-20 00:31:19 +01:00
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
await _accessPolicyRepository.CreateManyAsync(
|
2024-05-02 18:06:20 +02:00
|
|
|
|
[
|
|
|
|
|
new ServiceAccountProjectAccessPolicy
|
2023-01-20 00:31:19 +01:00
|
|
|
|
{
|
2024-05-02 18:06:20 +02:00
|
|
|
|
Read = true,
|
|
|
|
|
Write = true,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
ServiceAccountId = serviceAccountId,
|
|
|
|
|
GrantedProjectId = projectId
|
2024-05-02 18:06:20 +02:00
|
|
|
|
}
|
|
|
|
|
]);
|
2023-01-20 00:31:19 +01:00
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
return (projectId, serviceAccountId);
|
2023-01-20 00:31:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 17:42:40 +01:00
|
|
|
|
private async Task<(Project project, OrganizationUser currentUser)> SetupProjectPeoplePermissionAsync(
|
|
|
|
|
PermissionType permissionType,
|
|
|
|
|
OrganizationUser organizationUser)
|
|
|
|
|
{
|
|
|
|
|
var project = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = organizationUser.OrganizationId,
|
|
|
|
|
Name = _mockEncryptedString
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (permissionType == PermissionType.RunAsUserWithPermission)
|
|
|
|
|
{
|
|
|
|
|
var (email, orgUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(email);
|
2023-11-08 17:42:40 +01:00
|
|
|
|
organizationUser = orgUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var accessPolicies = new List<BaseAccessPolicy>
|
|
|
|
|
{
|
|
|
|
|
new UserProjectAccessPolicy
|
|
|
|
|
{
|
|
|
|
|
GrantedProjectId = project.Id,
|
|
|
|
|
OrganizationUserId = organizationUser.Id,
|
|
|
|
|
Read = true,
|
|
|
|
|
Write = true
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
await _accessPolicyRepository.CreateManyAsync(accessPolicies);
|
|
|
|
|
|
|
|
|
|
return (project, organizationUser);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
private async Task<(ServiceAccount serviceAccount, OrganizationUser currentUser)>
|
|
|
|
|
SetupServiceAccountPeoplePermissionAsync(
|
|
|
|
|
PermissionType permissionType,
|
|
|
|
|
OrganizationUser organizationUser)
|
2023-12-07 22:35:16 +01:00
|
|
|
|
{
|
|
|
|
|
var serviceAccount = await _serviceAccountRepository.CreateAsync(new ServiceAccount
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = organizationUser.OrganizationId,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Name = _mockEncryptedString
|
2023-12-07 22:35:16 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (permissionType == PermissionType.RunAsUserWithPermission)
|
|
|
|
|
{
|
|
|
|
|
var (email, orgUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
2024-03-29 17:00:30 +01:00
|
|
|
|
await _loginHelper.LoginAsync(email);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
organizationUser = orgUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var accessPolicies = new List<BaseAccessPolicy>
|
|
|
|
|
{
|
|
|
|
|
new UserServiceAccountAccessPolicy
|
|
|
|
|
{
|
|
|
|
|
GrantedServiceAccountId = serviceAccount.Id,
|
|
|
|
|
OrganizationUserId = organizationUser.Id,
|
|
|
|
|
Read = true,
|
|
|
|
|
Write = true
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-05-01 18:47:11 +02:00
|
|
|
|
|
2023-12-07 22:35:16 +01:00
|
|
|
|
await _accessPolicyRepository.CreateManyAsync(accessPolicies);
|
|
|
|
|
|
|
|
|
|
return (serviceAccount, organizationUser);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 17:42:40 +01:00
|
|
|
|
private async Task<(Project project, PeopleAccessPoliciesRequestModel request)> SetupProjectPeopleRequestAsync(
|
|
|
|
|
PermissionType permissionType, OrganizationUser organizationUser)
|
|
|
|
|
{
|
|
|
|
|
var (project, currentUser) = await SetupProjectPeoplePermissionAsync(permissionType, organizationUser);
|
|
|
|
|
var request = new PeopleAccessPoliciesRequestModel
|
|
|
|
|
{
|
|
|
|
|
UserAccessPolicyRequests = new List<AccessPolicyRequest>
|
|
|
|
|
{
|
|
|
|
|
new() { GranteeId = currentUser.Id, Read = true, Write = true }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return (project, request);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
private async Task<(ServiceAccount serviceAccount, PeopleAccessPoliciesRequestModel request)>
|
|
|
|
|
SetupServiceAccountPeopleRequestAsync(
|
|
|
|
|
PermissionType permissionType, OrganizationUser organizationUser)
|
2023-12-07 22:35:16 +01:00
|
|
|
|
{
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var (serviceAccount, currentUser) =
|
|
|
|
|
await SetupServiceAccountPeoplePermissionAsync(permissionType, organizationUser);
|
2023-12-07 22:35:16 +01:00
|
|
|
|
var request = new PeopleAccessPoliciesRequestModel
|
|
|
|
|
{
|
|
|
|
|
UserAccessPolicyRequests = new List<AccessPolicyRequest>
|
|
|
|
|
{
|
|
|
|
|
new() { GranteeId = currentUser.Id, Read = true, Write = true }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return (serviceAccount, request);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 18:46:01 +02:00
|
|
|
|
private async Task<(Guid ProjectId, Guid ServiceAccountId)> CreateProjectAndServiceAccountAsync(Guid organizationId,
|
|
|
|
|
bool misMatchOrganization = false)
|
|
|
|
|
{
|
2023-10-27 18:13:52 +02:00
|
|
|
|
var newOrg = new Organization();
|
|
|
|
|
if (misMatchOrganization)
|
|
|
|
|
{
|
|
|
|
|
newOrg = await _organizationHelper.CreateSmOrganizationAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-13 18:46:01 +02:00
|
|
|
|
var project = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
2023-10-27 18:13:52 +02:00
|
|
|
|
OrganizationId = misMatchOrganization ? newOrg.Id : organizationId,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Name = _mockEncryptedString
|
2023-07-13 18:46:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var serviceAccount = await _serviceAccountRepository.CreateAsync(new ServiceAccount
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = organizationId,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Name = _mockEncryptedString
|
2023-07-13 18:46:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (project.Id, serviceAccount.Id);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 21:56:58 +02:00
|
|
|
|
private async Task<(ServiceAccount serviceAccount, ServiceAccountGrantedPoliciesRequestModel request)>
|
|
|
|
|
SetupServiceAccountGrantedPoliciesRequestAsync(
|
|
|
|
|
PermissionType permissionType, OrganizationUser organizationUser, bool createPreviousAccessPolicy)
|
2024-05-01 18:47:11 +02:00
|
|
|
|
{
|
2024-05-06 21:56:58 +02:00
|
|
|
|
var (serviceAccount, currentUser) =
|
|
|
|
|
await SetupServiceAccountPeoplePermissionAsync(permissionType, organizationUser);
|
2024-05-01 18:47:11 +02:00
|
|
|
|
var project = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
Name = _mockEncryptedString,
|
|
|
|
|
OrganizationId = organizationUser.OrganizationId
|
|
|
|
|
});
|
|
|
|
|
var accessPolicies = new List<BaseAccessPolicy>
|
|
|
|
|
{
|
|
|
|
|
new UserProjectAccessPolicy
|
|
|
|
|
{
|
2024-05-06 21:56:58 +02:00
|
|
|
|
GrantedProjectId = project.Id, OrganizationUserId = currentUser.Id, Read = true, Write = true
|
|
|
|
|
}
|
2024-05-01 18:47:11 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (createPreviousAccessPolicy)
|
|
|
|
|
{
|
|
|
|
|
var anotherProject = await _projectRepository.CreateAsync(new Project
|
|
|
|
|
{
|
|
|
|
|
Name = _mockEncryptedString,
|
|
|
|
|
OrganizationId = organizationUser.OrganizationId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
accessPolicies.Add(new UserProjectAccessPolicy
|
|
|
|
|
{
|
|
|
|
|
GrantedProjectId = anotherProject.Id,
|
|
|
|
|
OrganizationUserId = currentUser.Id,
|
|
|
|
|
Read = true,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Write = true
|
2024-05-01 18:47:11 +02:00
|
|
|
|
});
|
|
|
|
|
accessPolicies.Add(new ServiceAccountProjectAccessPolicy
|
|
|
|
|
{
|
|
|
|
|
GrantedProjectId = anotherProject.Id,
|
|
|
|
|
ServiceAccountId = serviceAccount.Id,
|
|
|
|
|
Read = true,
|
2024-05-06 21:56:58 +02:00
|
|
|
|
Write = true
|
2024-05-01 18:47:11 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _accessPolicyRepository.CreateManyAsync(accessPolicies);
|
|
|
|
|
|
|
|
|
|
var request = new ServiceAccountGrantedPoliciesRequestModel
|
|
|
|
|
{
|
|
|
|
|
ProjectGrantedPolicyRequests = new List<GrantedAccessPolicyRequest>
|
|
|
|
|
{
|
|
|
|
|
new() { GrantedId = project.Id, Read = true, Write = true }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return (serviceAccount, request);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-02 18:06:20 +02:00
|
|
|
|
private async Task<(Project project, ProjectServiceAccountsAccessPoliciesRequestModel request)>
|
|
|
|
|
SetupProjectServiceAccountsAccessPoliciesRequestAsync(
|
|
|
|
|
PermissionType permissionType, OrganizationUser organizationUser, bool createPreviousAccessPolicy)
|
|
|
|
|
{
|
|
|
|
|
var (project, currentUser) = await SetupProjectPeoplePermissionAsync(permissionType, organizationUser);
|
|
|
|
|
var serviceAccount = await _serviceAccountRepository.CreateAsync(new ServiceAccount
|
|
|
|
|
{
|
|
|
|
|
Name = _mockEncryptedString,
|
|
|
|
|
OrganizationId = currentUser.OrganizationId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var accessPolicies = new List<BaseAccessPolicy>
|
|
|
|
|
{
|
|
|
|
|
new UserServiceAccountAccessPolicy
|
|
|
|
|
{
|
|
|
|
|
GrantedServiceAccountId = serviceAccount.Id,
|
|
|
|
|
OrganizationUserId = currentUser.Id,
|
|
|
|
|
Read = true,
|
|
|
|
|
Write = true
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var request = new ProjectServiceAccountsAccessPoliciesRequestModel
|
|
|
|
|
{
|
|
|
|
|
ServiceAccountAccessPolicyRequests =
|
|
|
|
|
[
|
2024-05-06 21:56:58 +02:00
|
|
|
|
new AccessPolicyRequest { GranteeId = serviceAccount.Id, Read = true, Write = true }
|
2024-05-02 18:06:20 +02:00
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (createPreviousAccessPolicy)
|
|
|
|
|
{
|
|
|
|
|
var anotherServiceAccount = await _serviceAccountRepository.CreateAsync(new ServiceAccount
|
|
|
|
|
{
|
|
|
|
|
Name = _mockEncryptedString,
|
|
|
|
|
OrganizationId = currentUser.OrganizationId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
accessPolicies.Add(new UserServiceAccountAccessPolicy
|
|
|
|
|
{
|
|
|
|
|
GrantedServiceAccountId = anotherServiceAccount.Id,
|
|
|
|
|
OrganizationUserId = currentUser.Id,
|
|
|
|
|
Read = true,
|
|
|
|
|
Write = true
|
|
|
|
|
});
|
|
|
|
|
accessPolicies.Add(new ServiceAccountProjectAccessPolicy
|
|
|
|
|
{
|
|
|
|
|
GrantedProjectId = project.Id,
|
|
|
|
|
ServiceAccountId = anotherServiceAccount.Id,
|
|
|
|
|
Read = true,
|
|
|
|
|
Write = true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _accessPolicyRepository.CreateManyAsync(accessPolicies);
|
|
|
|
|
|
|
|
|
|
return (project, request);
|
|
|
|
|
}
|
2024-06-07 19:08:38 +02:00
|
|
|
|
|
|
|
|
|
private async Task<(Guid SecretId, OrganizationUser currentOrgUser)> SetupSecretAccessPoliciesTest(
|
|
|
|
|
PermissionType permissionType)
|
|
|
|
|
{
|
|
|
|
|
var (org, orgAdmin) = await _organizationHelper.Initialize(true, true, true);
|
|
|
|
|
var currentOrgUser = orgAdmin;
|
|
|
|
|
if (permissionType == PermissionType.RunAsUserWithPermission)
|
|
|
|
|
{
|
|
|
|
|
var (email, orgUser) = await _organizationHelper.CreateNewUser(OrganizationUserType.User, true);
|
|
|
|
|
await _loginHelper.LoginAsync(email);
|
|
|
|
|
currentOrgUser = orgUser;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await _loginHelper.LoginAsync(_email);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var secret = await _secretRepository.CreateAsync(new Secret
|
|
|
|
|
{
|
|
|
|
|
OrganizationId = org.Id,
|
|
|
|
|
Key = _mockEncryptedString,
|
|
|
|
|
Value = _mockEncryptedString,
|
|
|
|
|
Note = _mockEncryptedString
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (secret.Id, currentOrgUser);
|
|
|
|
|
}
|
2023-01-20 00:31:19 +01:00
|
|
|
|
}
|