-
Notifications
You must be signed in to change notification settings - Fork 458
/
plugin.go
184 lines (158 loc) · 5.2 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
package catalog
import (
"context"
"fmt"
"io"
"sort"
"time"
"github.com/sirupsen/logrus"
"github.com/spiffe/spire-plugin-sdk/pluginsdk"
"github.com/spiffe/spire-plugin-sdk/private"
"github.com/spiffe/spire/pkg/common/telemetry"
"google.golang.org/grpc"
)
const (
deinitTimeout = 10 * time.Second
)
// Plugin is a loaded plugin.
type Plugin interface {
// Closer is used to unload the plugin. Any facades initialized by the
// call to bind are considered invalidated after the plugin is closed.
io.Closer
// Bind binds the given facades to the plugin. It also returns a Configurer
// that can be used to configure the plugin. If the plugin does not support
// a given facade, an error will be returned. This function is designed
// only for use by unit-tests for built-in plugin implementations or fake
// facade implementations that rely on built-ins.
Bind(facades ...Facade) (Configurer, error)
}
type pluginImpl struct {
closerGroup
conn grpc.ClientConnInterface
info PluginInfo
log logrus.FieldLogger
grpcServiceNames []string
}
func newPlugin(ctx context.Context, conn grpc.ClientConnInterface, info PluginInfo, log logrus.FieldLogger, closers closerGroup, hostServices []pluginsdk.ServiceServer) (*pluginImpl, error) {
grpcServiceNames, err := initPlugin(ctx, conn, hostServices)
if err != nil {
return nil, err
}
closers = append(closers, closerFunc(func() {
ctx, cancel := context.WithTimeout(context.Background(), deinitTimeout)
defer cancel()
if err := private.Deinit(ctx, conn); err != nil {
log.WithError(err).Error("Failed to deinitialize plugin")
} else {
log.Debug("Plugin deinitialized")
}
}))
return &pluginImpl{
conn: conn,
info: info,
log: log,
closerGroup: closers,
grpcServiceNames: grpcServiceNames,
}, nil
}
// Bind implements the Plugin interface method of the same name.
func (p *pluginImpl) Bind(facades ...Facade) (Configurer, error) {
grpcServiceNames := grpcServiceNameSet(p.grpcServiceNames)
for _, facade := range facades {
if _, ok := grpcServiceNames[facade.GRPCServiceName()]; !ok {
return nil, fmt.Errorf("plugin does not support facade service %q", facade.GRPCServiceName())
}
p.initFacade(facade)
}
configurer, err := p.makeConfigurer(grpcServiceNames)
if err != nil {
return nil, err
}
if configurer == nil {
configurer = configurerUnsupported{}
}
return configurer, nil
}
func (p *pluginImpl) bindFacade(repo bindable, facade Facade) any {
impl := p.initFacade(facade)
repo.bind(facade)
return impl
}
func (p *pluginImpl) initFacade(facade Facade) any {
facade.InitInfo(p.info)
facade.InitLog(p.log)
return facade.InitClient(p.conn)
}
func (p *pluginImpl) bindRepos(pluginRepo bindablePluginRepo, serviceRepos []bindableServiceRepo) (Configurer, error) {
grpcServiceNames := grpcServiceNameSet(p.grpcServiceNames)
impl := p.bindRepo(pluginRepo, grpcServiceNames)
for _, serviceRepo := range serviceRepos {
p.bindRepo(serviceRepo, grpcServiceNames)
}
configurer, err := p.makeConfigurer(grpcServiceNames)
if err != nil {
return nil, err
}
switch {
case impl == nil:
return nil, fmt.Errorf("no supported plugin interface found in: %q", p.grpcServiceNames)
case len(grpcServiceNames) > 0:
for _, grpcServiceName := range sortStringSet(grpcServiceNames) {
p.log.WithField(telemetry.PluginService, grpcServiceName).Warn("Unsupported plugin service found")
}
}
return configurer, nil
}
func (p *pluginImpl) makeConfigurer(grpcServiceNames map[string]struct{}) (Configurer, error) {
repo := new(configurerRepo)
bindable, err := makeBindableServiceRepo(repo)
if err != nil {
return nil, err
}
p.bindRepo(bindable, grpcServiceNames)
return repo.configurer, nil
}
func (p *pluginImpl) bindRepo(repo bindableServiceRepo, grpcServiceNames map[string]struct{}) any {
versions := repo.Versions()
var impl any
for _, version := range versions {
facade := version.New()
if _, ok := grpcServiceNames[facade.GRPCServiceName()]; ok {
delete(grpcServiceNames, facade.GRPCServiceName())
// Use the first matching version (in case the plugin implements
// more than one). The rest will be removed from the list of
// service names above so we can properly warn of unhandled
// services without false negatives.
if impl != nil {
continue
}
warnIfDeprecated(p.log, version, versions[0])
impl = p.bindFacade(repo, facade)
}
}
return impl
}
func warnIfDeprecated(log logrus.FieldLogger, thisVersion, latestVersion Version) {
if thisVersion.Deprecated() {
log = log.WithField(telemetry.DeprecatedServiceName, thisVersion.New().GRPCServiceName())
if !latestVersion.Deprecated() {
log = log.WithField(telemetry.PreferredServiceName, latestVersion.New().GRPCServiceName())
}
log.Warn("Service is deprecated and will be removed in a future release")
}
}
func grpcServiceNameSet(grpcServiceNames []string) map[string]struct{} {
set := make(map[string]struct{})
for _, grpcServiceName := range grpcServiceNames {
set[grpcServiceName] = struct{}{}
}
return set
}
func sortStringSet(set map[string]struct{}) []string {
ss := make([]string, 0, len(set))
for s := range set {
ss = append(ss, s)
}
sort.Strings(ss)
return ss
}