Skip to content

Commit f0582ec

Browse files
committed
fix: interim goplugin from hashicorp
1 parent 6b8689c commit f0582ec

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
lines changed

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ require (
2222
gopkg.in/yaml.v3 v3.0.1
2323
)
2424

25-
require github.com/aws/aws-sdk-go-v2/service/signin v1.0.1 // indirect
25+
require (
26+
github.com/aws/aws-sdk-go-v2/service/signin v1.0.1 // indirect
27+
github.com/golang/protobuf v1.5.4 // indirect
28+
github.com/hashicorp/yamux v0.1.2 // indirect
29+
github.com/oklog/run v1.1.0 // indirect
30+
)
2631

2732
require (
2833
cloud.google.com/go/auth v0.17.0 // indirect
@@ -59,6 +64,7 @@ require (
5964
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
6065
github.com/hashicorp/go-hclog v1.6.3 // indirect
6166
github.com/hashicorp/go-multierror v1.1.1 // indirect
67+
github.com/hashicorp/go-plugin v1.7.0
6268
github.com/hashicorp/go-retryablehttp v0.7.8 // indirect
6369
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
6470
github.com/hashicorp/go-secure-stdlib/awsutil v0.3.0 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB1
154154
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
155155
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
156156
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
157+
github.com/hashicorp/go-plugin v1.7.0 h1:YghfQH/0QmPNc/AZMTFE3ac8fipZyZECHdDPshfk+mA=
158+
github.com/hashicorp/go-plugin v1.7.0/go.mod h1:BExt6KEaIYx804z8k4gRzRLEvxKVb+kn0NMcihqOqb8=
157159
github.com/hashicorp/go-retryablehttp v0.7.8 h1:ylXZWnqa7Lhqpk0L1P1LzDtGcCR0rPVUrx/c8Unxc48=
158160
github.com/hashicorp/go-retryablehttp v0.7.8/go.mod h1:rjiScheydd+CxvumBsIrFKlx3iS0jrZ7LvzFGFmuKbw=
159161
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
@@ -174,6 +176,8 @@ github.com/hashicorp/vault/api v1.22.0 h1:+HYFquE35/B74fHoIeXlZIP2YADVboaPjaSicH
174176
github.com/hashicorp/vault/api v1.22.0/go.mod h1:IUZA2cDvr4Ok3+NtK2Oq/r+lJeXkeCrHRmqdyWfpmGM=
175177
github.com/hashicorp/vault/api/auth/aws v0.11.0 h1:lWdUxrzvPotg6idNr62al4w97BgI9xTDdzMCTViNH2s=
176178
github.com/hashicorp/vault/api/auth/aws v0.11.0/go.mod h1:PWqdH/xqaudapmnnGP9ip2xbxT/kRW2qEgpqiQff6Gc=
179+
github.com/hashicorp/yamux v0.1.2 h1:XtB8kyFOyHXYVFnwT5C3+Bdo8gArse7j2AQ0DA0Uey8=
180+
github.com/hashicorp/yamux v0.1.2/go.mod h1:C+zze2n6e/7wshOZep2A70/aQU6QBRWJO/G6FT1wIns=
177181
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
178182
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
179183
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
@@ -205,6 +209,8 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
205209
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
206210
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
207211
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
212+
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
213+
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
208214
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
209215
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
210216
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

internal/plugin/plugin.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package plugin
22

3-
import "github.com/DevLabFoundry/configmanager/v3/internal/config"
3+
import (
4+
"net/rpc"
5+
6+
"github.com/DevLabFoundry/configmanager/v3/internal/config"
7+
"github.com/hashicorp/go-plugin"
8+
)
49

510
// Plugin is responsible for managing plugins within configmanager
611
//
@@ -14,3 +19,58 @@ type Plugin struct {
1419
fallbackPaths []string
1520
engineInstance *Engine
1621
}
22+
23+
// ValueProvider is the interface that we're exposing as a plugin.
24+
type ValueProvider interface {
25+
Value(token string, metadata string) (string, error)
26+
}
27+
28+
// Here is an implementation that talks over RPC
29+
type StorePluginRPC struct{ client *rpc.Client }
30+
31+
func (g *StorePluginRPC) Greet() string {
32+
var resp string
33+
err := g.client.Call("Plugin.Greet", new(interface{}), &resp)
34+
if err != nil {
35+
// You usually want your interfaces to return errors. If they don't,
36+
// there isn't much other choice here.
37+
panic(err)
38+
}
39+
40+
return resp
41+
}
42+
43+
// Here is the RPC server that GreeterRPC talks to, conforming to
44+
// the requirements of net/rpc
45+
type GreeterRPCServer struct {
46+
// This is the real implementation
47+
Impl ValueProvider
48+
}
49+
50+
func (s *GreeterRPCServer) Greet(args interface{}, resp *string) error {
51+
*resp = s.Impl.Value()
52+
return nil
53+
}
54+
55+
// This is the implementation of plugin.Plugin so we can serve/consume this
56+
//
57+
// This has two methods: Server must return an RPC server for this plugin
58+
// type. We construct a GreeterRPCServer for this.
59+
//
60+
// Client must return an implementation of our interface that communicates
61+
// over an RPC client. We return GreeterRPC for this.
62+
//
63+
// Ignore MuxBroker. That is used to create more multiplexed streams on our
64+
// plugin connection and is a more advanced use case.
65+
type GreeterPlugin struct {
66+
// Impl Injection
67+
Impl ValueProvider
68+
}
69+
70+
func (p *GreeterPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
71+
return &GreeterRPCServer{Impl: p.Impl}, nil
72+
}
73+
74+
func (GreeterPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
75+
return &StorePluginRPC{client: c}, nil
76+
}

internal/plugin/provider.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package plugin
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"os/exec"
8+
9+
"github.com/hashicorp/go-hclog"
10+
"github.com/hashicorp/go-plugin"
11+
"github.com/hashicorp/go-plugin/examples/basic/shared"
12+
)
13+
14+
// handshakeConfigs are used to just do a basic handshake between
15+
// a plugin and host. If the handshake fails, a user friendly error is shown.
16+
// This prevents users from executing bad plugins or executing a plugin
17+
// directory. It is a UX feature, not a security feature.
18+
var handshakeConfig = plugin.HandshakeConfig{
19+
ProtocolVersion: 1,
20+
MagicCookieKey: "BASIC_PLUGIN",
21+
MagicCookieValue: "hello",
22+
}
23+
24+
// pluginMap is the map of plugins we can dispense.
25+
var pluginMap = map[string]plugin.Plugin{
26+
"greeter": &shared.GreeterPlugin{},
27+
}
28+
29+
func Init() {
30+
// Create an hclog.Logger
31+
logger := hclog.New(&hclog.LoggerOptions{
32+
Name: "plugin",
33+
Output: os.Stdout,
34+
Level: hclog.Debug,
35+
})
36+
37+
// We're a host! Start by launching the plugin process.
38+
client := plugin.NewClient(&plugin.ClientConfig{
39+
HandshakeConfig: handshakeConfig,
40+
Plugins: pluginMap,
41+
Cmd: exec.Command("./plugin/greeter"),
42+
Logger: logger,
43+
})
44+
defer client.Kill()
45+
46+
// Connect via RPC
47+
rpcClient, err := client.Client()
48+
if err != nil {
49+
log.Fatal(err)
50+
}
51+
52+
// Request the plugin
53+
raw, err := rpcClient.Dispense("greeter")
54+
if err != nil {
55+
log.Fatal(err)
56+
}
57+
58+
// We should have a Greeter now! This feels like a normal interface
59+
// implementation but is in fact over an RPC connection.
60+
greeter := raw.(shared.Greeter)
61+
fmt.Println(greeter.Greet())
62+
}

0 commit comments

Comments
 (0)