github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/moby/integration/plugin/common/plugin_test.go (about)

     1  package common // import "github.com/docker/docker/integration/plugin/common"
     2  
     3  import (
     4  	"context"
     5  	"encoding/base64"
     6  	"encoding/json"
     7  	"io"
     8  	"io/ioutil"
     9  	"net"
    10  	"net/http"
    11  	"path"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/docker/docker/api/types"
    16  	"github.com/docker/docker/testutil/daemon"
    17  	"github.com/docker/docker/testutil/fixtures/plugin"
    18  	"github.com/docker/docker/testutil/registry"
    19  	"github.com/docker/docker/testutil/request"
    20  	"gotest.tools/v3/assert"
    21  	is "gotest.tools/v3/assert/cmp"
    22  	"gotest.tools/v3/skip"
    23  )
    24  
    25  func TestPluginInvalidJSON(t *testing.T) {
    26  	defer setupTest(t)()
    27  
    28  	endpoints := []string{"/plugins/foobar/set"}
    29  
    30  	for _, ep := range endpoints {
    31  		t.Run(ep, func(t *testing.T) {
    32  			t.Parallel()
    33  
    34  			res, body, err := request.Post(ep, request.RawString("{invalid json"), request.JSON)
    35  			assert.NilError(t, err)
    36  			assert.Equal(t, res.StatusCode, http.StatusBadRequest)
    37  
    38  			buf, err := request.ReadBody(body)
    39  			assert.NilError(t, err)
    40  			assert.Check(t, is.Contains(string(buf), "invalid character 'i' looking for beginning of object key string"))
    41  
    42  			res, body, err = request.Post(ep, request.JSON)
    43  			assert.NilError(t, err)
    44  			assert.Equal(t, res.StatusCode, http.StatusBadRequest)
    45  
    46  			buf, err = request.ReadBody(body)
    47  			assert.NilError(t, err)
    48  			assert.Check(t, is.Contains(string(buf), "got EOF while reading request body"))
    49  		})
    50  	}
    51  }
    52  
    53  func TestPluginInstall(t *testing.T) {
    54  	skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
    55  	skip.If(t, testEnv.OSType == "windows")
    56  	skip.If(t, testEnv.IsRootless, "rootless mode has different view of localhost")
    57  
    58  	ctx := context.Background()
    59  	client := testEnv.APIClient()
    60  
    61  	t.Run("no auth", func(t *testing.T) {
    62  		defer setupTest(t)()
    63  
    64  		reg := registry.NewV2(t)
    65  		defer reg.Close()
    66  
    67  		name := "test-" + strings.ToLower(t.Name())
    68  		repo := path.Join(registry.DefaultURL, name+":latest")
    69  		assert.NilError(t, plugin.CreateInRegistry(ctx, repo, nil))
    70  
    71  		rdr, err := client.PluginInstall(ctx, repo, types.PluginInstallOptions{Disabled: true, RemoteRef: repo})
    72  		assert.NilError(t, err)
    73  		defer rdr.Close()
    74  
    75  		_, err = io.Copy(ioutil.Discard, rdr)
    76  		assert.NilError(t, err)
    77  
    78  		_, _, err = client.PluginInspectWithRaw(ctx, repo)
    79  		assert.NilError(t, err)
    80  	})
    81  
    82  	t.Run("with htpasswd", func(t *testing.T) {
    83  		defer setupTest(t)()
    84  
    85  		reg := registry.NewV2(t, registry.Htpasswd)
    86  		defer reg.Close()
    87  
    88  		name := "test-" + strings.ToLower(t.Name())
    89  		repo := path.Join(registry.DefaultURL, name+":latest")
    90  		auth := &types.AuthConfig{ServerAddress: registry.DefaultURL, Username: "testuser", Password: "testpassword"}
    91  		assert.NilError(t, plugin.CreateInRegistry(ctx, repo, auth))
    92  
    93  		authEncoded, err := json.Marshal(auth)
    94  		assert.NilError(t, err)
    95  
    96  		rdr, err := client.PluginInstall(ctx, repo, types.PluginInstallOptions{
    97  			RegistryAuth: base64.URLEncoding.EncodeToString(authEncoded),
    98  			Disabled:     true,
    99  			RemoteRef:    repo,
   100  		})
   101  		assert.NilError(t, err)
   102  		defer rdr.Close()
   103  
   104  		_, err = io.Copy(ioutil.Discard, rdr)
   105  		assert.NilError(t, err)
   106  
   107  		_, _, err = client.PluginInspectWithRaw(ctx, repo)
   108  		assert.NilError(t, err)
   109  	})
   110  	t.Run("with insecure", func(t *testing.T) {
   111  		skip.If(t, !testEnv.IsLocalDaemon())
   112  
   113  		addrs, err := net.InterfaceAddrs()
   114  		assert.NilError(t, err)
   115  
   116  		var bindTo string
   117  		for _, addr := range addrs {
   118  			ip, ok := addr.(*net.IPNet)
   119  			if !ok {
   120  				continue
   121  			}
   122  			if ip.IP.IsLoopback() || ip.IP.To4() == nil {
   123  				continue
   124  			}
   125  			bindTo = ip.IP.String()
   126  		}
   127  
   128  		if bindTo == "" {
   129  			t.Skip("No suitable interface to bind registry to")
   130  		}
   131  
   132  		regURL := bindTo + ":5000"
   133  
   134  		d := daemon.New(t)
   135  		defer d.Stop(t)
   136  
   137  		d.Start(t, "--insecure-registry="+regURL)
   138  		defer d.Stop(t)
   139  
   140  		reg := registry.NewV2(t, registry.URL(regURL))
   141  		defer reg.Close()
   142  
   143  		name := "test-" + strings.ToLower(t.Name())
   144  		repo := path.Join(regURL, name+":latest")
   145  		assert.NilError(t, plugin.CreateInRegistry(ctx, repo, nil, plugin.WithInsecureRegistry(regURL)))
   146  
   147  		client := d.NewClientT(t)
   148  		rdr, err := client.PluginInstall(ctx, repo, types.PluginInstallOptions{Disabled: true, RemoteRef: repo})
   149  		assert.NilError(t, err)
   150  		defer rdr.Close()
   151  
   152  		_, err = io.Copy(ioutil.Discard, rdr)
   153  		assert.NilError(t, err)
   154  
   155  		_, _, err = client.PluginInspectWithRaw(ctx, repo)
   156  		assert.NilError(t, err)
   157  	})
   158  	// TODO: test insecure registry with https
   159  }