|
| 1 | +# Blocking Requests |
| 2 | + |
| 3 | +This article provides examples that can be used as a starting point when configuring the proxy to block requests based on their method type or protocol. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +The examples that follow assume that you are using Docker v1.13+, Docker Compose v1.10+, and Docker Machine v0.9+. |
| 8 | + |
| 9 | +!!! info |
| 10 | + If you are a Windows user, please run all the examples from *Git Bash* (installed through *Docker Toolbox*). Also, make sure that your Git client is configured to check out the code *AS-IS*. Otherwise, Windows might change carriage returns to the Windows format. |
| 11 | + |
| 12 | +Please note that *Docker Flow Proxy* is not limited to *Docker Machine*. We're using it as an easy way to create a cluster. |
| 13 | + |
| 14 | +## Swarm Cluster Setup |
| 15 | + |
| 16 | +To setup an example Swarm cluster using Docker Machine, please run the commands that follow. |
| 17 | + |
| 18 | +!!! tip |
| 19 | + Feel free to skip this section if you already have a working Swarm cluster. |
| 20 | + |
| 21 | +```bash |
| 22 | +curl -o swarm-cluster.sh \ |
| 23 | + https://raw.githubusercontent.com/\ |
| 24 | +vfarcic/docker-flow-proxy/master/scripts/swarm-cluster.sh |
| 25 | + |
| 26 | +chmod +x swarm-cluster.sh |
| 27 | + |
| 28 | +./swarm-cluster.sh |
| 29 | + |
| 30 | +eval $(docker-machine env node-1) |
| 31 | +``` |
| 32 | + |
| 33 | +Now we're ready to deploy the services that form the proxy stack and the demo services. |
| 34 | + |
| 35 | +```bash |
| 36 | +docker network create --driver overlay proxy |
| 37 | + |
| 38 | +curl -o docker-compose-stack.yml \ |
| 39 | + https://raw.githubusercontent.com/\ |
| 40 | +vfarcic/docker-flow-proxy/master/docker-compose-stack.yml |
| 41 | + |
| 42 | +docker stack deploy -c docker-compose-stack.yml proxy |
| 43 | + |
| 44 | +curl -o docker-compose-go-demo.yml \ |
| 45 | + https://raw.githubusercontent.com/\ |
| 46 | +vfarcic/go-demo/master/docker-compose-stack.yml |
| 47 | + |
| 48 | +docker stack deploy -c docker-compose-go-demo.yml go-demo |
| 49 | +``` |
| 50 | + |
| 51 | +Please consult [Using Docker Stack To Run Docker Flow Proxy In Swarm Mode](/swarm-mode-stack/) for a more detailed set of examples of deployment with Docker stack. |
| 52 | + |
| 53 | +We should wait until all the services are running before proceeding towards the examples that will block requests. |
| 54 | + |
| 55 | +```bash |
| 56 | +docker service ls |
| 57 | +``` |
| 58 | + |
| 59 | +Now we are ready to explore way to block access requests. |
| 60 | + |
| 61 | +## Blocking Requests Based on Request Type |
| 62 | + |
| 63 | +In some cases, we want to deny certain types of methods to requests sent through the proxy. A common use case would be a service that can accept `DELETE` request which should be performed only by other services connected to it through internal networking. |
| 64 | + |
| 65 | +We can block requests by specifying which types are allowed. |
| 66 | + |
| 67 | +Please execute the command that follows. |
| 68 | + |
| 69 | +``` |
| 70 | +docker service update \ |
| 71 | + --label-add "com.df.allowedMethods=GET,DELETE" \ |
| 72 | + go-demo_main |
| 73 | +``` |
| 74 | + |
| 75 | +We specified the `com.df.allowedMethods` label that tells the proxy that only `GET` and `DELETE` methods are allowed. A request with any other method will be denied. |
| 76 | + |
| 77 | +Let's confirm that the feature indeed works as expected. |
| 78 | + |
| 79 | +```bash |
| 80 | +curl -i "http://$(docker-machine ip node-1)/demo/hello" |
| 81 | +``` |
| 82 | + |
| 83 | +We sent an `GET` request (default type) and the output is as follows. |
| 84 | + |
| 85 | +``` |
| 86 | +TODO |
| 87 | +``` |
| 88 | + |
| 89 | +Since get is on the list of allowed request methods, we got OK (status code `200`) indicating that the proxy allowed it to pass to the destination service. |
| 90 | + |
| 91 | +Let's confirm that the behavior is the same with a `DELETE` request. |
| 92 | + |
| 93 | +```bash |
| 94 | +curl -i -XDELETE \ |
| 95 | + "http://$(docker-machine ip node-1)/demo/hello" |
| 96 | +``` |
| 97 | + |
| 98 | +Just as with the `GET` request, the response is `200`. The proxy allowed it as well. |
| 99 | + |
| 100 | +According to the current configuration, any other request method should be denied. Let's test it with, for example, a `PUT` request. |
| 101 | + |
| 102 | +```bash |
| 103 | +curl -i -XPUT \ |
| 104 | + "http://$(docker-machine ip node-1)/demo/hello" |
| 105 | +``` |
| 106 | + |
| 107 | +``` |
| 108 | +TODO |
| 109 | +``` |
| 110 | + |
| 111 | +This time, the proxy responded with TODO (status code TODO). The request method is not on the list of those that are allowed and proxy choose not to forward it to the destination service. Instead, it returned with TODO. |
| 112 | + |
| 113 | +Similarly, we can choose which methods to deny. |
| 114 | + |
| 115 | +```bash |
| 116 | +docker service update \ |
| 117 | + --label-rm "com.df.allowedMethods" \ |
| 118 | + --label-add "com.df.deniedMethods=DELETE" \ |
| 119 | + go-demo_main |
| 120 | +``` |
| 121 | + |
| 122 | +We removed the `com.df.allowedMethods` label and created `com.df.deniedMethods` with the value `DELETE`. |
| 123 | + |
| 124 | +If we send an `GET` request, the response should be `200` since it is not on the list of those that are denied. |
| 125 | + |
| 126 | +```bash |
| 127 | +curl -i \ |
| 128 | + "http://$(docker-machine ip node-1)/demo/hello" |
| 129 | +``` |
| 130 | + |
| 131 | +On the other hand, if we choose to send an `DELETE` request, the response should be denied. |
| 132 | + |
| 133 | +```bash |
| 134 | +curl -i -XDELETE \ |
| 135 | + "http://$(docker-machine ip node-1)/demo/hello" |
| 136 | +``` |
| 137 | + |
| 138 | +We got the response TODO proving that no one can send a `DELETE` request to our service. |
| 139 | + |
| 140 | +Let's remove the `deniedMethods` label and explore how we can block HTTP request. |
| 141 | + |
| 142 | +```bash |
| 143 | +docker service update \ |
| 144 | + --label-rm "com.df.deniedMethods" \ |
| 145 | + go-demo_main |
| 146 | +``` |
| 147 | + |
| 148 | +## Blocking HTTP Requests |
| 149 | + |
| 150 | +TODO: Continue writing |
| 151 | + |
| 152 | +```bash |
| 153 | +docker service update \ |
| 154 | + --label-add "com.df.denyHttp=true" \ |
| 155 | + go-demo_main |
| 156 | + |
| 157 | +curl -i \ |
| 158 | + "http://$(docker-machine ip node-1)/demo/hello" |
| 159 | + |
| 160 | +# NOTE: No certs, so not HTTPS |
| 161 | +``` |
| 162 | + |
| 163 | +## Summary |
| 164 | + |
| 165 | +TODO: Write |
| 166 | + |
| 167 | +```bash |
| 168 | +docker-machine rm node-1 node-2 node-3 |
| 169 | +``` |
0 commit comments