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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use anyhow::{Context, Result, anyhow};
use reqwest::{Client, header};
use serde::Deserialize;
use tracing::warn;

use crate::{cli::commands::access_map::AccessMapArgs, validation::GLOBAL_USER_AGENT};

use super::{
    AccessMapResult, AccessSummary, AccessTokenDetails, PermissionSummary, ResourceExposure,
    RoleBinding, Severity, build_recommendations,
};

const DIGITALOCEAN_API: &str = "https://api.digitalocean.com";

#[derive(Deserialize)]
struct AccountResponse {
    #[serde(default)]
    account: Option<DigitalOceanAccount>,
}

#[derive(Deserialize)]
struct DigitalOceanAccount {
    #[serde(default)]
    email: Option<String>,
    #[serde(default)]
    uuid: Option<String>,
    #[serde(default)]
    #[expect(dead_code)]
    status: Option<String>,
    #[serde(default)]
    team: Option<DigitalOceanTeam>,
}

#[derive(Deserialize)]
struct DigitalOceanTeam {
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    #[expect(dead_code)]
    uuid: Option<String>,
}

#[derive(Deserialize)]
struct ProjectsResponse {
    #[serde(default)]
    projects: Vec<DigitalOceanProject>,
}

#[derive(Deserialize)]
struct DigitalOceanProject {
    #[serde(default)]
    id: Option<String>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    is_default: Option<bool>,
}

#[derive(Deserialize)]
struct DropletsResponse {
    #[serde(default)]
    droplets: Vec<DigitalOceanDroplet>,
}

#[derive(Deserialize)]
struct DigitalOceanDroplet {
    #[serde(default)]
    id: Option<u64>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    status: Option<String>,
}

#[derive(Deserialize)]
struct DatabasesResponse {
    #[serde(default)]
    databases: Vec<DigitalOceanDatabase>,
}

#[derive(Deserialize)]
struct DigitalOceanDatabase {
    #[serde(default)]
    id: Option<String>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    engine: Option<String>,
}

#[derive(Deserialize)]
struct KubernetesClustersResponse {
    #[serde(default)]
    kubernetes_clusters: Vec<DigitalOceanKubernetesCluster>,
}

#[derive(Deserialize)]
struct DigitalOceanKubernetesCluster {
    #[serde(default)]
    id: Option<String>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    region: Option<String>,
}

pub async fn map_access(args: &AccessMapArgs) -> Result<AccessMapResult> {
    let token = if let Some(path) = args.credential_path.as_deref() {
        let raw = std::fs::read_to_string(path).with_context(|| {
            format!("Failed to read DigitalOcean token from {}", path.display())
        })?;
        raw.trim().to_string()
    } else {
        return Err(anyhow!(
            "DigitalOcean access-map requires a validated token from scan results"
        ));
    };

    map_access_from_token(&token).await
}

pub async fn map_access_from_token(token: &str) -> Result<AccessMapResult> {
    let client = Client::builder()
        .user_agent(GLOBAL_USER_AGENT.as_str())
        .build()
        .context("Failed to build DigitalOcean HTTP client")?;

    let account = fetch_account(&client, token).await?;

    let username = account
        .email
        .clone()
        .unwrap_or_else(|| account.uuid.clone().unwrap_or_else(|| "do_user".to_string()));

    let team_name = account.team.as_ref().and_then(|t| t.name.clone());

    let identity = AccessSummary {
        id: username.clone(),
        access_type: "user".into(),
        project: None,
        tenant: team_name.clone(),
        account_id: account.uuid.clone(),
    };

    let mut risk_notes = Vec::new();
    let mut resources = Vec::new();
    let mut permissions = PermissionSummary::default();
    let mut roles = Vec::new();
    let mut has_droplets = false;
    let mut has_databases = false;
    let mut has_kubernetes = false;

    // Enumerate projects
    let projects = list_projects(&client, token).await.unwrap_or_else(|err| {
        warn!("DigitalOcean access-map: project enumeration failed: {err}");
        Vec::new()
    });

    for project in &projects {
        let project_name = project
            .name
            .clone()
            .or_else(|| project.id.clone())
            .unwrap_or_else(|| "unknown_project".to_string());

        let is_default = project.is_default.unwrap_or(false);

        resources.push(ResourceExposure {
            resource_type: "project".into(),
            name: project_name.clone(),
            permissions: vec!["project:read".to_string()],
            risk: severity_to_str(Severity::Low).to_string(),
            reason: if is_default {
                "Default DigitalOcean project".to_string()
            } else {
                "DigitalOcean project accessible with this token".to_string()
            },
        });
    }

    permissions.read_only.push("project:list".to_string());

    // Enumerate droplets
    let droplets = list_droplets(&client, token).await.unwrap_or_else(|err| {
        warn!("DigitalOcean access-map: droplet enumeration failed: {err}");
        Vec::new()
    });

    if !droplets.is_empty() {
        has_droplets = true;
        permissions.risky.push("droplet:list".to_string());

        let role = RoleBinding {
            name: "droplet:access".into(),
            source: "digitalocean".into(),
            permissions: vec!["droplet:read".to_string(), "droplet:write".to_string()],
        };
        roles.push(role);
    }

    for droplet in &droplets {
        let droplet_name = droplet.name.clone().unwrap_or_else(|| {
            droplet.id.map(|id| id.to_string()).unwrap_or_else(|| "unknown_droplet".to_string())
        });

        let status = droplet.status.as_deref().unwrap_or("unknown");

        resources.push(ResourceExposure {
            resource_type: "droplet".into(),
            name: droplet_name.clone(),
            permissions: vec!["droplet:read".to_string()],
            risk: severity_to_str(Severity::High).to_string(),
            reason: format!("Compute droplet (status: {status}) accessible with this token"),
        });
    }

    // Enumerate databases
    let databases = list_databases(&client, token).await.unwrap_or_else(|err| {
        warn!("DigitalOcean access-map: database enumeration failed: {err}");
        Vec::new()
    });

    if !databases.is_empty() {
        has_databases = true;
        permissions.admin.push("database:list".to_string());

        let role = RoleBinding {
            name: "database:access".into(),
            source: "digitalocean".into(),
            permissions: vec!["database:read".to_string(), "database:write".to_string()],
        };
        roles.push(role);
    }

    for db in &databases {
        let db_name = db
            .name
            .clone()
            .or_else(|| db.id.clone())
            .unwrap_or_else(|| "unknown_database".to_string());

        let engine = db.engine.as_deref().unwrap_or("unknown");

        resources.push(ResourceExposure {
            resource_type: "database".into(),
            name: db_name.clone(),
            permissions: vec!["database:read".to_string()],
            risk: severity_to_str(Severity::Critical).to_string(),
            reason: format!("Managed database ({engine}) accessible with this token"),
        });
    }

    // Enumerate Kubernetes clusters
    let clusters = list_kubernetes_clusters(&client, token).await.unwrap_or_else(|err| {
        warn!("DigitalOcean access-map: kubernetes enumeration failed: {err}");
        Vec::new()
    });

    if !clusters.is_empty() {
        has_kubernetes = true;
        permissions.risky.push("kubernetes:list".to_string());

        let role = RoleBinding {
            name: "kubernetes:access".into(),
            source: "digitalocean".into(),
            permissions: vec!["kubernetes:read".to_string(), "kubernetes:write".to_string()],
        };
        roles.push(role);
    }

    for cluster in &clusters {
        let cluster_name = cluster
            .name
            .clone()
            .or_else(|| cluster.id.clone())
            .unwrap_or_else(|| "unknown_cluster".to_string());

        let region = cluster.region.as_deref().unwrap_or("unknown");

        resources.push(ResourceExposure {
            resource_type: "kubernetes_cluster".into(),
            name: cluster_name.clone(),
            permissions: vec!["kubernetes:read".to_string()],
            risk: severity_to_str(Severity::High).to_string(),
            reason: format!("Kubernetes cluster in {region} accessible with this token"),
        });
    }

    if team_name.is_some() {
        permissions.risky.push("team:member".to_string());
        risk_notes.push("Token has team-level access".into());
    }

    permissions.admin.sort();
    permissions.admin.dedup();
    permissions.risky.sort();
    permissions.risky.dedup();
    permissions.read_only.sort();
    permissions.read_only.dedup();

    let severity = derive_severity(has_droplets, has_databases, has_kubernetes, &projects);

    if resources.is_empty() {
        resources.push(ResourceExposure {
            resource_type: "account".into(),
            name: username.clone(),
            permissions: Vec::new(),
            risk: severity_to_str(Severity::Low).to_string(),
            reason: "DigitalOcean account associated with the token".into(),
        });
        risk_notes.push("Token did not enumerate any resources".into());
    }

    Ok(AccessMapResult {
        cloud: "digitalocean".into(),
        identity,
        roles,
        permissions,
        resources,
        severity,
        recommendations: build_recommendations(severity),
        risk_notes,
        token_details: Some(AccessTokenDetails {
            name: None,
            username: account.email.clone(),
            account_type: None,
            company: team_name,
            location: None,
            email: account.email.clone(),
            url: None,
            token_type: None,
            created_at: None,
            last_used_at: None,
            expires_at: None,
            user_id: account.uuid,
            scopes: Vec::new(),
        }),
        provider_metadata: None,
        fingerprint: None,
    })
}

async fn fetch_account(client: &Client, token: &str) -> Result<DigitalOceanAccount> {
    let resp = client
        .get(format!("{DIGITALOCEAN_API}/v2/account"))
        .header(header::AUTHORIZATION, format!("Bearer {token}"))
        .header(header::ACCEPT, "application/json")
        .send()
        .await
        .context("DigitalOcean access-map: failed to fetch account info")?;

    if !resp.status().is_success() {
        return Err(anyhow!(
            "DigitalOcean access-map: account lookup failed with HTTP {}",
            resp.status()
        ));
    }

    let body: AccountResponse =
        resp.json().await.context("DigitalOcean access-map: invalid account JSON")?;

    body.account
        .ok_or_else(|| anyhow!("DigitalOcean access-map: account field missing from response"))
}

async fn list_projects(client: &Client, token: &str) -> Result<Vec<DigitalOceanProject>> {
    let resp = client
        .get(format!("{DIGITALOCEAN_API}/v2/projects"))
        .header(header::AUTHORIZATION, format!("Bearer {token}"))
        .header(header::ACCEPT, "application/json")
        .send()
        .await
        .context("DigitalOcean access-map: failed to list projects")?;

    if !resp.status().is_success() {
        warn!("DigitalOcean access-map: project enumeration failed with HTTP {}", resp.status());
        return Ok(Vec::new());
    }

    let body: ProjectsResponse =
        resp.json().await.context("DigitalOcean access-map: invalid projects JSON")?;
    Ok(body.projects)
}

async fn list_droplets(client: &Client, token: &str) -> Result<Vec<DigitalOceanDroplet>> {
    let resp = client
        .get(format!("{DIGITALOCEAN_API}/v2/droplets"))
        .header(header::AUTHORIZATION, format!("Bearer {token}"))
        .header(header::ACCEPT, "application/json")
        .send()
        .await
        .context("DigitalOcean access-map: failed to list droplets")?;

    if !resp.status().is_success() {
        warn!("DigitalOcean access-map: droplet enumeration failed with HTTP {}", resp.status());
        return Ok(Vec::new());
    }

    let body: DropletsResponse =
        resp.json().await.context("DigitalOcean access-map: invalid droplets JSON")?;
    Ok(body.droplets)
}

async fn list_databases(client: &Client, token: &str) -> Result<Vec<DigitalOceanDatabase>> {
    let resp = client
        .get(format!("{DIGITALOCEAN_API}/v2/databases"))
        .header(header::AUTHORIZATION, format!("Bearer {token}"))
        .header(header::ACCEPT, "application/json")
        .send()
        .await
        .context("DigitalOcean access-map: failed to list databases")?;

    if !resp.status().is_success() {
        warn!("DigitalOcean access-map: database enumeration failed with HTTP {}", resp.status());
        return Ok(Vec::new());
    }

    let body: DatabasesResponse =
        resp.json().await.context("DigitalOcean access-map: invalid databases JSON")?;
    Ok(body.databases)
}

async fn list_kubernetes_clusters(
    client: &Client,
    token: &str,
) -> Result<Vec<DigitalOceanKubernetesCluster>> {
    let resp = client
        .get(format!("{DIGITALOCEAN_API}/v2/kubernetes/clusters"))
        .header(header::AUTHORIZATION, format!("Bearer {token}"))
        .header(header::ACCEPT, "application/json")
        .send()
        .await
        .context("DigitalOcean access-map: failed to list kubernetes clusters")?;

    if !resp.status().is_success() {
        warn!("DigitalOcean access-map: kubernetes enumeration failed with HTTP {}", resp.status());
        return Ok(Vec::new());
    }

    let body: KubernetesClustersResponse =
        resp.json().await.context("DigitalOcean access-map: invalid kubernetes JSON")?;
    Ok(body.kubernetes_clusters)
}

fn derive_severity(
    has_droplets: bool,
    has_databases: bool,
    _has_kubernetes: bool,
    projects: &[DigitalOceanProject],
) -> Severity {
    if has_databases {
        return Severity::Critical;
    }

    if has_droplets {
        return Severity::High;
    }

    if !projects.is_empty() {
        return Severity::Medium;
    }

    Severity::Low
}

fn severity_to_str(severity: Severity) -> &'static str {
    match severity {
        Severity::Low => "low",
        Severity::Medium => "medium",
        Severity::High => "high",
        Severity::Critical => "critical",
    }
}