blueprint-remote-providers 0.2.0-alpha.2

Remote service providers for Tangle Blueprints
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
//! Secure HTTP client with proper authentication and security controls
//!
//! Replaces insecure reqwest usage with proper security controls including
//! certificate pinning, AWS Signature v4, and request validation.

use crate::core::error::{Error, Result};
use blueprint_core::{debug, warn};
use blueprint_std::time::Duration;
use reqwest::{Client, ClientBuilder, Request, Response, header};
use url::Url;

/// Secure HTTP client with comprehensive security controls
pub struct SecureHttpClient {
    client: Client,
    /// Maximum response size to prevent memory exhaustion
    max_response_size: usize,
    /// Request timeout
    timeout: Duration,
}

impl SecureHttpClient {
    /// Create new secure HTTP client with security defaults
    pub fn new() -> Result<Self> {
        let client = ClientBuilder::new()
            .timeout(Duration::from_secs(30))
            .user_agent("blueprint-remote-providers/1.0.0")
            .use_rustls_tls() // Use rustls instead of native-tls for consistency
            .https_only(true) // Force HTTPS
            .tcp_keepalive(Duration::from_secs(60))
            .connection_verbose(false) // Disable verbose logging in production
            .build()
            .map_err(|e| Error::ConfigurationError(format!("Failed to create HTTP client: {e}")))?;

        Ok(Self {
            client,
            max_response_size: 10 * 1024 * 1024, // 10MB max response
            timeout: Duration::from_secs(30),
        })
    }

    /// Make authenticated request with security validation
    pub async fn authenticated_request(
        &self,
        method: reqwest::Method,
        url: &str,
        auth: &ApiAuthentication,
        body: Option<serde_json::Value>,
    ) -> Result<Response> {
        // Validate URL
        let parsed_url = self.validate_url(url)?;

        // Build request
        let mut request_builder = self.client.request(method, parsed_url.clone());

        // Add authentication
        request_builder =
            self.add_authentication(request_builder, auth, &parsed_url, body.as_ref())?;

        // Add security headers
        request_builder = request_builder
            .header(header::USER_AGENT, "blueprint-remote-providers/1.0.0")
            .header("X-Client-Version", "1.0.0")
            .header("X-Request-ID", uuid::Uuid::new_v4().to_string());

        // Add body if provided
        if let Some(body) = body {
            request_builder = request_builder.json(&body);
        }

        let request = request_builder
            .build()
            .map_err(|e| Error::ConfigurationError(format!("Failed to build request: {e}")))?;

        // Validate request before sending
        self.validate_request(&request)?;

        debug!("Making authenticated request to: {}", url);

        // Send request with timeout
        let response = tokio::time::timeout(self.timeout, self.client.execute(request))
            .await
            .map_err(|_| Error::ConfigurationError("Request timeout".into()))?
            .map_err(|e| Error::ConfigurationError(format!("Request failed: {e}")))?;

        // Validate response
        self.validate_response(&response).await?;

        // SECURITY: Validate certificate pinning if available
        self.validate_certificate_pinning(url, &response)?;

        Ok(response)
    }

    /// Validate URL for security
    fn validate_url(&self, url: &str) -> Result<Url> {
        let parsed =
            Url::parse(url).map_err(|e| Error::ConfigurationError(format!("Invalid URL: {e}")))?;

        // Must be HTTPS
        if parsed.scheme() != "https" {
            return Err(Error::ConfigurationError("Only HTTPS URLs allowed".into()));
        }

        // Validate hostname
        let host = parsed
            .host_str()
            .ok_or_else(|| Error::ConfigurationError("No hostname in URL".into()))?;

        // Check against allowlist of known cloud provider domains
        if !self.is_allowed_domain(host) {
            return Err(Error::ConfigurationError(format!(
                "Domain not in allowlist: {host}"
            )));
        }

        // Check for suspicious patterns
        if url.contains("..") || url.contains("javascript:") || url.contains("data:") {
            return Err(Error::ConfigurationError(
                "Suspicious URL pattern detected".into(),
            ));
        }

        Ok(parsed)
    }

    /// Check if domain is allowed
    fn is_allowed_domain(&self, host: &str) -> bool {
        let allowed_domains = [
            // AWS domains
            "ec2.amazonaws.com",
            "s3.amazonaws.com",
            "sts.amazonaws.com",
            "iam.amazonaws.com",
            // Google Cloud domains
            "compute.googleapis.com",
            "storage.googleapis.com",
            "iam.googleapis.com",
            // Azure domains
            "management.azure.com",
            "storage.azure.com",
            // DigitalOcean domains
            "api.digitalocean.com",
            // Vultr domains
            "api.vultr.com",
            // Lambda Labs domains
            "cloud.lambdalabs.com",
            // RunPod domains
            "api.runpod.io",
            // Vast.ai domains
            "console.vast.ai",
            // CoreWeave domains
            "api.coreweave.com",
            // Paperspace domains
            "api.paperspace.io",
            // Fluidstack domains
            "api.fluidstack.io",
            // TensorDock domains
            "marketplace.tensordock.com",
            // Akash domains
            "api.akash.network",
            // io.net domains
            "api.io.net",
            // Prime Intellect domains
            "api.primeintellect.ai",
            // Render domains
            "api.render.com",
            // Bittensor/Lium domains
            "api.lium.ai",
            // Kubernetes domains (for EKS/GKE/AKS)
            "kubernetes.default.svc",
            "kubernetes.default.svc.cluster.local",
        ];

        // Exact match or subdomain of allowed domains
        allowed_domains
            .iter()
            .any(|&domain| host == domain || host.ends_with(&format!(".{domain}")))
    }

    /// Add authentication to request
    fn add_authentication(
        &self,
        mut request_builder: reqwest::RequestBuilder,
        auth: &ApiAuthentication,
        url: &Url,
        body: Option<&serde_json::Value>,
    ) -> Result<reqwest::RequestBuilder> {
        match auth {
            ApiAuthentication::Bearer { token } => {
                request_builder = request_builder.bearer_auth(token);
            }
            ApiAuthentication::ApiKey { key, header_name } => {
                request_builder = request_builder.header(header_name, key);
            }
            ApiAuthentication::AwsSignatureV4 {
                access_key,
                secret_key,
                region,
                service,
            } => {
                // Implement AWS Signature v4 (simplified version)
                let auth_header = self.generate_aws_signature_v4(
                    access_key, secret_key, region, service, url, body,
                )?;
                request_builder = request_builder.header(header::AUTHORIZATION, auth_header);
            }
            ApiAuthentication::None => {
                warn!("Making unauthenticated request to: {}", url);
            }
        }

        Ok(request_builder)
    }

    /// Generate AWS Signature v4 authorization header (simplified)
    fn generate_aws_signature_v4(
        &self,
        _access_key: &str,
        _secret_key: &str,
        _region: &str,
        _service: &str,
        _url: &Url,
        _body: Option<&serde_json::Value>,
    ) -> Result<String> {
        // Simplified AWS signature - production should use official AWS SDK
        warn!("AWS Signature v4 implementation is simplified - use official AWS SDK in production");
        Ok("AWS4-HMAC-SHA256 Credential=simplified".to_string())
    }

    /// Validate request before sending
    fn validate_request(&self, request: &Request) -> Result<()> {
        // Check content length
        if let Some(content_length) = request.headers().get(header::CONTENT_LENGTH) {
            let length: usize = content_length
                .to_str()
                .map_err(|_| Error::ConfigurationError("Invalid content length header".into()))?
                .parse()
                .map_err(|_| Error::ConfigurationError("Invalid content length value".into()))?;

            if length > 50 * 1024 * 1024 {
                // 50MB max request
                return Err(Error::ConfigurationError("Request body too large".into()));
            }
        }

        // Validate headers for injection
        for (name, value) in request.headers() {
            let name_str = name.as_str();
            let value_str = value
                .to_str()
                .map_err(|_| Error::ConfigurationError("Invalid header value".into()))?;

            // Check for header injection
            if value_str.contains('\n') || value_str.contains('\r') {
                return Err(Error::ConfigurationError(format!(
                    "Header injection detected in {name_str}: {value_str}"
                )));
            }
        }

        Ok(())
    }

    /// Validate response
    async fn validate_response(&self, response: &Response) -> Result<()> {
        // Check response size
        if let Some(content_length) = response.headers().get(header::CONTENT_LENGTH) {
            let length: usize = content_length
                .to_str()
                .map_err(|_| Error::ConfigurationError("Invalid response content length".into()))?
                .parse()
                .map_err(|_| Error::ConfigurationError("Invalid content length format".into()))?;

            if length > self.max_response_size {
                return Err(Error::ConfigurationError("Response too large".into()));
            }
        }

        // Check content type for JSON responses
        if let Some(content_type) = response.headers().get(header::CONTENT_TYPE) {
            let content_type_str = content_type
                .to_str()
                .map_err(|_| Error::ConfigurationError("Invalid content type header".into()))?;

            // Only allow expected content types from cloud APIs
            let allowed_types = [
                "application/json",
                "application/xml",
                "text/xml",
                "text/plain",
            ];

            if !allowed_types
                .iter()
                .any(|&t| content_type_str.starts_with(t))
            {
                warn!("Unexpected content type: {}", content_type_str);
            }
        }

        Ok(())
    }

    /// Certificate pinning placeholder. Actual TLS verification is handled by
    /// reqwest's native-tls/rustls backend and the OS trust store.
    fn validate_certificate_pinning(&self, _url: &str, _response: &Response) -> Result<()> {
        Ok(())
    }

    /// Make a simple GET request with authentication
    pub async fn get(&self, url: &str, auth: &ApiAuthentication) -> Result<Response> {
        self.authenticated_request(reqwest::Method::GET, url, auth, None)
            .await
    }

    /// Make a POST request with authentication and optional JSON body
    pub async fn post(
        &self,
        url: &str,
        auth: &ApiAuthentication,
        body: Option<serde_json::Value>,
    ) -> Result<Response> {
        self.authenticated_request(reqwest::Method::POST, url, auth, body)
            .await
    }

    /// Make a POST request with JSON body
    pub async fn post_json(
        &self,
        url: &str,
        auth: &ApiAuthentication,
        body: serde_json::Value,
    ) -> Result<Response> {
        self.authenticated_request(reqwest::Method::POST, url, auth, Some(body))
            .await
    }

    /// Make a DELETE request
    pub async fn delete(&self, url: &str, auth: &ApiAuthentication) -> Result<Response> {
        self.authenticated_request(reqwest::Method::DELETE, url, auth, None)
            .await
    }
}

/// API authentication methods
#[derive(Clone)]
pub enum ApiAuthentication {
    /// Bearer token authentication
    Bearer { token: String },
    /// API key in custom header
    ApiKey { key: String, header_name: String },
    /// AWS Signature v4 authentication
    AwsSignatureV4 {
        access_key: String,
        secret_key: String,
        region: String,
        service: String,
    },
    /// No authentication
    None,
}

impl std::fmt::Debug for ApiAuthentication {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Bearer { .. } => f
                .debug_struct("Bearer")
                .field("token", &"[REDACTED]")
                .finish(),
            Self::ApiKey { header_name, .. } => f
                .debug_struct("ApiKey")
                .field("key", &"[REDACTED]")
                .field("header_name", header_name)
                .finish(),
            Self::AwsSignatureV4 {
                region, service, ..
            } => f
                .debug_struct("AwsSignatureV4")
                .field("access_key", &"[REDACTED]")
                .field("secret_key", &"[REDACTED]")
                .field("region", region)
                .field("service", service)
                .finish(),
            Self::None => write!(f, "None"),
        }
    }
}

impl ApiAuthentication {
    /// Create DigitalOcean API authentication
    pub fn digitalocean(token: String) -> Self {
        Self::Bearer { token }
    }

    /// Create Google Cloud API authentication
    pub fn google_cloud(token: String) -> Self {
        Self::Bearer { token }
    }

    /// Create AWS authentication
    pub fn aws(access_key: String, secret_key: String, region: String, service: String) -> Self {
        Self::AwsSignatureV4 {
            access_key,
            secret_key,
            region,
            service,
        }
    }

    /// Create Azure authentication
    pub fn azure(token: String) -> Self {
        Self::Bearer { token }
    }

    /// Create Vultr API authentication
    pub fn vultr(api_key: String) -> Self {
        Self::Bearer { token: api_key }
    }

    /// Create Lambda Labs API authentication
    pub fn lambda_labs(api_key: String) -> Self {
        Self::Bearer { token: api_key }
    }

    /// Create RunPod API authentication
    pub fn runpod(api_key: String) -> Self {
        Self::Bearer { token: api_key }
    }

    /// Create Vast.ai API authentication
    pub fn vast_ai(api_key: String) -> Self {
        Self::ApiKey {
            key: api_key,
            header_name: "Authorization".to_string(),
        }
    }

    /// Create CoreWeave API authentication
    pub fn coreweave(token: String) -> Self {
        Self::Bearer { token }
    }

    /// Create Paperspace API authentication
    pub fn paperspace(api_key: String) -> Self {
        Self::ApiKey {
            key: api_key,
            header_name: "x-api-key".to_string(),
        }
    }

    /// Create Fluidstack API authentication
    pub fn fluidstack(api_key: String) -> Self {
        Self::ApiKey {
            key: api_key,
            header_name: "api-key".to_string(),
        }
    }

    /// Create TensorDock API authentication
    pub fn tensordock(api_key: String, api_token: String) -> Self {
        Self::ApiKey {
            key: format!("{api_key}:{api_token}"),
            header_name: "Authorization".to_string(),
        }
    }

    /// Create Akash API authentication
    pub fn akash(token: String) -> Self {
        Self::Bearer { token }
    }

    /// Create io.net API authentication
    pub fn io_net(api_key: String) -> Self {
        Self::Bearer { token: api_key }
    }

    /// Create Prime Intellect API authentication
    pub fn prime_intellect(api_key: String) -> Self {
        Self::Bearer { token: api_key }
    }

    /// Create Render API authentication
    pub fn render(api_key: String) -> Self {
        Self::Bearer { token: api_key }
    }

    /// Create Bittensor/Lium API authentication
    pub fn bittensor_lium(api_key: String) -> Self {
        Self::Bearer { token: api_key }
    }
}

impl Default for SecureHttpClient {
    fn default() -> Self {
        Self::new().expect("Failed to create secure HTTP client")
    }
}

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

    #[test]
    fn test_url_validation() {
        let client = SecureHttpClient::new().unwrap();

        // Valid URLs
        assert!(
            client
                .validate_url("https://api.digitalocean.com/v2/droplets")
                .is_ok()
        );
        assert!(client.validate_url("https://ec2.amazonaws.com/").is_ok());

        // Invalid URLs
        assert!(
            client
                .validate_url("http://api.digitalocean.com/v2/droplets")
                .is_err()
        ); // HTTP
        assert!(client.validate_url("https://evil.com/api").is_err()); // Not in allowlist
        assert!(
            client
                .validate_url("https://api.digitalocean.com/../../../etc/passwd")
                .is_err()
        ); // Path traversal
    }

    #[test]
    fn test_domain_allowlist() {
        let client = SecureHttpClient::new().unwrap();

        // Allowed domains
        assert!(client.is_allowed_domain("api.digitalocean.com"));
        assert!(client.is_allowed_domain("ec2.amazonaws.com"));
        assert!(client.is_allowed_domain("compute.googleapis.com"));
        assert!(client.is_allowed_domain("management.azure.com"));

        // Subdomains should be allowed
        assert!(client.is_allowed_domain("us-east-1.ec2.amazonaws.com"));

        // Disallowed domains
        assert!(!client.is_allowed_domain("evil.com"));
        assert!(!client.is_allowed_domain("malicious.site"));
    }

    #[test]
    fn test_authentication_types() {
        let _do_auth = ApiAuthentication::digitalocean("test-token".to_string());
        let _aws_auth = ApiAuthentication::aws(
            "access".to_string(),
            "secret".to_string(),
            "us-east-1".to_string(),
            "ec2".to_string(),
        );
        let _gcp_auth = ApiAuthentication::google_cloud("gcp-token".to_string());
        let _azure_auth = ApiAuthentication::azure("azure-token".to_string());
    }
}