netget-tool 0.3.4

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Maven repository protocol actions implementation

use crate::llm::actions::{
    protocol_trait::{ActionResult, Protocol, Server},
    ActionDefinition, Parameter,
};
use crate::protocol::EventType;
use crate::state::app_state::AppState;
use anyhow::{Context, Result};
use serde_json::json;
use std::collections::HashMap;
use std::sync::LazyLock;

use crate::protocol::log_template::LogTemplate;

/// Maven protocol action handler
pub struct MavenProtocol;

impl MavenProtocol {
    pub fn new() -> Self {
        Self
    }
}

// Implement Protocol trait (common functionality)
impl Protocol for MavenProtocol {
    fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
        // Maven has no async actions - it's purely request-response
        Vec::new()
    }
    fn get_sync_actions(&self) -> Vec<ActionDefinition> {
        vec![
            send_maven_artifact_action(),
            send_maven_metadata_action(),
            send_maven_error_action(),
        ]
    }
    fn protocol_name(&self) -> &'static str {
        "Maven"
    }
    fn get_event_types(&self) -> Vec<EventType> {
        get_maven_event_types()
    }
    fn stack_name(&self) -> &'static str {
        "ETH>IP>TCP>HTTP>Maven"
    }
    fn keywords(&self) -> Vec<&'static str> {
        vec!["maven", "maven repository", "maven repo", "via maven"]
    }
    fn metadata(&self) -> crate::protocol::metadata::ProtocolMetadataV2 {
        use crate::protocol::metadata::{DevelopmentState, ProtocolMetadataV2};

        ProtocolMetadataV2::builder()
            .state(DevelopmentState::Beta)
            .implementation("hyper v1.0 HTTP server with Maven repository path parsing")
            .llm_control(
                "Artifact availability, content generation (POM, JAR, checksums), version metadata",
            )
            .e2e_testing(
                "The real `mvn` binary, in \
                 tests/server/maven/e2e_test.rs::test_maven_cli_download, which is not \
                 #[ignore]d and fails rather than skips when mvn is absent: \
                 `mvn dependency:get` resolves com.netget.test:maven-test:1.0.0, verifies \
                 the .sha1 companions (computed by `shasum`, not by NetGet) and writes the \
                 artifact into its local repository, and the test asserts the stored JAR and \
                 POM are byte-for-byte what NetGet served. Nothing outside 127.0.0.1 is \
                 contacted: a test-owned settings.xml mirrors * at the server under test and \
                 Maven's split local repository resolves its own plugins from the machine's \
                 existing ~/.m2 cache. Three further mocked suites cover POM/JAR/checksum/\
                 metadata routing, multi-version and classifier selection; \
                 llm_failure_test.rs asserts the fail-closed 500/503 and that no internal \
                 error text reaches the wire. The JAR served is a REAL archive - a zip built \
                 by Python's zipfile, which NetGet does not own - and the same test then runs \
                 `mvn dependency:unpack`, so Maven opens the artifact with its own unarchiver \
                 and the test asserts the resource inside it. A client has therefore used an \
                 artifact from this repository, not merely fetched and checksummed one; that \
                 was the one gap this field named while the rating sat at Experimental. \
                 Untested: deploy/PUT (not implemented), SNAPSHOT resolution, GPG signatures, \
                 and any client other than mvn (Gradle and sbt are unproven)",
            )
            .max_inbound_bytes(crate::server::maven::MAX_REQUEST_BODY_BYTES)
            .request_only(
                "Maven is HTTP request/response; a server cannot send a peer anything unprompted",
            )
            .build()
    }
    fn description(&self) -> &'static str {
        "Maven repository server serving Java artifacts"
    }
    fn example_prompt(&self) -> &'static str {
        "Maven repository on port 8080 serving a simple library com.example:hello-world:1.0.0 with a JAR and POM file"
    }
    fn group_name(&self) -> &'static str {
        "Application"
    }

    fn get_startup_examples(&self) -> crate::llm::actions::StartupExamples {
        use crate::llm::actions::StartupExamples;

        // Deterministic: serve fixed maven-metadata for every artifact request,
        // no LLM call.
        let script = r#"import json, sys
data = json.load(sys.stdin)
event = data["event"]
if data["event_type_id"] == "maven_artifact_request":
    actions = [{"type": "send_maven_metadata", "group_id": "com.example",
                "artifact_id": "hello-world", "versions": ["1.0.0"],
                "latest": "1.0.0", "release": "1.0.0"}]
else:
    actions = []
print(json.dumps({"actions": actions}))"#;

        StartupExamples::new(
            // LLM mode
            json!({
                "type": "open_server",
                "port": 8080,
                "base_stack": "maven",
                "instruction": "Maven repository server. Serve artifact com.example:hello-world:1.0.0 with JAR and POM files."
            }),
            // Script mode
            json!({
                "type": "open_server",
                "port": 8080,
                "base_stack": "maven",
                "event_handlers": [{
                    "event_pattern": "maven_artifact_request",
                    "handler": {
                        "type": "script",
                        "language": "python",
                        "code": script
                    }
                }]
            }),
            // Static mode
            json!({
                "type": "open_server",
                "port": 8080,
                "base_stack": "maven",
                "event_handlers": [{
                    "event_pattern": "maven_artifact_request",
                    "handler": {
                        "type": "static",
                        "actions": [{
                            "type": "send_maven_metadata",
                            "group_id": "com.example",
                            "artifact_id": "hello-world",
                            "versions": ["1.0.0"],
                            "latest": "1.0.0",
                            "release": "1.0.0"
                        }]
                    }
                }]
            }),
        )
    }
}

// Implement Server trait (server-specific functionality)
impl Server for MavenProtocol {
    fn spawn(
        &self,
        ctx: crate::protocol::SpawnContext,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = anyhow::Result<std::net::SocketAddr>> + Send>,
    > {
        Box::pin(async move {
            use crate::server::maven::MavenServer;
            MavenServer::spawn_with_llm_actions(
                ctx.legacy_listen_addr(),
                ctx.llm_client,
                ctx.state,
                ctx.status_tx,
                ctx.server_id,
            )
            .await
        })
    }
    fn execute_action(&self, action: serde_json::Value) -> Result<ActionResult> {
        let action_type = action
            .get("type")
            .and_then(|v| v.as_str())
            .context("Missing 'type' field in action")?;

        match action_type {
            "send_maven_artifact" => self.execute_send_maven_artifact(action),
            "send_maven_metadata" => self.execute_send_maven_metadata(action),
            "send_maven_error" => self.execute_send_maven_error(action),
            _ => Err(anyhow::anyhow!("Unknown Maven action: {action_type}")),
        }
    }
}

/// Escape XML text content so a coordinate containing `&` or `<` cannot produce
/// a maven-metadata.xml that Maven refuses to parse.
fn xml_escape(text: &str) -> String {
    let mut escaped = String::with_capacity(text.len());
    for c in text.chars() {
        match c {
            '&' => escaped.push_str("&amp;"),
            '<' => escaped.push_str("&lt;"),
            '>' => escaped.push_str("&gt;"),
            '"' => escaped.push_str("&quot;"),
            '\'' => escaped.push_str("&apos;"),
            _ => escaped.push(c),
        }
    }
    escaped
}

/// Read an HTTP status code from an action, rejecting values hyper cannot build
/// a response from instead of silently truncating them into a valid-looking one.
fn status_from_action(action: &serde_json::Value, default: u16) -> Result<u16> {
    match action.get("status").and_then(|v| v.as_u64()) {
        None => Ok(default),
        Some(status) if (100..=599).contains(&status) => Ok(status as u16),
        Some(status) => Err(anyhow::anyhow!(
            "Invalid 'status' {status}: HTTP status codes are 100-599"
        )),
    }
}

impl MavenProtocol {
    /// Execute send_maven_artifact sync action
    fn execute_send_maven_artifact(&self, action: serde_json::Value) -> Result<ActionResult> {
        let status = status_from_action(&action, 200)?;

        let content_type = action
            .get("content_type")
            .and_then(|v| v.as_str())
            .unwrap_or("application/octet-stream");

        let mut headers = HashMap::new();
        headers.insert("Content-Type".to_string(), content_type.to_string());

        // Add optional custom headers
        if let Some(custom_headers) = action.get("headers").and_then(|v| v.as_object()) {
            for (k, v) in custom_headers {
                if let Some(v_str) = v.as_str() {
                    headers.insert(k.clone(), v_str.to_string());
                }
            }
        }

        // The body is either UTF-8 text (POM, checksum, maven-metadata.xml) or
        // base64-encoded bytes. Exactly one of the two must be present: guessing
        // would make "48656c6c6f" ambiguous between text and encoded bytes.
        let body = action.get("body").and_then(|v| v.as_str());
        let body_base64 = action.get("body_base64").and_then(|v| v.as_str());

        let response_data = match (body, body_base64) {
            (Some(_), Some(_)) => {
                anyhow::bail!("Provide either 'body' or 'body_base64', not both")
            }
            (None, None) => {
                anyhow::bail!("Missing 'body' parameter (or 'body_base64' for binary artifacts)")
            }
            (Some(text), None) => json!({
                "status": status,
                "headers": headers,
                "body": text
            }),
            (None, Some(encoded)) => {
                use base64::Engine;
                base64::engine::general_purpose::STANDARD
                    .decode(encoded)
                    .context("'body_base64' is not valid base64")?;
                json!({
                    "status": status,
                    "headers": headers,
                    "body_base64": encoded
                })
            }
        };

        Ok(ActionResult::Output(
            serde_json::to_vec(&response_data)
                .context("Failed to serialize Maven artifact response")?,
        ))
    }

    /// Execute send_maven_metadata sync action
    fn execute_send_maven_metadata(&self, action: serde_json::Value) -> Result<ActionResult> {
        let group_id = action
            .get("group_id")
            .and_then(|v| v.as_str())
            .context("Missing 'group_id' parameter")?;

        let artifact_id = action
            .get("artifact_id")
            .and_then(|v| v.as_str())
            .context("Missing 'artifact_id' parameter")?;

        let versions = action
            .get("versions")
            .and_then(|v| v.as_array())
            .context("Missing 'versions' parameter")?;

        let latest = action.get("latest").and_then(|v| v.as_str());

        let release = action.get("release").and_then(|v| v.as_str());

        // Generate maven-metadata.xml. Element order follows the repository
        // metadata schema Maven itself writes: latest, release, then versions.
        let mut xml = format!(
            r#"<?xml version="1.0" encoding="UTF-8"?>
<metadata>
  <groupId>{}</groupId>
  <artifactId>{}</artifactId>
  <versioning>
"#,
            xml_escape(group_id),
            xml_escape(artifact_id)
        );

        if let Some(latest_ver) = latest {
            xml.push_str(&format!(
                "    <latest>{}</latest>\n",
                xml_escape(latest_ver)
            ));
        }

        if let Some(release_ver) = release {
            xml.push_str(&format!(
                "    <release>{}</release>\n",
                xml_escape(release_ver)
            ));
        }

        xml.push_str("    <versions>\n");

        for version in versions {
            let v = version
                .as_str()
                .context("Every entry in 'versions' must be a version string")?;
            xml.push_str(&format!("      <version>{}</version>\n", xml_escape(v)));
        }

        xml.push_str("    </versions>\n  </versioning>\n</metadata>\n");

        let mut headers = HashMap::new();
        headers.insert("Content-Type".to_string(), "application/xml".to_string());

        let response_data = json!({
            "status": 200,
            "headers": headers,
            "body": xml
        });

        Ok(ActionResult::Output(
            serde_json::to_vec(&response_data)
                .context("Failed to serialize Maven metadata response")?,
        ))
    }

    /// Execute send_maven_error sync action
    fn execute_send_maven_error(&self, action: serde_json::Value) -> Result<ActionResult> {
        let status = status_from_action(&action, 404)?;

        let message = action
            .get("message")
            .and_then(|v| v.as_str())
            .unwrap_or("Not Found");

        let response_data = json!({
            "status": status,
            "headers": {
                "Content-Type": "text/plain"
            },
            "body": message
        });

        Ok(ActionResult::Output(
            serde_json::to_vec(&response_data)
                .context("Failed to serialize Maven error response")?,
        ))
    }
}

/// Action definition for send_maven_artifact (sync)
fn send_maven_artifact_action() -> ActionDefinition {
    ActionDefinition {
        name: "send_maven_artifact".to_string(),
        description: "Send a Maven artifact file (JAR, POM, checksum, etc.)".to_string(),
        parameters: vec![
            Parameter {
                name: "status".to_string(),
                type_hint: "number".to_string(),
                description: "HTTP status code (default: 200)".to_string(),
                required: false,
            },
            Parameter {
                name: "content_type".to_string(),
                type_hint: "string".to_string(),
                description: "Content-Type header (default: application/octet-stream)".to_string(),
                required: false,
            },
            Parameter {
                name: "body".to_string(),
                type_hint: "string".to_string(),
                description: "Artifact content as UTF-8 text: POM XML, maven-metadata.xml, or a checksum digest. Sent byte for byte. Required unless body_base64 is given".to_string(),
                required: false,
            },
            Parameter {
                name: "body_base64".to_string(),
                type_hint: "string".to_string(),
                description: "Artifact content as base64-encoded bytes, decoded before sending. Only for a real binary artifact a script or static handler already holds; do not try to write JAR (ZIP) bytes by hand - serve text and a matching Content-Type instead".to_string(),
                required: false,
            },
            Parameter {
                name: "headers".to_string(),
                type_hint: "object".to_string(),
                description: "Optional additional headers".to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "send_maven_artifact",
            "status": 200,
            "content_type": "application/xml",
            "body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project>\n  <modelVersion>4.0.0</modelVersion>\n  <groupId>com.example</groupId>\n  <artifactId>mylib</artifactId>\n  <version>1.0.0</version>\n</project>\n"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Maven {status} {content_type} ({body_len}B)")
                .with_debug("Maven send_maven_artifact: status={status} type={content_type}"),
        ),
    }
}

/// Action definition for send_maven_metadata (sync)
fn send_maven_metadata_action() -> ActionDefinition {
    ActionDefinition {
        name: "send_maven_metadata".to_string(),
        description: "Send Maven metadata XML listing available versions".to_string(),
        parameters: vec![
            Parameter {
                name: "group_id".to_string(),
                type_hint: "string".to_string(),
                description: "Maven group ID (e.g., 'com.example')".to_string(),
                required: true,
            },
            Parameter {
                name: "artifact_id".to_string(),
                type_hint: "string".to_string(),
                description: "Maven artifact ID".to_string(),
                required: true,
            },
            Parameter {
                name: "versions".to_string(),
                type_hint: "array".to_string(),
                description: "Array of available version strings".to_string(),
                required: true,
            },
            Parameter {
                name: "latest".to_string(),
                type_hint: "string".to_string(),
                description: "Latest version (optional)".to_string(),
                required: false,
            },
            Parameter {
                name: "release".to_string(),
                type_hint: "string".to_string(),
                description: "Latest release version (optional)".to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "send_maven_metadata",
            "group_id": "com.example",
            "artifact_id": "mylib",
            "versions": ["1.0.0", "1.0.1", "1.1.0"],
            "latest": "1.1.0",
            "release": "1.1.0"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Maven metadata {group_id}:{artifact_id}")
                .with_debug(
                    "Maven send_maven_metadata: {group_id}:{artifact_id} versions={versions_len}",
                ),
        ),
    }
}

/// Action definition for send_maven_error (sync)
fn send_maven_error_action() -> ActionDefinition {
    ActionDefinition {
        name: "send_maven_error".to_string(),
        description: "Send an error response (typically 404 Not Found)".to_string(),
        parameters: vec![
            Parameter {
                name: "status".to_string(),
                type_hint: "number".to_string(),
                description: "HTTP status code (default: 404)".to_string(),
                required: false,
            },
            Parameter {
                name: "message".to_string(),
                type_hint: "string".to_string(),
                description: "Error message (default: 'Not Found')".to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "send_maven_error",
            "status": 404,
            "message": "Artifact not found"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Maven error {status}: {message}")
                .with_debug("Maven send_maven_error: status={status} message={message}"),
        ),
    }
}

// ============================================================================
// Maven Action Constants
// ============================================================================

pub static SEND_MAVEN_ARTIFACT_ACTION: LazyLock<ActionDefinition> =
    LazyLock::new(|| send_maven_artifact_action());
pub static SEND_MAVEN_METADATA_ACTION: LazyLock<ActionDefinition> =
    LazyLock::new(|| send_maven_metadata_action());
pub static SEND_MAVEN_ERROR_ACTION: LazyLock<ActionDefinition> =
    LazyLock::new(|| send_maven_error_action());

// ============================================================================
// Maven Event Type Constants
// ============================================================================

/// Maven artifact request event - triggered when client requests an artifact
pub static MAVEN_ARTIFACT_REQUEST_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "maven_artifact_request",
        "Maven artifact request received from client",
        json!({
            "type": "send_maven_artifact",
            "status": 200,
            "content_type": "application/xml",
            "body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project>\n  <modelVersion>4.0.0</modelVersion>\n  <groupId>com.example</groupId>\n  <artifactId>mylib</artifactId>\n  <version>1.0.0</version>\n</project>\n"
        }),
    )
    .with_parameters(vec![
        Parameter {
            name: "method".to_string(),
            type_hint: "string".to_string(),
            description: "HTTP method (usually GET)".to_string(),
            required: true,
        },
        Parameter {
            name: "uri".to_string(),
            type_hint: "string".to_string(),
            description: "Full request URI".to_string(),
            required: true,
        },
        Parameter {
            name: "group_id".to_string(),
            type_hint: "string".to_string(),
            description: "Maven group ID (e.g., 'com.example')".to_string(),
            required: true,
        },
        Parameter {
            name: "artifact_id".to_string(),
            type_hint: "string".to_string(),
            description: "Maven artifact ID".to_string(),
            required: true,
        },
        Parameter {
            name: "version".to_string(),
            type_hint: "string".to_string(),
            description: "Artifact version (null for metadata requests)".to_string(),
            required: false,
        },
        Parameter {
            name: "classifier".to_string(),
            type_hint: "string".to_string(),
            description: "Artifact classifier (e.g., 'sources', 'javadoc')".to_string(),
            required: false,
        },
        Parameter {
            name: "extension".to_string(),
            type_hint: "string".to_string(),
            description: "File extension (e.g., 'jar', 'pom', 'xml')".to_string(),
            required: true,
        },
        Parameter {
            name: "is_metadata".to_string(),
            type_hint: "boolean".to_string(),
            description: "True if requesting maven-metadata.xml".to_string(),
            required: true,
        },
        Parameter {
            name: "is_checksum".to_string(),
            type_hint: "boolean".to_string(),
            description: "True if requesting a checksum file".to_string(),
            required: true,
        },
        Parameter {
            name: "checksum_type".to_string(),
            type_hint: "string".to_string(),
            description: "Checksum type if is_checksum is true (sha1, md5, sha256, sha512)"
                .to_string(),
            required: false,
        },
        Parameter {
            name: "headers".to_string(),
            type_hint: "object".to_string(),
            description: "HTTP request headers".to_string(),
            required: true,
        },
    ])
    .with_actions(vec![
        SEND_MAVEN_ARTIFACT_ACTION.clone(),
        SEND_MAVEN_METADATA_ACTION.clone(),
        SEND_MAVEN_ERROR_ACTION.clone(),
    ])
});

/// Get Maven event types
pub fn get_maven_event_types() -> Vec<EventType> {
    vec![MAVEN_ARTIFACT_REQUEST_EVENT.clone()]
}