github.com/dpiddy/docker@v1.12.2-rc1/docs/security/trust/trust_sandbox.md (about) 1 <!--[metadata]> 2 +++ 3 title = "Play in a content trust sandbox" 4 description = "Play in a trust sandbox" 5 keywords = ["trust, security, root, keys, repository, sandbox"] 6 [menu.main] 7 parent= "smn_content_trust" 8 +++ 9 <![end-metadata]--> 10 11 # Play in a content trust sandbox 12 13 This page explains how to set up and use a sandbox for experimenting with trust. 14 The sandbox allows you to configure and try trust operations locally without 15 impacting your production images. 16 17 Before working through this sandbox, you should have read through the [trust 18 overview](content_trust.md). 19 20 ### Prerequisites 21 22 These instructions assume you are running in Linux or Mac OS X. You can run 23 this sandbox on a local machine or on a virtual machine. You will need to 24 have privileges to run docker commands on your local machine or in the VM. 25 26 This sandbox requires you to install two Docker tools: Docker Engine >= 1.10.0 27 and Docker Compose >= 1.6.0. To install the Docker Engine, choose from the 28 [list of supported platforms](../../installation/index.md). To install 29 Docker Compose, see the 30 [detailed instructions here](https://docs.docker.com/compose/install/). 31 32 Finally, you'll need to have a text editor installed on your local system or VM. 33 34 ## What is in the sandbox? 35 36 If you are just using trust out-of-the-box you only need your Docker Engine 37 client and access to the Docker hub. The sandbox mimics a 38 production trust environment, and sets up these additional components. 39 40 | Container | Description | 41 |-----------------|---------------------------------------------------------------------------------------------------------------------------------------------| 42 | trustsandbox | A container with the latest version of Docker Engine and with some preconfigured certificates. This is your sandbox where you can use the `docker` client to test trust operations. | 43 | Registry server | A local registry service. | 44 | Notary server | The service that does all the heavy-lifting of managing trust | 45 46 This means you will be running your own content trust (Notary) server and registry. 47 If you work exclusively with the Docker Hub, you would not need with these components. 48 They are built into the Docker Hub for you. For the sandbox, however, you build 49 your own entire, mock production environment. 50 51 Within the `trustsandbox` container, you interact with your local registry rather 52 than the Docker Hub. This means your everyday image repositories are not used. 53 They are protected while you play. 54 55 When you play in the sandbox, you'll also create root and repository keys. The 56 sandbox is configured to store all the keys and files inside the `trustsandbox` 57 container. Since the keys you create in the sandbox are for play only, 58 destroying the container destroys them as well. 59 60 By using a docker-in-docker image for the `trustsandbox` container, you will also 61 not pollute your real docker daemon cache with any images you push and pull. The 62 images will instead be stored in an anonymous volume attached to this container, 63 and can be destroyed after you destroy the container. 64 65 ## Build the sandbox 66 67 In this section, you'll use Docker Compose to specify how to set up and link together 68 the `trustsandbox` container, the Notary server, and the Registry server. 69 70 71 1. Create a new `trustsandbox` directory and change into it. 72 73 $ mkdir `trustsandbox` 74 $ cd `trustsandbox` 75 76 2. Create a filed called `docker-compose.yml` with your favorite editor. For example, using vim: 77 78 $ touch docker-compose.yml 79 $ vim docker-compose.yml 80 81 3. Add the following to the new file. 82 83 version: "2" 84 services: 85 notaryserver: 86 image: dockersecurity/notary_autobuilds:server 87 volumes: 88 - notarycerts:/go/src/github.com/docker/notary/fixtures 89 networks: 90 - sandbox 91 environment: 92 - NOTARY_SERVER_STORAGE_TYPE=memory 93 - NOTARY_SERVER_TRUST_SERVICE_TYPE=local 94 sandboxregistry: 95 image: registry:2.4.1 96 networks: 97 - sandbox 98 container_name: sandboxregistry 99 trustsandbox: 100 image: docker:dind 101 networks: 102 - sandbox 103 volumes: 104 - notarycerts:/notarycerts 105 privileged: true 106 container_name: trustsandbox 107 entrypoint: "" 108 command: |- 109 sh -c ' 110 cp /notarycerts/root-ca.crt /usr/local/share/ca-certificates/root-ca.crt && 111 update-ca-certificates && 112 dockerd-entrypoint.sh --insecure-registry sandboxregistry:5000' 113 volumes: 114 notarycerts: 115 external: false 116 networks: 117 sandbox: 118 external: false 119 120 4. Save and close the file. 121 122 5. Run the containers on your local system. 123 124 $ docker-compose up -d 125 126 The first time you run this, the docker-in-docker, Notary server, and registry 127 images will be first downloaded from Docker Hub. 128 129 130 ## Playing in the sandbox 131 132 Now that everything is setup, you can go into your `trustsandbox` container and 133 start testing Docker content trust. From your host machine, obtain a shell 134 in the `trustsandbox` container. 135 136 $ docker exec -it trustsandbox sh 137 / # 138 139 ### Test some trust operations 140 141 Now, you'll pull some images from within the `trustsandbox` container. 142 143 1. Download a `docker` image to test with. 144 145 / # docker pull docker/trusttest 146 docker pull docker/trusttest 147 Using default tag: latest 148 latest: Pulling from docker/trusttest 149 150 b3dbab3810fc: Pull complete 151 a9539b34a6ab: Pull complete 152 Digest: sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a 153 Status: Downloaded newer image for docker/trusttest:latest 154 155 2. Tag it to be pushed to our sandbox registry: 156 157 / # docker tag docker/trusttest sandboxregistry:5000/test/trusttest:latest 158 159 3. Enable content trust. 160 161 / # export DOCKER_CONTENT_TRUST=1 162 163 4. Identify the trust server. 164 165 / # export DOCKER_CONTENT_TRUST_SERVER=https://notaryserver:4443 166 167 This step is only necessary because the sandbox is using its own server. 168 Normally, if you are using the Docker Public Hub this step isn't necessary. 169 170 5. Pull the test image. 171 172 / # docker pull sandboxregistry:5000/test/trusttest 173 Using default tag: latest 174 Error: remote trust data does not exist for sandboxregistry:5000/test/trusttest: notaryserver:4443 does not have trust data for sandboxregistry:5000/test/trusttest 175 176 You see an error, because this content doesn't exist on the `notaryserver` yet. 177 178 6. Push and sign the trusted image. 179 180 / # docker push sandboxregistry:5000/test/trusttest:latest 181 The push refers to a repository [sandboxregistry:5000/test/trusttest] 182 5f70bf18a086: Pushed 183 c22f7bc058a9: Pushed 184 latest: digest: sha256:ebf59c538accdf160ef435f1a19938ab8c0d6bd96aef8d4ddd1b379edf15a926 size: 734 185 Signing and pushing trust metadata 186 You are about to create a new root signing key passphrase. This passphrase 187 will be used to protect the most sensitive key in your signing system. Please 188 choose a long, complex passphrase and be careful to keep the password and the 189 key file itself secure and backed up. It is highly recommended that you use a 190 password manager to generate the passphrase and keep it safe. There will be no 191 way to recover this key. You can find the key in your config directory. 192 Enter passphrase for new root key with ID 27ec255: 193 Repeat passphrase for new root key with ID 27ec255: 194 Enter passphrase for new repository key with ID 58233f9 (sandboxregistry:5000/test/trusttest): 195 Repeat passphrase for new repository key with ID 58233f9 (sandboxregistry:5000/test/trusttest): 196 Finished initializing "sandboxregistry:5000/test/trusttest" 197 Successfully signed "sandboxregistry:5000/test/trusttest":latest 198 199 Because you are pushing this repository for the first time, docker creates new root and repository keys and asks you for passphrases with which to encrypt them. If you push again after this, it will only ask you for repository passphrase so it can decrypt the key and sign again. 200 201 7. Try pulling the image you just pushed: 202 203 / # docker pull sandboxregistry:5000/test/trusttest 204 Using default tag: latest 205 Pull (1 of 1): sandboxregistry:5000/test/trusttest:latest@sha256:ebf59c538accdf160ef435f1a19938ab8c0d6bd96aef8d4ddd1b379edf15a926 206 sha256:ebf59c538accdf160ef435f1a19938ab8c0d6bd96aef8d4ddd1b379edf15a926: Pulling from test/trusttest 207 Digest: sha256:ebf59c538accdf160ef435f1a19938ab8c0d6bd96aef8d4ddd1b379edf15a926 208 Status: Downloaded newer image for sandboxregistry:5000/test/trusttest@sha256:ebf59c538accdf160ef435f1a19938ab8c0d6bd96aef8d4ddd1b379edf15a926 209 Tagging sandboxregistry:5000/test/trusttest@sha256:ebf59c538accdf160ef435f1a19938ab8c0d6bd96aef8d4ddd1b379edf15a926 as sandboxregistry:5000/test/trusttest:latest 210 211 212 ### Test with malicious images 213 214 What happens when data is corrupted and you try to pull it when trust is 215 enabled? In this section, you go into the `sandboxregistry` and tamper with some 216 data. Then, you try and pull it. 217 218 1. Leave the `trustsandbox` shell and and container running. 219 220 2. Open a new interactive terminal from your host, and obtain a shell into the 221 `sandboxregistry` container. 222 223 $ docker exec -it sandboxregistry bash 224 root@65084fc6f047:/# 225 226 3. List the layers for the `test/trusttest` image you pushed: 227 228 root@65084fc6f047:/# ls -l /var/lib/registry/docker/registry/v2/repositories/test/trusttest/_layers/sha256 229 total 12 230 drwxr-xr-x 2 root root 4096 Jun 10 17:26 a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4 231 drwxr-xr-x 2 root root 4096 Jun 10 17:26 aac0c133338db2b18ff054943cee3267fe50c75cdee969aed88b1992539ed042 232 drwxr-xr-x 2 root root 4096 Jun 10 17:26 cc7629d1331a7362b5e5126beb5bf15ca0bf67eb41eab994c719a45de53255cd 233 234 4. Change into the registry storage for one of those layers (note that this is in a different directory) 235 236 root@65084fc6f047:/# cd /var/lib/registry/docker/registry/v2/blobs/sha256/aa/aac0c133338db2b18ff054943cee3267fe50c75cdee969aed88b1992539ed042 237 238 5. Add malicious data to one of the trusttest layers: 239 240 root@65084fc6f047:/# echo "Malicious data" > data 241 242 6. Go back to your `trustsandbox` terminal. 243 244 7. List the trusttest image. 245 246 / # docker images | grep trusttest 247 REPOSITORY TAG IMAGE ID CREATED SIZE 248 docker/trusttest latest cc7629d1331a 11 months ago 5.025 MB 249 sandboxregistry:5000/test/trusttest latest cc7629d1331a 11 months ago 5.025 MB 250 sandboxregistry:5000/test/trusttest <none> cc7629d1331a 11 months ago 5.025 MB 251 252 8. Remove the `trusttest:latest` image from our local cache. 253 254 / # docker rmi -f cc7629d1331a 255 Untagged: docker/trusttest:latest 256 Untagged: sandboxregistry:5000/test/trusttest:latest 257 Untagged: sandboxregistry:5000/test/trusttest@sha256:ebf59c538accdf160ef435f1a19938ab8c0d6bd96aef8d4ddd1b379edf15a926 258 Deleted: sha256:cc7629d1331a7362b5e5126beb5bf15ca0bf67eb41eab994c719a45de53255cd 259 Deleted: sha256:2a1f6535dc6816ffadcdbe20590045e6cbf048d63fd4cc753a684c9bc01abeea 260 Deleted: sha256:c22f7bc058a9a8ffeb32989b5d3338787e73855bf224af7aa162823da015d44c 261 262 Docker does not re-download images that it already has cached, but we want 263 Docker to attempt to download the tampered image from the registry and reject 264 it because it is invalid. 265 266 8. Pull the image again. This will download the image from the registry, because we don't have it cached. 267 268 / # docker pull sandboxregistry:5000/test/trusttest 269 Using default tag: latest 270 Pull (1 of 1): sandboxregistry:5000/test/trusttest:latest@sha256:35d5bc26fd358da8320c137784fe590d8fcf9417263ef261653e8e1c7f15672e 271 sha256:35d5bc26fd358da8320c137784fe590d8fcf9417263ef261653e8e1c7f15672e: Pulling from test/trusttest 272 273 aac0c133338d: Retrying in 5 seconds 274 a3ed95caeb02: Download complete 275 error pulling image configuration: unexpected EOF 276 277 You'll see the pull did not complete because the trust system was 278 unable to verify the image. 279 280 ## More play in the sandbox 281 282 Now, that you have a full Docker content trust sandbox on your local system, 283 feel free to play with it and see how it behaves. If you find any security 284 issues with Docker, feel free to send us an email at <security@docker.com>. 285 286 287 ## Cleaning up your sandbox 288 289 When you are done, and want to clean up all the services you've started and any 290 anonymous volumes that have been created, just run the following command in the 291 directory where you've created your Docker Compose file: 292 293 $ docker-compose down -v