-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (61 loc) · 1.54 KB
/
main.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
package main
import (
"context"
"os"
"sync"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/sdk/plugin"
)
type backend struct {
framework.Backend
initOnce sync.Once
thingWeNeedToInitialize int
}
func (b *backend) init() {
// Initialize here
b.thingWeNeedToInitialize = 42
}
func (b *backend) doSomething(ctx context.Context, request *logical.Request, data *framework.FieldData) (*logical.Response, error) {
// Initialization actually triggered here
b.initOnce.Do(b.init)
return &logical.Response{
Data: map[string]interface{}{
"hello": "world",
"thingWeNeedToInitialize": b.thingWeNeedToInitialize,
},
}, nil
}
func factory(context.Context, *logical.BackendConfig) (logical.Backend, error) {
var b backend
b.Backend = framework.Backend{
Paths: []*framework.Path{
{
Pattern: "something",
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.doSomething,
},
},
},
},
BackendType: logical.TypeLogical,
}
// Don't initialize more than necessary here
return &b, nil
}
func main() {
var apiClientMeta api.PluginAPIClientMeta
err := apiClientMeta.FlagSet().Parse(os.Args[1:])
if err != nil {
panic(err)
}
err = plugin.Serve(&plugin.ServeOpts{
BackendFactoryFunc: factory,
TLSProviderFunc: api.VaultPluginTLSProvider(apiClientMeta.GetTLSConfig()),
})
if err != nil {
panic(err)
}
}