-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
85 lines (69 loc) · 1.89 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
74
75
76
77
78
79
80
81
82
83
84
85
package vault
import (
"context"
vaultapi "github.com/hashicorp/vault/api"
"github.com/rarimo/passport-identity-provider/internal/config"
"gitlab.com/distributed_lab/figure/v3"
"gitlab.com/distributed_lab/logan/v3/errors"
"math/big"
)
const (
vaultIssuerPath = "issuer"
vaultVerifierPath = "verifier"
)
type VaultClient struct {
client *vaultapi.Client
mountPath string
}
func NewVaultClient(cfg *config.VaultConfig) (*VaultClient, error) {
conf := vaultapi.DefaultConfig()
conf.Address = cfg.Address
client, err := vaultapi.NewClient(conf)
if err != nil {
return nil, errors.Wrap(err, "failed to initialize new client")
}
client.SetToken(cfg.Token)
return &VaultClient{
client: client,
mountPath: cfg.MountPath,
}, nil
}
func (v *VaultClient) IssuerAuthData() (string, string, error) {
conf := struct {
IssuerLogin string `fig:"login,required"`
IssuerPassword string `fig:"password,required"`
}{}
secret, err := v.client.KVv2(v.mountPath).Get(context.Background(), vaultIssuerPath)
if err != nil {
return "", "", errors.Wrap(err, "failed to get secret")
}
if err := figure.
Out(&conf).
With(figure.BaseHooks).
From(secret.Data).
Please(); err != nil {
return "", "", errors.Wrap(err, "failed to figure out")
}
return conf.IssuerLogin, conf.IssuerPassword, nil
}
func (v *VaultClient) Blinder() (*big.Int, error) {
conf := struct {
Blinder string `fig:"blinder,required"`
}{}
secret, err := v.client.KVv2(v.mountPath).Get(context.Background(), vaultVerifierPath)
if err != nil {
return nil, errors.Wrap(err, "failed to get secret")
}
if err := figure.
Out(&conf).
With(figure.BaseHooks).
From(secret.Data).
Please(); err != nil {
return nil, errors.Wrap(err, "failed to figure out")
}
blinder, ok := new(big.Int).SetString(conf.Blinder, 10)
if !ok {
return nil, errors.New("failed to set string to big.Int")
}
return blinder, nil
}