1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct CredentialRequest {
6 pub allow: Option<String>,
8 pub profile: Option<String>,
10 pub resource: Option<String>,
12 #[serde(default = "default_provider")]
14 pub provider: String,
15 #[serde(default = "default_ttl")]
17 pub ttl: String,
18 pub role_arn: Option<String>,
20 #[serde(default)]
22 pub command: Vec<String>,
23 pub agent_id: Option<String>,
25}
26
27fn default_provider() -> String {
28 "aws".to_string()
29}
30
31fn default_ttl() -> String {
32 "15m".to_string()
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct CredentialResponse {
38 pub session_id: String,
39 pub provider: String,
40 pub expires_at: String,
41 pub ttl_seconds: u64,
42 pub credentials: CredentialEnvVars,
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct CredentialEnvVars {
48 #[serde(skip_serializing_if = "Option::is_none")]
49 pub aws_access_key_id: Option<String>,
50 #[serde(skip_serializing_if = "Option::is_none")]
51 pub aws_secret_access_key: Option<String>,
52 #[serde(skip_serializing_if = "Option::is_none")]
53 pub aws_session_token: Option<String>,
54 #[serde(skip_serializing_if = "Option::is_none")]
55 pub gcp_access_token: Option<String>,
56 #[serde(skip_serializing_if = "Option::is_none")]
57 pub azure_token: Option<String>,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct ServerConfig {
63 #[serde(default = "default_port")]
65 pub port: u16,
66 #[serde(default = "default_bind")]
68 pub bind: String,
69 pub api_key: Option<String>,
71}
72
73fn default_port() -> u16 {
74 8080
75}
76
77fn default_bind() -> String {
78 "0.0.0.0".to_string()
79}
80
81impl Default for ServerConfig {
82 fn default() -> Self {
83 Self {
84 port: default_port(),
85 bind: default_bind(),
86 api_key: None,
87 }
88 }
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct ApiError {
94 pub error: String,
95 pub code: u16,
96}
97
98pub fn http_response(status: u16, status_text: &str, content_type: &str, body: &str) -> String {
100 format!(
101 "HTTP/1.1 {} {}\r\nContent-Type: {}\r\nContent-Length: {}\r\nAccess-Control-Allow-Origin: *\r\n\r\n{}",
102 status, status_text, content_type, body.len(), body
103 )
104}
105
106pub fn json_ok(body: &str) -> String {
107 http_response(200, "OK", "application/json", body)
108}
109
110pub fn json_error(code: u16, message: &str) -> String {
111 let err = ApiError {
112 error: message.to_string(),
113 code,
114 };
115 let body = serde_json::to_string(&err)
116 .unwrap_or_else(|_| format!("{{\"error\":\"{}\",\"code\":{}}}", message, code));
117 let status_text = match code {
118 400 => "Bad Request",
119 401 => "Unauthorized",
120 404 => "Not Found",
121 405 => "Method Not Allowed",
122 500 => "Internal Server Error",
123 _ => "Error",
124 };
125 http_response(code, status_text, "application/json", &body)
126}
127
128pub fn parse_http_request(raw: &str) -> Option<(&str, &str, &str)> {
130 let mut lines = raw.split("\r\n");
131 let first_line = lines.next()?;
132 let mut parts = first_line.split_whitespace();
133 let method = parts.next()?;
134 let path = parts.next()?;
135
136 let body = raw.split("\r\n\r\n").nth(1).unwrap_or("");
138
139 Some((method, path, body))
140}
141
142#[cfg(test)]
143mod tests {
144 use super::*;
145
146 #[test]
147 fn test_parse_http_request_get() {
148 let raw = "GET /v1/sessions HTTP/1.1\r\nHost: localhost\r\n\r\n";
149 let (method, path, body) = parse_http_request(raw).unwrap();
150 assert_eq!(method, "GET");
151 assert_eq!(path, "/v1/sessions");
152 assert_eq!(body, "");
153 }
154
155 #[test]
156 fn test_parse_http_request_post() {
157 let raw = "POST /v1/credentials HTTP/1.1\r\nContent-Type: application/json\r\n\r\n{\"allow\":\"s3:GetObject\"}";
158 let (method, path, body) = parse_http_request(raw).unwrap();
159 assert_eq!(method, "POST");
160 assert_eq!(path, "/v1/credentials");
161 assert_eq!(body, "{\"allow\":\"s3:GetObject\"}");
162 }
163
164 #[test]
165 fn test_json_ok() {
166 let resp = json_ok("{\"status\":\"ok\"}");
167 assert!(resp.contains("200 OK"));
168 assert!(resp.contains("application/json"));
169 }
170
171 #[test]
172 fn test_json_error() {
173 let resp = json_error(400, "bad request");
174 assert!(resp.contains("400 Bad Request"));
175 assert!(resp.contains("bad request"));
176 }
177
178 #[test]
179 fn test_credential_request_defaults() {
180 let json = r#"{"allow":"s3:GetObject"}"#;
181 let req: CredentialRequest = serde_json::from_str(json).unwrap();
182 assert_eq!(req.provider, "aws");
183 assert_eq!(req.ttl, "15m");
184 assert!(req.role_arn.is_none());
185 }
186
187 #[test]
188 fn test_server_config_default() {
189 let config = ServerConfig::default();
190 assert_eq!(config.port, 8080);
191 assert_eq!(config.bind, "0.0.0.0");
192 assert!(config.api_key.is_none());
193 }
194}