-
Notifications
You must be signed in to change notification settings - Fork 18.9k
Expand file tree
/
Copy pathplugin_install.go
More file actions
175 lines (150 loc) · 5.39 KB
/
plugin_install.go
File metadata and controls
175 lines (150 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package client
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
cerrdefs "github.com/containerd/errdefs"
"github.com/distribution/reference"
"github.com/moby/moby/api/types/plugin"
"github.com/moby/moby/api/types/registry"
)
// PluginInstallOptions holds parameters to install a plugin.
type PluginInstallOptions struct {
Disabled bool
AcceptAllPermissions bool
RegistryAuth string // RegistryAuth is the base64 encoded credentials for the registry
RemoteRef string // RemoteRef is the plugin name on the registry
// PrivilegeFunc is a function that clients can supply to retry operations
// after getting an authorization error. This function returns the registry
// authentication header value in base64 encoded format, or an error if the
// privilege request fails.
//
// For details, refer to [github.com/moby/moby/api/types/registry.RequestAuthConfig].
PrivilegeFunc func(context.Context) (string, error)
AcceptPermissionsFunc func(context.Context, plugin.Privileges) (bool, error)
Args []string
}
// PluginInstallResult holds the result of a plugin install operation.
// It is an io.ReadCloser from which the caller can read installation progress or result.
type PluginInstallResult struct {
io.ReadCloser
}
// PluginInstall installs a plugin
func (cli *Client) PluginInstall(ctx context.Context, name string, options PluginInstallOptions) (_ PluginInstallResult, retErr error) {
query := url.Values{}
if _, err := reference.ParseNormalizedNamed(options.RemoteRef); err != nil {
return PluginInstallResult{}, fmt.Errorf("invalid remote reference: %w", err)
}
query.Set("remote", options.RemoteRef)
privileges, err := cli.checkPluginPermissions(ctx, query, &options)
if err != nil {
return PluginInstallResult{}, err
}
// set name for plugin pull, if empty should default to remote reference
query.Set("name", name)
resp, err := cli.tryPluginPull(ctx, query, privileges, options.RegistryAuth)
if err != nil {
return PluginInstallResult{}, err
}
name = resp.Header.Get("Docker-Plugin-Name")
pr, pw := io.Pipe()
go func() { // todo: the client should probably be designed more around the actual api
_, err := io.Copy(pw, resp.Body)
if err != nil {
_ = pw.CloseWithError(err)
return
}
defer func() {
if retErr != nil {
delResp, _ := cli.delete(ctx, "/plugins/"+name, nil, nil)
ensureReaderClosed(delResp)
}
}()
if len(options.Args) > 0 {
if _, err := cli.PluginSet(ctx, name, PluginSetOptions{Args: options.Args}); err != nil {
_ = pw.CloseWithError(err)
return
}
}
if options.Disabled {
_ = pw.Close()
return
}
_, enableErr := cli.PluginEnable(ctx, name, PluginEnableOptions{Timeout: 0})
_ = pw.CloseWithError(enableErr)
}()
return PluginInstallResult{pr}, nil
}
func (cli *Client) tryPluginPrivileges(ctx context.Context, query url.Values, registryAuth string) (*http.Response, error) {
return cli.get(ctx, "/plugins/privileges", query, http.Header{
registry.AuthHeader: {registryAuth},
})
}
func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, privileges plugin.Privileges, registryAuth string) (*http.Response, error) {
return cli.post(ctx, "/plugins/pull", query, privileges, http.Header{
registry.AuthHeader: {registryAuth},
})
}
func (cli *Client) checkPluginPermissions(ctx context.Context, query url.Values, options pluginOptions) (plugin.Privileges, error) {
resp, err := cli.tryPluginPrivileges(ctx, query, options.getRegistryAuth())
if cerrdefs.IsUnauthorized(err) && options.getPrivilegeFunc() != nil {
// TODO: do inspect before to check existing name before checking privileges
newAuthHeader, privilegeErr := options.getPrivilegeFunc()(ctx)
if privilegeErr != nil {
ensureReaderClosed(resp)
return nil, privilegeErr
}
options.setRegistryAuth(newAuthHeader)
resp, err = cli.tryPluginPrivileges(ctx, query, options.getRegistryAuth())
}
if err != nil {
ensureReaderClosed(resp)
return nil, err
}
var privileges plugin.Privileges
if err := json.NewDecoder(resp.Body).Decode(&privileges); err != nil {
ensureReaderClosed(resp)
return nil, err
}
ensureReaderClosed(resp)
if !options.getAcceptAllPermissions() && options.getAcceptPermissionsFunc() != nil && len(privileges) > 0 {
accept, err := options.getAcceptPermissionsFunc()(ctx, privileges)
if err != nil {
return nil, err
}
if !accept {
return nil, errors.New("permission denied while installing plugin " + options.getRemoteRef())
}
}
return privileges, nil
}
type pluginOptions interface {
getRegistryAuth() string
setRegistryAuth(string)
getPrivilegeFunc() func(context.Context) (string, error)
getAcceptAllPermissions() bool
getAcceptPermissionsFunc() func(context.Context, plugin.Privileges) (bool, error)
getRemoteRef() string
}
func (o *PluginInstallOptions) getRegistryAuth() string {
return o.RegistryAuth
}
func (o *PluginInstallOptions) setRegistryAuth(auth string) {
o.RegistryAuth = auth
}
func (o *PluginInstallOptions) getPrivilegeFunc() func(context.Context) (string, error) {
return o.PrivilegeFunc
}
func (o *PluginInstallOptions) getAcceptAllPermissions() bool {
return o.AcceptAllPermissions
}
func (o *PluginInstallOptions) getAcceptPermissionsFunc() func(context.Context, plugin.Privileges) (bool, error) {
return o.AcceptPermissionsFunc
}
func (o *PluginInstallOptions) getRemoteRef() string {
return o.RemoteRef
}