Skip to content

Commit 61cbf25

Browse files
author
Devdutt Shenoi
committed
refactor
1 parent 0819601 commit 61cbf25

File tree

2 files changed

+26
-40
lines changed

2 files changed

+26
-40
lines changed

src/audit.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ const DROP_HEADERS: [&str; 4] = ["authorization", "cookie", "user-agent", "x-p-s
160160

161161
pub struct AuditLogBuilder {
162162
version: AuditLogVersion,
163-
pub deployment_id: Ulid,
163+
deployment_id: Ulid,
164164
audit_id: Ulid,
165165
start_time: DateTime<Utc>,
166-
pub stream: String,
166+
stream: String,
167167
pub actor: ActorLog,
168168
pub request: RequestLog,
169169
pub response: ResponseLog,
@@ -198,7 +198,23 @@ impl AuditLogBuilder {
198198
}
199199

200200
pub fn update_from_http(&mut self, req: &mut ServiceRequest) {
201-
let (username, authorization_method) = get_auth_details(req);
201+
let mut username = "Unknown".to_owned();
202+
let mut authorization_method = "None".to_owned();
203+
204+
// Extract authorization details from request, either from basic auth
205+
// header or cookie, else use default value.
206+
if let Ok(creds) = req.extract::<BasicAuth>().into_inner() {
207+
username = creds.user_id().trim().to_owned();
208+
authorization_method = "Basic Auth".to_owned();
209+
} else if let Some(cookie) = req.cookie("session") {
210+
authorization_method = "Session Cookie".to_owned();
211+
if let Some(user_id) = Ulid::from_string(cookie.value())
212+
.ok()
213+
.and_then(|ulid| Users.get_username_from_session(&SessionKey::SessionId(ulid)))
214+
{
215+
username = user_id;
216+
}
217+
}
202218

203219
let conn = req.connection_info();
204220
self.request = RequestLog {
@@ -235,27 +251,6 @@ impl AuditLogBuilder {
235251
}
236252
}
237253

238-
fn get_auth_details(req: &mut ServiceRequest) -> (String, String) {
239-
let mut username = "Unknown".to_owned();
240-
let mut auth_method = "None".to_owned();
241-
242-
if let Ok(creds) = req.extract::<BasicAuth>().into_inner() {
243-
return (creds.user_id().trim().to_owned(), "Basic Auth".to_owned());
244-
}
245-
246-
if let Some(cookie) = req.cookie("session") {
247-
auth_method = "Session Cookie".to_owned();
248-
if let Some(user_id) = Ulid::from_string(cookie.value())
249-
.ok()
250-
.and_then(|ulid| Users.get_username_from_session(&SessionKey::SessionId(ulid)))
251-
{
252-
username = user_id;
253-
}
254-
}
255-
256-
(username, auth_method)
257-
}
258-
259254
impl Drop for AuditLogBuilder {
260255
fn drop(&mut self) {
261256
let audit_json = json!({

src/handlers/http/middleware.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,8 @@ where
139139
140140
## Section start */
141141
let mut stream_name = None;
142-
if let Some((_, kinesis_common_attributes)) = req
143-
.request()
144-
.headers()
145-
.iter()
146-
.find(|&(key, _)| key == KINESIS_COMMON_ATTRIBUTES_KEY)
142+
if let Some(kinesis_common_attributes) =
143+
req.request().headers().get(KINESIS_COMMON_ATTRIBUTES_KEY)
147144
{
148145
let attribute_value: &str = kinesis_common_attributes.to_str().unwrap();
149146
let message: Message = serde_json::from_str(attribute_value).unwrap();
@@ -159,14 +156,12 @@ where
159156
HeaderName::from_static(LOG_SOURCE_KEY),
160157
header::HeaderValue::from_static(LOG_SOURCE_KINESIS),
161158
);
162-
stream_name = Some(message.common_attributes.x_p_stream);
163-
}
164-
165-
if let Some(stream) = req.match_info().get("logstream") {
166-
stream_name = Some(stream.to_owned());
159+
stream_name.replace(message.common_attributes.x_p_stream);
160+
} else if let Some(stream) = req.match_info().get("logstream") {
161+
stream_name.replace(stream.to_owned());
167162
} else if let Some(value) = req.headers().get(STREAM_NAME_HEADER_KEY) {
168163
if let Ok(stream) = value.to_str() {
169-
stream_name = Some(stream.to_owned())
164+
stream_name.replace(stream.to_owned());
170165
}
171166
}
172167

@@ -223,11 +218,7 @@ pub fn auth_stream_context(
223218
let creds = extract_session_key(req);
224219
let mut stream = req.match_info().get("logstream");
225220
if stream.is_none() {
226-
if let Some((_, stream_name)) = req
227-
.headers()
228-
.iter()
229-
.find(|&(key, _)| key == STREAM_NAME_HEADER_KEY)
230-
{
221+
if let Some(stream_name) = req.headers().get(STREAM_NAME_HEADER_KEY) {
231222
stream = Some(stream_name.to_str().unwrap());
232223
}
233224
}

0 commit comments

Comments
 (0)