-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth.rs
187 lines (176 loc) · 7.87 KB
/
auth.rs
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
use crate::configuration::FailureMode;
use crate::data::{get_attribute, store_metadata};
use crate::envoy::{
Address, AttributeContext, AttributeContext_HttpRequest, AttributeContext_Peer,
AttributeContext_Request, CheckRequest, CheckResponse_oneof_http_response, Metadata,
SocketAddress, StatusCode,
};
use crate::service::grpc_message::{GrpcMessageResponse, GrpcMessageResult};
use crate::service::{GrpcResult, GrpcService};
use chrono::{DateTime, FixedOffset};
use log::{debug, warn};
use protobuf::well_known_types::Timestamp;
use protobuf::Message;
use proxy_wasm::hostcalls;
use proxy_wasm::types::{Bytes, MapType};
use std::collections::HashMap;
pub const AUTH_SERVICE_NAME: &str = "envoy.service.auth.v3.Authorization";
pub const AUTH_METHOD_NAME: &str = "Check";
pub struct AuthService;
impl AuthService {
pub fn request_message(ce_host: String) -> CheckRequest {
AuthService::build_check_req(ce_host)
}
pub fn response_message(res_body_bytes: &Bytes) -> GrpcMessageResult<GrpcMessageResponse> {
match Message::parse_from_bytes(res_body_bytes) {
Ok(res) => Ok(GrpcMessageResponse::Auth(res)),
Err(e) => Err(e),
}
}
fn build_check_req(ce_host: String) -> CheckRequest {
let mut auth_req = CheckRequest::default();
let mut attr = AttributeContext::default();
attr.set_request(AuthService::build_request());
attr.set_destination(AuthService::build_peer(
get_attribute::<String>(&"destination.address".into())
.expect("Error!")
.unwrap_or_default(),
get_attribute::<i64>(&"destination.port".into())
.expect("Error!")
.unwrap_or_default() as u32,
));
attr.set_source(AuthService::build_peer(
get_attribute::<String>(&"source.address".into())
.expect("Error!")
.unwrap_or_default(),
get_attribute::<i64>(&"source.port".into())
.expect("Error!")
.unwrap_or_default() as u32,
));
// the ce_host is the identifier for authorino to determine which authconfig to use
let context_extensions = HashMap::from([("host".to_string(), ce_host)]);
attr.set_context_extensions(context_extensions);
attr.set_metadata_context(Metadata::default());
auth_req.set_attributes(attr);
auth_req
}
fn build_request() -> AttributeContext_Request {
let mut request = AttributeContext_Request::default();
let mut http = AttributeContext_HttpRequest::default();
let headers: HashMap<String, String> = hostcalls::get_map(MapType::HttpRequestHeaders)
.expect("failed to retrieve HttpRequestHeaders from host")
.into_iter()
.collect();
http.set_host(
get_attribute::<String>(&"request.host".into())
.expect("Error!")
.unwrap_or_default(),
);
http.set_method(
get_attribute::<String>(&"request.method".into())
.expect("Error!")
.unwrap_or_default(),
);
http.set_scheme(
get_attribute::<String>(&"request.scheme".into())
.expect("Error!")
.unwrap_or_default(),
);
http.set_path(
get_attribute::<String>(&"request.path".into())
.expect("Error!")
.unwrap_or_default(),
);
http.set_protocol(
get_attribute::<String>(&"request.protocol".into())
.expect("Error!")
.unwrap_or_default(),
);
http.set_headers(headers);
request.set_time(
get_attribute(&"request.time".into())
.expect("Error!")
.map_or(Timestamp::new(), |date_time: DateTime<FixedOffset>| {
Timestamp {
nanos: date_time.timestamp_subsec_nanos() as i32,
seconds: date_time.timestamp(),
unknown_fields: Default::default(),
cached_size: Default::default(),
}
}),
);
request.set_http(http);
request
}
fn build_peer(host: String, port: u32) -> AttributeContext_Peer {
let mut peer = AttributeContext_Peer::default();
let mut address = Address::default();
let mut socket_address = SocketAddress::default();
socket_address.set_address(host);
socket_address.set_port_value(port);
address.set_socket_address(socket_address);
peer.set_address(address);
peer
}
pub fn process_auth_grpc_response(
auth_resp: GrpcMessageResponse,
failure_mode: FailureMode,
) -> Result<GrpcResult, StatusCode> {
if let GrpcMessageResponse::Auth(check_response) = auth_resp {
// store dynamic metadata in filter state
store_metadata(check_response.get_dynamic_metadata());
match check_response.http_response {
Some(CheckResponse_oneof_http_response::ok_response(ok_response)) => {
debug!("process_auth_grpc_response: received OkHttpResponse");
if !ok_response.get_response_headers_to_add().is_empty() {
panic!("process_auth_grpc_response: response contained response_headers_to_add which is unsupported!")
}
if !ok_response.get_headers_to_remove().is_empty() {
panic!("process_auth_grpc_response: response contained headers_to_remove which is unsupported!")
}
if !ok_response.get_query_parameters_to_set().is_empty() {
panic!("process_auth_grpc_response: response contained query_parameters_to_set which is unsupported!")
}
if !ok_response.get_query_parameters_to_remove().is_empty() {
panic!("process_auth_grpc_response: response contained query_parameters_to_remove which is unsupported!")
}
ok_response.get_headers().iter().for_each(|header| {
hostcalls::add_map_value(
MapType::HttpRequestHeaders,
header.get_header().get_key(),
header.get_header().get_value(),
)
.expect("failed to add_map_value to HttpRequestHeaders")
});
Ok(GrpcResult::default())
}
Some(CheckResponse_oneof_http_response::denied_response(denied_response)) => {
debug!("process_auth_grpc_response: received DeniedHttpResponse");
let mut response_headers = vec![];
let status_code = denied_response.get_status().code;
denied_response.get_headers().iter().for_each(|header| {
response_headers.push((
header.get_header().get_key(),
header.get_header().get_value(),
))
});
hostcalls::send_http_response(
status_code as u32,
response_headers,
Some(denied_response.get_body().as_ref()),
)
.expect("failed to send_http_response");
Err(status_code)
}
None => {
GrpcService::handle_error_on_grpc_response(failure_mode);
Err(StatusCode::InternalServerError)
}
}
} else {
warn!("not a GrpcMessageResponse::Auth(CheckResponse)!");
GrpcService::handle_error_on_grpc_response(failure_mode);
Err(StatusCode::InternalServerError)
}
}
}