mirror of
https://github.com/coollabsio/coolify.git
synced 2026-08-24 10:05:47 -05:00
Add an admin-only audit-events API endpoint and restrict audit-log UI access. Record integration token and secret manager changes, key access, and references in audit events.
128 lines
4.0 KiB
PHP
128 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Security;
|
|
|
|
use App\Models\IntegrationToken;
|
|
use App\Services\IntegrationTokenValidator;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Livewire\Component;
|
|
|
|
class IntegrationTokenForm extends Component
|
|
{
|
|
use AuthorizesRequests;
|
|
|
|
public bool $modal_mode = false;
|
|
|
|
public string $provider = 'cloudflare';
|
|
|
|
public string $name = '';
|
|
|
|
public string $token = '';
|
|
|
|
public array $capabilities = ['dns'];
|
|
|
|
public array $metadata = [];
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->authorize('create', IntegrationToken::class);
|
|
}
|
|
|
|
public function updatedProvider(): void
|
|
{
|
|
if ($this->provider === 'cloudflare') {
|
|
$this->capabilities = ['dns'];
|
|
$this->metadata = [];
|
|
} else {
|
|
$this->capabilities = ['secrets'];
|
|
$this->metadata = $this->provider === 'infisical'
|
|
? ['base_url' => 'https://app.infisical.com']
|
|
: [];
|
|
}
|
|
}
|
|
|
|
protected function rules(): array
|
|
{
|
|
$allowedCapability = $this->provider === 'cloudflare' ? 'dns' : 'secrets';
|
|
|
|
$rules = [
|
|
'provider' => ['required', 'in:'.implode(',', array_keys(IntegrationToken::PROVIDER_NAMES))],
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'token' => ['required', 'string'],
|
|
'capabilities' => ['required', 'array', 'min:1'],
|
|
'capabilities.*' => ['required', 'in:'.$allowedCapability],
|
|
];
|
|
|
|
if ($this->provider === 'infisical') {
|
|
$rules['metadata.base_url'] = ['required', 'url:http,https'];
|
|
$rules['metadata.client_id'] = ['required', 'string'];
|
|
}
|
|
|
|
if ($this->provider === 'doppler') {
|
|
$rules['token'][] = 'regex:/^dp\.(st|sa)\./';
|
|
}
|
|
|
|
if ($this->provider === 'vault') {
|
|
$rules['metadata.base_url'] = ['required', 'url:http,https'];
|
|
$rules['metadata.namespace'] = ['nullable', 'string'];
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
protected function messages(): array
|
|
{
|
|
return [
|
|
'capabilities.required' => 'Select at least one capability.',
|
|
'capabilities.min' => 'Select at least one capability.',
|
|
'token.regex' => 'Use a Doppler service token (dp.st.*) or service account token (dp.sa.*).',
|
|
];
|
|
}
|
|
|
|
public function addToken(IntegrationTokenValidator $validator): void
|
|
{
|
|
$validated = $this->validate();
|
|
$metadata = array_filter(data_get($validated, 'metadata', []), fn ($value) => filled($value));
|
|
|
|
try {
|
|
if (! $validator->validate($validated['provider'], $validated['token'], $validated['capabilities'], $metadata)) {
|
|
$this->dispatch('error', $validator->errorMessage($validated['provider']));
|
|
|
|
return;
|
|
}
|
|
|
|
$integrationToken = IntegrationToken::query()->create([
|
|
'provider' => $validated['provider'],
|
|
'name' => $validated['name'],
|
|
'token' => $validated['token'],
|
|
'capabilities' => $validated['capabilities'],
|
|
'metadata' => $metadata ?: null,
|
|
'team_id' => currentTeam()->id,
|
|
]);
|
|
|
|
auditLog('ui.integration_token.created', [
|
|
'team_id' => currentTeam()->id,
|
|
'integration_token_uuid' => $integrationToken->uuid,
|
|
'integration_token_name' => $integrationToken->name,
|
|
'provider' => $integrationToken->provider,
|
|
]);
|
|
|
|
$this->reset(['name', 'token']);
|
|
$this->dispatch('integrationTokenAdded')->to(IntegrationTokens::class);
|
|
|
|
if ($this->modal_mode) {
|
|
$this->dispatch('close-modal');
|
|
}
|
|
|
|
$this->dispatch('success', 'Integration token added successfully.');
|
|
} catch (\Throwable $e) {
|
|
handleError($e, $this);
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.security.integration-token-form');
|
|
}
|
|
}
|