forked from open-horizon/vault-exchange-auth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend.go
92 lines (79 loc) · 2.17 KB
/
backend.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
package openhorizon
import (
"fmt"
"net/http"
"github.com/openbao/openbao/api/v2"
"github.com/openbao/openbao/sdk/v2/framework"
"github.com/openbao/openbao/sdk/v2/logical"
)
const AUTH_USER_KEY = "id"
const AUTH_TOKEN_KEY = "token"
const CONFIG_EXCHANGE_URL_KEY = "url"
const CONFIG_TOKEN_KEY = "token"
const CONFIG_AGBOT_RENEWAL_KEY = "renewal"
const CONFIG_VAULT_API_KEY = "apiurl"
type backend struct {
// The bao auth plugin framework.
*framework.Backend
// An HTTP client used to call the openhorizon exchange.
httpClient *http.Client
// A bao client used to interact with the system.
vc *api.Client
}
func OHAuthPlugin(c *logical.BackendConfig) *backend {
var b backend
var err error
b.httpClient, err = NewHTTPClient()
if err != nil {
panic(ohlog(fmt.Sprintf("could not establish HTTP client, error: %v", err)))
}
b.vc, err = api.NewClient(nil)
if err != nil {
panic(ohlog(fmt.Sprintf("could not create bao client, error: %v", err)))
}
b.Backend = &framework.Backend{
BackendType: logical.TypeCredential,
AuthRenew: b.pathAuthRenew,
PathsSpecial: &logical.Paths{
Unauthenticated: []string{"login"},
SealWrapStorage: []string{"config"},
},
Paths: []*framework.Path{
&framework.Path{
Pattern: "login",
Fields: map[string]*framework.FieldSchema{
AUTH_USER_KEY: &framework.FieldSchema{
Type: framework.TypeString,
},
AUTH_TOKEN_KEY: &framework.FieldSchema{
Type: framework.TypeString,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathAuthLogin,
},
},
&framework.Path{
Pattern: "config",
Fields: map[string]*framework.FieldSchema{
CONFIG_EXCHANGE_URL_KEY: &framework.FieldSchema{
Type: framework.TypeString,
},
CONFIG_TOKEN_KEY: &framework.FieldSchema{
Type: framework.TypeString,
},
CONFIG_AGBOT_RENEWAL_KEY: &framework.FieldSchema{
Type: framework.TypeInt,
},
CONFIG_VAULT_API_KEY: &framework.FieldSchema{
Type: framework.TypeString,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathConfig,
},
},
},
}
return &b
}