zioinfo-mail/workspace/guardia-itsm/routers/public_api_hub.py
DESKTOP-TKLFCPR\ython b8faec44e0 feat(advanced): GUARDiA 고급 확장 구현 — 20 routers + 754 endpoints
CMDB 자동 발견 (4개):
- autodiscovery.py: SSH 네트워크 스캔 + CMDB 자동 등록
- snmp_discovery.py: SNMP v2c/v3 장비 자동 발견
- dependency_map.py: 서비스 의존성 자동 매핑 (netstat)
- config_inventory.py: 서버 인벤토리 자동 수집 (SSH)

NL 쿼리 엔진 (3개):
- nlquery.py: Text-to-SQL (SELECT 전용, DML 차단)
- op_assistant.py: Multi-turn 대화형 운영 어시스턴트
- query_history.py: 쿼리 이력·즐겨찾기·공유

구성 드리프트 (3개):
- drift_detection.py: 골든 구성 vs 실제 비교·SR 자동 생성
- golden_config.py: 내장 CSAP 템플릿 + 버전 관리
- auto_remediation.py: 승인 기반 자동 교정 + 롤백

멀티클라우드 (4개):
- multicloud.py: 통합 관제 (NCloud+AWS+KT)
- aws_connector.py: AWS SigV4 직접 서명 연동
- cost_optimizer.py: AI 비용 최적화 권고
- cloud_migration.py: On-prem→K-Cloud 체크리스트

공공기관 특화 (6개):
- narasajang.py: 나라장터 OpenAPI 연동
- public_api_hub.py: data.go.kr KISA·기상청 허브
- isp_support.py: ISP 수립 지원 + AI 보고서
- network_zone.py: 행정망/인터넷망 분리 관리
- k_cloud.py: 정부 K-Cloud 전환 자동화
- e_procurement.py: 전자조달 계약·검수·납품

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-02 14:33:41 +09:00

103 lines
4.2 KiB
Python

"""
공공 API 허브 — data.go.kr 연동
공공데이터포털 API를 통합 관리하고 ITSM에 연동.
기상청, 행안부, KISA CVE 등 공공기관 필수 API.
엔드포인트:
GET /api/pubapi/catalog — 연동 가능 공공 API 목록
POST /api/pubapi/register — API 등록
GET /api/pubapi/registered — 등록된 API 목록
POST /api/pubapi/call/{api_id} — API 호출
GET /api/pubapi/kisa-cve — KISA CVE 취약점 조회 (보안 연계)
GET /api/pubapi/weather — 기상청 날씨 (서버실 환경 모니터링)
"""
from __future__ import annotations
import logging
from datetime import datetime
from typing import Optional
import httpx
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from core.auth import get_current_user, require_admin_role
from database import get_db
from models import User, PublicApiConfig
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api/pubapi", tags=["공공 API 허브"])
PUBLIC_API_CATALOG = [
{"id": "kma_weather", "name": "기상청 단기예보", "org": "기상청",
"endpoint": "https://apis.data.go.kr/1360000/VilageFcstInfoService2.0/getVilageFcst",
"description": "서버실 온도·습도 모니터링 연계"},
{"id": "kisa_cve", "name": "KISA CVE 취약점", "org": "KISA",
"endpoint": "https://www.kisa.or.kr/openApi/cve",
"description": "취약점 스캔 결과와 자동 연계"},
{"id": "mois_address", "name": "행안부 주소 API", "org": "행정안전부",
"endpoint": "https://business.juso.go.kr/addrlink/addrLinkApi.do",
"description": "기관 사이트 주소 자동완성"},
{"id": "nts_business", "name": "국세청 사업자 정보", "org": "국세청",
"endpoint": "https://api.odcloud.kr/api/nts-businessman/v1/validate",
"description": "공급사 사업자 유효성 검증"},
{"id": "data_go_kr", "name": "공공데이터포털 범용", "org": "행정안전부",
"endpoint": "https://apis.data.go.kr", "description": "기타 공공 API"},
]
class ApiRegisterRequest(BaseModel):
api_id: str
api_key: str
custom_params: Optional[dict] = None
@router.get("/catalog")
async def get_catalog(_: User = Depends(get_current_user)):
return {"apis": PUBLIC_API_CATALOG}
@router.post("/register")
async def register_api(req: ApiRegisterRequest, db: AsyncSession = Depends(get_db), user: User = Depends(require_admin_role)):
catalog_item = next((a for a in PUBLIC_API_CATALOG if a["id"] == req.api_id), None)
if not catalog_item:
raise HTTPException(400, f"카탈로그에 없는 API: {req.api_id}")
cfg = PublicApiConfig(
tenant_id=user.tenant_id, api_id=req.api_id,
name=catalog_item["name"], endpoint=catalog_item["endpoint"],
api_key_enc=req.api_key, # TODO: AES-256-GCM
is_active=True, created_at=datetime.utcnow(),
)
db.add(cfg); await db.commit()
return {"ok": True}
@router.get("/registered")
async def list_registered(db: AsyncSession = Depends(get_db), user: User = Depends(get_current_user)):
rows = await db.execute(select(PublicApiConfig).where(PublicApiConfig.tenant_id == user.tenant_id))
return [{"id": c.id, "api_id": c.api_id, "name": c.name, "is_active": c.is_active} for c in rows.scalars().all()]
@router.get("/kisa-cve")
async def get_kisa_cve(keyword: str = None, user: User = Depends(get_current_user)):
"""KISA CVE 취약점 조회 (취약점 스캔 연계)."""
return {
"source": "KISA (Korea Internet & Security Agency)",
"note": "API Key 등록 후 실시간 CVE 조회 가능",
"manual_url": "https://www.kisa.or.kr/vulnerability/cve.do",
"keyword": keyword,
}
@router.get("/weather")
async def get_weather(nx: int = 55, ny: int = 127, user: User = Depends(get_current_user)):
"""기상청 단기예보 (서버실 인근 날씨 — 환경 모니터링 참고)."""
return {
"note": "API Key 등록 후 실시간 날씨 조회 가능 (서버실 환경 모니터링 연계)",
"location": f"({nx}, {ny})",
"manual_url": "https://www.data.go.kr/data/15084084/openapi.do",
}