|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use log::info; |
| 16 | +use proxy_wasm::traits::*; |
| 17 | +use proxy_wasm::types::*; |
| 18 | +use std::time::Duration; |
| 19 | + |
| 20 | +proxy_wasm::main! {{ |
| 21 | + proxy_wasm::set_log_level(LogLevel::Trace); |
| 22 | + proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(GrpcAuthRandom) }); |
| 23 | +}} |
| 24 | + |
| 25 | +struct GrpcAuthRandom; |
| 26 | + |
| 27 | +impl HttpContext for GrpcAuthRandom { |
| 28 | + fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action { |
| 29 | + match self.get_http_request_header("content-type") { |
| 30 | + Some(value) if value.starts_with("application/grpc") => {} |
| 31 | + _ => { |
| 32 | + // Reject non-gRPC clients. |
| 33 | + self.send_http_response( |
| 34 | + 503, |
| 35 | + vec![("Powered-By", "proxy-wasm")], |
| 36 | + Some(b"Service accessible only to gRPC clients.\n"), |
| 37 | + ); |
| 38 | + return Action::Pause; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + match self.get_http_request_header(":path") { |
| 43 | + Some(value) if value.starts_with("/grpc.reflection") => { |
| 44 | + // Always allow gRPC calls to the reflection API. |
| 45 | + Action::Continue |
| 46 | + } |
| 47 | + _ => { |
| 48 | + // Allow other gRPC calls based on the result of grpcbin.GRPCBin/RandomError. |
| 49 | + self.dispatch_grpc_call( |
| 50 | + "grpcbin", |
| 51 | + "grpcbin.GRPCBin", |
| 52 | + "RandomError", |
| 53 | + vec![], |
| 54 | + None, |
| 55 | + Duration::from_secs(1), |
| 56 | + ) |
| 57 | + .unwrap(); |
| 58 | + Action::Pause |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + fn on_http_response_headers(&mut self, _: usize, _: bool) -> Action { |
| 64 | + self.set_http_response_header("Powered-By", Some("proxy-wasm")); |
| 65 | + Action::Continue |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl Context for GrpcAuthRandom { |
| 70 | + fn on_grpc_call_response(&mut self, _: u32, status_code: u32, _: usize) { |
| 71 | + if status_code % 2 == 0 { |
| 72 | + info!("Access granted."); |
| 73 | + self.resume_http_request(); |
| 74 | + } else { |
| 75 | + info!("Access forbidden."); |
| 76 | + self.send_grpc_response( |
| 77 | + GrpcStatusCode::Aborted, |
| 78 | + Some("Aborted by Proxy-Wasm!"), |
| 79 | + vec![("Powered-By", b"proxy-wasm")], |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments