-
Notifications
You must be signed in to change notification settings - Fork 23
/
plugin.go
244 lines (216 loc) · 7.45 KB
/
plugin.go
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package plugin
import (
"encoding/json"
"fmt"
"io"
"sync"
"github.com/open-component-model/ocm/pkg/cobrautils/flagsets"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/credentials/identity/hostpath"
action2 "github.com/open-component-model/ocm/pkg/contexts/datacontext/action"
"github.com/open-component-model/ocm/pkg/contexts/oci/identity"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/cache"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/config"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/cmds/accessmethod"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/cmds/accessmethod/compose"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/cmds/accessmethod/get"
accval "github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/cmds/accessmethod/validate"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/cmds/action"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/cmds/action/execute"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/cmds/download"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/cmds/upload"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/cmds/upload/put"
uplval "github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/cmds/upload/validate"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/runtime"
)
type Plugin = *pluginImpl
var _ config.Target = (*pluginImpl)(nil)
type impl = cache.Plugin
// //nolint: errname // is no error.
type pluginImpl struct {
lock sync.RWMutex
ctx ocm.Context
impl
config json.RawMessage
}
func NewPlugin(ctx ocm.Context, impl cache.Plugin, config json.RawMessage) Plugin {
return &pluginImpl{
ctx: ctx,
impl: impl,
config: config,
}
}
func (p *pluginImpl) Context() ocm.Context {
return p.ctx
}
func (p *pluginImpl) ConfigurePlugin(name string, data json.RawMessage) {
if name == p.Name() {
p.config = data
}
}
func (p *pluginImpl) SetConfig(config json.RawMessage) {
p.lock.Lock()
defer p.lock.Unlock()
p.config = config
}
func (p *pluginImpl) Action(spec ppi.ActionSpec, creds json.RawMessage) (ppi.ActionResult, error) {
desc := p.GetActionDescriptor(spec.GetKind())
if desc == nil {
return nil, errors.ErrNotSupported(KIND_ACTION, spec.GetKind(), KIND_PLUGIN, p.Name())
}
if desc.ConsumerType != "" {
cid := spec.GetConsumerAttributes()
cid[identity.ID_TYPE] = desc.ConsumerType
c, err := credentials.CredentialsForConsumer(p.Context(), credentials.ConsumerIdentity(cid), hostpath.Matcher)
if err != nil || c == nil {
return nil, errors.ErrNotFound(credentials.KIND_CREDENTIALS, cid.String())
}
creds, err = json.Marshal(c.Properties())
if err != nil {
return nil, errors.Wrapf(err, "cannot marshal credentials")
}
}
data, err := action2.EncodeActionSpec(spec)
if err != nil {
return nil, err
}
args := []string{action.Name, execute.Name, string(data)}
if creds != nil {
args = append(args, "--"+get.OptCreds, string(creds))
}
result, err := p.Exec(nil, nil, args...)
if err != nil {
return nil, errors.Wrapf(err, "plugin %s", p.Name())
}
info, err := action2.DecodeActionResult(result)
if err != nil {
return nil, errors.Wrapf(err, "plugin %s: cannot unmarshal action result", p.Name())
}
return info, nil
}
func (p *pluginImpl) ValidateAccessMethod(spec []byte) (*ppi.AccessSpecInfo, error) {
result, err := p.Exec(nil, nil, accessmethod.Name, accval.Name, string(spec))
if err != nil {
return nil, errors.Wrapf(err, "plugin %s", p.Name())
}
var info ppi.AccessSpecInfo
err = json.Unmarshal(result, &info)
if err != nil {
return nil, errors.Wrapf(err, "plugin %s: cannot unmarshal access spec info", p.Name())
}
return &info, nil
}
func (p *pluginImpl) ComposeAccessMethod(name string, opts flagsets.ConfigOptions, base flagsets.Config) error {
cfg := flagsets.Config{}
for _, o := range opts.Options() {
cfg[o.GetName()] = o.Value()
}
optsdata, err := json.Marshal(cfg)
if err != nil {
return errors.Wrapf(err, "cannot marshal option values")
}
basedata, err := json.Marshal(base)
if err != nil {
return errors.Wrapf(err, "cannot marshal access specification base value")
}
result, err := p.Exec(nil, nil, accessmethod.Name, compose.Name, name, string(optsdata), string(basedata))
if err != nil {
return err
}
var r flagsets.Config
err = json.Unmarshal(result, &r)
if err != nil {
return errors.Wrapf(err, "cannot unmarshal composition result")
}
for k := range base {
delete(base, k)
}
for k, v := range r {
base[k] = v
}
return nil
}
func (p *pluginImpl) ValidateUploadTarget(name string, spec []byte) (*ppi.UploadTargetSpecInfo, error) {
result, err := p.Exec(nil, nil, upload.Name, uplval.Name, name, string(spec))
if err != nil {
return nil, errors.Wrapf(err, "plugin uploader %s/%s", p.Name(), name)
}
var info ppi.UploadTargetSpecInfo
err = json.Unmarshal(result, &info)
if err != nil {
return nil, errors.Wrapf(err, "plugin uploader %s/%s: cannot unmarshal upload target info", p.Name(), name)
}
return &info, nil
}
func (p *pluginImpl) Get(w io.Writer, creds, spec json.RawMessage) error {
args := []string{accessmethod.Name, get.Name, string(spec)}
if creds != nil {
args = append(args, "--"+get.OptCreds, string(creds))
}
_, err := p.Exec(nil, w, args...)
return err
}
func (p *pluginImpl) Put(name string, r io.Reader, artType, mimeType, hint string, creds, target json.RawMessage) (ocm.AccessSpec, error) {
args := []string{upload.Name, put.Name, name, string(target)}
if creds != nil {
args = append(args, "--"+put.OptCreds, string(creds))
}
if hint != "" {
args = append(args, "--"+put.OptHint, hint)
}
if mimeType != "" {
args = append(args, "--"+put.OptMedia, mimeType)
}
if artType != "" {
args = append(args, "--"+put.OptArt, artType)
}
result, err := p.Exec(r, nil, args...)
if err != nil {
return nil, err
}
var m map[string]interface{}
err = json.Unmarshal(result, &m)
if err != nil {
return nil, errors.Wrapf(err, "cannot unmarshal put result")
}
if len(m) == 0 {
return nil, nil // not used
}
return p.ctx.AccessSpecForConfig(result, runtime.DefaultJSONEncoding)
}
func (p *pluginImpl) Download(name string, r io.Reader, artType, mimeType, target string) (bool, string, error) {
args := []string{download.Name, name, target}
if mimeType != "" {
args = append(args, "--"+put.OptMedia, mimeType)
}
if artType != "" {
args = append(args, "--"+put.OptArt, artType)
}
result, err := p.Exec(r, nil, args...)
if err != nil {
return true, "", err
}
var m download.Result
err = json.Unmarshal(result, &m)
if err != nil {
return true, "", errors.Wrapf(err, "cannot unmarshal put result")
}
if m.Error != "" {
return true, "", fmt.Errorf("%s", m.Error)
}
return m.Path != "", m.Path, nil
}
func (p *pluginImpl) Exec(r io.Reader, w io.Writer, args ...string) ([]byte, error) {
if len(p.config) == 0 {
p.ctx.Logger(TAG).Debug("execute plugin action", "path", p.Path(), "args", args)
} else {
p.ctx.Logger(TAG).Debug("execute plugin action", "path", p.Path(), "args", args, "config", p.config)
}
return cache.Exec(p.Path(), p.config, r, w, args...)
}