117 lines
4.6 KiB
PHP
117 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloudflare;
|
|
|
|
use App\Entities\Cloudflare\AccountEntity;
|
|
use App\Models\Cloudflare\AccountModel;
|
|
use App\Models\Cloudflare\AuditLogModel;
|
|
use App\Models\Cloudflare\ZoneModel;
|
|
|
|
class AuditLogService extends CloudflareService
|
|
{
|
|
private ?AuditLogModel $_model = null;
|
|
private ?AccountModel $_accountModel = null;
|
|
private ?ZoneModel $_zoneModel = null;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct("AuditLog");
|
|
}
|
|
|
|
public 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'] = $result->action->info ?? "";
|
|
$formDatas['actor'] = $result->actor->type;
|
|
$formDatas['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(AccountEntity $account_entity, \stdClass $result): void
|
|
{
|
|
//Auditlog 신규등록
|
|
$formDatas = $this->getArrayByResult($result);
|
|
$entity = $this->getModel()->create($formDatas);
|
|
|
|
//auditlog의 domain에 해당하는 Zone이 존재하는지 확인
|
|
$this->getZoneModel()->where('domain', $entity->getZoneName());
|
|
$zone_entity = $this->getZoneModel()->getEntity();
|
|
if ($zone_entity !== null) {
|
|
//해당 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)) {
|
|
//Auditlog에 이미 등록되어있는지 확인
|
|
$entity = $this->getModel()->getEntityByPK($result->id);
|
|
if ($entity === null) {
|
|
$this->auditlog_process($account_entity, $result);
|
|
}
|
|
} else {
|
|
log_message("debug", var_export($result, true));
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
log_message("error", $e->getMessage());
|
|
throw new \Exception($e->getMessage());
|
|
}
|
|
log_message("notice", "\n-----------Account {$account_entity->getTitle()}의 AuditLog 처리 완료-----------");
|
|
}
|
|
}
|