cfmgrv4/app/Services/Cloudflare/AuditLogService.php
2024-10-30 13:02:59 +09:00

118 lines
5.0 KiB
PHP

<?php
namespace App\Services\Cloudflare;
use App\Entities\Cloudflare\AccountEntity;
use App\Entities\Cloudflare\AuditLogEntity;
use App\Models\Cloudflare\AccountModel;
use App\Models\Cloudflare\AuditLogModel;
use App\Models\Cloudflare\ZoneModel;
use stdClass;
class AuditLogService extends CloudflareService
{
private ?AuditLogModel $_model = null;
private ?AccountModel $_accountModel = null;
private ?ZoneModel $_zoneModel = null;
public function __construct()
{
$this->class_name = "AuditLog";
parent::__construct();
$this->class_path .= $this->class_name;
}
protected function getModel(): AuditLogModel
{
if ($this->_model === null) {
$this->_model = new AuditLogModel();
}
return $this->_model;
}
protected function getZoneModel(): ZoneModel
{
if ($this->_zoneModel === null) {
$this->_zoneModel = new ZoneModel();
}
return $this->_zoneModel;
}
protected function getAccountModel(): AccountModel
{
if ($this->_accountModel === null) {
$this->_accountModel = new AccountModel();
}
return $this->_accountModel;
}
protected function getArrayByResult(\stdClass $result, array $formDatas = []): array
{
$formDatas[AuditLogModel::PK] = $result->id;
$formDatas[AuditLogModel::TITLE] = $result->action->type;
$formDatas['action_info'] = isset($result->action->info) ? $result->action->info : "";
$formDatas['actor'] = $result->actor->type;
$formDatas['interface'] = isset($result->interface) ? $result->interface : "";
$formDatas['zone_name'] = $result->metadata->zone_name;
$formDatas['meta'] = isset($result->metadata) ? var_export($result->metadata, true) : "";
$formDatas['resource'] = isset($result->newValueJson) ? var_export($result->newValueJson, true) : "";
$formDatas['status'] = $result->action->result ? "true" : "false";
$formDatas['updated_at'] = date("Y-m-d H:i:s");
$formDatas['created_at'] = $result->when;
return $formDatas;
}
private function auditlog_process(AuditLogEntity $entity): void
{
//해당 Zone을 Sync작업한다
$this->getZoneModel()->where('domain', $entity->getZoneName());
$zone_entity = $this->getZoneModel()->getEntity();
if ($zone_entity !== null) {
$account_entity = $this->getAccountModel()->getEntityBYPK($zone_entity->getParent());
if ($account_entity === null) {
throw new \Exception("Account: [{$zone_entity->getParent()}] 정보를 찾을수 없습니다.");
}
//해당 Zone만 Sync작업을 한다.
$zone_service = new ZoneService();
$zone_entity = $zone_service->sync($account_entity, $zone_entity);
//해당 Zone의 Record reload작업한다
$record_service = new RecordService();
$record_service->reload($zone_entity);
//해당 Zone의 Firewall reload작업한다
$firewall_service = new FirewallService();
$firewall_service->reload($zone_entity);
log_message("debug", "AuditLog Process의 {$zone_entity->getTitle()} Sync및 Record,Firewall Reload 처리작업");
}
}
public function reload(AccountEntity $account_entity): void
{
//Socket인증정보 정의
$auth_entity = $this->getAuthModel()->getEntityByPK($account_entity->getParent());
if ($auth_entity === null) {
throw new \Exception("해당 계정정보를 찾을수 없습니다.");
}
$this->setAuthEntity($auth_entity);
log_message("notice", "\n----------Account {$account_entity->getTitle()}의 AuditLog 처리 시작-----------");
try {
// 오늘 날짜의 ISO 8601 형식 문자열 생성
$today = date('Y-m-d') . 'T00:00:00';
$response = $this->getMySocket()->get("accounts/{$account_entity->getPK()}/audit_logs", ['since' => $today]);
$body = json_decode($response->getBody());
foreach ($body->result as $result) {
if (isset($result->action->result) && $result->action->result && isset($result->metadata->zone_name)) {
//이미 등록되어있는지 확인
$entity = $this->getModel()->getEntityByPK($result->id);
if ($entity === null) {
$formDatas = $this->getArrayByResult($result);
$entity = $this->getModel()->create($formDatas);
$this->auditlog_process($entity);
}
} else {
log_message("debug", var_export($result, true));
}
}
} catch (\Exception $e) {
log_message("error", $e->getMessage());
throw new \Exception($e->getMessage());
}
log_message("notice", message: "\n-----------Account {$account_entity->getTitle()}의 AuditLog 처리 완료-----------");
}
}