Skip to content

Commit

Permalink
[CLOSES #68] Use the YnhServer::name property as a tage when creating…
Browse files Browse the repository at this point in the history
… an asset.
  • Loading branch information
csavelief committed Sep 17, 2024
1 parent 6dd414c commit 3aecd8d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/YnhServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function configure(YnhServer $server, ConfigureHostRequest $request)
'updated' => false,
]));
if ($user) {
event(new CreateAsset($user, $request->domain, true));
event(new CreateAsset($user, $request->domain, true, [$server->name]));
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/ConfigureHostListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function handle2($event)
// $isOk = $isOk && $server->sshRestartDocker($ssh);

$ssh->newTrace(SshTraceStateEnum::IN_PROGRESS, 'Starting asset monitoring...');
event(new CreateAsset($user, $server->ip(), true));
event(new CreateAsset($user, $server->ip(), true, [$server->name]));
$ssh->newTrace(SshTraceStateEnum::DONE, 'Asset monitoring started.');
}
if ($isOk) {
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/InstallAppListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function handle2($event)
}

$ssh->newTrace(SshTraceStateEnum::IN_PROGRESS, 'Starting asset monitoring...');
event(new CreateAsset($user, $domain, true));
event(new CreateAsset($user, $domain, true, [$server->name]));
$ssh->newTrace(SshTraceStateEnum::DONE, 'Asset monitoring started.');
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/YnhServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ public function pullServerInfos(?string $uid = null, ?User $user = null): void
'updated' => true,
]);
if ($user) {
event(new CreateAsset($user, $domain, true));
event(new CreateAsset($user, $domain, true, [$this->name]));
}
}
DB::transaction(function () {
Expand Down
4 changes: 3 additions & 1 deletion app/Modules/AdversaryMeter/Events/CreateAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ class CreateAsset
public User $user;
public string $asset;
public bool $monitor;
public array $tags;

public function __construct(User $user, string $asset, bool $monitor)
public function __construct(User $user, string $asset, bool $monitor, array $tags = [])
{
$this->user = $user;
$this->asset = $asset;
$this->monitor = $monitor;
$this->tags = $tags;
}

public function broadcastOn()
Expand Down
20 changes: 17 additions & 3 deletions app/Modules/AdversaryMeter/Listeners/CreateAssetListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
use App\Modules\AdversaryMeter\Enums\AssetTypesEnum;
use App\Modules\AdversaryMeter\Events\CreateAsset;
use App\Modules\AdversaryMeter\Models\Asset;
use App\Modules\AdversaryMeter\Models\AssetTag;
use App\Modules\AdversaryMeter\Rules\IsValidAsset;
use App\Modules\AdversaryMeter\Rules\IsValidDomain;
use App\Modules\AdversaryMeter\Rules\IsValidIpAddress;
use App\Modules\AdversaryMeter\Rules\IsValidTag;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class CreateAssetListener extends AbstractListener
{
public static function execute(User $user, string $asset, bool $monitor): ?Asset
public static function execute(User $user, string $asset, bool $monitor, array $tags = []): ?Asset
{
if (!IsValidAsset::test($asset)) {
Log::error("Invalid asset : {$asset}");
Expand All @@ -28,7 +31,8 @@ public static function execute(User $user, string $asset, bool $monitor): ?Asset
} else {
$assetType = AssetTypesEnum::RANGE;
}
return Asset::updateOrCreate(
/** @var Asset $azzet */
$azzet = Asset::updateOrCreate(
[
'asset' => $asset,
'created_by' => $user->id,
Expand All @@ -40,6 +44,16 @@ public static function execute(User $user, string $asset, bool $monitor): ?Asset
'created_by' => $user->id,
]
);
collect($tags)->map(fn(string $tag) => Str::lower($tag))
->filter(fn(string $tag) => IsValidTag::test($tag))
->each(function (string $tag) use ($azzet) {
/** @var ?AssetTag $obj */
$obj = $azzet->tags()->where('tag', $tag)->first();
if (!$obj) {
$obj = $azzet->tags()->create(['tag' => $tag]);
}
});
return $azzet;
}

protected function handle2($event)
Expand All @@ -48,6 +62,6 @@ protected function handle2($event)
throw new \Exception('Invalid event type!');
}
Auth::login($event->user); // otherwise the tenant will not be properly set
self::execute($event->user, $event->asset, $event->monitor);
self::execute($event->user, $event->asset, $event->monitor, $event->tags);
}
}

0 comments on commit 3aecd8d

Please sign in to comment.