-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.go
More file actions
267 lines (240 loc) · 8.6 KB
/
adapter.go
File metadata and controls
267 lines (240 loc) · 8.6 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package external
import (
"context"
"fmt"
"net/http"
"path/filepath"
"github.com/GoCodeAlone/modular"
"github.com/GoCodeAlone/workflow/capability"
"github.com/GoCodeAlone/workflow/config"
"github.com/GoCodeAlone/workflow/deploy"
"github.com/GoCodeAlone/workflow/plugin"
pb "github.com/GoCodeAlone/workflow/plugin/external/proto"
"github.com/GoCodeAlone/workflow/schema"
"google.golang.org/protobuf/types/known/emptypb"
"gopkg.in/yaml.v3"
)
// ExternalPluginAdapter wraps a gRPC plugin client to implement plugin.EnginePlugin.
// The engine sees this as a regular plugin — no changes to engine.go needed.
type ExternalPluginAdapter struct {
name string
client *PluginClient
manifest *pb.Manifest
configFragment []byte
pluginDir string
}
// NewExternalPluginAdapter creates an adapter from a connected plugin client.
func NewExternalPluginAdapter(name string, client *PluginClient) (*ExternalPluginAdapter, error) {
ctx := context.Background()
manifest, err := client.client.GetManifest(ctx, &emptypb.Empty{})
if err != nil {
return nil, fmt.Errorf("get manifest from plugin %s: %w", name, err)
}
a := &ExternalPluginAdapter{
name: name,
client: client,
manifest: manifest,
}
// Fetch config fragment eagerly so it's available before BuildFromConfig runs.
if resp, fragErr := client.client.GetConfigFragment(ctx, &emptypb.Empty{}); fragErr == nil && len(resp.YamlConfig) > 0 {
a.configFragment = resp.YamlConfig
a.pluginDir = resp.PluginDir
}
return a, nil
}
// --- NativePlugin interface ---
func (a *ExternalPluginAdapter) Name() string { return a.manifest.Name }
func (a *ExternalPluginAdapter) Version() string { return a.manifest.Version }
func (a *ExternalPluginAdapter) Description() string { return a.manifest.Description }
func (a *ExternalPluginAdapter) Dependencies() []plugin.PluginDependency { return nil }
func (a *ExternalPluginAdapter) UIPages() []plugin.UIPageDef { return nil }
func (a *ExternalPluginAdapter) RegisterRoutes(_ *http.ServeMux) {}
func (a *ExternalPluginAdapter) OnEnable(_ plugin.PluginContext) error { return nil }
func (a *ExternalPluginAdapter) OnDisable(_ plugin.PluginContext) error { return nil }
// --- EnginePlugin interface ---
func (a *ExternalPluginAdapter) EngineManifest() *plugin.PluginManifest {
ctx := context.Background()
modTypes, _ := a.client.client.GetModuleTypes(ctx, &emptypb.Empty{})
stepTypes, _ := a.client.client.GetStepTypes(ctx, &emptypb.Empty{})
triggerTypes, _ := a.client.client.GetTriggerTypes(ctx, &emptypb.Empty{})
m := &plugin.PluginManifest{
Name: a.manifest.Name,
Version: a.manifest.Version,
Author: a.manifest.Author,
Description: a.manifest.Description,
}
if modTypes != nil {
m.ModuleTypes = modTypes.Types
}
if stepTypes != nil {
m.StepTypes = stepTypes.Types
}
if triggerTypes != nil {
m.TriggerTypes = triggerTypes.Types
}
return m
}
// GetAsset fetches a static asset from the plugin by path.
func (a *ExternalPluginAdapter) GetAsset(path string) ([]byte, string, error) {
resp, err := a.client.client.GetAsset(context.Background(), &pb.GetAssetRequest{Path: path})
if err != nil {
return nil, "", err
}
if resp.Error != "" {
return nil, "", fmt.Errorf("%s", resp.Error)
}
return resp.Content, resp.ContentType, nil
}
// IsSamplePlugin returns true if the plugin declares a sample category.
func (a *ExternalPluginAdapter) IsSamplePlugin() bool {
return a.manifest.SampleCategory != ""
}
// IsConfigMutable returns true if tenants can override the plugin's config fragment.
func (a *ExternalPluginAdapter) IsConfigMutable() bool {
return a.manifest.ConfigMutable
}
// SampleCategory returns the sample category declared by the plugin.
func (a *ExternalPluginAdapter) SampleCategory() string {
return a.manifest.SampleCategory
}
// ConfigFragmentBytes returns the raw YAML config fragment fetched from the plugin.
func (a *ExternalPluginAdapter) ConfigFragmentBytes() []byte {
return a.configFragment
}
func (a *ExternalPluginAdapter) Capabilities() []capability.Contract {
return nil
}
func (a *ExternalPluginAdapter) ModuleFactories() map[string]plugin.ModuleFactory {
ctx := context.Background()
resp, err := a.client.client.GetModuleTypes(ctx, &emptypb.Empty{})
if err != nil || resp == nil {
return nil
}
factories := make(map[string]plugin.ModuleFactory, len(resp.Types))
for _, typeName := range resp.Types {
tn := typeName // capture
factories[tn] = func(name string, cfg map[string]any) modular.Module {
createResp, createErr := a.client.client.CreateModule(ctx, &pb.CreateModuleRequest{
Type: tn,
Name: name,
Config: mapToStruct(cfg),
})
if createErr != nil || createResp.Error != "" {
return nil
}
remote := NewRemoteModule(name, createResp.HandleId, a.client.client)
if tn == "security.scanner" {
return NewSecurityScannerRemoteModule(remote)
}
return remote
}
}
return factories
}
func (a *ExternalPluginAdapter) StepFactories() map[string]plugin.StepFactory {
ctx := context.Background()
resp, err := a.client.client.GetStepTypes(ctx, &emptypb.Empty{})
if err != nil || resp == nil {
return nil
}
factories := make(map[string]plugin.StepFactory, len(resp.Types))
for _, typeName := range resp.Types {
tn := typeName // capture
factories[tn] = func(name string, cfg map[string]any, _ modular.Application) (any, error) {
createResp, createErr := a.client.client.CreateStep(ctx, &pb.CreateStepRequest{
Type: tn,
Name: name,
Config: mapToStruct(cfg),
})
if createErr != nil {
return nil, fmt.Errorf("create remote step %s: %w", tn, createErr)
}
if createResp.Error != "" {
return nil, fmt.Errorf("create remote step %s: %s", tn, createResp.Error)
}
return NewRemoteStep(name, createResp.HandleId, a.client.client, cfg), nil
}
}
return factories
}
func (a *ExternalPluginAdapter) TriggerFactories() map[string]plugin.TriggerFactory {
ctx := context.Background()
resp, err := a.client.client.GetTriggerTypes(ctx, &emptypb.Empty{})
if err != nil || resp == nil || len(resp.Types) == 0 {
return nil
}
factories := make(map[string]plugin.TriggerFactory, len(resp.Types))
for _, typeName := range resp.Types {
tn := typeName // capture
factories[tn] = func() any {
createResp, createErr := a.client.client.CreateModule(ctx, &pb.CreateModuleRequest{
Type: tn,
Name: tn,
Config: nil,
})
if createErr != nil || createResp.Error != "" {
return nil
}
return NewRemoteTrigger(tn, tn, createResp.HandleId, a.client.client, nil)
}
}
return factories
}
func (a *ExternalPluginAdapter) WorkflowHandlers() map[string]plugin.WorkflowHandlerFactory {
return nil
}
func (a *ExternalPluginAdapter) ModuleSchemas() []*schema.ModuleSchema {
ctx := context.Background()
resp, err := a.client.client.GetModuleSchemas(ctx, &emptypb.Empty{})
if err != nil || resp == nil {
return nil
}
schemas := make([]*schema.ModuleSchema, 0, len(resp.Schemas))
for _, ps := range resp.Schemas {
schemas = append(schemas, protoSchemaToSchema(ps))
}
return schemas
}
func (a *ExternalPluginAdapter) StepSchemas() []*schema.StepSchema {
// External plugins provide step schemas via their plugin.json manifest (StepSchemas field).
// The manifest is loaded by the PluginLoader, so we return nil here.
return nil
}
func (a *ExternalPluginAdapter) WiringHooks() []plugin.WiringHook {
return nil
}
func (a *ExternalPluginAdapter) DeployTargets() map[string]deploy.DeployTarget {
return nil
}
func (a *ExternalPluginAdapter) SidecarProviders() map[string]deploy.SidecarProvider {
return nil
}
func (a *ExternalPluginAdapter) ConfigTransformHooks() []plugin.ConfigTransformHook {
if len(a.configFragment) == 0 {
return nil
}
return []plugin.ConfigTransformHook{
{
Name: a.manifest.Name + "-config-merge",
Priority: 100,
Hook: func(cfg *config.WorkflowConfig) error {
var fragment config.WorkflowConfig
if err := yaml.Unmarshal(a.configFragment, &fragment); err != nil {
return fmt.Errorf("failed to parse config fragment from plugin %s: %w", a.manifest.Name, err)
}
// Resolve relative paths against plugin directory.
if a.pluginDir != "" {
for i := range fragment.Modules {
if mc, ok := fragment.Modules[i].Config["root"].(string); ok && !filepath.IsAbs(mc) {
fragment.Modules[i].Config["root"] = filepath.Join(a.pluginDir, mc)
}
}
}
config.MergeConfigs(cfg, &fragment)
return nil
},
},
}
}
// Ensure ExternalPluginAdapter satisfies plugin.EnginePlugin at compile time.
var _ plugin.EnginePlugin = (*ExternalPluginAdapter)(nil)