tfmcp 0.1.9

Terraform Model Context Protocol Tool - A CLI tool to manage Terraform through MCP
Documentation
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! Plan analyzer for detailed terraform plan analysis with risk scoring.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Risk level for plan changes
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum RiskLevel {
    Low,
    Medium,
    High,
    Critical,
}

/// A single resource change from terraform plan
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceChange {
    pub address: String,
    pub resource_type: String,
    pub provider: String,
    pub action: String,
    pub before: Option<serde_json::Value>,
    pub after: Option<serde_json::Value>,
    pub after_unknown: Option<serde_json::Value>,
}

/// Change summary statistics
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ChangeSummary {
    pub add: i32,
    pub change: i32,
    pub destroy: i32,
    pub replace: i32,
    pub no_op: i32,
}

/// Risk assessment for the plan
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiskAssessment {
    pub level: RiskLevel,
    pub score: i32,
    pub warnings: Vec<String>,
    pub recommendations: Vec<String>,
}

/// Dependency impact analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DependencyImpact {
    pub resource: String,
    pub affected_by: Vec<String>,
    pub affects: Vec<String>,
}

/// Complete plan analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlanAnalysis {
    pub summary: ChangeSummary,
    pub resource_changes: Vec<ResourceChange>,
    pub risk_assessment: RiskAssessment,
    pub dependency_impacts: Vec<DependencyImpact>,
    pub terraform_version: Option<String>,
    pub format_version: Option<String>,
}

/// Terraform plan JSON output structure
#[derive(Debug, Deserialize)]
struct TerraformPlanJson {
    format_version: Option<String>,
    terraform_version: Option<String>,
    resource_changes: Option<Vec<PlanResourceChange>>,
    #[allow(dead_code)]
    prior_state: Option<serde_json::Value>,
    #[allow(dead_code)]
    configuration: Option<serde_json::Value>,
}

#[derive(Debug, Deserialize)]
struct PlanResourceChange {
    address: String,
    #[serde(rename = "type")]
    resource_type: String,
    provider_name: Option<String>,
    change: Option<PlanChange>,
}

#[derive(Debug, Deserialize)]
struct PlanChange {
    actions: Vec<String>,
    before: Option<serde_json::Value>,
    after: Option<serde_json::Value>,
    after_unknown: Option<serde_json::Value>,
}

/// High-risk resource types that require extra caution
const HIGH_RISK_RESOURCES: &[&str] = &[
    "aws_db_instance",
    "aws_rds_cluster",
    "aws_elasticache_cluster",
    "aws_elasticsearch_domain",
    "aws_opensearch_domain",
    "google_sql_database_instance",
    "azurerm_sql_database",
    "azurerm_postgresql_server",
    "aws_s3_bucket",
    "google_storage_bucket",
    "azurerm_storage_account",
    "aws_iam_role",
    "aws_iam_policy",
    "google_project_iam_binding",
    "azurerm_role_assignment",
    "aws_security_group",
    "google_compute_firewall",
    "azurerm_network_security_group",
    "aws_vpc",
    "google_compute_network",
    "azurerm_virtual_network",
    "aws_kms_key",
    "google_kms_crypto_key",
    "azurerm_key_vault",
];

/// Analyze terraform plan JSON output
pub fn analyze_plan(plan_json: &str, include_risk: bool) -> anyhow::Result<PlanAnalysis> {
    // Try to parse as JSON array of lines (terraform plan -json outputs NDJSON)
    let plan = parse_plan_json(plan_json)?;

    let mut summary = ChangeSummary::default();
    let mut resource_changes = Vec::new();

    if let Some(changes) = plan.resource_changes {
        for change in changes {
            let action = if let Some(ref c) = change.change {
                actions_to_string(&c.actions)
            } else {
                "unknown".to_string()
            };

            // Update summary
            match action.as_str() {
                "create" => summary.add += 1,
                "update" => summary.change += 1,
                "delete" => summary.destroy += 1,
                "replace" | "create_delete" | "delete_create" => summary.replace += 1,
                "no-op" | "read" => summary.no_op += 1,
                _ => {}
            }

            let rc = ResourceChange {
                address: change.address.clone(),
                resource_type: change.resource_type.clone(),
                provider: change
                    .provider_name
                    .unwrap_or_else(|| "unknown".to_string()),
                action: action.clone(),
                before: change.change.as_ref().and_then(|c| c.before.clone()),
                after: change.change.as_ref().and_then(|c| c.after.clone()),
                after_unknown: change.change.as_ref().and_then(|c| c.after_unknown.clone()),
            };
            resource_changes.push(rc);
        }
    }

    let risk_assessment = if include_risk {
        assess_risk(&resource_changes, &summary)
    } else {
        RiskAssessment {
            level: RiskLevel::Low,
            score: 0,
            warnings: vec![],
            recommendations: vec![],
        }
    };

    let dependency_impacts = analyze_dependencies(&resource_changes);

    Ok(PlanAnalysis {
        summary,
        resource_changes,
        risk_assessment,
        dependency_impacts,
        terraform_version: plan.terraform_version,
        format_version: plan.format_version,
    })
}

/// Parse terraform plan JSON (handles both single JSON and NDJSON format)
fn parse_plan_json(json_str: &str) -> anyhow::Result<TerraformPlanJson> {
    // First try to parse as a single JSON object
    if let Ok(plan) = serde_json::from_str::<TerraformPlanJson>(json_str) {
        return Ok(plan);
    }

    // If that fails, try to parse as NDJSON (newline-delimited JSON)
    // This is the format terraform plan -json outputs
    let mut resource_changes = Vec::new();
    let mut terraform_version = None;
    let mut format_version = None;

    for line in json_str.lines() {
        if line.trim().is_empty() {
            continue;
        }

        if let Ok(obj) = serde_json::from_str::<serde_json::Value>(line) {
            // Check if this is a version message
            if let Some(v) = obj.get("terraform_version").and_then(|v| v.as_str()) {
                terraform_version = Some(v.to_string());
            }
            if let Some(v) = obj.get("format_version").and_then(|v| v.as_str()) {
                format_version = Some(v.to_string());
            }

            // Check if this is a resource_drift or planned_change message
            if let Some(change_type) = obj.get("type").and_then(|t| t.as_str()) {
                if change_type == "planned_change" || change_type == "resource_drift" {
                    if let Some(change) = obj.get("change") {
                        if let Ok(rc) = serde_json::from_value::<PlanResourceChange>(change.clone())
                        {
                            resource_changes.push(rc);
                        }
                    }
                }
            }
        }
    }

    Ok(TerraformPlanJson {
        format_version,
        terraform_version,
        resource_changes: if resource_changes.is_empty() {
            None
        } else {
            Some(resource_changes)
        },
        prior_state: None,
        configuration: None,
    })
}

/// Convert action array to a single action string
fn actions_to_string(actions: &[String]) -> String {
    match actions.len() {
        0 => "no-op".to_string(),
        1 => actions[0].clone(),
        2 => {
            if actions.contains(&"create".to_string()) && actions.contains(&"delete".to_string()) {
                "replace".to_string()
            } else {
                actions.join("_")
            }
        }
        _ => actions.join("_"),
    }
}

/// Assess risk based on resource changes
fn assess_risk(changes: &[ResourceChange], summary: &ChangeSummary) -> RiskAssessment {
    let mut score = 0;
    let mut warnings = Vec::new();
    let mut recommendations = Vec::new();

    // Base score from change counts
    score += summary.destroy * 30;
    score += summary.replace * 20;
    score += summary.change * 5;
    score += summary.add * 2;

    // Check for high-risk resources
    for change in changes {
        let is_high_risk = HIGH_RISK_RESOURCES
            .iter()
            .any(|&r| change.resource_type == r);

        if is_high_risk {
            match change.action.as_str() {
                "delete" => {
                    score += 50;
                    warnings.push(format!(
                        "CRITICAL: High-risk resource '{}' will be DESTROYED",
                        change.address
                    ));
                }
                "replace" | "create_delete" | "delete_create" => {
                    score += 40;
                    warnings.push(format!(
                        "WARNING: High-risk resource '{}' will be REPLACED (data loss possible)",
                        change.address
                    ));
                }
                "update" => {
                    score += 15;
                    warnings.push(format!(
                        "CAUTION: High-risk resource '{}' will be modified",
                        change.address
                    ));
                }
                _ => {}
            }
        }

        // Check for IAM/security changes
        let is_security_resource = change.resource_type.contains("iam")
            || change.resource_type.contains("security")
            || change.resource_type.contains("firewall");
        if is_security_resource && change.action != "no-op" && change.action != "read" {
            score += 10;
            warnings.push(format!(
                "Security-related resource '{}' will be modified",
                change.address
            ));
        }

        // Check for network changes
        let is_network_resource = change.resource_type.contains("vpc")
            || change.resource_type.contains("network")
            || change.resource_type.contains("subnet");
        if is_network_resource && (change.action == "delete" || change.action.contains("replace")) {
            score += 25;
            warnings.push(format!(
                "Network infrastructure '{}' change may cause connectivity issues",
                change.address
            ));
        }
    }

    // Generate recommendations
    if summary.destroy > 0 {
        recommendations.push("Review all resources marked for destruction carefully".to_string());
        recommendations.push("Ensure backups exist for any stateful resources".to_string());
    }

    if summary.replace > 0 {
        recommendations
            .push("Resources being replaced may have brief downtime or data loss".to_string());
    }

    if score > 50 {
        recommendations.push("Consider applying changes during a maintenance window".to_string());
        recommendations.push("Have a rollback plan ready".to_string());
    }

    let level = match score {
        0..=10 => RiskLevel::Low,
        11..=30 => RiskLevel::Medium,
        31..=60 => RiskLevel::High,
        _ => RiskLevel::Critical,
    };

    RiskAssessment {
        level,
        score,
        warnings,
        recommendations,
    }
}

/// Analyze dependencies between resources
fn analyze_dependencies(changes: &[ResourceChange]) -> Vec<DependencyImpact> {
    let mut impacts = Vec::new();
    let mut resource_refs: HashMap<String, Vec<String>> = HashMap::new();

    // Build a map of references from after values
    for change in changes {
        if let Some(after) = &change.after {
            let refs = extract_references(after, &change.address);
            for ref_addr in refs {
                resource_refs
                    .entry(ref_addr)
                    .or_default()
                    .push(change.address.clone());
            }
        }
    }

    // Create impact analysis for each changed resource
    for change in changes {
        if change.action == "no-op" || change.action == "read" {
            continue;
        }

        let affected_by: Vec<String> = changes
            .iter()
            .filter(|c| {
                c.address != change.address
                    && (c.action == "delete"
                        || c.action.contains("replace")
                        || c.action == "update")
            })
            .filter(|c| {
                // Check if this change might affect the current resource
                if let Some(after) = &change.after {
                    let refs = extract_references(after, &change.address);
                    refs.contains(&c.address)
                } else {
                    false
                }
            })
            .map(|c| c.address.clone())
            .collect();

        let affects = resource_refs
            .get(&change.address)
            .cloned()
            .unwrap_or_default();

        if !affected_by.is_empty() || !affects.is_empty() {
            impacts.push(DependencyImpact {
                resource: change.address.clone(),
                affected_by,
                affects,
            });
        }
    }

    impacts
}

/// Extract resource references from a JSON value
fn extract_references(value: &serde_json::Value, current_addr: &str) -> Vec<String> {
    let mut refs = Vec::new();

    fn walk(v: &serde_json::Value, refs: &mut Vec<String>, current: &str) {
        match v {
            serde_json::Value::String(s) => {
                // Look for resource address patterns like "aws_instance.example"
                if s.contains('.') && !s.starts_with("http") && !s.contains('/') && s != current {
                    // Check if it looks like a resource address
                    let parts: Vec<&str> = s.split('.').collect();
                    if parts.len() >= 2
                        && !parts[0].is_empty()
                        && parts[0]
                            .chars()
                            .all(|c| c.is_alphanumeric() || c == '_' || c == '-')
                    {
                        refs.push(s.clone());
                    }
                }
            }
            serde_json::Value::Array(arr) => {
                for item in arr {
                    walk(item, refs, current);
                }
            }
            serde_json::Value::Object(obj) => {
                for (_, v) in obj {
                    walk(v, refs, current);
                }
            }
            _ => {}
        }
    }

    walk(value, &mut refs, current_addr);
    refs.sort();
    refs.dedup();
    refs
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_actions_to_string() {
        assert_eq!(actions_to_string(&[]), "no-op");
        assert_eq!(actions_to_string(&["create".to_string()]), "create");
        assert_eq!(
            actions_to_string(&["create".to_string(), "delete".to_string()]),
            "replace"
        );
    }

    #[test]
    fn test_risk_assessment_empty() {
        let changes = vec![];
        let summary = ChangeSummary::default();
        let risk = assess_risk(&changes, &summary);
        assert_eq!(risk.level, RiskLevel::Low);
        assert_eq!(risk.score, 0);
    }

    #[test]
    fn test_risk_assessment_destroy() {
        let changes = vec![ResourceChange {
            address: "aws_instance.example".to_string(),
            resource_type: "aws_instance".to_string(),
            provider: "aws".to_string(),
            action: "delete".to_string(),
            before: None,
            after: None,
            after_unknown: None,
        }];
        let summary = ChangeSummary {
            destroy: 1,
            ..Default::default()
        };
        let risk = assess_risk(&changes, &summary);
        assert!(risk.score > 0);
    }

    #[test]
    fn test_high_risk_resource() {
        let changes = vec![ResourceChange {
            address: "aws_db_instance.main".to_string(),
            resource_type: "aws_db_instance".to_string(),
            provider: "aws".to_string(),
            action: "delete".to_string(),
            before: None,
            after: None,
            after_unknown: None,
        }];
        let summary = ChangeSummary {
            destroy: 1,
            ..Default::default()
        };
        let risk = assess_risk(&changes, &summary);
        assert_eq!(risk.level, RiskLevel::Critical);
        assert!(risk.warnings.iter().any(|w| w.contains("CRITICAL")));
    }
}