axonml-server 0.6.2

REST API server for AxonML Machine Learning Framework
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
//! Kaggle API endpoints for AxonML
//!
//! # File
//! `crates/axonml-server/src/api/kaggle.rs`
//!
//! # Author
//! Andrew Jewell Sr. — AutomataNexus LLC
//! ORCID: 0009-0005-2158-7060
//!
//! # Updated
//! April 14, 2026 11:15 PM EST
//!
//! # Disclaimer
//! Use at own risk. This software is provided "as is", without warranty of any
//! kind, express or implied. The author and AutomataNexus shall not be held
//! liable for any damages arising from the use of this software.

use axum::{
    Json,
    extract::{Query, State},
};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
use url::Url;

/// Hardcoded Kaggle API base URL - all requests must go to this host
const KAGGLE_API_BASE: &str = "https://www.kaggle.com/api/v1";

use crate::api::AppState;
use crate::auth::{AuthError, AuthUser};

// ============================================================================
// Request/Response Types
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KaggleCredentials {
    pub username: String,
    pub key: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KaggleStatusResponse {
    pub configured: bool,
    pub username: Option<String>,
    pub config_path: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KaggleDataset {
    pub ref_name: String,
    pub title: String,
    pub size: String,
    pub download_count: u64,
    pub vote_count: u64,
    pub last_updated: String,
    pub description: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KaggleSearchResponse {
    pub datasets: Vec<KaggleDataset>,
    pub total: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KaggleDownloadRequest {
    pub dataset_ref: String,
    pub output_dir: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KaggleDownloadResponse {
    pub dataset_ref: String,
    pub path: String,
    pub size_bytes: u64,
    pub files: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KaggleLocalDataset {
    pub filename: String,
    pub size_mb: f64,
    pub path: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct SearchQuery {
    pub query: String,
    pub limit: Option<usize>,
    pub page: Option<usize>,
}

// ============================================================================
// Handlers
// ============================================================================

/// Save Kaggle credentials
pub async fn save_credentials(
    State(_state): State<AppState>,
    user: AuthUser,
    Json(credentials): Json<KaggleCredentials>,
) -> Result<Json<KaggleStatusResponse>, AuthError> {
    // Validate non-empty credentials
    if credentials.username.trim().is_empty() || credentials.key.trim().is_empty() {
        return Err(AuthError::Forbidden(
            "Username and API key are required".to_string(),
        ));
    }

    // Validate credentials by making a test API call
    let client = Client::new();
    let validate_url = kaggle_url("datasets/list")?;
    let response = client
        .get(validate_url)
        .basic_auth(&credentials.username, Some(&credentials.key))
        .timeout(std::time::Duration::from_secs(10))
        .send()
        .await
        .map_err(|e| AuthError::Internal(format!("Failed to validate credentials: {}", e)))?;

    if !response.status().is_success() {
        return Err(AuthError::Forbidden(
            "Invalid Kaggle credentials".to_string(),
        ));
    }

    // Save credentials to user's config directory
    let config_dir = get_kaggle_config_dir(&user.id);
    fs::create_dir_all(&config_dir)
        .map_err(|e| AuthError::Internal(format!("Failed to create config directory: {}", e)))?;

    let config_path = config_dir.join("kaggle.json");
    let credentials_json = serde_json::to_string_pretty(&credentials)
        .map_err(|e| AuthError::Internal(e.to_string()))?;

    fs::write(&config_path, credentials_json)
        .map_err(|e| AuthError::Internal(format!("Failed to save credentials: {}", e)))?;

    // Set file permissions (Unix only)
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let permissions = fs::Permissions::from_mode(0o600);
        let _ = fs::set_permissions(&config_path, permissions);
    }

    Ok(Json(KaggleStatusResponse {
        configured: true,
        username: Some(credentials.username),
        config_path: config_path.to_string_lossy().to_string(),
    }))
}

/// Get Kaggle configuration status
pub async fn get_status(
    State(_state): State<AppState>,
    user: AuthUser,
) -> Result<Json<KaggleStatusResponse>, AuthError> {
    let config_path = get_kaggle_config_dir(&user.id).join("kaggle.json");

    if config_path.exists() {
        let content =
            fs::read_to_string(&config_path).map_err(|e| AuthError::Internal(e.to_string()))?;
        let credentials: KaggleCredentials =
            serde_json::from_str(&content).map_err(|e| AuthError::Internal(e.to_string()))?;

        Ok(Json(KaggleStatusResponse {
            configured: true,
            username: Some(credentials.username),
            config_path: config_path.to_string_lossy().to_string(),
        }))
    } else {
        Ok(Json(KaggleStatusResponse {
            configured: false,
            username: None,
            config_path: config_path.to_string_lossy().to_string(),
        }))
    }
}

/// Search Kaggle datasets
pub async fn search_datasets(
    State(_state): State<AppState>,
    user: AuthUser,
    Query(query): Query<SearchQuery>,
) -> Result<Json<KaggleSearchResponse>, AuthError> {
    let credentials = get_credentials(&user.id)?;

    let client = Client::new();
    let limit = query.limit.unwrap_or(10);
    let page = query.page.unwrap_or(1);

    // SECURITY: Build URL from validated base, append query params safely
    let mut search_url = kaggle_url("datasets/list")?;
    search_url
        .query_pairs_mut()
        .append_pair("search", &query.query)
        .append_pair("page", &page.to_string())
        .append_pair("pageSize", &limit.to_string());

    let response = client
        .get(search_url)
        .basic_auth(&credentials.username, Some(&credentials.key))
        .timeout(std::time::Duration::from_secs(30))
        .send()
        .await
        .map_err(|e| AuthError::Internal(format!("Kaggle API error: {}", e)))?;

    if !response.status().is_success() {
        return Err(AuthError::Internal("Kaggle API request failed".to_string()));
    }

    let kaggle_response: Vec<serde_json::Value> = response
        .json()
        .await
        .map_err(|e| AuthError::Internal(format!("Failed to parse Kaggle response: {}", e)))?;

    let datasets: Vec<KaggleDataset> = kaggle_response
        .into_iter()
        .map(|d| KaggleDataset {
            ref_name: d["ref"].as_str().unwrap_or("").to_string(),
            title: d["title"].as_str().unwrap_or("").to_string(),
            size: format_size(d["totalBytes"].as_u64().unwrap_or(0)),
            download_count: d["downloadCount"].as_u64().unwrap_or(0),
            vote_count: d["voteCount"].as_u64().unwrap_or(0),
            last_updated: d["lastUpdated"].as_str().unwrap_or("").to_string(),
            description: d["subtitle"].as_str().map(|s| s.to_string()),
        })
        .collect();

    let total = datasets.len();

    Ok(Json(KaggleSearchResponse { datasets, total }))
}

/// SECURITY: Validate dataset reference to prevent path traversal
fn validate_dataset_ref(dataset_ref: &str) -> Result<(), AuthError> {
    // Check for path traversal patterns
    if dataset_ref.contains("..") || dataset_ref.contains("./") || dataset_ref.starts_with('/') {
        return Err(AuthError::InvalidInput(
            "Invalid dataset reference: path traversal detected".to_string(),
        ));
    }
    // Only allow alphanumeric, hyphens, underscores, and single forward slash
    let valid = dataset_ref
        .chars()
        .all(|c| c.is_alphanumeric() || c == '-' || c == '_' || c == '/');
    if !valid {
        return Err(AuthError::InvalidInput(
            "Invalid dataset reference: contains invalid characters".to_string(),
        ));
    }
    // Must contain exactly one slash (owner/dataset format)
    if dataset_ref.matches('/').count() != 1 {
        return Err(AuthError::InvalidInput(
            "Invalid dataset reference: must be in format 'owner/dataset'".to_string(),
        ));
    }
    Ok(())
}

/// SECURITY: Validate that a path is within the allowed base directory
fn validate_path_within_base(
    path: &std::path::Path,
    base: &std::path::Path,
) -> Result<(), AuthError> {
    // Canonicalize both paths to resolve any .. or symlinks
    let canonical_base = base
        .canonicalize()
        .map_err(|_| AuthError::Internal("Failed to resolve base path".to_string()))?;

    // Create the directory first so we can canonicalize it
    fs::create_dir_all(path)
        .map_err(|e| AuthError::Internal(format!("Failed to create directory: {}", e)))?;

    let canonical_path = path
        .canonicalize()
        .map_err(|_| AuthError::Internal("Failed to resolve output path".to_string()))?;

    if !canonical_path.starts_with(&canonical_base) {
        tracing::warn!(
            path = %canonical_path.display(),
            base = %canonical_base.display(),
            "Path traversal attempt detected"
        );
        return Err(AuthError::InvalidInput(
            "Invalid output directory: path traversal detected".to_string(),
        ));
    }
    Ok(())
}

/// Download a Kaggle dataset
pub async fn download_dataset(
    State(_state): State<AppState>,
    user: AuthUser,
    Json(request): Json<KaggleDownloadRequest>,
) -> Result<Json<KaggleDownloadResponse>, AuthError> {
    // SECURITY: Validate dataset reference
    validate_dataset_ref(&request.dataset_ref)?;

    let credentials = get_credentials(&user.id)?;

    // Determine base directory (user's data directory)
    let base_dir = get_data_dir(&user.id);

    // Determine output directory
    let output_dir = if let Some(ref custom_dir) = request.output_dir {
        // SECURITY: Custom directory must be within user's data directory
        let requested = PathBuf::from(custom_dir);
        if requested.is_absolute() {
            return Err(AuthError::InvalidInput(
                "Absolute paths not allowed for output directory".to_string(),
            ));
        }
        base_dir.join(requested)
    } else {
        base_dir.clone()
    };

    // SECURITY: Validate the output path is within allowed base
    fs::create_dir_all(&base_dir)
        .map_err(|e| AuthError::Internal(format!("Failed to create base directory: {}", e)))?;
    validate_path_within_base(&output_dir, &base_dir)?;

    // Download dataset
    let client = Client::new();
    // SECURITY: dataset_ref is validated above, build URL from trusted base
    let download_url = kaggle_url(&format!("datasets/download/{}", request.dataset_ref))?;

    let response = client
        .get(download_url)
        .basic_auth(&credentials.username, Some(&credentials.key))
        .timeout(std::time::Duration::from_secs(300))
        .send()
        .await
        .map_err(|e| AuthError::Internal(format!("Download failed: {}", e)))?;

    if !response.status().is_success() {
        return Err(AuthError::Internal(format!(
            "Download failed with status: {}",
            response.status()
        )));
    }

    // Save to file - SECURITY: filename is derived from validated dataset_ref
    let filename = request.dataset_ref.replace('/', "_") + ".zip";
    let output_path = output_dir.join(&filename);

    let bytes = response
        .bytes()
        .await
        .map_err(|e| AuthError::Internal(format!("Failed to read response: {}", e)))?;

    let size_bytes = bytes.len() as u64;

    fs::write(&output_path, &bytes)
        .map_err(|e| AuthError::Internal(format!("Failed to save file: {}", e)))?;

    Ok(Json(KaggleDownloadResponse {
        dataset_ref: request.dataset_ref,
        path: output_path.to_string_lossy().to_string(),
        size_bytes,
        files: vec![filename],
    }))
}

/// List downloaded Kaggle datasets
pub async fn list_downloaded(
    State(_state): State<AppState>,
    user: AuthUser,
) -> Result<Json<Vec<KaggleLocalDataset>>, AuthError> {
    let data_dir = get_data_dir(&user.id);

    let mut datasets = Vec::new();

    if data_dir.exists() {
        if let Ok(entries) = fs::read_dir(&data_dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                if path.extension().map(|e| e == "zip").unwrap_or(false) {
                    if let Ok(meta) = entry.metadata() {
                        let size_mb = meta.len() as f64 / (1024.0 * 1024.0);
                        datasets.push(KaggleLocalDataset {
                            filename: entry.file_name().to_string_lossy().to_string(),
                            size_mb,
                            path: path.to_string_lossy().to_string(),
                        });
                    }
                }
            }
        }
    }

    Ok(Json(datasets))
}

/// Delete Kaggle credentials
pub async fn delete_credentials(
    State(_state): State<AppState>,
    user: AuthUser,
) -> Result<Json<KaggleStatusResponse>, AuthError> {
    let config_path = get_kaggle_config_dir(&user.id).join("kaggle.json");

    if config_path.exists() {
        fs::remove_file(&config_path)
            .map_err(|e| AuthError::Internal(format!("Failed to remove credentials: {}", e)))?;
    }

    Ok(Json(KaggleStatusResponse {
        configured: false,
        username: None,
        config_path: config_path.to_string_lossy().to_string(),
    }))
}

// ============================================================================
// Helper Functions
// ============================================================================

/// SECURITY: Build a Kaggle API URL from a path, ensuring it always points to kaggle.com.
fn kaggle_url(path: &str) -> Result<Url, AuthError> {
    let base = Url::parse(KAGGLE_API_BASE)
        .map_err(|e| AuthError::Internal(format!("Invalid Kaggle base URL: {}", e)))?;
    let url = base
        .join(path)
        .map_err(|e| AuthError::Internal(format!("Invalid Kaggle API path: {}", e)))?;

    // SECURITY: Verify the final URL still points to kaggle.com
    if url.host_str() != Some("www.kaggle.com") {
        return Err(AuthError::Internal(
            "URL does not point to kaggle.com".to_string(),
        ));
    }

    Ok(url)
}

fn get_kaggle_config_dir(user_id: &str) -> PathBuf {
    dirs::data_dir()
        .unwrap_or_else(|| PathBuf::from("."))
        .join("axonml")
        .join("users")
        .join(user_id)
        .join(".kaggle")
}

fn get_data_dir(user_id: &str) -> PathBuf {
    dirs::data_dir()
        .unwrap_or_else(|| PathBuf::from("."))
        .join("axonml")
        .join("users")
        .join(user_id)
        .join("data")
}

fn get_credentials(user_id: &str) -> Result<KaggleCredentials, AuthError> {
    let config_path = get_kaggle_config_dir(user_id).join("kaggle.json");

    if !config_path.exists() {
        return Err(AuthError::Forbidden(
            "Kaggle credentials not configured. Please configure your Kaggle API credentials first.".to_string()
        ));
    }

    let content = fs::read_to_string(&config_path)
        .map_err(|e| AuthError::Internal(format!("Failed to read credentials: {}", e)))?;

    serde_json::from_str(&content)
        .map_err(|e| AuthError::Internal(format!("Invalid credentials file: {}", e)))
}

fn format_size(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;

    if bytes >= GB {
        format!("{:.2} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.2} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.2} KB", bytes as f64 / KB as f64)
    } else {
        format!("{} B", bytes)
    }
}