-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoss_tools.py
More file actions
183 lines (163 loc) · 6.6 KB
/
Copy pathoss_tools.py
File metadata and controls
183 lines (163 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
OSS Tools Integration API Endpoints
"""
from fastapi import APIRouter, HTTPException, BackgroundTasks
from pydantic import BaseModel
from typing import Dict, Any, Optional
import structlog
from src.services.oss_integrations import OSSIntegrationService
logger = structlog.get_logger()
router = APIRouter(prefix="/oss", tags=["oss-tools"])
# Initialize OSS integration service
oss_service = OSSIntegrationService()
class ScanRequest(BaseModel):
target: str
scan_type: str = "image" # image, filesystem, repository
class PolicyEvalRequest(BaseModel):
policy_name: str
input_data: Dict[str, Any]
@router.get("/status")
async def get_oss_status():
"""Get status of all OSS tools"""
try:
status = oss_service.get_status()
return {
"status": "success",
"tools": status,
"summary": {
"total_tools": len(status),
"available_tools": len([t for t in status.values() if t["available"]]),
"missing_tools": [name for name, info in status.items() if not info["available"]]
}
}
except Exception as e:
logger.error(f"Failed to get OSS status: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/scan/comprehensive")
async def run_comprehensive_scan(request: ScanRequest, background_tasks: BackgroundTasks):
"""Run comprehensive security scan using multiple OSS tools"""
try:
# Run scan in background for long-running operations
results = await oss_service.comprehensive_scan(
target=request.target,
image_type=(request.scan_type == "image")
)
return {
"status": "success",
"scan_id": f"scan_{request.target.replace(':', '_').replace('/', '_')}",
"results": results
}
except Exception as e:
logger.error(f"Comprehensive scan failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/scan/trivy")
async def run_trivy_scan(request: ScanRequest):
"""Run Trivy vulnerability scan"""
try:
if oss_service.trivy.version == "not-installed":
raise HTTPException(status_code=404, detail="Trivy not installed")
results = await oss_service.trivy.scan_image(request.target)
return results
except HTTPException:
raise
except Exception as e:
logger.error(f"Trivy scan failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/scan/grype")
async def run_grype_scan(request: ScanRequest):
"""Run Grype vulnerability scan"""
try:
if oss_service.grype.version == "not-installed":
raise HTTPException(status_code=404, detail="Grype not installed")
results = await oss_service.grype.scan_target(request.target)
return results
except HTTPException:
raise
except Exception as e:
logger.error(f"Grype scan failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/verify/sigstore")
async def verify_sigstore_signature(request: ScanRequest, public_key: Optional[str] = None):
"""Verify container signatures using Sigstore"""
try:
if oss_service.sigstore.version == "not-installed":
raise HTTPException(status_code=404, detail="Cosign/Sigstore not installed")
results = await oss_service.sigstore.verify_signature(request.target, public_key)
return results
except HTTPException:
raise
except Exception as e:
logger.error(f"Sigstore verification failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/policy/evaluate")
async def evaluate_policy(request: PolicyEvalRequest):
"""Evaluate security policy using OPA"""
try:
if oss_service.opa.version == "not-installed":
raise HTTPException(status_code=404, detail="OPA not installed")
results = await oss_service.opa.evaluate_policy(
policy_name=request.policy_name,
input_data=request.input_data
)
return results
except HTTPException:
raise
except Exception as e:
logger.error(f"Policy evaluation failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/policies")
async def list_policies():
"""List available OPA policies"""
try:
policies_dir = oss_service.opa.policies_dir
policies = []
for policy_file in policies_dir.glob("*.rego"):
policies.append({
"name": policy_file.stem,
"file": policy_file.name,
"path": str(policy_file)
})
return {
"status": "success",
"policies": policies,
"count": len(policies)
}
except Exception as e:
logger.error(f"Failed to list policies: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/tools")
async def list_supported_tools():
"""List all supported OSS tools and their capabilities"""
return {
"status": "success",
"tools": {
"trivy": {
"name": "Trivy",
"type": "vulnerability_scanner",
"description": "Container and filesystem vulnerability scanner",
"capabilities": ["image_scan", "filesystem_scan", "sarif_output"],
"installation": "https://aquasecurity.github.io/trivy/latest/getting-started/installation/"
},
"grype": {
"name": "Grype",
"type": "vulnerability_scanner",
"description": "Container and filesystem vulnerability scanner",
"capabilities": ["image_scan", "filesystem_scan", "sbom_scan"],
"installation": "https://github.com/anchore/grype#installation"
},
"opa": {
"name": "Open Policy Agent",
"type": "policy_engine",
"description": "General-purpose policy engine",
"capabilities": ["policy_evaluation", "rego_policies", "decision_engine"],
"installation": "https://www.openpolicyagent.org/docs/latest/"
},
"cosign": {
"name": "Cosign/Sigstore",
"type": "supply_chain_security",
"description": "Container signature verification",
"capabilities": ["signature_verification", "attestation", "keyless_signing"],
"installation": "https://docs.sigstore.dev/cosign/installation/"
}
}
}