github.com/0xfoo/docker@v1.8.2/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 `sudo` privileges on your local machine or in the VM.
    25  
    26  This sandbox requires you to install two Docker tools: Docker Engine and Docker
    27  Compose. To install the Docker Engine, choose from the [list of supported
    28  platforms]({{< relref "installation.md" >}}). To install Docker Compose, see the
    29  [detailed instructions here]({{< relref "compose/install" >}}).
    30  
    31  Finally, you'll need to have `git` installed on your local system or VM.
    32  
    33  ## What is in the sandbox?
    34  
    35  If you are just using trust out-of-the-box you only need your Docker Engine
    36  client and access to Docker's own public hub. The sandbox mimics a
    37  production trust environment, and requires these additional components:
    38  
    39  | Container       | Description                                                                                                                                 |
    40  |-----------------|---------------------------------------------------------------------------------------------------------------------------------------------|
    41  | nostarysandbox  | A container with the latest version of Docker Engine and with some preconfigured certifications. This is your sandbox where you can use the `docker` client to test trust operations. |
    42  | Registry server | A local registry service.                                                                                                                 |
    43  | Notary server   | The service that does all the heavy-lifting of managing trust                                                                               |
    44  | Notary signer   | A service that ensures that your keys are secure.                                                                                           |
    45  | MySQL           | The database where all of the trust information will be stored                                                                              |
    46  
    47  The sandbox uses the Docker daemon on your local system. Within the `nostarysandbox`
    48  you interact with a local registry rather than the public Docker Hub. This means
    49  your everyday image repositories are not used. They are protected while you play.
    50  
    51  When you play in the sandbox, you'll also create root and tagging keys. The
    52  sandbox is configured to store all the keys and files inside the `notarysandbox`
    53  container. Since the keys you create in the sandbox are for play only,
    54  destroying the container destroys them as well.
    55  
    56  
    57  ## Build the sandbox
    58  
    59  In this section, you build the Docker components for your trust sandbox. If you
    60  work exclusively with the Docker Hub, you would not need with these components.
    61  They are built into the Docker Hub for you. For the sandbox, however, you must
    62  build your own entire, mock production environment and registry.
    63  
    64  ### Configure /etc/hosts
    65  
    66  The sandbox' `notaryserver` and `sandboxregistry` run on your local server. The
    67  client inside the `notarysandbox` container connects to them over your network.
    68  So, you'll need an entry for both the servers in your local `/etc/hosts` file.
    69  
    70  1. Add an entry for the `notaryserver` to `/etc/hosts`.
    71        
    72          $ sudo sh -c 'echo "127.0.0.1 notaryserver" >> /etc/hosts'
    73          
    74  2. Add an entry for the `sandboxregistry` to `/etc/hosts`.
    75  
    76          $ sudo sh -c 'echo "127.0.0.1 sandboxregistry" >> /etc/hosts'
    77  
    78  
    79  ### Build the notarytest image
    80  
    81  1. Create a `notarytest` directory on your system.
    82  
    83          $ mkdir notarysandbox
    84  
    85  2. Change into your `notarysandbox` directory.
    86  
    87          $ cd notarysandbox
    88  
    89  3. Create a `notarytest` directory then change into that.
    90  
    91          $ mkdir notarytest
    92          $ cd nostarytest
    93  
    94  4. Create a filed called `Dockerfile` with your favorite editor.
    95  
    96  5. Add the following to the new file.
    97  
    98          FROM debian:jessie
    99  
   100          ADD https://master.dockerproject.org/linux/amd64/docker /usr/bin/docker
   101          RUN chmod +x /usr/bin/docker \
   102            && apt-get update \
   103            && apt-get install -y \
   104            tree \
   105            vim \
   106            git \
   107            ca-certificates \
   108            --no-install-recommends
   109  
   110          WORKDIR /root
   111          RUN git clone -b trust-sandbox https://github.com/docker/notary.git
   112          RUN cp /root/notary/fixtures/root-ca.crt /usr/local/share/ca-certificates/root-ca.crt
   113          RUN update-ca-certificates
   114  
   115          ENTRYPOINT ["bash"]
   116          
   117  6. Save and close the file.
   118  
   119  7. Build the testing container.
   120  
   121          $ docker build -t nostarysandbox .
   122          Sending build context to Docker daemon 2.048 kB
   123          Step 0 : FROM debian:jessie
   124           ...
   125           Successfully built 5683f17e9d72
   126       
   127  
   128  ### Build and start up the trust servers
   129  
   130  In this step, you get the source code for your notary and registry services.
   131  Then, you'll use Docker Compose to build and start them on your local system.
   132  
   133  1. Change to back to the root of your  `notarysandbox` directory.
   134  
   135          $ cd notarysandbox
   136  
   137  2. Clone the `notary` project.
   138  
   139            $ git clone -b trust-sandbox https://github.com/docker/notary.git
   140            
   141  3. Clone the `distribution` project.
   142  
   143          $ git clone https://github.com/docker/distribution.git
   144  
   145  4. Change to the `notary` project directory.
   146  
   147          $ cd notary
   148  
   149     The directory contains a `docker-compose` file that you'll use to run a
   150     notary server together with a notary signer and the corresponding MySQL
   151     databases. The databases store the trust information for an image.
   152  
   153  5. Build the server images.
   154  
   155          $  docker-compose build 
   156          
   157      The first time you run this, the build takes some time.
   158          
   159  6. Run the server containers on your local system.
   160  
   161          $ docker-compose up -d
   162  
   163      Once the trust services are up, you'll setup a local version of the Docker
   164      Registry v2. 
   165      
   166  7. Change to the `nostarysandbox/distribution` directory.
   167  
   168  8. Build the `sandboxregistry` server.
   169  
   170          $ docker build -t sandboxregistry .
   171  
   172  9. Start the `sandboxregistry` server running. 
   173          
   174          $ docker run -p 5000:5000 --name sandboxregistry sandboxregistry &
   175  
   176  ## Playing in the sandbox
   177  
   178  Now that everything is setup, you can go into your `nostarysandbox` container and
   179  start testing Docker content trust. 
   180  
   181  
   182  ### Start the notarysandbox container
   183  
   184  In this procedure, you start the `notarysandbox` and link it to the running
   185  `notary_notaryserver_1` and `sandboxregistry` containers. The links allow
   186  communication among the containers.
   187  
   188  ```
   189  $ docker run -it -v /var/run/docker.sock:/var/run/docker.sock --link notary_notaryserver_1:notaryserver --link sandboxregistry:sandboxregistry nostarysandbox
   190  root@0710762bb59a:/# 
   191  ```
   192  
   193  Mounting the `docker.sock` gives the `nostarysandbox` access to the `docker`
   194  deamon on your host, while storing all the keys and files inside the sandbox
   195  container.  When you destroy the container, you destroy the "play" keys.
   196  
   197  ### Test some trust operations
   198  
   199  Now, you'll pull some images. 
   200  
   201  1. Download a `docker` image to test with.
   202  
   203          # docker pull docker/trusttest 
   204          docker pull docker/trusttest
   205          Using default tag: latest
   206          latest: Pulling from docker/trusttest
   207  
   208          b3dbab3810fc: Pull complete 
   209          a9539b34a6ab: Pull complete 
   210          Digest: sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a
   211          Status: Downloaded newer image for docker/trusttest:latest
   212  
   213  2. Tag it to be pushed to our sandbox registry: 
   214  
   215          # docker tag docker/trusttest sandboxregistry:5000/test/trusttest:latest
   216  
   217  3. Enable content trust.
   218  
   219          # export DOCKER_CONTENT_TRUST=1
   220          
   221  4. Identify the trust server.
   222  
   223          # export DOCKER_CONTENT_TRUST_SERVER=https://notaryserver:4443
   224  
   225      This step is only necessary because the sandbox is using its own server.
   226      Normally, if you are using the Docker Public Hub this step isn't necessary.
   227  
   228  5. Pull the test image.
   229  
   230          # docker pull sandboxregistry:5000/test/trusttest
   231          Using default tag: latest
   232          no trust data available
   233  
   234        You see an error, because this content doesn't exist on the `sandboxregistry` yet.
   235  
   236  6. Push the trusted image.
   237  
   238          # docker push sandboxregistry:5000/test/trusttest:latest
   239          The push refers to a repository [sandboxregistry:5000/test/trusttest] (len: 1)
   240          a9539b34a6ab: Image successfully pushed 
   241          b3dbab3810fc: Image successfully pushed 
   242          latest: digest: sha256:1d871dcb16805f0604f10d31260e79c22070b35abc71a3d1e7ee54f1042c8c7c size: 3348
   243          Signing and pushing trust metadata
   244          You are about to create a new root signing key passphrase. This passphrase
   245          will be used to protect the most sensitive key in your signing system. Please
   246          choose a long, complex passphrase and be careful to keep the password and the
   247          key file itself secure and backed up. It is highly recommended that you use a
   248          password manager to generate the passphrase and keep it safe. There will be no
   249          way to recover this key. You can find the key in your config directory.
   250          Enter passphrase for new offline key with id 8c69e04: 
   251          Repeat passphrase for new offline key with id 8c69e04: 
   252          Enter passphrase for new tagging key with id sandboxregistry:5000/test/trusttest (93c362a): 
   253          Repeat passphrase for new tagging key with id sandboxregistry:5000/test/trusttest (93c362a): 
   254          Finished initializing "sandboxregistry:5000/test/trusttest"
   255          latest: digest: sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a size: 3355
   256          Signing and pushing trust metadata
   257  
   258  7. Try pulling the image you just pushed:
   259  
   260          # docker pull sandboxregistry:5000/test/trusttest  
   261          Using default tag: latest
   262          Pull (1 of 1): sandboxregistry:5000/test/trusttest:latest@sha256:1d871dcb16805f0604f10d31260e79c22070b35abc71a3d1e7ee54f1042c8c7c
   263          sha256:1d871dcb16805f0604f10d31260e79c22070b35abc71a3d1e7ee54f1042c8c7c: Pulling from test/trusttest
   264          b3dbab3810fc: Already exists 
   265          a9539b34a6ab: Already exists 
   266          Digest: sha256:1d871dcb16805f0604f10d31260e79c22070b35abc71a3d1e7ee54f1042c8c7c
   267          Status: Downloaded newer image for sandboxregistry:5000/test/trusttest@sha256:1d871dcb16805f0604f10d31260e79c22070b35abc71a3d1e7ee54f1042c8c7c
   268          Tagging sandboxregistry:5000/test/trusttest@sha256:1d871dcb16805f0604f10d31260e79c22070b35abc71a3d1e7ee54f1042c8c7c as sandboxregistry:5000/test/trusttest:latest
   269  
   270  
   271  ### Test with malicious images
   272  
   273  What happens when data is corrupted and you try to pull it when trust is
   274  enabled? In this section, you go into the `sandboxregistry` and tamper with some
   275  data. Then, you try and pull it.
   276  
   277  1. Leave the sandbox container running.
   278  
   279  2. Open a new bash terminal from your host into the `sandboxregistry`.
   280  
   281          $ docker exec -it sandboxregistry bash
   282          296db6068327#
   283  
   284  3. Change into the registry storage. 
   285  
   286      You'll need to provide the `sha` you received when you pushed the image.
   287  
   288          # cd /var/lib/registry/docker/registry/v2/blobs/sha256/aa/aac0c133338db2b18ff054943cee3267fe50c75cdee969aed88b1992539ed042
   289  
   290  4. Add malicious data to one of the trusttest layers:
   291  
   292          # echo "Malicious data" > data
   293  
   294  5. Got back to your sandbox terminal.
   295  
   296  6. List the trusttest image.
   297  
   298          # docker images | grep trusttest
   299          docker/trusttest                 latest              a9539b34a6ab        7 weeks ago         5.025 MB
   300          sandboxregistry:5000/test/trusttest   latest              a9539b34a6ab        7 weeks ago         5.025 MB
   301          sandboxregistry:5000/test/trusttest   <none>              a9539b34a6ab        7 weeks ago         5.025 MB
   302  
   303  7. Remove the `trusttest:latest` image.
   304  
   305          # docker rmi -f a9539b34a6ab
   306          Untagged: docker/trusttest:latest
   307          Untagged: sandboxregistry:5000/test/trusttest:latest
   308          Untagged: sandboxregistry:5000/test/trusttest@sha256:1d871dcb16805f0604f10d31260e79c22070b35abc71a3d1e7ee54f1042c8c7c
   309          Deleted: a9539b34a6aba01d3942605dfe09ab821cd66abf3cf07755b0681f25ad81f675
   310          Deleted: b3dbab3810fc299c21f0894d39a7952b363f14520c2f3d13443c669b63b6aa20
   311  
   312  8. Pull the image again.
   313  
   314          # docker pull sandboxregistry:5000/test/trusttest
   315          Using default tag: latest
   316          ...
   317          b3dbab3810fc: Verifying Checksum 
   318          a9539b34a6ab: Pulling fs layer 
   319          filesystem layer verification failed for digest sha256:aac0c133338db2b18ff054943cee3267fe50c75cdee969aed88b1992539ed042
   320  
   321        You'll see the the pull did not complete because the trust system was
   322        unable to verify the image.
   323  
   324  ## More play in the sandbox
   325  
   326  Now, that you have a full Docker content trust sandbox on your local system,
   327  feel free to play with it and see how it behaves. If you find any security
   328  issues with Docker, feel free to send us an email at <security@docker.com>.
   329  
   330  
   331  &nbsp;