forked from ketch-com/orlop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
59 lines (47 loc) · 1.35 KB
/
client.go
File metadata and controls
59 lines (47 loc) · 1.35 KB
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
package orlop
import (
"fmt"
"github.com/switch-bit/orlop/log"
"google.golang.org/grpc"
)
// Connect creates a new client from configuration
func Connect(cfg HasClientConfig, vault HasVaultConfig) (*grpc.ClientConn, error) {
var opts []grpc.DialOption
l := log.WithField("url", cfg.GetURL())
if len(cfg.GetURL()) == 0 {
l.Errorf("client: url required")
return nil, fmt.Errorf("client: url required")
}
if cfg.GetTLS().GetEnabled() {
l.Trace("tls enabled")
creds, err := NewClientTLSCredentials(cfg.GetTLS(), vault)
if err != nil {
return nil, err
}
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
l.Trace("tls disabled")
opts = append(opts, grpc.WithInsecure())
}
shared := cfg.GetToken().GetShared()
if len(shared.GetID()) > 0 || len(shared.GetFile()) > 0 || len(shared.GetSecret()) > 0 {
l.Trace("loading token from configuration")
s, err := LoadKey(shared, vault, "secret")
if err != nil {
return nil, err
}
opts = append(opts, grpc.WithPerRPCCredentials(SharedContextCredentials{
token: string(s),
}))
} else {
l.Trace("using context credentials")
opts = append(opts, grpc.WithPerRPCCredentials(ContextCredentials{}))
}
l.Trace("dialling")
conn, err := grpc.Dial(cfg.GetURL(), opts...)
if err != nil {
l.WithError(err).Error("failed dialling")
return nil, err
}
return conn, nil
}