├── .gitignore ├── LICENSE ├── README.md ├── custom-headers ├── README.md ├── build_envoy_docker.sh ├── build_run_all.sh ├── clean_envoy_docker.sh ├── envoy.Dockerfile ├── img │ └── envoy_network.png ├── service-envoy-mac.yaml ├── service-envoy.yaml └── start_envoy.sh ├── explicit-proxy-config ├── README.md ├── set_proxy.sh └── unset_proxy.sh ├── ext-authz-proxy ├── README.md ├── build_envoy_docker.sh ├── clean_envoy_docker.sh ├── envoy.Dockerfile ├── go.mod ├── go.sum ├── img │ └── envoy_network.png ├── main.go ├── service-envoy-mac.yaml ├── service-envoy.yaml └── start_envoy.sh ├── forward-proxy ├── README.md ├── build_envoy_docker.sh ├── clean_envoy_docker.sh ├── clean_iptables.sh ├── create_iptables.sh ├── envoy.Dockerfile ├── img │ └── envoy_network.png ├── service-envoy.yaml ├── show_iptables.sh └── start_envoy.sh ├── original-dst ├── README.md ├── build_envoy_docker.sh ├── clean_envoy_docker.sh ├── clean_iptables.sh ├── create_iptables.sh ├── envoy.Dockerfile ├── img │ └── envoy_network_original_dst.png ├── original_destination.py ├── service-envoy.yaml ├── show_iptables.sh └── start_envoy.sh ├── resources.md ├── simple-front-proxy ├── README.md ├── build_envoy_docker.sh ├── build_run_all.sh ├── clean_envoy_docker.sh ├── clean_web_docker.sh ├── envoy.Dockerfile ├── run_web_docker.sh ├── service-envoy-mac.yaml ├── service-envoy.yaml └── start_envoy.sh ├── simple-go-server ├── LICENSE ├── README.md ├── build_docker.sh ├── go.mod ├── main.go ├── server.Dockerfile └── simple-go-server ├── tcp-proxy ├── README.md ├── build_envoy_docker.sh ├── clean_envoy_docker.sh ├── clean_web_docker.sh ├── envoy.Dockerfile ├── run_web_docker.sh ├── service-envoy-mac.yaml ├── service-envoy.yaml └── start_envoy.sh ├── tproxy-aws-identity ├── README.md ├── build_envoy_docker_net_admin.sh ├── clean_envoy_docker.sh ├── clean_iptables.sh ├── clean_web_docker.sh ├── cmd │ └── ext-auth │ │ ├── main.go │ │ └── netstat.go ├── create_iptables.sh ├── envoy.Dockerfile ├── go.mod ├── go.sum ├── img │ ├── aws_change_ip_src_dest_check.png │ └── envoy_network.png ├── run_web_docker.sh ├── service-envoy.yaml ├── show_iptables.sh ├── start_envoy.sh └── tproxy_install.sh ├── tproxy-outgoing ├── README.md ├── build_envoy_docker_net_admin.sh ├── clean_envoy_docker.sh ├── clean_iptables.sh ├── clean_web_docker.sh ├── create_iptables.sh ├── envoy.Dockerfile ├── img │ ├── aws_change_ip_src_dest_check.png │ └── envoy_network.png ├── run_web_docker.sh ├── service-envoy.yaml ├── start_envoy.sh └── tproxy_install.sh ├── workload-identity-aws ├── README.md ├── build_envoy_docker.sh ├── clean_envoy_docker.sh ├── envoy.Dockerfile ├── go.mod ├── go.sum ├── img │ └── envoy_network.png ├── main.go ├── netstat.go ├── service-envoy-mac.yaml ├── service-envoy.yaml └── start_envoy.sh └── workload-identity ├── README.md ├── build_envoy_docker.sh ├── clean_envoy_docker.sh ├── envoy.Dockerfile ├── go.mod ├── go.sum ├── img └── envoy_network.png ├── main.go ├── netstat.go ├── service-envoy-mac.yaml ├── service-envoy.yaml └── start_envoy.sh /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all 2 | * 3 | 4 | # Unignore all with extensions 5 | !*.* 6 | 7 | # Unignore all dirs 8 | !*/ 9 | 10 | ### Above combination will ignore all files without extension ### 11 | 12 | 13 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 14 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 15 | 16 | # User-specific stuff 17 | .idea/ 18 | venv/ 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [2019] [Reinaldo Penno] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Envoy Proxy Big Book of Examples 2 | 3 | I had a dream! I tried to understand Kubernetes/Istio's data plane in detail...unsuccessfully. Every time I would ask a deeper question the answer would inevitably be "oh, this is done by Envoy and IPTables, but I do not know what goes under the hood". 4 | 5 | Therefore it became clear that understanding Envoy and the IPTables associated with each scenario was key to understanding data plane and provisioning in Kubernetes. During this journey it dawned on me how amazing is Envoy Proxy. 6 | 7 | I decide to compile each example as best as I could to help others going through the same pains since documentation is at times fragmented and incomplete. 8 | 9 | Please be aware that all these examples were tested on **AWS Ubuntu 18.04.03** 10 | 11 | ## Envoy Resources 12 | 13 | Started a new document on Envoy Resources such as [videos](resources.md) 14 | 15 | *Notice: Updating Examples for Envoy 1.13.0* 16 | 17 | ## 1. Examples: 18 | 19 | ### 1.1 [Forward Proxy](./forward-proxy) 20 | 21 | In this example of we run a [Forward Envoy Proxy](https://www.envoyproxy.io/docs/envoy/v1.13.0/configuration/http/http_filters/dynamic_forward_proxy_filter) that listens on port 4999 and directs requests to their original destination. 22 | 23 | The practical use-case is to confine applications running on the same host as the envoy proxy by using a combination of forward proxy and IPTables rules. 24 | 25 | ### 1.2 [Explicit Proxy Config](./explicit-proxy-config) 26 | 27 | This example uses the same container as the Envoy Forward Proxy example but instead of using IPTables to redirect packets, we explicitly set HTTP Proxy environment variables. 28 | 29 | ### 1.3 [Original Destination](./original-dst) 30 | 31 | This tutorial is one of the most interesting to me because it lays the ground work to understand workload identification and policy by creating an administrative boundary around an application 32 | 33 | This tutorial shows how to use a [original destination cluster](https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/upstream/service_discovery#arch-overview-service-discovery-types-original-destination) to run Envoy Proxy as a forward proxy. There is no need to explicitly configure cluster IP addresses and ports since Envoy will proxy connections to the original destination IP:port 34 | 35 | 36 | ### 1.4 [Transparent Proxy (TPROXY)](./tproxy-outgoing) 37 | 38 | Certainly the more challenging example but one that does not require changes to client applications. 39 | 40 | This tutorial shows how to use Envoy in [Transparent Proxy](https://www.envoyproxy.io/docs/envoy/latest/api-v2/api/v2/lds.proto#envoy-api-field-listener-transparent) mode. The distinguishing feature in this scenario is that **there is no NAT**. 41 | 42 | Transparent Proxy or TPROXY is a Linux Kernel feature without a lot of documentation. The common referenced documentation is the [original feature write-up](https://www.kernel.org/doc/Documentation/networking/tproxy.txt) 43 | 44 | ### 1.5 [Simple Front Proxy](./simple-front-proxy) 45 | 46 | In this example of we run a Envoy Proxy on that listens on port 4999 and directs to a server running on port 5000. 47 | 48 | The web server runs as a separate container from Envoy so any web server will do as long as it is listening on port 5000. 49 | 50 | ### 1.6 [External Authorization](./ext-authz-proxy) 51 | 52 | This example shows Envoy proxy using an external authorization server to decide whether requests should be forwarded. This has quite a few practical applications such as: 53 | 54 | * Client Identity (JWT) 55 | * Workload Identity 56 | * Policy Enforcement 57 | * In-depth statistics 58 | 59 | This is example is based on the [rate limit example](https://github.com/jbarratt/envoy_ratelimit_example) 60 | 61 | 62 | ### 1.7 [Custom HTTP Headers](./custom-headers) 63 | 64 | This example shows Envoy proxy adding custom HTTP headers to a request. I wanted to understand how to add more than one header and also append to an existing header. It turns out Envoy appends by adding a copy of the header with a different value. 65 | 66 | My goal is to use this setup in the external authz with workload identity. 67 | 68 | ### 1.8 [Workload-Identity](./workload-identity) 69 | 70 | This example demonstrates how to use Envoy Proxy and Authz server to create a soft boundary around an **existing** application in order to create or provide **workload identity**. The operative word here is **existing**. There are many practical applications such as: 71 | 72 | * Policy 73 | * Telemetry 74 | * Audit 75 | * Security 76 | 77 | ### 1.9 [Workload-Identity-AWS](./workload-identity-aws) 78 | 79 | This example demonstrates how to use Envoy Proxy and Authz server to create a soft boundary around an application in order to create or provide **workload identity** within an AWS EC2 deployment. 80 | 81 | More specifically, it integrates AWS EC2 instance and user metadata into the application identity. It seems clear to me that any serious workload identity solution needs to incorporate a cloud provider's information in order to be deployed seamlessly and provide useful information. 82 | 83 | ### 1.10 [Transparent Proxy (TPROXY) AWS Identity](./tproxy-aws-identity) 84 | 85 | WIP 86 | 87 | ### 1.11 [TCP-Proxy](./tcp-proxy) 88 | 89 | This example demonstrates just a plain TCP Proxy. Interestingly I could not find official documentation on how to configure it, but managed to dig an example from a [github issue](https://github.com/envoyproxy/envoy/issues/2891). 90 | 91 | Even more interesting is that a simple TCP Proxy can be used for HTTP traffic if there is no interest in HTTP specific functionality. Based on the logs we can see the amount of processing is much lower and therefore performance should be much higher. -------------------------------------------------------------------------------- /custom-headers/README.md: -------------------------------------------------------------------------------- 1 | # Tutorial 2 | 3 | **Tested on Ubuntu 18.04** 4 | 5 | In this example we will demonstrate how to add custom HTTP headers to a request. For that we run a Envoy Proxy on that listens on port 4999 and directs to a local server running on port 5000. 6 | 7 | We will use simple-go-server for this example since we want to show the HTTP headers received. 8 | 9 | ## 1. Network Diagram 10 | 11 | The HTTP Client (cURL), Envoy proxy and Web Server share the same host. cURL and Web Server run as a native applications while Envoy runs in a docker container 12 | 13 | ![You need to see the network diagram](./img/envoy_network.png) 14 | 15 | ## 2 Custom Headers 16 | 17 | We will (theoretically) add and append two different headers. Custom header documentation is [here](https://www.envoyproxy.io/docs/envoy/v1.13.0/configuration/http/http_conn_man/headers#custom-request-response-headers) 18 | 19 | Notice that Envoy's custom header nomenclature can be [confusing](https://github.com/envoyproxy/envoy/issues/8127) 20 | 21 | First under route config 22 | 23 | ``` 24 | request_headers_to_add: 25 | - header: 26 | key: "x-request-upstream" 27 | value: "%UPSTREAM_REMOTE_ADDRESS%" 28 | append: true 29 | - header: 30 | key: "x-request-downstream-combo" 31 | value: "%START_TIME(%Y/%m/%dT%H:%M:%S%z %s)%" 32 | append: true 33 | ``` 34 | 35 | Then append at virtual host level. 36 | 37 | ``` 38 | - header: 39 | key: "x-request-upstream" 40 | value: "%UPSTREAM_REMOTE_ADDRESS%" 41 | append: true 42 | - header: 43 | key: "x-request-downstream-combo" 44 | value: "%DOWNSTREAM_LOCAL_ADDRESS%" 45 | append: true 46 | ``` 47 | 48 | 49 | ## 3. Envoy Docker 50 | 51 | Build and run the envoy container. The container runs with *--network host* in order to reach the web server running on the host. 52 | 53 | ``` 54 | ./build_envoy_docker.sh 55 | ``` 56 | 57 | 58 | ## 4. Web Server 59 | 60 | In this example we used the simple-go-server because we want to show the HTTP headers received. 61 | 62 | Go to *simple-go-server* directory: 63 | 64 | ``` 65 | go build 66 | ./simple-go-server 67 | ``` 68 | 69 | ## 5. HTTP Request 70 | 71 | Now with the Web Server running issue the request. It will be processed by the Envoy Proxy container and directed to the web Server 72 | 73 | ``` 74 | ubuntu$ curl -v localhost:4999 75 | * Rebuilt URL to: localhost:4999/ 76 | * Trying ::1... 77 | * TCP_NODELAY set 78 | * Connected to localhost (::1) port 4999 (#0) 79 | > GET / HTTP/1.1 80 | > Host: localhost:4999 81 | > User-Agent: curl/7.54.0 82 | > Accept: */* 83 | > 84 | < HTTP/1.1 200 OK 85 | < content-type: text/plain; charset=utf-8 86 | < x-content-type-options: nosniff 87 | < x-request-id: 3cce8bc6-c471-46d6-8f6d-b86d6f278f29 88 | < date: Thu, 19 Sep 2019 06:00:58 GMT 89 | < content-length: 14 90 | < x-envoy-upstream-service-time: 2 91 | < server: envoy 92 | < 93 | Hello, World! 94 | * Connection #0 to host localhost left intact 95 | ``` 96 | 97 | ## 6. Web Server Logs 98 | 99 | These are the headers received by the web server. Notice the headers we added. You might have noticed that X-Request-Upstream is missing. Unfortunately I do not know why and opened a [bug](https://github.com/envoyproxy/envoy/issues/8127) for it. 100 | 101 | ``` 102 | $ ./simple-go-server 103 | http: 2019/09/18 22:56:16 Simple go server 104 | http: 2019/09/18 22:56:16 Version: 105 | http: 2019/09/18 22:56:16 GitTag: 106 | http: 2019/09/18 22:56:16 GitCommit: 107 | http: 2019/09/18 22:56:16 GitTreeState: 108 | http: 2019/09/18 22:56:16 Server is starting... 109 | http: 2019/09/18 22:56:16 Server is ready to handle requests at :5000 110 | 111 | HTTP Headers Received: 112 | ====================== 113 | Accept : */* 114 | X-Request-Id : 3cce8bc6-c471-46d6-8f6d-b86d6f278f29 115 | X-Request-Downstream : 172.17.0.2:4999 116 | X-Request-Downstream : 172.17.0.2:4999 117 | X-Request-Start-Time : 2019/09/19T06:00:59+0000 1568872859 118 | User-Agent : curl/7.54.0 119 | X-Forwarded-Proto : http 120 | X-Envoy-Expected-Rq-Timeout-Ms : 15000 121 | Content-Length : 0 122 | 123 | http: 2019/09/18 23:00:59 3cce8bc6-c471-46d6-8f6d-b86d6f278f29 GET / 127.0.0.1:64735 curl/7.54.0 124 | 125 | ``` 126 | 127 | ## 7. Envoy Logs 128 | 129 | Envoy Logs from a successful run. 130 | 131 | ``` 132 | [2019-09-19 06:00:55.403][8][debug][main] [source/server/server.cc:170] flushing stats 133 | [2019-09-19 06:00:59.220][18][debug][filter] [source/extensions/filters/listener/original_dst/original_dst.cc:18] original_dst: New connection accepted 134 | [2019-09-19 06:00:59.220][18][debug][main] [source/server/connection_handler_impl.cc:280] [C0] new connection 135 | [2019-09-19 06:00:59.221][18][debug][http] [source/common/http/conn_manager_impl.cc:246] [C0] new stream 136 | [2019-09-19 06:00:59.222][18][debug][http] [source/common/http/conn_manager_impl.cc:619] [C0][S16993900835688832754] request headers complete (end_stream=true): 137 | ':authority', 'localhost:4999' 138 | ':path', '/' 139 | ':method', 'GET' 140 | 'user-agent', 'curl/7.54.0' 141 | 'accept', '*/*' 142 | 143 | [2019-09-19 06:00:59.222][18][debug][http] [source/common/http/conn_manager_impl.cc:1111] [C0][S16993900835688832754] request end stream 144 | [2019-09-19 06:00:59.222][18][debug][router] [source/common/router/router.cc:401] [C0][S16993900835688832754] cluster 'cluster1' match for URL '/' 145 | [2019-09-19 06:00:59.223][18][debug][router] [source/common/router/router.cc:514] [C0][S16993900835688832754] router decoding headers: 146 | ':authority', 'localhost:4999' 147 | ':path', '/' 148 | ':method', 'GET' 149 | ':scheme', 'http' 150 | 'user-agent', 'curl/7.54.0' 151 | 'accept', '*/*' 152 | 'x-forwarded-proto', 'http' 153 | 'x-request-id', '3cce8bc6-c471-46d6-8f6d-b86d6f278f29' 154 | 'x-envoy-expected-rq-timeout-ms', '15000' 155 | 'x-request-downstream', '172.17.0.2:4999' 156 | 'x-request-downstream', '172.17.0.2:4999' 157 | 'x-request-start-time', '2019/09/19T06:00:59+0000 1568872859' 158 | 159 | [2019-09-19 06:00:59.223][18][debug][pool] [source/common/http/http1/conn_pool.cc:88] creating a new connection 160 | [2019-09-19 06:00:59.223][18][debug][client] [source/common/http/codec_client.cc:26] [C1] connecting 161 | [2019-09-19 06:00:59.223][18][debug][connection] [source/common/network/connection_impl.cc:704] [C1] connecting to 192.168.65.2:5000 162 | [2019-09-19 06:00:59.223][18][debug][connection] [source/common/network/connection_impl.cc:713] [C1] connection in progress 163 | [2019-09-19 06:00:59.223][18][debug][pool] [source/common/http/conn_pool_base.cc:20] queueing request due to no available connections 164 | [2019-09-19 06:00:59.224][18][debug][connection] [source/common/network/connection_impl.cc:552] [C1] connected 165 | [2019-09-19 06:00:59.224][18][debug][client] [source/common/http/codec_client.cc:64] [C1] connected 166 | [2019-09-19 06:00:59.224][18][debug][pool] [source/common/http/http1/conn_pool.cc:241] [C1] attaching to next request 167 | [2019-09-19 06:00:59.224][18][debug][router] [source/common/router/router.cc:1503] [C0][S16993900835688832754] pool ready 168 | [2019-09-19 06:00:59.225][18][debug][router] [source/common/router/router.cc:994] [C0][S16993900835688832754] upstream headers complete: end_stream=false 169 | [2019-09-19 06:00:59.225][18][debug][http] [source/common/http/conn_manager_impl.cc:1378] [C0][S16993900835688832754] encoding headers via codec (end_stream=false): 170 | ':status', '200' 171 | 'content-type', 'text/plain; charset=utf-8' 172 | 'x-content-type-options', 'nosniff' 173 | 'x-request-id', '3cce8bc6-c471-46d6-8f6d-b86d6f278f29' 174 | 'date', 'Thu, 19 Sep 2019 06:00:58 GMT' 175 | 'content-length', '14' 176 | 'x-envoy-upstream-service-time', '2' 177 | 'server', 'envoy' 178 | 179 | [2019-09-19 06:00:59.225][18][debug][client] [source/common/http/codec_client.cc:95] [C1] response complete 180 | [2019-09-19 06:00:59.226][18][debug][pool] [source/common/http/http1/conn_pool.cc:198] [C1] response complete 181 | [2019-09-19 06:00:59.226][18][debug][pool] [source/common/http/http1/conn_pool.cc:236] [C1] moving to ready 182 | [2019-09-19 06:00:59.228][18][debug][connection] [source/common/network/connection_impl.cc:520] [C0] remote close 183 | [2019-09-19 06:00:59.228][18][debug][connection] [source/common/network/connection_impl.cc:190] [C0] closing socket: 0 184 | [2019-09-19 06:00:59.228][18][debug][main] [source/server/connection_handler_impl.cc:80] [C0] adding to cleanup list 185 | [2019-09-19 06:01:00.395][8][debug][main] [source/server/server.cc:170] flushing stats 186 | [2019-09-19 06:01:05.395][8][debug][main] [source/server/server.cc:170] flushing stats 187 | [2019-09-19 06:01:10.365][8][debug][main] [source/server/server.cc:170] flushing stats 188 | [2019-09-19 06:01:14.212][18][debug][connection] [source/common/network/connection_impl.cc:520] [C1] remote close 189 | [2019-09-19 06:01:14.212][18][debug][connection] [source/common/network/connection_impl.cc:190] [C1] closing socket: 0 190 | [2019-09-19 06:01:14.212][18][debug][client] [source/common/http/codec_client.cc:82] [C1] disconnect. resetting 0 pending requests 191 | [2019-09-19 06:01:14.212][18][debug][pool] [source/common/http/http1/conn_pool.cc:129] [C1] client disconnected, failure reason: 192 | [2019-09-19 06:01:15.369][8][debug][main] [source/server/server.cc:170] flushing stats 193 | 194 | ``` 195 | ## 8. Cleaning 196 | 197 | ``` 198 | ./clean_envoy_docker.sh 199 | ``` 200 | 201 | Stop *simple-go-server*. CTRL-C will do (;-) 202 | -------------------------------------------------------------------------------- /custom-headers/build_envoy_docker.sh: -------------------------------------------------------------------------------- 1 | # Enable exit on non 0 2 | set -e 3 | 4 | . clean_envoy_docker.sh 5 | 6 | if [ -z "${ENVOY_PORT}" ]; then 7 | PORT=4999 8 | else 9 | PORT="${ENVOY_PORT}" 10 | fi 11 | 12 | if [ -z "${ENVOY_ADMIN_PORT}" ]; then 13 | ADMIN_PORT=19000 14 | else 15 | ADMIN_PORT="${ENVOY_ADMIN_PORT}" 16 | fi 17 | 18 | CONTAINER_NAME=custom-headers 19 | DOCKERFILE=envoy.Dockerfile 20 | ENVOY_FILE=service-envoy.yaml 21 | 22 | if [[ "$OSTYPE" == "darwin"* ]]; then 23 | ENVOY_FILE=service-envoy-mac.yaml 24 | fi 25 | 26 | docker build -f ${DOCKERFILE} -t ${CONTAINER_NAME} . --build-arg envoy_file="${ENVOY_FILE}" 27 | 28 | if [[ "$OSTYPE" == "linux-gnu" ]]; then 29 | EXTRA_FLAGS="--cap-add=NET_ADMIN --network host" 30 | fi 31 | 32 | # NULL expansion 33 | # ${EXTRA_FLAGS:-"${EXTRA_FLAGS}"} 34 | 35 | DOCKER_COMMAND="docker run -d ${EXTRA_FLAGS:-${EXTRA_FLAGS}} -p \"${PORT}\":\"${PORT}\" -p \"${ADMIN_PORT}\":\"${ADMIN_PORT}\" --name \"${CONTAINER_NAME}\" \"${CONTAINER_NAME}\"" 36 | 37 | # printf "%s\n" "${DOCKER_COMMAND}" 38 | 39 | eval "${DOCKER_COMMAND}" 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /custom-headers/build_run_all.sh: -------------------------------------------------------------------------------- 1 | # Enable exit on non 0 2 | set -e 3 | 4 | printf "\n\n" 5 | printf "%s\n" "Building and running Envoy Docker" 6 | printf "%s\n" "=================================" 7 | 8 | . build_envoy_docker.sh 9 | 10 | printf "\n\n" 11 | -------------------------------------------------------------------------------- /custom-headers/clean_envoy_docker.sh: -------------------------------------------------------------------------------- 1 | # Enable exit on non 0 2 | set -e 3 | 4 | CONTAINER_NAME=custom-headers 5 | 6 | docker stop ${CONTAINER_NAME} 2> /dev/null || true 7 | docker rm ${CONTAINER_NAME} 2> /dev/null || true 8 | docker rmi -f ${CONTAINER_NAME} 2> /dev/null || true 9 | 10 | # https://stackoverflow.com/questions/15678796/suppress-shell-script-error-messages -------------------------------------------------------------------------------- /custom-headers/envoy.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM envoyproxy/envoy:v1.11.1 2 | 3 | EXPOSE 4999 4 | EXPOSE 19000 5 | EXPOSE 8443 6 | 7 | ARG envoy_file 8 | 9 | ENV DEBIAN_FRONTEND noninteractive 10 | 11 | RUN apt-get -qq update && \ 12 | apt-get -qq install \ 13 | apt-utils \ 14 | iputils-ping \ 15 | curl \ 16 | < /dev/null > /dev/null 17 | 18 | ADD ${envoy_file} /etc/service-envoy.yaml 19 | ADD ./start_envoy.sh /usr/local/bin/start_envoy.sh 20 | WORKDIR /usr/local/bin 21 | RUN chmod u+x start_envoy.sh 22 | ENTRYPOINT ./start_envoy.sh 23 | 24 | 25 | -------------------------------------------------------------------------------- /custom-headers/img/envoy_network.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BenderScript/envoybigbook/c02debcd2ac466e045775d22397a607a60ef0da6/custom-headers/img/envoy_network.png -------------------------------------------------------------------------------- /custom-headers/service-envoy-mac.yaml: -------------------------------------------------------------------------------- 1 | node: 2 | id: "id_identity" 3 | cluster: "cluster_identity" 4 | static_resources: 5 | listeners: 6 | - name: listener_0 7 | transparent: true 8 | address: 9 | socket_address: 10 | protocol: TCP 11 | address: 0.0.0.0 12 | port_value: 4999 13 | filter_chains: 14 | - filters: 15 | - name: envoy.http_connection_manager 16 | config: 17 | idle_timeout: 1s 18 | codec_type: auto 19 | access_log: 20 | - name: envoy.file_access_log 21 | config: 22 | path: "/tmp/envoy-access-4999.log" 23 | stat_prefix: ingress_http 24 | route_config: 25 | name: local_route 26 | request_headers_to_add: 27 | - header: 28 | key: "x-request-downstream" 29 | value: "%DOWNSTREAM_LOCAL_ADDRESS%" 30 | - header: 31 | key: "x-request-upstream" 32 | value: "%UPSTREAM_REMOTE_ADDRESS%" 33 | append: true 34 | - header: 35 | key: "x-request-start-time" 36 | value: "%START_TIME(%Y/%m/%dT%H:%M:%S%z %s)%" 37 | append: true 38 | virtual_hosts: 39 | - name: local_service 40 | domains: 41 | - "*" 42 | routes: 43 | - match: 44 | prefix: "/" 45 | route: 46 | cluster: cluster1 47 | request_headers_to_add: 48 | - header: 49 | key: "x-request-downstream" 50 | value: "%DOWNSTREAM_LOCAL_ADDRESS%" 51 | append: true 52 | http_filters: 53 | - name: envoy.router 54 | typed_config: {} 55 | listener_filters: 56 | - name: envoy.listener.original_dst 57 | typed_config: {} 58 | 59 | clusters: 60 | - name: cluster1 61 | connect_timeout: 0.25s 62 | type: STRICT_DNS 63 | lb_policy: round_robin 64 | hosts: 65 | - socket_address: 66 | address: "host.docker.internal" 67 | port_value: 5000 68 | admin: 69 | access_log_path: "/dev/null" 70 | address: 71 | socket_address: 72 | address: 0.0.0.0 73 | port_value: 19000 74 | -------------------------------------------------------------------------------- /custom-headers/service-envoy.yaml: -------------------------------------------------------------------------------- 1 | node: 2 | id: "id_identity" 3 | cluster: "cluster_identity" 4 | static_resources: 5 | listeners: 6 | - name: listener_0 7 | transparent: true 8 | address: 9 | socket_address: 10 | protocol: TCP 11 | address: 0.0.0.0 12 | port_value: 4999 13 | filter_chains: 14 | - filters: 15 | - name: envoy.http_connection_manager 16 | config: 17 | idle_timeout: 1s 18 | codec_type: auto 19 | stat_prefix: ingress_http 20 | route_config: 21 | name: local_route 22 | request_headers_to_add: 23 | - header: 24 | key: "x-request-downstream" 25 | value: "%DOWNSTREAM_LOCAL_ADDRESS%" 26 | - header: 27 | key: "x-request-upstream" 28 | value: "%UPSTREAM_REMOTE_ADDRESS%" 29 | append: true 30 | - header: 31 | key: "x-request-start-time" 32 | value: "%START_TIME(%Y/%m/%dT%H:%M:%S%z %s)%" 33 | append: true 34 | virtual_hosts: 35 | - name: local_service 36 | domains: 37 | - "*" 38 | routes: 39 | - match: 40 | prefix: "/" 41 | route: 42 | cluster: cluster1 43 | request_headers_to_add: 44 | - header: 45 | key: "x-request-downstream" 46 | value: "%DOWNSTREAM_LOCAL_ADDRESS%" 47 | append: true 48 | http_filters: 49 | - name: envoy.router 50 | typed_config: {} 51 | listener_filters: 52 | - name: envoy.listener.original_dst 53 | typed_config: {} 54 | 55 | clusters: 56 | - name: cluster1 57 | connect_timeout: 0.25s 58 | type: STRICT_DNS 59 | lb_policy: round_robin 60 | hosts: 61 | - socket_address: 62 | address: localhost 63 | port_value: 5000 64 | admin: 65 | access_log_path: "/dev/null" 66 | address: 67 | socket_address: 68 | address: 0.0.0.0 69 | port_value: 19000 70 | -------------------------------------------------------------------------------- /custom-headers/start_envoy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | envoy -c /etc/service-envoy.yaml --log-level debug -------------------------------------------------------------------------------- /explicit-proxy-config/README.md: -------------------------------------------------------------------------------- 1 | # Tutorial 2 | 3 | **This was tested on Ubuntu 18.04.3 LTS** 4 | 5 | This example uses the same container as the Envoy Forward Proxy example but instead of using IPTables to redirect packets, we set HTTP Proxy environment variables. 6 | 7 | Reference for [Ubuntu Proxy settings](https://askubuntu.com/questions/175172/how-do-i-configure-proxies-without-gui) 8 | 9 | ## 1. Network Diagram 10 | 11 | In this tutorial we use a single host where we run the HTTP client and Envoy Proxy. The Web Server can be any accessible Internet web site. 12 | 13 | ## 2. Envoy Docker 14 | 15 | Go to the [forward-proxy](../forward-proxy) directory, build and run the envoy container: 16 | 17 | ``` 18 | ./build_envoy_docker.sh 19 | ``` 20 | 21 | Make sure you are still able to access the Internet unhindered 22 | 23 | ``` 24 | ubuntu$ curl -v www.cnn.com 25 | * Rebuilt URL to: www.cnn.com/ 26 | * Trying 151.101.41.67... 27 | * TCP_NODELAY set 28 | * Connected to www.cnn.com (151.101.41.67) port 80 (#0) 29 | > GET / HTTP/1.1 30 | > Host: www.cnn.com 31 | > User-Agent: curl/7.58.0 32 | > Accept: */* 33 | > 34 | < HTTP/1.1 301 Moved Permanently 35 | < Server: Varnish 36 | < Retry-After: 0 37 | < Content-Length: 0 38 | < Cache-Control: public, max-age=600 39 | < Location: https://www.cnn.com/ 40 | < Accept-Ranges: bytes 41 | < Date: Tue, 18 Feb 2020 06:11:20 GMT 42 | < Via: 1.1 varnish 43 | < Connection: close 44 | < Set-Cookie: countryCode=US; Domain=.cnn.com; Path=/; SameSite=Lax 45 | < Set-Cookie: geoData=san jose|CA|95123|US|NA|-800|broadband; Domain=.cnn.com; Path=/; SameSite=Lax 46 | < X-Served-By: cache-sjc10048-SJC 47 | < X-Cache: HIT 48 | < X-Cache-Hits: 0 49 | < 50 | * Closing connection 0 51 | ``` 52 | 53 | ## 3. Environment variables 54 | 55 | Now execute the script to set the environment variables 56 | 57 | ``` 58 | source ./set_proxy.sh 59 | ``` 60 | 61 | ## 4. HTTP Request 62 | 63 | Access to websites on ports 80 and 443 should go through the envoy proxy. Noticed the *x-envoy-upstream-service-time: 3* HTTP header 64 | 65 | ``` 66 | ubuntu$ curl -v www.cnn.com 67 | * Rebuilt URL to: www.cnn.com/ 68 | * Trying 127.0.0.1... 69 | * TCP_NODELAY set 70 | * Connected to localhost (127.0.0.1) port 4999 (#0) 71 | > GET http://www.cnn.com/ HTTP/1.1 72 | > Host: www.cnn.com 73 | > User-Agent: curl/7.58.0 74 | > Accept: */* 75 | > Proxy-Connection: Keep-Alive 76 | > 77 | < HTTP/1.1 301 Moved Permanently 78 | < server: envoy 79 | < retry-after: 0 80 | < content-length: 0 81 | < cache-control: public, max-age=600 82 | < location: https://www.cnn.com/ 83 | < accept-ranges: bytes 84 | < date: Tue, 18 Feb 2020 06:15:48 GMT 85 | < via: 1.1 varnish 86 | < set-cookie: countryCode=US; Domain=.cnn.com; Path=/; SameSite=Lax 87 | < set-cookie: geoData=san jose|CA|95123|US|NA|-800|broadband; Domain=.cnn.com; Path=/; SameSite=Lax 88 | < x-served-by: cache-pao17446-PAO 89 | < x-cache: HIT 90 | < x-cache-hits: 0 91 | < x-envoy-upstream-service-time: 3 92 | < 93 | * Connection #0 to host localhost left intact 94 | ``` 95 | 96 | ## 5. Envoy Logs 97 | 98 | Envoy Logs for successful run. 99 | 100 | ``` 101 | [2020-02-18 06:15:49.072][13][debug][conn_handler] [source/server/connection_handler_impl.cc:353] [C0] new connection 102 | [2020-02-18 06:15:49.073][13][debug][http] [source/common/http/conn_manager_impl.cc:263] [C0] new stream 103 | [2020-02-18 06:15:49.074][13][debug][http] [source/common/http/conn_manager_impl.cc:731] [C0][S12400113964677374341] request headers complete (end_stream=true): 104 | ':authority', 'www.cnn.com' 105 | ':path', '/' 106 | ':method', 'GET' 107 | 'user-agent', 'curl/7.58.0' 108 | 'accept', '*/*' 109 | 'proxy-connection', 'Keep-Alive' 110 | 111 | [2020-02-18 06:15:49.074][13][debug][http] [source/common/http/conn_manager_impl.cc:1276] [C0][S12400113964677374341] request end stream 112 | [2020-02-18 06:15:49.074][13][debug][forward_proxy] [source/extensions/common/dynamic_forward_proxy/dns_cache_impl.cc:47] thread local lookup for host 'www.cnn.com' 113 | [2020-02-18 06:15:49.074][13][debug][forward_proxy] [source/extensions/common/dynamic_forward_proxy/dns_cache_impl.cc:61] thread local miss for host 'www.cnn.com', posting to main thread 114 | [2020-02-18 06:15:49.074][7][debug][forward_proxy] [source/extensions/common/dynamic_forward_proxy/dns_cache_impl.cc:133] starting main thread resolve for host='www.cnn.com' dns='www.cnn.com' port='80' 115 | [2020-02-18 06:15:49.074][13][debug][forward_proxy] [source/extensions/filters/http/dynamic_forward_proxy/proxy_filter.cc:113] [C0][S12400113964677374341] waiting to load DNS cache entry 116 | [2020-02-18 06:15:49.078][7][debug][forward_proxy] [source/extensions/common/dynamic_forward_proxy/dns_cache_impl.cc:146] main thread resolve complete for host 'www.cnn.com'. 1 results 117 | [2020-02-18 06:15:49.078][7][debug][forward_proxy] [source/extensions/common/dynamic_forward_proxy/dns_cache_impl.cc:176] host 'www.cnn.com' address has changed 118 | [2020-02-18 06:15:49.078][7][debug][upstream] [source/extensions/clusters/dynamic_forward_proxy/cluster.cc:101] adding new dfproxy cluster host 'www.cnn.com' 119 | [2020-02-18 06:15:49.078][7][debug][upstream] [source/common/upstream/upstream_impl.cc:262] transport socket match, socket default selected for host with address 151.101.189.67:80 120 | [2020-02-18 06:15:49.078][7][debug][upstream] [source/common/upstream/cluster_manager_impl.cc:1084] membership update for TLS cluster dynamic_forward_proxy_cluster added 1 removed 0 121 | [2020-02-18 06:15:49.078][7][debug][upstream] [source/common/upstream/cluster_manager_impl.cc:1091] re-creating local LB for TLS cluster dynamic_forward_proxy_cluster 122 | [2020-02-18 06:15:49.079][13][debug][upstream] [source/common/upstream/cluster_manager_impl.cc:1084] membership update for TLS cluster dynamic_forward_proxy_cluster added 1 removed 0 123 | [2020-02-18 06:15:49.079][13][debug][upstream] [source/common/upstream/cluster_manager_impl.cc:1091] re-creating local LB for TLS cluster dynamic_forward_proxy_cluster 124 | [2020-02-18 06:15:49.079][13][debug][forward_proxy] [source/extensions/filters/http/dynamic_forward_proxy/proxy_filter.cc:130] [C0][S12400113964677374341] load DNS cache complete, continuing 125 | [2020-02-18 06:15:49.079][13][debug][router] [source/common/router/router.cc:474] [C0][S12400113964677374341] cluster 'dynamic_forward_proxy_cluster' match for URL '/' 126 | [2020-02-18 06:15:49.079][13][debug][router] [source/common/router/router.cc:614] [C0][S12400113964677374341] router decoding headers: 127 | ':authority', 'www.cnn.com' 128 | ':path', '/' 129 | ':method', 'GET' 130 | ':scheme', 'http' 131 | 'user-agent', 'curl/7.58.0' 132 | 'accept', '*/*' 133 | 'x-forwarded-proto', 'http' 134 | 'x-request-id', '51f89076-5172-464e-b3d9-d98605c17ecb' 135 | 'x-envoy-expected-rq-timeout-ms', '15000' 136 | 137 | [2020-02-18 06:15:49.079][13][debug][pool] [source/common/http/http1/conn_pool.cc:95] creating a new connection 138 | [2020-02-18 06:15:49.079][13][debug][client] [source/common/http/codec_client.cc:34] [C1] connecting 139 | [2020-02-18 06:15:49.079][13][debug][connection] [source/common/network/connection_impl.cc:691] [C1] connecting to 151.101.189.67:80 140 | [2020-02-18 06:15:49.079][13][debug][connection] [source/common/network/connection_impl.cc:700] [C1] connection in progress 141 | [2020-02-18 06:15:49.079][13][debug][pool] [source/common/http/conn_pool_base.cc:55] queueing request due to no available connections 142 | [2020-02-18 06:15:49.081][13][debug][connection] [source/common/network/connection_impl.cc:563] [C1] connected 143 | [2020-02-18 06:15:49.081][13][debug][client] [source/common/http/codec_client.cc:72] [C1] connected 144 | [2020-02-18 06:15:49.081][13][debug][pool] [source/common/http/http1/conn_pool.cc:244] [C1] attaching to next request 145 | [2020-02-18 06:15:49.081][13][debug][router] [source/common/router/router.cc:1711] [C0][S12400113964677374341] pool ready 146 | [2020-02-18 06:15:49.083][13][debug][client] [source/common/http/codec_client.cc:104] [C1] response complete 147 | [2020-02-18 06:15:49.083][13][debug][router] [source/common/router/router.cc:1115] [C0][S12400113964677374341] upstream headers complete: end_stream=true 148 | [2020-02-18 06:15:49.083][13][debug][http] [source/common/http/conn_manager_impl.cc:1615] [C0][S12400113964677374341] encoding headers via codec (end_stream=true): 149 | ':status', '301' 150 | 'server', 'envoy' 151 | 'retry-after', '0' 152 | 'content-length', '0' 153 | 'cache-control', 'public, max-age=600' 154 | 'location', 'https://www.cnn.com/' 155 | 'accept-ranges', 'bytes' 156 | 'date', 'Tue, 18 Feb 2020 06:15:48 GMT' 157 | 'via', '1.1 varnish' 158 | 'set-cookie', 'countryCode=US; Domain=.cnn.com; Path=/; SameSite=Lax' 159 | 'set-cookie', 'geoData=san jose|CA|95123|US|NA|-800|broadband; Domain=.cnn.com; Path=/; SameSite=Lax' 160 | 'x-served-by', 'cache-pao17446-PAO' 161 | 'x-cache', 'HIT' 162 | 'x-cache-hits', '0' 163 | 'x-envoy-upstream-service-time', '3' 164 | 165 | [2020-02-18 06:15:49.083][13][debug][pool] [source/common/http/http1/conn_pool.cc:201] [C1] response complete 166 | [2020-02-18 06:15:49.084][13][debug][pool] [source/common/http/http1/conn_pool.cc:206] [C1] saw upstream close connection 167 | [2020-02-18 06:15:49.084][13][debug][connection] [source/common/network/connection_impl.cc:101] [C1] closing data_to_write=0 type=1 168 | [2020-02-18 06:15:49.084][13][debug][connection] [source/common/network/connection_impl.cc:192] [C1] closing socket: 1 169 | [2020-02-18 06:15:49.084][13][debug][client] [source/common/http/codec_client.cc:91] [C1] disconnect. resetting 0 pending requests 170 | [2020-02-18 06:15:49.084][13][debug][pool] [source/common/http/http1/conn_pool.cc:136] [C1] client disconnected, failure reason: 171 | [2020-02-18 06:15:49.084][13][debug][connection] [source/common/network/connection_impl.cc:531] [C1] remote close 172 | [2020-02-18 06:15:49.085][13][debug][connection] [source/common/network/connection_impl.cc:531] [C0] remote close 173 | [2020-02-18 06:15:49.085][13][debug][connection] [source/common/network/connection_impl.cc:192] [C0] closing socket: 0 174 | [2020-02-18 06:15:49.085][13][debug][conn_handler] [source/server/connection_handler_impl.cc:86] [C0] adding to cleanup list 175 | ``` 176 | 177 | ## 6. Cleaning 178 | 179 | ``` 180 | source ./unset_proxy.sh 181 | ``` 182 | 183 | Go to the [forward-proxy](../forward-proxy) directory and execute 184 | 185 | ``` 186 | ./clean_envoy_docker.sh 187 | ``` 188 | 189 | 190 | 191 | -------------------------------------------------------------------------------- /explicit-proxy-config/set_proxy.sh: -------------------------------------------------------------------------------- 1 | export http_proxy="http://localhost:4999/" 2 | export https_proxy="http://localhost:4999/" 3 | export no_proxy="localhost,127.0.0.1,::1" -------------------------------------------------------------------------------- /explicit-proxy-config/unset_proxy.sh: -------------------------------------------------------------------------------- 1 | unset http_proxy 2 | unset https_proxy 3 | unset no_proxy -------------------------------------------------------------------------------- /ext-authz-proxy/README.md: -------------------------------------------------------------------------------- 1 | # Tutorial 2 | 3 | This example shows Envoy proxy using an external authorization server. 4 | 5 | This is example is based on the [rate limit example](https://github.com/jbarratt/envoy_ratelimit_example) 6 | 7 | ## 1. Network Diagram 8 | 9 | In this tutorial everything runs on a single host in order to simplify the deployment. 10 | 11 | ![You need to see the network diagram](./img/envoy_network.png) 12 | 13 | ## 2. Envoy Docker 14 | 15 | Build and run Envoy Docker 16 | 17 | ``` 18 | ./build_envoy_docker.sh 19 | ``` 20 | 21 | ## 3. External Authorization Server 22 | 23 | ``` 24 | go build 25 | ./ext-authz-proxy 26 | ``` 27 | 28 | ## 4. Simple Web Server 29 | 30 | Go to simple-go-server directory 31 | 32 | ``` 33 | go build 34 | ./simple-go-server 35 | http: 2020/02/20 15:58:30 Simple go server 36 | http: 2020/02/20 15:58:30 Version: 37 | http: 2020/02/20 15:58:30 GitTag: 38 | http: 2020/02/20 15:58:30 GitCommit: 39 | http: 2020/02/20 15:58:30 GitTreeState: 40 | http: 2020/02/20 15:58:30 Server is starting... 41 | http: 2020/02/20 15:58:30 Server is ready to handle requests at :5000 42 | 43 | ``` 44 | 45 | ## 5. Client Request 46 | 47 | Issue the HTTP request 48 | 49 | ``` 50 | curl localhost:4999 51 | ``` 52 | 53 | Response from Server on successful authorization 54 | 55 | ``` 56 | ubuntu$ curl -v localhost:4999 57 | * Rebuilt URL to: localhost:4999/ 58 | * Trying ::1... 59 | * TCP_NODELAY set 60 | * Connected to localhost (::1) port 4999 (#0) 61 | > GET / HTTP/1.1 62 | > Host: localhost:4999 63 | > User-Agent: curl/7.54.0 64 | > Accept: */* 65 | > 66 | < HTTP/1.1 200 OK 67 | < content-type: text/plain; charset=utf-8 68 | < x-content-type-options: nosniff 69 | < x-request-id: 2cbd2bad-8298-4e75-af98-c9301b7ba061 70 | < date: Thu, 20 Feb 2020 23:59:00 GMT 71 | < content-length: 14 72 | < x-envoy-upstream-service-time: 2 73 | < server: envoy 74 | < 75 | Hello, World! 76 | * Connection #0 to host localhost left intact 77 | ``` 78 | 79 | 80 | ## 6. External Server 81 | 82 | HTTP Headers received by external authorization server. One or more of these could be used together with other sources of data. 83 | 84 | ``` 85 | ubuntu$ ./ext-authz-proxy 86 | 2019/08/19 19:46:59 listening on [::]:5010 87 | Source IP:port 172.17.0.1:58772 88 | { 89 | "id": "4357586331639866428", 90 | "method": "GET", 91 | "headers": { 92 | ":authority": "localhost:4999", 93 | ":method": "GET", 94 | ":path": "/", 95 | "accept": "*/*", 96 | "user-agent": "curl/7.54.0", 97 | "x-envoy-internal": "true", 98 | "x-forwarded-for": "172.17.0.1", 99 | "x-forwarded-proto": "http", 100 | "x-request-id": "2cbd2bad-8298-4e75-af98-c9301b7ba061" 101 | }, 102 | "path": "/", 103 | "host": "localhost:4999", 104 | "protocol": "HTTP/1.1" 105 | } 106 | 107 | ``` 108 | 109 | Finally, the external authorization server will inject two headers on the response that should be added to the request by Envoy. 110 | 111 | ``` 112 | X-Ext-Auth-Id : curl 113 | X-Ext-Auth-Id-User : bob 114 | ``` 115 | 116 | 117 | 118 | ## 7. Web Server 119 | 120 | We can see that the two headers that the external server injected were received by the web server. 121 | 122 | ``` 123 | HTTP Headers Received: 124 | ====================== 125 | User-Agent : curl/7.54.0 126 | Accept : */* 127 | X-Forwarded-Proto : http 128 | X-Envoy-Internal : true 129 | X-Ext-Auth-Id : curl 130 | X-Envoy-Expected-Rq-Timeout-Ms : 15000 131 | X-Request-Downstream-Combo : 2020/02/20T23:59:00+0000 1582243140 132 | Content-Length : 0 133 | X-Forwarded-For : 172.17.0.1 134 | X-Request-Id : 2cbd2bad-8298-4e75-af98-c9301b7ba061 135 | X-Ext-Auth-Id-User : bob 136 | 137 | http: 2020/02/20 15:59:00 2cbd2bad-8298-4e75-af98-c9301b7ba061 GET / 127.0.0.1:50131 curl/7.54.0 138 | 139 | ``` 140 | 141 | ## 7.Authorized Request Envoy Logs 142 | 143 | Envoy logs from a request that was authorized 144 | 145 | ``` 146 | [2020-02-20 23:59:00.592][18][debug][conn_handler] [source/server/connection_handler_impl.cc:353] [C6] new connection 147 | [2020-02-20 23:59:00.592][18][debug][http] [source/common/http/conn_manager_impl.cc:263] [C6] new stream 148 | [2020-02-20 23:59:00.592][18][debug][http] [source/common/http/conn_manager_impl.cc:731] [C6][S4357586331639866428] request headers complete (end_stream=true): 149 | ':authority', 'localhost:4999' 150 | ':path', '/' 151 | ':method', 'GET' 152 | 'user-agent', 'curl/7.54.0' 153 | 'accept', '*/*' 154 | 155 | [2020-02-20 23:59:00.592][18][debug][http] [source/common/http/conn_manager_impl.cc:1276] [C6][S4357586331639866428] request end stream 156 | [2020-02-20 23:59:00.593][18][debug][router] [source/common/router/router.cc:474] [C0][S4823854182361172018] cluster 'ext-authz' match for URL '/envoy.service.auth.v2.Authorization/Check' 157 | [2020-02-20 23:59:00.593][18][debug][router] [source/common/router/router.cc:614] [C0][S4823854182361172018] router decoding headers: 158 | ':method', 'POST' 159 | ':path', '/envoy.service.auth.v2.Authorization/Check' 160 | ':authority', 'ext-authz' 161 | ':scheme', 'http' 162 | 'te', 'trailers' 163 | 'grpc-timeout', '200m' 164 | 'content-type', 'application/grpc' 165 | 'x-envoy-internal', 'true' 166 | 'x-forwarded-for', '172.17.0.2' 167 | 'x-envoy-expected-rq-timeout-ms', '200' 168 | 169 | [2020-02-20 23:59:00.593][18][debug][pool] [source/common/http/http2/conn_pool.cc:97] [C4] creating stream 170 | [2020-02-20 23:59:00.593][18][debug][router] [source/common/router/router.cc:1711] [C0][S4823854182361172018] pool ready 171 | [2020-02-20 23:59:00.594][18][debug][router] [source/common/router/router.cc:1115] [C0][S4823854182361172018] upstream headers complete: end_stream=false 172 | [2020-02-20 23:59:00.594][18][debug][http] [source/common/http/async_client_impl.cc:95] async http request response headers (end_stream=false): 173 | ':status', '200' 174 | 'content-type', 'application/grpc' 175 | 'x-envoy-upstream-service-time', '1' 176 | 177 | [2020-02-20 23:59:00.594][18][debug][client] [source/common/http/codec_client.cc:104] [C4] response complete 178 | [2020-02-20 23:59:00.594][18][debug][pool] [source/common/http/http2/conn_pool.cc:232] [C4] destroying stream: 0 remaining 179 | [2020-02-20 23:59:00.594][18][debug][http] [source/common/http/async_client_impl.cc:121] async http request response trailers: 180 | 'grpc-status', '0' 181 | 'grpc-message', '' 182 | 183 | [2020-02-20 23:59:00.594][18][debug][router] [source/common/router/router.cc:474] [C6][S4357586331639866428] cluster 'simple-server' match for URL '/' 184 | [2020-02-20 23:59:00.594][18][debug][router] [source/common/router/router.cc:614] [C6][S4357586331639866428] router decoding headers: 185 | ':authority', 'localhost:4999' 186 | ':path', '/' 187 | ':method', 'GET' 188 | ':scheme', 'http' 189 | 'user-agent', 'curl/7.54.0' 190 | 'accept', '*/*' 191 | 'x-forwarded-for', '172.17.0.1' 192 | 'x-forwarded-proto', 'http' 193 | 'x-envoy-internal', 'true' 194 | 'x-request-id', '2cbd2bad-8298-4e75-af98-c9301b7ba061' 195 | 'x-ext-auth-id', 'curl' 196 | 'x-ext-auth-id-user', 'bob' 197 | 'x-envoy-expected-rq-timeout-ms', '15000' 198 | 'x-request-downstream-combo', '2020/02/20T23:59:00+0000 1582243140' 199 | 200 | [2020-02-20 23:59:00.595][18][debug][pool] [source/common/http/http1/conn_pool.cc:95] creating a new connection 201 | [2020-02-20 23:59:00.595][18][debug][client] [source/common/http/codec_client.cc:34] [C7] connecting 202 | [2020-02-20 23:59:00.595][18][debug][connection] [source/common/network/connection_impl.cc:691] [C7] connecting to 192.168.65.2:5000 203 | [2020-02-20 23:59:00.595][18][debug][connection] [source/common/network/connection_impl.cc:700] [C7] connection in progress 204 | [2020-02-20 23:59:00.595][18][debug][pool] [source/common/http/conn_pool_base.cc:55] queueing request due to no available connections 205 | [2020-02-20 23:59:00.595][18][debug][http2] [source/common/http/http2/codec_impl.cc:732] [C4] stream closed: 0 206 | [2020-02-20 23:59:00.596][18][debug][connection] [source/common/network/connection_impl.cc:563] [C7] connected 207 | [2020-02-20 23:59:00.596][18][debug][client] [source/common/http/codec_client.cc:72] [C7] connected 208 | [2020-02-20 23:59:00.596][18][debug][pool] [source/common/http/http1/conn_pool.cc:244] [C7] attaching to next request 209 | [2020-02-20 23:59:00.596][18][debug][router] [source/common/router/router.cc:1711] [C6][S4357586331639866428] pool ready 210 | [2020-02-20 23:59:00.597][18][debug][router] [source/common/router/router.cc:1115] [C6][S4357586331639866428] upstream headers complete: end_stream=false 211 | [2020-02-20 23:59:00.597][18][debug][http] [source/common/http/conn_manager_impl.cc:1615] [C6][S4357586331639866428] encoding headers via codec (end_stream=false): 212 | ':status', '200' 213 | 'content-type', 'text/plain; charset=utf-8' 214 | 'x-content-type-options', 'nosniff' 215 | 'x-request-id', '2cbd2bad-8298-4e75-af98-c9301b7ba061' 216 | 'date', 'Thu, 20 Feb 2020 23:59:00 GMT' 217 | 'content-length', '14' 218 | 'x-envoy-upstream-service-time', '2' 219 | 'server', 'envoy' 220 | 221 | [2020-02-20 23:59:00.597][18][debug][client] [source/common/http/codec_client.cc:104] [C7] response complete 222 | [2020-02-20 23:59:00.598][18][debug][pool] [source/common/http/http1/conn_pool.cc:201] [C7] response complete 223 | [2020-02-20 23:59:00.598][18][debug][pool] [source/common/http/http1/conn_pool.cc:239] [C7] moving to ready 224 | [2020-02-20 23:59:00.600][18][debug][connection] [source/common/network/connection_impl.cc:531] [C6] remote close 225 | [2020-02-20 23:59:00.600][18][debug][connection] [source/common/network/connection_impl.cc:192] [C6] closing socket: 0 226 | [2020-02-20 23:59:00.600][18][debug][conn_handler] [source/server/connection_handler_impl.cc:86] [C6] adding to cleanup list 227 | [2020-02-20 23:59:03.180][7][debug][main] [source/server/server.cc:174] flushing stats 228 | ``` 229 | 230 | 231 | 232 | 233 | 234 | -------------------------------------------------------------------------------- /ext-authz-proxy/build_envoy_docker.sh: -------------------------------------------------------------------------------- 1 | # Enable exit on non 0 2 | set -e 3 | 4 | . clean_envoy_docker.sh 5 | 6 | if [ -z "${ENVOY_PORT}" ]; then 7 | PORT=4999 8 | else 9 | PORT="${ENVOY_PORT}" 10 | fi 11 | 12 | if [ -z "${ENVOY_ADMIN_PORT}" ]; then 13 | ADMIN_PORT=19000 14 | else 15 | ADMIN_PORT="${ENVOY_ADMIN_PORT}" 16 | fi 17 | 18 | CONTAINER_NAME=ext-auth-proxy 19 | DOCKERFILE=envoy.Dockerfile 20 | ENVOY_FILE=service-envoy.yaml 21 | 22 | if [[ "$OSTYPE" == "darwin"* ]]; then 23 | ENVOY_FILE=service-envoy-mac.yaml 24 | fi 25 | 26 | docker build -f ${DOCKERFILE} -t ${CONTAINER_NAME} . --build-arg envoy_file="${ENVOY_FILE}" 27 | 28 | if [[ "$OSTYPE" == "linux-gnu" ]]; then 29 | EXTRA_FLAGS="--cap-add=NET_ADMIN --network host" 30 | fi 31 | 32 | # NULL expansion 33 | # ${EXTRA_FLAGS:-"${EXTRA_FLAGS}"} 34 | 35 | DOCKER_COMMAND="docker run -d ${EXTRA_FLAGS:-${EXTRA_FLAGS}} -p \"${PORT}\":\"${PORT}\" -p \"${ADMIN_PORT}\":\"${ADMIN_PORT}\" --name \"${CONTAINER_NAME}\" \"${CONTAINER_NAME}\"" 36 | 37 | # printf "%s\n" "${DOCKER_COMMAND}" 38 | 39 | eval "${DOCKER_COMMAND}" 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /ext-authz-proxy/clean_envoy_docker.sh: -------------------------------------------------------------------------------- 1 | # Enable exit on non 0 2 | set -e 3 | 4 | CONTAINER_NAME=ext-auth-proxy 5 | 6 | docker stop ${CONTAINER_NAME} 2> /dev/null || true 7 | docker rm ${CONTAINER_NAME} 2> /dev/null || true 8 | docker rmi -f ${CONTAINER_NAME} 2> /dev/null || true -------------------------------------------------------------------------------- /ext-authz-proxy/envoy.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM envoyproxy/envoy:v1.13.0 2 | 3 | EXPOSE 4999 4 | EXPOSE 19000 5 | EXPOSE 8443 6 | 7 | ARG envoy_file 8 | 9 | ENV DEBIAN_FRONTEND noninteractive 10 | 11 | RUN apt-get -qq update && \ 12 | apt-get -qq install \ 13 | apt-utils \ 14 | iputils-ping \ 15 | curl \ 16 | < /dev/null > /dev/null 17 | 18 | ADD ${envoy_file} /etc/service-envoy.yaml 19 | ADD ./start_envoy.sh /usr/local/bin/start_envoy.sh 20 | WORKDIR /usr/local/bin 21 | RUN chmod u+x start_envoy.sh 22 | ENTRYPOINT ./start_envoy.sh 23 | 24 | 25 | -------------------------------------------------------------------------------- /ext-authz-proxy/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/repenno/envoybigbook/ext-authz-proxy 2 | 3 | go 1.12 4 | 5 | require ( 6 | github.com/envoyproxy/go-control-plane v0.8.6 7 | github.com/golang/protobuf v1.3.2 8 | google.golang.org/grpc v1.23.0 9 | istio.io/gogo-genproto v0.0.0-20190819131816-7a8328e41c1a 10 | ) 11 | -------------------------------------------------------------------------------- /ext-authz-proxy/go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 3 | github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= 4 | github.com/envoyproxy/go-control-plane v0.8.6 h1:qygnyt9M1DjMSKIvr4YZS76yt28JiAu/o2LFEA68bko= 5 | github.com/envoyproxy/go-control-plane v0.8.6/go.mod h1:XB9+ce7x+IrsjgIVnRnql0O61gj/np0/bGDfhJI3sCU= 6 | github.com/envoyproxy/protoc-gen-validate v0.0.0-20190405222122-d6164de49109 h1:FNgqGzbOm637YKRbYGKb9cqGo8i50++w/LWvMau7jrw= 7 | github.com/envoyproxy/protoc-gen-validate v0.0.0-20190405222122-d6164de49109/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= 8 | github.com/gogo/protobuf v1.2.2-0.20190730201129-28a6bbf47e48 h1:X+zN6RZXsvnrSJaAIQhZezPfAfvsqihKKR8oiLHid34= 9 | github.com/gogo/protobuf v1.2.2-0.20190730201129-28a6bbf47e48/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= 10 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= 11 | github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= 12 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 13 | github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= 14 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 15 | github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= 16 | github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= 17 | github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= 18 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 19 | golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= 20 | golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 21 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 22 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 23 | golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c h1:uOCk1iQW6Vc18bnC13MfzScl+wdKBmM9Y9kU7Z83/lw= 24 | golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 25 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 26 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 27 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 28 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 29 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 30 | golang.org/x/sys v0.0.0-20190508220229-2d0786266e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 31 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 32 | golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= 33 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 34 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 35 | golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 36 | golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 37 | golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 38 | golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= 39 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 40 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc= 41 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= 42 | google.golang.org/grpc v1.19.1/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= 43 | google.golang.org/grpc v1.23.0 h1:AzbTB6ux+okLTzP8Ru1Xs41C303zdcfEht7MQnYJt5A= 44 | google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= 45 | honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 46 | honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 47 | istio.io/gogo-genproto v0.0.0-20190731221249-06e20ada0df2/go.mod h1:IjvrbUlRbbw4JCpsgvgihcz9USUwEoNTL/uwMtyV5yk= 48 | istio.io/gogo-genproto v0.0.0-20190819131816-7a8328e41c1a h1:QN+P7SPcjI4az1Lb40MdhJg/OkV03u5XS1DVIk8PS6E= 49 | istio.io/gogo-genproto v0.0.0-20190819131816-7a8328e41c1a/go.mod h1:IjvrbUlRbbw4JCpsgvgihcz9USUwEoNTL/uwMtyV5yk= 50 | -------------------------------------------------------------------------------- /ext-authz-proxy/img/envoy_network.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BenderScript/envoybigbook/c02debcd2ac466e045775d22397a607a60ef0da6/ext-authz-proxy/img/envoy_network.png -------------------------------------------------------------------------------- /ext-authz-proxy/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "context" 6 | "encoding/json" 7 | "fmt" 8 | "log" 9 | "net" 10 | 11 | "github.com/envoyproxy/go-control-plane/envoy/api/v2/core" 12 | auth "github.com/envoyproxy/go-control-plane/envoy/service/auth/v2" 13 | "github.com/golang/protobuf/jsonpb" 14 | "google.golang.org/grpc" 15 | rpc "istio.io/gogo-genproto/googleapis/google/rpc" 16 | ) 17 | 18 | // empty struct because this isn't a fancy example 19 | type AuthorizationServer struct{} 20 | 21 | // inject a header that can be used for future rate limiting 22 | func (a *AuthorizationServer) Check(ctx context.Context, req *auth.CheckRequest) (*auth.CheckResponse, error) { 23 | 24 | httpRequest := req.Attributes.Request.Http 25 | socketAddress := req.Attributes.Source.Address.GetSocketAddress() 26 | fmt.Printf("Source IP:port %s:%d\n", socketAddress.GetAddress(), socketAddress.GetPortValue()) 27 | 28 | marshaler := jsonpb.Marshaler{} 29 | jsonString, _ := marshaler.MarshalToString(httpRequest) 30 | var out bytes.Buffer 31 | err := json.Indent(&out, []byte(jsonString), "", " ") 32 | if err == nil { 33 | println(out.String()) 34 | 35 | return &auth.CheckResponse{ 36 | Status: &rpc.Status{ 37 | Code: int32(rpc.OK), 38 | }, 39 | HttpResponse: &auth.CheckResponse_OkResponse{ 40 | OkResponse: &auth.OkHttpResponse{ 41 | // https://www.envoyproxy.io/docs/envoy/latest/api-v2/service/auth/v2/external_auth.proto#service-auth-v2-checkrequest 42 | Headers: []*core.HeaderValueOption{ 43 | { 44 | Header: &core.HeaderValue{ 45 | Key: "x-ext-auth-id", 46 | Value: "curl", 47 | }, 48 | }, 49 | { 50 | Header: &core.HeaderValue{ 51 | Key: "x-ext-auth-id-user", 52 | Value: "bob", 53 | }, 54 | }, 55 | }, 56 | }, 57 | }, 58 | }, nil 59 | } else { 60 | println("Error encoding JSON: " + err.Error()) 61 | return &auth.CheckResponse{ 62 | Status: &rpc.Status{ 63 | Code: int32(rpc.PERMISSION_DENIED), 64 | }, 65 | HttpResponse: &auth.CheckResponse_DeniedResponse{}, 66 | }, nil 67 | } 68 | } 69 | 70 | func main() { 71 | // create a TCP listener on port 5010 72 | lis, err := net.Listen("tcp", ":5010") 73 | if err != nil { 74 | log.Fatalf("failed to listen: %v", err) 75 | } 76 | log.Printf("listening on %s", lis.Addr()) 77 | 78 | grpcServer := grpc.NewServer() 79 | authServer := &AuthorizationServer{} 80 | auth.RegisterAuthorizationServer(grpcServer, authServer) 81 | 82 | if err := grpcServer.Serve(lis); err != nil { 83 | log.Fatalf("Failed to start server: %v", err) 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /ext-authz-proxy/service-envoy-mac.yaml: -------------------------------------------------------------------------------- 1 | node: 2 | id: "id_identity" 3 | cluster: "cluster_identity" 4 | static_resources: 5 | listeners: 6 | - address: 7 | socket_address: 8 | address: 0.0.0.0 9 | port_value: 4999 10 | filter_chains: 11 | - filters: 12 | - name: envoy.http_connection_manager 13 | # https://www.envoyproxy.io/docs/envoy/latest/api-v2/config/filter/network/http_connection_manager/v2/http_connection_manager.proto#envoy-api-field-config-filter-network-http-connection-manager-v2-httpconnectionmanager-use-remote-address 14 | config: 15 | use_remote_address: true 16 | skip_xff_append: false 17 | codec_type: auto 18 | access_log: 19 | - name: envoy.file_access_log 20 | config: 21 | path: "/tmp/envoy-access-4999.log" 22 | stat_prefix: ingress_http 23 | route_config: 24 | name: local_route 25 | request_headers_to_add: 26 | - header: 27 | key: "x-request-upstream" 28 | value: "%UPSTREAM_REMOTE_ADDRESS%" 29 | append: true 30 | - header: 31 | key: "x-request-downstream-combo" 32 | value: "%START_TIME(%Y/%m/%dT%H:%M:%S%z %s)%" 33 | append: true 34 | virtual_hosts: 35 | - name: local_service 36 | domains: ["*"] 37 | routes: 38 | - match: 39 | prefix: "/" 40 | route: 41 | cluster: simple-server 42 | http_filters: 43 | # Authz filters must be the first one for it to work 44 | # https://www.envoyproxy.io/docs/envoy/latest/api-v2/config/filter/http/ext_authz/v2/ext_authz.proto#envoy-api-msg-config-filter-http-ext-authz-v2-extauthz 45 | - name: envoy.ext_authz 46 | config: 47 | failure_mode_allow: false 48 | grpc_service: 49 | envoy_grpc: 50 | cluster_name: ext-authz 51 | - name: envoy.router 52 | 53 | clusters: 54 | - name: simple-server 55 | connect_timeout: 0.25s 56 | type: STRICT_DNS 57 | lb_policy: round_robin 58 | hosts: 59 | - socket_address: 60 | address: "host.docker.internal" 61 | port_value: 5000 62 | - name: ext-authz 63 | type: STRICT_DNS 64 | http2_protocol_options: {} 65 | load_assignment: 66 | cluster_name: ext-authz 67 | endpoints: 68 | - lb_endpoints: 69 | - endpoint: 70 | address: 71 | socket_address: 72 | address: "host.docker.internal" 73 | port_value: 5010 74 | # This timeout controls the initial TCP handshake timeout - not the timeout for the 75 | # entire request. 76 | connect_timeout: 0.25s 77 | 78 | admin: 79 | access_log_path: "/dev/null" 80 | address: 81 | socket_address: 82 | address: 0.0.0.0 83 | port_value: 19000 -------------------------------------------------------------------------------- /ext-authz-proxy/service-envoy.yaml: -------------------------------------------------------------------------------- 1 | node: 2 | id: "id_identity" 3 | cluster: "cluster_identity" 4 | static_resources: 5 | listeners: 6 | - address: 7 | socket_address: 8 | address: 0.0.0.0 9 | port_value: 4999 10 | filter_chains: 11 | - filters: 12 | - name: envoy.http_connection_manager 13 | # https://www.envoyproxy.io/docs/envoy/latest/api-v2/config/filter/network/http_connection_manager/v2/http_connection_manager.proto#envoy-api-field-config-filter-network-http-connection-manager-v2-httpconnectionmanager-use-remote-address 14 | config: 15 | use_remote_address: true 16 | skip_xff_append: false 17 | codec_type: auto 18 | access_log: 19 | - name: envoy.file_access_log 20 | config: 21 | path: "/tmp/envoy-access-4999.log" 22 | stat_prefix: ingress_http 23 | route_config: 24 | name: local_route 25 | virtual_hosts: 26 | - name: local_service 27 | domains: ["*"] 28 | routes: 29 | - match: 30 | prefix: "/" 31 | route: 32 | cluster: simple-server 33 | http_filters: 34 | # Authz filters must be the first one for it to work 35 | # https://www.envoyproxy.io/docs/envoy/latest/api-v2/config/filter/http/ext_authz/v2/ext_authz.proto#envoy-api-msg-config-filter-http-ext-authz-v2-extauthz 36 | - name: envoy.ext_authz 37 | config: 38 | failure_mode_allow: false 39 | grpc_service: 40 | envoy_grpc: 41 | cluster_name: ext-authz 42 | - name: envoy.router 43 | 44 | clusters: 45 | - name: simple-server 46 | connect_timeout: 0.25s 47 | type: STRICT_DNS 48 | lb_policy: round_robin 49 | hosts: 50 | - socket_address: 51 | address: localhost 52 | port_value: 5000 53 | - name: ext-authz 54 | type: STRICT_DNS 55 | http2_protocol_options: {} 56 | load_assignment: 57 | cluster_name: ext-authz 58 | endpoints: 59 | - lb_endpoints: 60 | - endpoint: 61 | address: 62 | socket_address: 63 | address: localhost 64 | port_value: 5010 65 | # This timeout controls the initial TCP handshake timeout - not the timeout for the 66 | # entire request. 67 | connect_timeout: 0.25s 68 | 69 | admin: 70 | access_log_path: "/dev/null" 71 | address: 72 | socket_address: 73 | address: 0.0.0.0 74 | port_value: 19000 -------------------------------------------------------------------------------- /ext-authz-proxy/start_envoy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | envoy -c /etc/service-envoy.yaml --log-level debug -------------------------------------------------------------------------------- /forward-proxy/README.md: -------------------------------------------------------------------------------- 1 | # Tutorial 2 | 3 | **This was tested on Ubuntu 18.04.3 LTS** 4 | 5 | In this example of we run a [Dynamic Forward Envoy Proxy](https://www.envoyproxy.io/docs/envoy/v1.13.0/configuration/http/http_filters/dynamic_forward_proxy_filter) that listens on port 4999 and directs requests to their original destination. 6 | 7 | The practical use-case is to confine applications running on the same host as the envoy proxy by using it as a forward proxy. Applications can not communicate directly out due IPTables rules. 8 | 9 | ## 1. Network Diagram 10 | 11 | The HTTP Client (cURL) and Envoy proxy share the same host. cURL runs as a native application and Envoy runs in a docker container 12 | 13 | An optional second host could run a web server in case of a self-contained example. 14 | 15 | ![You need to see the network diagram](./img/envoy_network.png) 16 | 17 | ## 2. Envoy Docker 18 | 19 | Build and run the envoy container: 20 | 21 | ``` 22 | ./build_envoy_docker.sh 23 | ``` 24 | 25 | Make sure you are still able to access the Internet unhindered 26 | 27 | ``` 28 | ubuntu$ curl -v www.cnn.com 29 | * Rebuilt URL to: www.cnn.com/ 30 | * Trying 151.101.189.67... 31 | * TCP_NODELAY set 32 | * Connected to www.cnn.com (151.101.189.67) port 80 (#0) 33 | > GET / HTTP/1.1 34 | > Host: www.cnn.com 35 | > User-Agent: curl/7.58.0 36 | > Accept: */* 37 | > 38 | < HTTP/1.1 301 Moved Permanently 39 | < Server: Varnish 40 | < Retry-After: 0 41 | < Content-Length: 0 42 | < Cache-Control: public, max-age=600 43 | < Location: https://www.cnn.com/ 44 | < Accept-Ranges: bytes 45 | < Date: Tue, 18 Feb 2020 06:44:10 GMT 46 | < Via: 1.1 varnish 47 | < Connection: close 48 | < Set-Cookie: countryCode=US; Domain=.cnn.com; Path=/; SameSite=Lax 49 | < Set-Cookie: geoData=san jose|CA|95123|US|NA|-800|broadband; Domain=.cnn.com; Path=/; SameSite=Lax 50 | < X-Served-By: cache-pao17442-PAO 51 | < X-Cache: HIT 52 | < X-Cache-Hits: 0 53 | < 54 | * Closing connection 0 55 | ``` 56 | 57 | ## 3. IPTables 58 | 59 | Now install the IPtables redirect rules 60 | 61 | ``` 62 | ./create_iptables.sh 63 | ``` 64 | 65 | ## 4. HTTP Request 66 | 67 | Access to websites on ports 80 and 443 should go through the envoy proxy. Noticed the *x-envoy-upstream-service-time: 3* HTTP header 68 | 69 | ``` 70 | ubuntu$ curl -v www.cnn.com 71 | * Rebuilt URL to: www.cnn.com/ 72 | * Trying 151.101.189.67... 73 | * TCP_NODELAY set 74 | * Connected to www.cnn.com (151.101.189.67) port 80 (#0) 75 | > GET / HTTP/1.1 76 | > Host: www.cnn.com 77 | > User-Agent: curl/7.58.0 78 | > Accept: */* 79 | > 80 | < HTTP/1.1 301 Moved Permanently 81 | < server: envoy 82 | < retry-after: 0 83 | < content-length: 0 84 | < cache-control: public, max-age=600 85 | < location: https://www.cnn.com/ 86 | < accept-ranges: bytes 87 | < date: Tue, 18 Feb 2020 06:47:14 GMT 88 | < via: 1.1 varnish 89 | < set-cookie: countryCode=US; Domain=.cnn.com; Path=/; SameSite=Lax 90 | < set-cookie: geoData=san jose|CA|95123|US|NA|-800|broadband; Domain=.cnn.com; Path=/; SameSite=Lax 91 | < x-served-by: cache-pao17421-PAO 92 | < x-cache: HIT 93 | < x-cache-hits: 0 94 | < x-envoy-upstream-service-time: 3 95 | < 96 | * Connection #0 to host www.cnn.com left intact 97 | ``` 98 | 99 | ## 5. IPTables Stats 100 | 101 | IPTables statistics should show the redirected packets 102 | 103 | ``` 104 | ubuntu$ ./show_iptables.sh 105 | Chain OUTPUT (policy ACCEPT 13 packets, 1013 bytes) 106 | pkts bytes target prot opt in out source destination 107 | 0 0 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL 108 | 2 120 REDIRECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 ! owner UID match 0 redir ports 4999 109 | 0 0 REDIRECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 ! owner UID match 0 redir ports 8443 110 | ``` 111 | 112 | ## 6. Envoy Logs 113 | 114 | Envoy Logs for successful run. 115 | 116 | ``` 117 | [2020-02-18 06:48:12.588][13][debug][conn_handler] [source/server/connection_handler_impl.cc:353] [C2] new connection 118 | [2020-02-18 06:48:12.588][13][debug][http] [source/common/http/conn_manager_impl.cc:263] [C2] new stream 119 | [2020-02-18 06:48:12.588][13][debug][http] [source/common/http/conn_manager_impl.cc:731] [C2][S4358495654629944488] request headers complete (end_stream=true): 120 | ':authority', 'www.cnn.com' 121 | ':path', '/' 122 | ':method', 'GET' 123 | 'user-agent', 'curl/7.58.0' 124 | 'accept', '*/*' 125 | 126 | [2020-02-18 06:48:12.588][13][debug][http] [source/common/http/conn_manager_impl.cc:1276] [C2][S4358495654629944488] request end stream 127 | [2020-02-18 06:48:12.588][13][debug][forward_proxy] [source/extensions/common/dynamic_forward_proxy/dns_cache_impl.cc:47] thread local lookup for host 'www.cnn.com' 128 | [2020-02-18 06:48:12.588][13][debug][forward_proxy] [source/extensions/common/dynamic_forward_proxy/dns_cache_impl.cc:51] thread local hit for host 'www.cnn.com' 129 | [2020-02-18 06:48:12.588][13][debug][forward_proxy] [source/extensions/filters/http/dynamic_forward_proxy/proxy_filter.cc:108] [C2][S4358495654629944488] DNS cache entry already loaded, continuing 130 | [2020-02-18 06:48:12.588][13][debug][router] [source/common/router/router.cc:474] [C2][S4358495654629944488] cluster 'dynamic_forward_proxy_cluster' match for URL '/' 131 | [2020-02-18 06:48:12.589][13][debug][router] [source/common/router/router.cc:614] [C2][S4358495654629944488] router decoding headers: 132 | ':authority', 'www.cnn.com' 133 | ':path', '/' 134 | ':method', 'GET' 135 | ':scheme', 'http' 136 | 'user-agent', 'curl/7.58.0' 137 | 'accept', '*/*' 138 | 'x-forwarded-proto', 'http' 139 | 'x-request-id', '2dc75550-e63e-4a61-922c-fa71b3411392' 140 | 'x-envoy-expected-rq-timeout-ms', '15000' 141 | 142 | [2020-02-18 06:48:12.589][13][debug][pool] [source/common/http/http1/conn_pool.cc:95] creating a new connection 143 | [2020-02-18 06:48:12.589][13][debug][client] [source/common/http/codec_client.cc:34] [C3] connecting 144 | [2020-02-18 06:48:12.589][13][debug][connection] [source/common/network/connection_impl.cc:691] [C3] connecting to 151.101.189.67:80 145 | [2020-02-18 06:48:12.589][13][debug][connection] [source/common/network/connection_impl.cc:700] [C3] connection in progress 146 | [2020-02-18 06:48:12.589][13][debug][pool] [source/common/http/conn_pool_base.cc:55] queueing request due to no available connections 147 | [2020-02-18 06:48:12.590][13][debug][connection] [source/common/network/connection_impl.cc:563] [C3] connected 148 | [2020-02-18 06:48:12.590][13][debug][client] [source/common/http/codec_client.cc:72] [C3] connected 149 | [2020-02-18 06:48:12.590][13][debug][pool] [source/common/http/http1/conn_pool.cc:244] [C3] attaching to next request 150 | [2020-02-18 06:48:12.590][13][debug][router] [source/common/router/router.cc:1711] [C2][S4358495654629944488] pool ready 151 | [2020-02-18 06:48:12.592][13][debug][client] [source/common/http/codec_client.cc:104] [C3] response complete 152 | [2020-02-18 06:48:12.592][13][debug][router] [source/common/router/router.cc:1115] [C2][S4358495654629944488] upstream headers complete: end_stream=true 153 | [2020-02-18 06:48:12.592][13][debug][http] [source/common/http/conn_manager_impl.cc:1615] [C2][S4358495654629944488] encoding headers via codec (end_stream=true): 154 | ':status', '301' 155 | 'server', 'envoy' 156 | 'retry-after', '0' 157 | 'content-length', '0' 158 | 'cache-control', 'public, max-age=600' 159 | 'location', 'https://www.cnn.com/' 160 | 'accept-ranges', 'bytes' 161 | 'date', 'Tue, 18 Feb 2020 06:48:12 GMT' 162 | 'via', '1.1 varnish' 163 | 'set-cookie', 'countryCode=US; Domain=.cnn.com; Path=/; SameSite=Lax' 164 | 'set-cookie', 'geoData=san jose|CA|95123|US|NA|-800|broadband; Domain=.cnn.com; Path=/; SameSite=Lax' 165 | 'x-served-by', 'cache-pao17438-PAO' 166 | 'x-cache', 'HIT' 167 | 'x-cache-hits', '0' 168 | 'x-envoy-upstream-service-time', '3' 169 | 170 | [2020-02-18 06:48:12.592][13][debug][pool] [source/common/http/http1/conn_pool.cc:201] [C3] response complete 171 | [2020-02-18 06:48:12.592][13][debug][pool] [source/common/http/http1/conn_pool.cc:206] [C3] saw upstream close connection 172 | [2020-02-18 06:48:12.592][13][debug][connection] [source/common/network/connection_impl.cc:101] [C3] closing data_to_write=0 type=1 173 | [2020-02-18 06:48:12.592][13][debug][connection] [source/common/network/connection_impl.cc:192] [C3] closing socket: 1 174 | [2020-02-18 06:48:12.592][13][debug][client] [source/common/http/codec_client.cc:91] [C3] disconnect. resetting 0 pending requests 175 | [2020-02-18 06:48:12.592][13][debug][pool] [source/common/http/http1/conn_pool.cc:136] [C3] client disconnected, failure reason: 176 | [2020-02-18 06:48:12.593][13][debug][connection] [source/common/network/connection_impl.cc:531] [C2] remote close 177 | [2020-02-18 06:48:12.593][13][debug][connection] [source/common/network/connection_impl.cc:192] [C2] closing socket: 0 178 | [2020-02-18 06:48:12.593][13][debug][conn_handler] [source/server/connection_handler_impl.cc:86] [C2] adding to cleanup list 179 | [2020-02-18 06:48:14.398][7][debug][forward_proxy] [source/extensions/common/dynamic_forward_proxy/dns_cache_impl.cc:120] host='www.cnn.com' TTL check: now=45270372276985 last_used=45268562464739 180 | [2020-02-18 06:48:14.398][7][debug][forward_proxy] [source/extensions/common/dynamic_forward_proxy/dns_cache_impl.cc:133] starting main thread resolve for host='www.cnn.com' dns='www.cnn.com' port='80' 181 | [2020-02-18 06:48:14.417][7][debug][forward_proxy] [source/extensions/common/dynamic_forward_proxy/dns_cache_impl.cc:146] main thread resolve complete for host 'www.cnn.com'. 1 results 182 | [2020-02-18 06:48:17.076][7][debug][main] [source/server/server.cc:174] flushing stats 183 | ``` 184 | 185 | ## 7. Cleaning 186 | 187 | ``` 188 | ./clean_envoy_docker.sh 189 | ./clean_iptables.sh 190 | ``` 191 | 192 | 193 | 194 | -------------------------------------------------------------------------------- /forward-proxy/build_envoy_docker.sh: -------------------------------------------------------------------------------- 1 | # Enable exit on non 0 2 | set -e 3 | 4 | . clean_envoy_docker.sh 5 | 6 | if [ -z "${ENVOY_PORT}" ]; then 7 | PORT=4999 8 | else 9 | PORT="${ENVOY_PORT}" 10 | fi 11 | 12 | if [ -z "${ENVOY_ADMIN_PORT}" ]; then 13 | ADMIN_PORT=19000 14 | else 15 | ADMIN_PORT="${ENVOY_ADMIN_PORT}" 16 | fi 17 | 18 | CONTAINER_NAME=envoy-forward 19 | DOCKERFILE=envoy.Dockerfile 20 | ENVOY_FILE=service-envoy.yaml 21 | 22 | if [[ "$OSTYPE" == "darwin"* ]]; then 23 | printf "%s\n" "IPTables support is needed" 24 | exit 1 25 | fi 26 | 27 | docker build -f ${DOCKERFILE} -t ${CONTAINER_NAME} . --build-arg envoy_file="${ENVOY_FILE}" 28 | 29 | if [[ "$OSTYPE" == "linux-gnu" ]]; then 30 | EXTRA_FLAGS="--cap-add=NET_ADMIN --network host" 31 | fi 32 | 33 | # NULL expansion 34 | # ${EXTRA_FLAGS:-"${EXTRA_FLAGS}"} 35 | 36 | DOCKER_COMMAND="docker run -d ${EXTRA_FLAGS:-${EXTRA_FLAGS}} -p \"${PORT}\":\"${PORT}\" -p \"${ADMIN_PORT}\":\"${ADMIN_PORT}\" --name \"${CONTAINER_NAME}\" \"${CONTAINER_NAME}\"" 37 | 38 | # printf "%s\n" "${DOCKER_COMMAND}" 39 | 40 | eval "${DOCKER_COMMAND}" 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /forward-proxy/clean_envoy_docker.sh: -------------------------------------------------------------------------------- 1 | # Enable exit on non 0 2 | set -e 3 | 4 | CONTAINER_NAME=envoy-forward 5 | 6 | docker stop ${CONTAINER_NAME} 2> /dev/null || true 7 | docker rm ${CONTAINER_NAME} 2> /dev/null || true 8 | docker rmi -f ${CONTAINER_NAME} 2> /dev/null || true -------------------------------------------------------------------------------- /forward-proxy/clean_iptables.sh: -------------------------------------------------------------------------------- 1 | # Enable exit on non 0 2 | set -e 3 | 4 | sudo iptables -t nat -D OUTPUT -p tcp -m tcp --dport 80 -m owner ! --uid-owner 0 -j REDIRECT --to-port 4999 5 | sudo iptables -t nat -D OUTPUT -p tcp -m tcp --dport 443 -m owner ! --uid-owner 0 -j REDIRECT --to-ports 8443 -------------------------------------------------------------------------------- /forward-proxy/create_iptables.sh: -------------------------------------------------------------------------------- 1 | # Enable exit on non 0 2 | set -e 3 | 4 | sudo iptables -t nat -A OUTPUT -p tcp -m tcp --dport 80 -m owner ! --uid-owner 0 -j REDIRECT --to-port 4999 5 | sudo iptables -t nat -A OUTPUT -p tcp -m tcp --dport 443 -m owner ! --uid-owner 0 -j REDIRECT --to-ports 8443 6 | 7 | -------------------------------------------------------------------------------- /forward-proxy/envoy.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM envoyproxy/envoy:v1.13.0 2 | 3 | EXPOSE 4999 4 | EXPOSE 19000 5 | EXPOSE 8443 6 | 7 | ARG envoy_file 8 | 9 | ENV DEBIAN_FRONTEND noninteractive 10 | 11 | RUN apt-get -qq update && \ 12 | apt-get -qq install \ 13 | apt-utils \ 14 | iputils-ping \ 15 | curl \ 16 | < /dev/null > /dev/null 17 | 18 | ADD ${envoy_file} /etc/service-envoy.yaml 19 | ADD ./start_envoy.sh /usr/local/bin/start_envoy.sh 20 | WORKDIR /usr/local/bin 21 | RUN chmod u+x start_envoy.sh 22 | ENTRYPOINT ./start_envoy.sh 23 | 24 | 25 | -------------------------------------------------------------------------------- /forward-proxy/img/envoy_network.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BenderScript/envoybigbook/c02debcd2ac466e045775d22397a607a60ef0da6/forward-proxy/img/envoy_network.png -------------------------------------------------------------------------------- /forward-proxy/service-envoy.yaml: -------------------------------------------------------------------------------- 1 | admin: 2 | access_log_path: /tmp/admin_access.log 3 | address: 4 | socket_address: 5 | protocol: TCP 6 | address: 127.0.0.1 7 | port_value: 19000 8 | static_resources: 9 | listeners: 10 | - name: listener_0 11 | address: 12 | socket_address: 13 | protocol: TCP 14 | address: 0.0.0.0 15 | port_value: 4999 16 | filter_chains: 17 | - filters: 18 | - name: envoy.http_connection_manager 19 | typed_config: 20 | "@type": type.googleapis.com/envoy.config.filter.network.http_connection_manager.v2.HttpConnectionManager 21 | stat_prefix: ingress_http 22 | http_protocol_options: 23 | allow_absolute_url: true 24 | route_config: 25 | name: local_route 26 | virtual_hosts: 27 | - name: local_service 28 | domains: ["*"] 29 | routes: 30 | - match: 31 | prefix: "/" 32 | route: 33 | cluster: dynamic_forward_proxy_cluster 34 | http_filters: 35 | - name: envoy.filters.http.dynamic_forward_proxy 36 | config: 37 | dns_cache_config: 38 | name: dynamic_forward_proxy_cache_config 39 | dns_lookup_family: V4_ONLY 40 | - name: envoy.router 41 | clusters: 42 | - name: dynamic_forward_proxy_cluster 43 | connect_timeout: 1s 44 | lb_policy: CLUSTER_PROVIDED 45 | cluster_type: 46 | name: envoy.clusters.dynamic_forward_proxy 47 | typed_config: 48 | "@type": type.googleapis.com/envoy.config.cluster.dynamic_forward_proxy.v2alpha.ClusterConfig 49 | dns_cache_config: 50 | name: dynamic_forward_proxy_cache_config 51 | dns_lookup_family: V4_ONLY 52 | -------------------------------------------------------------------------------- /forward-proxy/show_iptables.sh: -------------------------------------------------------------------------------- 1 | 2 | sudo iptables -t nat -nvL OUTPUT 3 | 4 | printf "\n" 5 | -------------------------------------------------------------------------------- /forward-proxy/start_envoy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | envoy -c /etc/service-envoy.yaml --log-level debug -------------------------------------------------------------------------------- /original-dst/build_envoy_docker.sh: -------------------------------------------------------------------------------- 1 | # Enable exit on non 0 2 | set -e 3 | 4 | . clean_envoy_docker.sh 5 | 6 | if [ -z "${ENVOY_PORT}" ]; then 7 | PORT=4999 8 | else 9 | PORT="${ENVOY_PORT}" 10 | fi 11 | 12 | if [ -z "${ENVOY_ADMIN_PORT}" ]; then 13 | ADMIN_PORT=19000 14 | else 15 | ADMIN_PORT="${ENVOY_ADMIN_PORT}" 16 | fi 17 | 18 | CONTAINER_NAME=envoy-original-dest 19 | DOCKERFILE=envoy.Dockerfile 20 | ENVOY_FILE=service-envoy.yaml 21 | 22 | if [[ "$OSTYPE" == "darwin"* ]]; then 23 | printf "%s\n" "IPTables support is needed" 24 | exit 1 25 | fi 26 | 27 | docker build -f ${DOCKERFILE} -t ${CONTAINER_NAME} . --build-arg envoy_file="${ENVOY_FILE}" 28 | 29 | if [[ "$OSTYPE" == "linux-gnu" ]]; then 30 | EXTRA_FLAGS="--cap-add=NET_ADMIN --network host" 31 | fi 32 | 33 | # NULL expansion 34 | # ${EXTRA_FLAGS:-"${EXTRA_FLAGS}"} 35 | 36 | DOCKER_COMMAND="docker run -d ${EXTRA_FLAGS:-${EXTRA_FLAGS}} -p \"${PORT}\":\"${PORT}\" -p \"${ADMIN_PORT}\":\"${ADMIN_PORT}\" --name \"${CONTAINER_NAME}\" \"${CONTAINER_NAME}\"" 37 | 38 | # printf "%s\n" "${DOCKER_COMMAND}" 39 | 40 | eval "${DOCKER_COMMAND}" 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /original-dst/clean_envoy_docker.sh: -------------------------------------------------------------------------------- 1 | # Enable exit on non 0 2 | set -e 3 | 4 | CONTAINER_NAME=envoy-original-dest 5 | 6 | docker stop ${CONTAINER_NAME} 2> /dev/null || true 7 | docker rm ${CONTAINER_NAME} 2> /dev/null || true 8 | docker rmi -f ${CONTAINER_NAME} 2> /dev/null || true -------------------------------------------------------------------------------- /original-dst/clean_iptables.sh: -------------------------------------------------------------------------------- 1 | # Enable exit on non 0 2 | set -e 3 | 4 | sudo iptables -t nat -D OUTPUT -p tcp -m tcp --dport 80 -m owner ! --uid-owner 0 -j REDIRECT --to-port 4999 5 | 6 | printf "\n" -------------------------------------------------------------------------------- /original-dst/create_iptables.sh: -------------------------------------------------------------------------------- 1 | # Enable exit on non 0 2 | set -e 3 | 4 | sudo iptables -t nat -A OUTPUT -p tcp -m tcp --dport 80 -m owner ! --uid-owner 0 -j REDIRECT --to-port 4999 5 | 6 | printf "\n" -------------------------------------------------------------------------------- /original-dst/envoy.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM envoyproxy/envoy:v1.11.1 2 | 3 | EXPOSE 4999 4 | EXPOSE 19000 5 | EXPOSE 8443 6 | 7 | ARG envoy_file 8 | 9 | ENV DEBIAN_FRONTEND noninteractive 10 | 11 | RUN apt-get -qq update && \ 12 | apt-get -qq install \ 13 | apt-utils \ 14 | iputils-ping \ 15 | curl \ 16 | < /dev/null > /dev/null 17 | 18 | ADD ${envoy_file} /etc/service-envoy.yaml 19 | ADD ./start_envoy.sh /usr/local/bin/start_envoy.sh 20 | WORKDIR /usr/local/bin 21 | RUN chmod u+x start_envoy.sh 22 | ENTRYPOINT ./start_envoy.sh 23 | 24 | 25 | -------------------------------------------------------------------------------- /original-dst/img/envoy_network_original_dst.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BenderScript/envoybigbook/c02debcd2ac466e045775d22397a607a60ef0da6/original-dst/img/envoy_network_original_dst.png -------------------------------------------------------------------------------- /original-dst/original_destination.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Small program to demonstrate how to get original IP:port from redirected connections 4 | # https://tor.stackexchange.com/questions/16654/how-can-i-verify-that-transport-is-working-correctly 5 | 6 | import socket 7 | from struct import unpack 8 | if __name__ == '__main__': 9 | print("Starting") 10 | s = socket.socket() 11 | s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 12 | s.bind(('0.0.0.0',4999)) 13 | s.listen(1) 14 | conn, addr = s.accept() 15 | orig = conn.getsockopt(socket.SOL_IP, 80, 16) # 80 is socket.SO_ORIGINAL_DST but python doesn't define it. 16 | port, ip = unpack("!2xH4s8x", orig) 17 | print('{}:{}'.format(socket.inet_ntoa(ip), port)) -------------------------------------------------------------------------------- /original-dst/service-envoy.yaml: -------------------------------------------------------------------------------- 1 | static_resources: 2 | listeners: 3 | - address: 4 | socket_address: 5 | address: 0.0.0.0 6 | port_value: 4999 7 | filter_chains: 8 | - filters: 9 | - name: envoy.http_connection_manager 10 | typed_config: 11 | "@type": type.googleapis.com/envoy.config.filter.network.http_connection_manager.v2.HttpConnectionManager 12 | stat_prefix: ingress_http 13 | route_config: 14 | name: local_service 15 | virtual_hosts: 16 | - name: backend 17 | domains: 18 | - "*" 19 | routes: 20 | - match: 21 | prefix: "/" 22 | route: 23 | cluster: cluster1 24 | http_filters: 25 | - name: envoy.router 26 | typed_config: {} 27 | codec_type: auto 28 | listener_filters: 29 | - name: envoy.listener.original_dst 30 | typed_config: {} 31 | clusters: 32 | - name: cluster1 33 | type: ORIGINAL_DST 34 | connect_timeout: 6s 35 | lb_policy: ORIGINAL_DST_LB 36 | dns_lookup_family: V4_ONLY 37 | cluster_manager: {} 38 | watchdog: {} 39 | admin: 40 | access_log_path: /tmp/admin_access.log 41 | address: 42 | socket_address: 43 | address: 0.0.0.0 44 | port_value: 19000 -------------------------------------------------------------------------------- /original-dst/show_iptables.sh: -------------------------------------------------------------------------------- 1 | 2 | sudo iptables -t nat -nvL OUTPUT 3 | 4 | printf "\n" 5 | -------------------------------------------------------------------------------- /original-dst/start_envoy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | envoy -c /etc/service-envoy.yaml --log-level debug -------------------------------------------------------------------------------- /resources.md: -------------------------------------------------------------------------------- 1 | # Envoy Resources 2 | 3 | ## Videos 4 | 5 | If you work with/on Envoy the UDPA presentation below is a must watch. 6 | 7 | [The Universal Dataplane API (UDPA): Envoy's Next Generation APIs - Harvey Tuch, Google](https://youtu.be/CIRyemwUSbQ) 8 | 9 | Other presentations I consider 'canon' 10 | 11 | [Deep Dive: Envoy - Lizan Zhou, Tetrate](https://youtu.be/spzfupads2o) 12 | 13 | [Envoy Internals Deep Dive - Matt Klein, Lyft (Advanced Skill Level)](https://youtu.be/gQF23Vw0keg) 14 | 15 | [Intro: Envoy - Matt Klein & Constance Caramanolis, Lyft](https://youtu.be/P719qI2h2yY) -------------------------------------------------------------------------------- /simple-front-proxy/README.md: -------------------------------------------------------------------------------- 1 | # Tutorial 2 | 3 | **Tested on Ubuntu 18.04.3 LTS** 4 | 5 | In this example of we run a Envoy Proxy on that listens on port 4999 and directs to a server running on port 5000. 6 | 7 | The web server runs as a separate container from Envoy so any web server will do as long as it is listening on port 5000. 8 | 9 | ## Envoy Docker 10 | 11 | Build and run the envoy container. The container runs with *--network host* in order to reach the web server running on the host. 12 | 13 | ``` 14 | ./build_envoy_docker.sh 15 | ``` 16 | 17 | Without a web server a request would look like this: 18 | 19 | ``` 20 | curl -v http://localhost:4999/ 21 | 22 | * Trying 127.0.0.1... 23 | * TCP_NODELAY set 24 | * Connected to localhost (127.0.0.1) port 4999 (#0) 25 | > GET / HTTP/1.1 26 | > Host: localhost:4999 27 | > User-Agent: curl/7.58.0 28 | > Accept: */* 29 | > 30 | < HTTP/1.1 503 Service Unavailable 31 | < content-length: 91 32 | < content-type: text/plain 33 | < date: Tue, 18 Feb 2020 19:22:52 GMT 34 | < server: envoy 35 | < 36 | * Connection #0 to host localhost left intact 37 | ``` 38 | ## Web Server 39 | 40 | I normally use [httpbin](http://httpbin.org/) as the Web Server. A reliable, no-hassle, perfect-for-testing web server. 41 | 42 | ``` 43 | ./run_web_docker.sh 44 | ``` 45 | 46 | ## HTTP Request 47 | 48 | Now with the Web Server running repeat the request. It will be processed by the Envoy Proxy container and directed to the web Server 49 | 50 | ``` 51 | curl -v localhost:4999 52 | * Rebuilt URL to: localhost:4999/ 53 | * Trying 127.0.0.1... 54 | * TCP_NODELAY set 55 | * Connected to localhost (127.0.0.1) port 4999 (#0) 56 | > GET / HTTP/1.1 57 | > Host: localhost:4999 58 | > User-Agent: curl/7.58.0 59 | > Accept: */* 60 | > 61 | < HTTP/1.1 200 OK 62 | < server: envoy 63 | < date: Tue, 18 Feb 2020 19:24:03 GMT 64 | < content-type: text/html; charset=utf-8 65 | < content-length: 9593 66 | < access-control-allow-origin: * 67 | < access-control-allow-credentials: true 68 | < x-envoy-upstream-service-time: 30 69 | < 70 | 71 | 72 | 73 | 74 | 75 | httpbin.org 76 | 78 | 79 | 80 |