/
plugin.go
153 lines (133 loc) · 5.42 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
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v0
import (
"context"
"fmt"
"github.com/hashicorp/go-plugin"
grpc "google.golang.org/grpc"
)
const (
handshakeCookieKey = "JVS_PLUGIN"
handshakeCookieValue = "cc400ef1c6e74ee20be491c6013ae2120fb04c11703d05fbbf18dbb2e5e0"
// DefaultJustificationCategory is the default justification category
// supported. An "explanation" justification represents a manual free text
// reason from the requester.
DefaultJustificationCategory = "explanation"
DefaultJustificationDisplayName = "Explanation"
DefaultJustificationHint = "A justification reason in free-form text."
)
// DefaultJustificationValidator is the [Validator] for the
// [DefaultJustificationCategory].
var DefaultJustificationValidator = &ExplanationValidator{}
// Handshake is a common handshake that is shared by plugin and host.
// handshakeConfigs are used to just do a basic handshake between
// a plugin and host. If the handshake fails, a user friendly error is shown.
// This prevents users from executing bad plugins or executing a plugin
// directory. It is a UX feature, not a security feature.
var Handshake = plugin.HandshakeConfig{
// This isn't required when using VersionedPlugins
ProtocolVersion: 1,
MagicCookieKey: handshakeCookieKey,
MagicCookieValue: handshakeCookieValue,
}
// The interface we are exposing as a plugin.
type Validator interface {
Validate(context.Context, *ValidateJustificationRequest) (*ValidateJustificationResponse, error)
GetUIData(context.Context, *GetUIDataRequest) (*UIData, error)
}
// ExplanationValidator is the built-in [Validator] for the "explanation"
// justifications. An "explanation" justification represents a manual free text
// reason from the requester.
type ExplanationValidator struct{}
// Validate only checks if the input is not empty.
func (v *ExplanationValidator) Validate(_ context.Context, req *ValidateJustificationRequest) (*ValidateJustificationResponse, error) {
if req.Justification == nil || req.Justification.Value == "" {
return &ValidateJustificationResponse{
Valid: false,
Error: []string{"explanation cannot be empty"},
}, nil
}
return &ValidateJustificationResponse{Valid: true}, nil
}
// GetUIData retrieves plugin's display data.
func (v *ExplanationValidator) GetUIData(_ context.Context, req *GetUIDataRequest) (*UIData, error) {
return &UIData{
DisplayName: DefaultJustificationDisplayName,
Hint: DefaultJustificationHint,
}, nil
}
// ValidatorPlugin implements [plugin.GRPCPlugin].
//
// [plugin.GRPCPlugin]: https://github.com/hashicorp/go-plugin/blob/a88a423a8813d0b26c8e3219f71b0f30447b5d2e/plugin.go#L36
type ValidatorPlugin struct {
// GRPCPlugin must still implement the Plugin interface.
plugin.Plugin
// Concrete implementation, written in Go. This is only used for plugins
// that are written in Go.
Impl Validator
}
// GRPCServer is required by [plugin.GRPCPlugin].
//
// [plugin.GRPCPlugin]: https://github.com/hashicorp/go-plugin/blob/a88a423a8813d0b26c8e3219f71b0f30447b5d2e/plugin.go#L36
func (p *ValidatorPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
RegisterJVSPluginServer(s, &PluginServer{Impl: p.Impl})
return nil
}
// GRPCClient is required by [plugin.GRPCPlugin].
//
// [plugin.GRPCPlugin]: https://github.com/hashicorp/go-plugin/blob/a88a423a8813d0b26c8e3219f71b0f30447b5d2e/plugin.go#L36
func (p *ValidatorPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (any, error) {
return &PluginClient{client: NewJVSPluginClient(c)}, nil
}
// PluginClient is an implementation of Validator that talks over RPC.
type PluginClient struct {
client JVSPluginClient
}
func (m *PluginClient) Validate(ctx context.Context, req *ValidateJustificationRequest) (*ValidateJustificationResponse, error) {
resp, err := m.client.Validate(ctx, req)
if err != nil {
return resp, fmt.Errorf("failed to validate justification: %w", err)
}
return resp, nil
}
// GetUIData retrieves plugin's display data.
func (m *PluginClient) GetUIData(ctx context.Context, req *GetUIDataRequest) (*UIData, error) {
resp, err := m.client.GetUIData(ctx, req)
if err != nil {
return resp, fmt.Errorf("failed to get UI data: %w", err)
}
return resp, nil
}
// Here is the gRPC server that PluginClient talks to.
type PluginServer struct {
JVSPluginServer
// This is the real implementation
Impl Validator
}
func (m *PluginServer) Validate(ctx context.Context, req *ValidateJustificationRequest) (*ValidateJustificationResponse, error) {
resp, err := m.Impl.Validate(ctx, req)
if err != nil {
return resp, fmt.Errorf("failed to validate justification: %w", err)
}
return resp, nil
}
// GetUIData retrieves plugin's display data.
func (m *PluginServer) GetUIData(ctx context.Context, req *GetUIDataRequest) (*UIData, error) {
resp, err := m.Impl.GetUIData(ctx, req)
if err != nil {
return resp, fmt.Errorf("failed to get UI data: %w", err)
}
return resp, nil
}