Skip to content

Commit e4be154

Browse files
authored
Merge pull request #1808 from oracle-devrel/health-check-forensics-tool
First release of oci health check forensics tool
2 parents 7245442 + a44cb1e commit e4be154

36 files changed

+4820
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Copyright (c) 2025 Oracle and/or its affiliates.
2+
3+
The Universal Permissive License (UPL), Version 1.0
4+
5+
Subject to the condition set forth below, permission is hereby granted to any
6+
person obtaining a copy of this software, associated documentation and/or data
7+
(collectively the "Software"), free of charge and under any and all copyright
8+
rights in the Software, and any and all patent rights owned or freely
9+
licensable by each licensor hereunder covering either (i) the unmodified
10+
Software as contributed to or provided by such licensor, or (ii) the Larger
11+
Works (as defined below), to deal in both
12+
13+
(a) the Software, and
14+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
15+
one is included with the Software (each a "Larger Work" to which the Software
16+
is contributed by such licensors),
17+
18+
without restriction, including without limitation the rights to copy, create
19+
derivative works of, display, perform, and distribute the Software and make,
20+
use, sell, offer for sale, import, export, have made, and have sold the
21+
Software and the Larger Work(s), and to sublicense the foregoing rights on
22+
either these or other terms.
23+
24+
This license is subject to the following condition:
25+
The above copyright notice and either this complete permission notice or at
26+
a minimum a reference to the UPL must be included in all copies or
27+
substantial portions of the Software.
28+
29+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35+
SOFTWARE.
Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
# SHOW_OCI CSV Query Tool
2+
3+
The SHOW_OCI Query Tool is designed to load and analyze data from Oracle Cloud Infrastructure (OCI) environments using SQL. This tool enables users to import CSV files containing OCI resource information (e.g., compute instances, users, compartments) and perform SQL queries on the data.
4+
5+
## Features
6+
- Automatic OCI data fetching using showoci integration
7+
- **Audit events** and **Cloud Guard problems** fetching with parallel processing
8+
- Advanced filtering capabilities for age-based and compartment analysis
9+
- - Load CSV files with OCI data from multiple tenancies
10+
- Execute SQL queries on the loaded data using DuckDB backend. Stay tuned for autonomous DB support.
11+
- Support for `SHOW TABLES` and `DESCRIBE table_name` commands
12+
- Interactive tenancy selection from combined OCI configuration files
13+
- Command history and help system
14+
- Batch query execution from YAML files
15+
16+
The tool will be used for forensic purposes. Data can be collected by the customer and shipped to Oracle for forensic research.
17+
18+
The tool is in development and the following is on the backlog:
19+
- Switch back-end DB for large data sets. ADB support.
20+
- Customer documentation to extract data and ship to Oracle in a secure way
21+
22+
## Know Errors
23+
- Error shown when a query results in an empty data frame when a filter is applied.
24+
25+
## Installation
26+
27+
Clone the repository:
28+
```bash
29+
git clone <repository-url>
30+
cd healthcheck-forensic
31+
```
32+
33+
Set up a Python virtual environment and install dependencies:
34+
```bash
35+
python3.10 -m venv .venv
36+
source .venv/bin/activate
37+
pip install -r requirements.txt
38+
```
39+
40+
The `requirements.txt` file contains dependencies for DuckDB, pandas, OCI SDK, and other required libraries.
41+
42+
### OCI Configuration Files
43+
44+
The tool now supports split OCI configuration:
45+
46+
- **`~/.oci/config`**: Contains only the DEFAULT domain configuration
47+
- **`qt_config`**: Contains additional tenancy configurations
48+
49+
The tool automatically combines these files when selecting tenancies. This separation allows you to keep your main OCI config clean while managing multiple tenancies in a separate file.
50+
51+
## Usage
52+
53+
### Running the Tool
54+
55+
To start the tool, use:
56+
```bash
57+
python healthcheck_forensic_tool.py
58+
```
59+
### Interactive Mode
60+
61+
The tool supports an interactive mode for running SQL queries dynamically. Available commands include:
62+
63+
#### Basic Commands
64+
- `show tables`: Lists all loaded tables
65+
- `describe <table_name>`: Displays columns and data types for a given table
66+
- `history`: Shows command history
67+
- `help [command]`: Shows help for commands
68+
- `exit` or `quit`: Exits the application
69+
70+
#### Data Management
71+
- `set tenancy`: Switch between different OCI tenancies
72+
- `set queries [directory]`: Load queries from YAML files for batch execution
73+
- `run queries`: Execute all loaded queries in sequence
74+
75+
#### Data Fetching
76+
- `audit_events fetch <DD-MM-YYYY> <days>`: Fetch <days> of audit events prior to specified date.
77+
- `audit_events fetch`: Interactive loader for existing audit data
78+
- `audit_events delete`: Delete audit events files and tables
79+
- `cloudguard fetch <DD-MM-YYYY> <days>`: Fetch <days> of cloud guard problems prior to specified date.
80+
- `cloudguard fetch`: Interactive loader for existing Cloud Guard data
81+
- `cloudguard delete`: Delete Cloud Guard files and tables
82+
83+
#### Filtering and Analysis
84+
- `filter age <column> <older|younger> <days>`: Filter results by date age
85+
- `filter compartment <subcommand>`: Analyze compartment structures
86+
- `root`: Show root compartment
87+
- `depth`: Show maximum depth
88+
- `tree_view`: Display compartment tree
89+
- `path_to <compartment>`: Show path to specific compartment
90+
91+
### Command-line Switches
92+
93+
| Switch | Description |
94+
|------------------|---------------------------------------------------|
95+
| `--config-file` | Path to the configuration file (`config.yaml`). |
96+
| `--interactive` | Enable interactive SQL mode. <Default> |
97+
98+
Example usage:
99+
```bash
100+
python healthcheck_forensic_tool.py
101+
```
102+
103+
## Configuration Options (`config.yaml`)
104+
105+
| Setting | Description |
106+
|----------------------------|-------------|
107+
| `oci_config_file` | Path to the main OCI config file (default: `~/.oci/config`) |
108+
| `tqt_config_file` | Path to the additional tenancies config file (default: `config/qt_config`) |
109+
| `csv_dir` | Directory for CSV files |
110+
| `prefix` | Filename prefix for filtering CSVs |
111+
| `resource_argument` | Resource argument for showoci (a: all, i: identity, n: network, c: compute, etc.) |
112+
| `delimiter` | Delimiter used in CSV files |
113+
| `case_insensitive_headers` | Convert column headers to lowercase |
114+
| `log_level` | Logging level (`INFO`, `DEBUG`, `ERROR`) |
115+
| `interactive` | Enable interactive mode |
116+
| `audit_worker_count` | Number of parallel workers for audit/Cloud Guard fetching (default: 10) |
117+
| `audit_worker_window` | Hours per batch for parallel fetching (default: 1) |
118+
119+
### Example `config.yaml`
120+
```yaml
121+
# OCI Configuration
122+
oci_config_file: "~/.oci/config" # Main OCI config (DEFAULT domain)
123+
tqt_config_file: "qt_config" # Additional tenancies
124+
125+
# Data Management
126+
csv_dir: "data"
127+
prefix: "oci"
128+
resource_argument: "a"
129+
130+
# Output Settings
131+
output_format: "DataFrame"
132+
log_level: "INFO"
133+
delimiter: ","
134+
case_insensitive_headers: true
135+
136+
# Interactive Mode
137+
interactive: true
138+
139+
# Parallel Fetching Configuration
140+
audit_worker_count: 10
141+
audit_worker_window: 1
142+
```
143+
144+
## Predefined Queries
145+
146+
Queries can be defined in YAML files for batch execution. Example `queries.yaml`:
147+
```yaml
148+
queries:
149+
- description: "List all users with API access"
150+
sql: "SELECT display_name, can_use_api_keys FROM identity_domains_users WHERE can_use_api_keys = 1"
151+
- description: "Show compute instances by compartment"
152+
sql: "SELECT server_name, compartment_name, status FROM compute WHERE status = 'STOPPED'"
153+
filter: "age last_modified older 30"
154+
sql: "sql: "SELECT server_name, compartment_name, status FROM compute WHERE compartment_name = '<YOUR COMPARTMENT_NAME>'"
155+
```
156+
157+
## Example Usage Scenarios
158+
159+
### Getting Started
160+
```bash
161+
# Start the tool
162+
python healthcheck_forensic_tool.py
163+
164+
# Select tenancy and load data
165+
# Tool will prompt for tenancy selection from combined configs
166+
167+
# Basic exploration
168+
CMD> show tables
169+
CMD> describe identity_domains_users
170+
CMD> SELECT COUNT(*) FROM compute;
171+
```
172+
173+
### Data Fetching
174+
```bash
175+
# Fetch 2 days of audit events ending June 15, 2025
176+
CMD> audit_events fetch 15-06-2025 2
177+
178+
# Fetch 30 days of Cloud Guard problems ending January 1, 2025
179+
CMD> cloudguard fetch 01-01-2025 30
180+
181+
# Load existing audit data interactively
182+
CMD> audit_events fetch
183+
```
184+
185+
### Advanced Analysis
186+
```bash
187+
# Filter API keys older than 90 days
188+
CMD> SELECT display_name, api_keys FROM identity_domains_users;
189+
CMD> filter age api_keys older 90
190+
191+
# Analyze compartment structure
192+
CMD> SELECT path FROM identity_compartments;
193+
CMD> filter compartment tree_view
194+
CMD> filter compartment path_to my-compartment
195+
```
196+
197+
### Batch Operations
198+
```bash
199+
# Load and run predefined queries
200+
CMD> set queries < Select a query file using the query file browser >
201+
CMD> run queries
202+
203+
# Switch between tenancies
204+
CMD> set tenancy
205+
```
206+
207+
## Data Organization
208+
209+
The tool organizes data in the following structure:
210+
```
211+
data/
212+
├── tenancy1/
213+
│ ├── tenancy1_20241215_143022/
214+
│ │ ├── oci_compute.csv
215+
│ │ ├── oci_identity_domains_users.csv
216+
│ │ ├── audit_events_15-06-2025_7.json
217+
│ │ └── cloudguard_problems_15062025_7.json
218+
│ └── tenancy1_20241214_091545/
219+
└── tenancy2/
220+
└── tenancy2_20241215_100530/
221+
```
222+
223+
## Logging
224+
225+
Logging is configured via the `log_level` setting in `config.yaml`. The tool provides detailed logging for:
226+
- Configuration loading and validation
227+
- CSV file loading and table creation
228+
- Query execution and results
229+
- Data fetching operations with progress tracking
230+
- Error handling and troubleshooting information
231+
232+
## Troubleshooting
233+
234+
### Common Issues
235+
236+
**OCI Configuration Problems**
237+
- Ensure both `~/.oci/config` and `config/qt_config` exist and are readable
238+
- Verify that tenancy profiles are properly configured with required keys
239+
- Check that API keys and permissions are correctly set up
240+
241+
**CSV Loading Issues**
242+
- Ensure CSV files are properly formatted with consistent delimiters
243+
- Column names in queries should match those in the loaded data (case-sensitive by default)
244+
- Check that the specified prefix matches your CSV file naming convention
245+
246+
**Data Fetching Problems**
247+
- Verify OCI permissions for audit events and Cloud Guard APIs
248+
- Check network connectivity and OCI service availability
249+
- Ensure the date range doesn't exceed OCI's retention periods (365 days for audit events)
250+
251+
**Query Execution**
252+
- Use DuckDB-compatible SQL syntax
253+
- Table names are derived from CSV filenames (minus prefix and extension)
254+
- Check available tables with `show tables` and column structure with `describe <table>`
255+
256+
### Getting Help
257+
258+
For detailed command help:
259+
```bash
260+
CMD> help # Show all commands
261+
CMD> help audit_events fetch # Show audit_events fetch options
262+
CMD> help filter age # Show filter age options
263+
```
264+
265+
## Advanced Features
266+
267+
### Parallel Data Fetching
268+
The tool supports parallel fetching for large datasets:
269+
- Configurable worker count and time windows
270+
- Progress tracking with detailed summaries
271+
- Automatic retry handling for failed intervals
272+
- Clean temporary file management
273+
274+
### Smart Configuration Management
275+
- Automatic detection and combination of split OCI configs
276+
- Interactive tenancy selection with metadata display
277+
- Temporary file creation for showoci integration
278+
- Graceful handling of missing or invalid configurations
279+
280+
### Comprehensive Filtering
281+
- Date-based filtering with flexible column support
282+
- Compartment hierarchy analysis and visualization
283+
- Support for complex nested data structures
284+
- Chainable filter operations on query results
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .output_formatter import OutputFormatter

0 commit comments

Comments
 (0)