kingfisher-bin 2.8.0

MongoDB's blazingly fast and accurate secret scanning and validation tool
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Direct blast-radius mapping of a known secret without pattern matching.

use std::collections::BTreeMap;
use std::io::Write;
use std::path::Path;
use std::sync::Arc;

use anyhow::{Result, anyhow, bail};
use serde::Serialize;
use tracing::debug;

use crate::{
    access_map::{AccessMapAttempt, AccessMapResult, map_request_attempts},
    cli::{commands::blast_radius::BlastRadiusArgs, global::GlobalArgs},
    direct_validate::{
        build_globals, create_minimal_scan_args, extract_validation_vars, find_rules_by_selector,
        get_global_var, read_secret,
    },
    provider_endpoints::ProviderEndpointOverrides,
    reporter::{
        FindingRecordData, FindingReporterRecord, ReportEnvelope, RuleMetadata, ScanReportMetadata,
        ScanReportSummary, ValidationInfo, access_map_entry_from_result,
    },
    rule_loader::RuleLoader,
    rules::Validation,
    scanner::direct_access_map_requests,
    util::get_writer_for_file_or_stdout,
};

fn record_mapping_attempt(
    rule_id: &str,
    rule_name: &str,
    attempt: AccessMapAttempt,
    results: &mut Vec<DirectAccessMapResult>,
    failures: &mut Vec<String>,
) {
    if attempt.succeeded {
        let mut result = attempt.result;
        // Direct mappings are not tied to a scan finding. The request uses a synthetic
        // fingerprint internally for collector deduplication, but exposing it would make every
        // direct result appear to have the same real fingerprint. The viewer assigns its own
        // per-result correlation fingerprint when needed.
        result.fingerprint = None;
        results.push(DirectAccessMapResult {
            rule_id: rule_id.to_string(),
            rule_name: rule_name.to_string(),
            result,
        });
        return;
    }
    let detail =
        attempt.result.risk_notes.first().map(String::as_str).unwrap_or("identity mapping failed");
    failures.push(format!("{rule_id}: {detail}"));
}

#[derive(Debug, Clone, Serialize)]
pub struct DirectAccessMapResult {
    pub rule_id: String,
    pub rule_name: String,
    pub result: AccessMapResult,
}

pub async fn run_direct_access_map(
    args: &BlastRadiusArgs,
    global_args: &GlobalArgs,
) -> Result<Vec<DirectAccessMapResult>> {
    let rule_selector = args
        .rule
        .as_deref()
        .ok_or_else(|| anyhow!("--rule is required for direct blast-radius mapping"))?;
    if args.credential_path.is_some() {
        bail!("a credential path cannot be used with --rule");
    }

    let secret = read_secret(args.input.as_deref())?;
    if secret.is_empty() {
        bail!("Secret cannot be empty");
    }

    let loader = RuleLoader::new()
        .load_builtins(!args.no_builtins)
        .additional_rule_load_paths(&args.rules_path);
    let loaded = loader.load(&create_minimal_scan_args())?;
    let matching_rules = find_rules_by_selector(rule_selector, loaded.id_to_rule())?;
    let endpoint_overrides = ProviderEndpointOverrides::from_global_args(global_args)?;
    let mut results = Vec::new();
    let mut failures = Vec::new();

    for rule in matching_rules {
        let Some(validation) = rule.syntax().validation.as_ref() else {
            continue;
        };

        let mut template_vars = extract_validation_vars(validation);
        if matches!(validation, Validation::AWS)
            && crate::validation::is_aws_session_token_rule(rule)
        {
            template_vars.insert("AWS_SECRET_ACCESS_KEY".to_string());
        }

        let globals = build_globals(
            rule.id(),
            &secret,
            &args.args,
            &args.variables,
            &template_vars,
            &endpoint_overrides,
        )?;
        let variables = template_vars
            .into_iter()
            .filter(|name| name != "TOKEN")
            .filter_map(|name| get_global_var(&globals, &name).map(|value| (name, value)))
            .collect::<BTreeMap<_, _>>();

        let requests = direct_access_map_requests(Arc::new(rule.clone()), &secret, variables);
        if requests.is_empty() {
            continue;
        }

        for attempt in map_request_attempts(requests).await {
            record_mapping_attempt(rule.id(), rule.name(), attempt, &mut results, &mut failures);
        }
    }

    if results.is_empty() {
        if !failures.is_empty() {
            bail!(
                "Blast-radius mapping failed for all rules matching '{}':\n- {}",
                rule_selector,
                failures.join("\n- ")
            );
        }
        bail!(
            "No supported blast-radius mapping found for rule '{}'. Check the rule and provide any required --arg or --var values.",
            rule_selector
        );
    }
    if !failures.is_empty() {
        debug!(
            selector = rule_selector,
            failures = ?failures,
            "Some matching rules failed blast-radius mapping"
        );
    }

    Ok(results)
}

pub fn print_results(
    results: &[DirectAccessMapResult],
    format: &str,
    output: Option<&Path>,
) -> Result<()> {
    let mut writer = get_writer_for_file_or_stdout(output)?;
    match format {
        "json" => writeln!(writer, "{}", serde_json::to_string_pretty(results)?)?,
        "toon" => writeln!(
            writer,
            "{}",
            crate::toon::encode_llm_friendly(&serde_json::to_value(results)?)?
        )?,
        "html" => {
            let access_map_results =
                results.iter().map(|result| result.result.clone()).collect::<Vec<_>>();
            writeln!(
                writer,
                "{}",
                crate::access_map::report::render_html_report_multi(&access_map_results)?
            )?;
        }
        _ => {
            for (index, result) in results.iter().enumerate() {
                if index > 0 {
                    writeln!(writer)?;
                }
                writeln!(writer, "Rule: {} ({})", result.rule_name, result.rule_id)?;
                writeln!(writer, "{}", serde_json::to_string_pretty(&result.result)?)?;
            }
        }
    }
    Ok(())
}

/// Build an interactive viewer envelope for direct blast-radius results.
pub fn build_viewer_report_bytes(results: &[DirectAccessMapResult]) -> Result<Vec<u8>> {
    let generated_at = chrono::Local::now().to_rfc3339();
    let mut findings = Vec::with_capacity(results.len());
    let mut access_map = Vec::with_capacity(results.len());

    for (index, result) in results.iter().enumerate() {
        let fingerprint = format!("direct-{}-{index}", result.rule_id);
        findings.push(FindingReporterRecord {
            rule: RuleMetadata {
                title: result.rule_id.clone(),
                name: result.rule_name.clone(),
                id: result.rule_id.clone(),
                description: result.rule_name.clone(),
            },
            finding: FindingRecordData {
                dependent_captures: Default::default(),
                ambiguous_dependencies: Default::default(),
                snippet: "[credential supplied directly]".to_string(),
                fingerprint: fingerprint.clone(),
                confidence: "high".to_string(),
                entropy: "0.00".to_string(),
                validation: ValidationInfo {
                    outcome: kingfisher_core::ValidationOutcome::VerifiedActive,
                    status: "Active Credential".to_string(),
                    response: "Direct blast-radius mapping completed".to_string(),
                },
                language: "Direct input".to_string(),
                line: 0,
                column_start: 0,
                column_end: 0,
                path: "direct input".to_string(),
                encoding: None,
                git_metadata: None,
                validate_command: None,
                revoke_command: None,
                blast_radius_command: None,
            },
        });

        let mut mapped = result.result.clone();
        mapped.fingerprint = Some(fingerprint);
        access_map.push(access_map_entry_from_result(&mapped));
    }

    let finding_count = findings.len();
    let envelope = ReportEnvelope {
        findings,
        access_map: Some(access_map),
        audit: None,
        metadata: Some(ScanReportMetadata {
            generated_at: generated_at.clone(),
            scan_timestamp: generated_at,
            target: Some("direct blast-radius mapping".to_string()),
            command_line_args: vec!["kingfisher".to_string(), "blast-radius".to_string()],
            kingfisher_version: env!("CARGO_PKG_VERSION").to_string(),
            latest_version_available: None,
            update_check_status: None,
            summary: ScanReportSummary {
                findings: finding_count,
                active_findings: finding_count,
                inactive_findings: 0,
                locally_derived_findings: 0,
                invalid_material_findings: 0,
                unknown_validation_findings: 0,
                access_map_identities: finding_count,
                rules_applied: None,
                confidence_level: "high".to_string(),
                custom_rules_used: false,
                successful_validations: Some(finding_count),
                failed_validations: Some(0),
                skipped_validations: Some(0),
                blobs_scanned: None,
                bytes_scanned: None,
                scan_duration_seconds: None,
            },
        }),
    };

    Ok(serde_json::to_vec_pretty(&envelope)?)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::access_map::{AccessSummary, PermissionSummary, Severity};
    use tempfile::tempdir;

    #[test]
    fn viewer_report_correlates_direct_results_with_findings() {
        let result = DirectAccessMapResult {
            rule_id: "custom.example".to_string(),
            rule_name: "Example credential".to_string(),
            result: AccessMapResult {
                cloud: "example".to_string(),
                fingerprint: None,
                identity: AccessSummary {
                    id: "example-user".to_string(),
                    access_type: "user".to_string(),
                    project: None,
                    tenant: None,
                    account_id: None,
                },
                roles: Vec::new(),
                permissions: PermissionSummary::default(),
                resources: Vec::new(),
                severity: Severity::Low,
                recommendations: Vec::new(),
                risk_notes: Vec::new(),
                token_details: None,
                provider_metadata: None,
            },
        };

        let report: serde_json::Value =
            serde_json::from_slice(&build_viewer_report_bytes(&[result]).unwrap()).unwrap();
        assert_eq!(report["findings"][0]["finding"]["fingerprint"], "direct-custom.example-0");
        assert_eq!(report["access_map"][0]["fingerprint"], "direct-custom.example-0");
        assert_eq!(report["access_map"][0]["account"], "example-user");
    }

    #[test]
    fn direct_results_honor_output_path() {
        let temp = tempdir().unwrap();
        let output = temp.path().join("report.json");
        let result = DirectAccessMapResult {
            rule_id: "custom.example".to_string(),
            rule_name: "Example credential".to_string(),
            result: AccessMapResult {
                cloud: "example".to_string(),
                fingerprint: None,
                identity: AccessSummary {
                    id: "example-user".to_string(),
                    access_type: "user".to_string(),
                    project: None,
                    tenant: None,
                    account_id: None,
                },
                roles: Vec::new(),
                permissions: PermissionSummary::default(),
                resources: Vec::new(),
                severity: Severity::Low,
                recommendations: Vec::new(),
                risk_notes: Vec::new(),
                token_details: None,
                provider_metadata: None,
            },
        };

        print_results(&[result], "json", Some(&output)).unwrap();

        let report: serde_json::Value =
            serde_json::from_slice(&std::fs::read(output).unwrap()).unwrap();
        assert_eq!(report[0]["rule_id"], "custom.example");
    }

    #[test]
    fn mixed_mapping_attempts_keep_successes_and_failure_diagnostics() {
        let failed_attempt = AccessMapAttempt {
            result: AccessMapResult {
                cloud: "example".to_string(),
                fingerprint: None,
                identity: AccessSummary {
                    id: "token".to_string(),
                    access_type: "unknown".to_string(),
                    project: None,
                    tenant: None,
                    account_id: None,
                },
                roles: Vec::new(),
                permissions: PermissionSummary::default(),
                resources: Vec::new(),
                severity: Severity::Medium,
                recommendations: Vec::new(),
                risk_notes: vec!["Identity mapping failed: unauthorized".to_string()],
                token_details: None,
                provider_metadata: None,
            },
            succeeded: false,
        };
        let successful_attempt = AccessMapAttempt {
            result: AccessMapResult {
                cloud: "example".to_string(),
                fingerprint: Some("0".to_string()),
                identity: AccessSummary {
                    id: "valid-token".to_string(),
                    access_type: "user".to_string(),
                    project: None,
                    tenant: None,
                    account_id: None,
                },
                roles: Vec::new(),
                permissions: PermissionSummary::default(),
                resources: Vec::new(),
                severity: Severity::Low,
                recommendations: Vec::new(),
                risk_notes: Vec::new(),
                token_details: None,
                provider_metadata: None,
            },
            succeeded: true,
        };

        let mut results = Vec::new();
        let mut failures = Vec::new();
        record_mapping_attempt(
            "custom.first",
            "First rule",
            failed_attempt,
            &mut results,
            &mut failures,
        );
        record_mapping_attempt(
            "custom.second",
            "Second rule",
            successful_attempt,
            &mut results,
            &mut failures,
        );

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].rule_id, "custom.second");
        assert_eq!(results[0].result.fingerprint, None);
        assert_eq!(failures, ["custom.first: Identity mapping failed: unauthorized"]);
    }
}