-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
151 lines (120 loc) · 2.83 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"syscall"
"github.com/hashicorp/vault/api"
"github.com/pkg/errors"
)
type App struct {
config *Config
client *api.Client
}
func socketSecretListen(ctx context.Context, client *api.Client, mount *api.KVv2, socketRoot string, secret Secret) {
sockPath := socketRoot + secret.SocketPath
err := os.RemoveAll(sockPath)
if err != nil {
log.Fatalf("%+v", err)
return
}
log.Printf("Listening on %s for secret path %s", sockPath, secret.VaultPath)
// Ensure created unix sockets are mode 0700
syscall.Umask(0077)
ln, err := net.Listen("unix", sockPath)
if err != nil {
log.Print(err)
return
}
for {
c, err := ln.Accept()
if err != nil {
fmt.Println(err)
continue
}
log.Printf("Serving secret value for %s on socket %s", secret.VaultPath, sockPath)
obj, err := mount.Get(ctx, secret.VaultPath)
if err != nil {
log.Print(err)
return
}
if secret.Field != "" {
value := obj.Data[secret.Field].(string)
if _, err = c.Write([]byte(value)); err != nil {
log.Print(err)
return
}
} else {
if _, err = c.Write([]byte(fmt.Sprintf("%+v", obj))); err != nil {
log.Print(err)
return
}
}
if err = c.Close(); err != nil {
log.Print(err)
}
}
}
func newApp(config *Config) *App {
return &App{
config: config,
}
}
func setupVault(app *App) error {
apiConfig := api.DefaultConfig()
if app.config.VaultServer != nil {
apiConfig.Address = *app.config.VaultServer
}
client, err := api.NewClient(apiConfig)
if err != nil {
return errors.Wrap(err, "error creating Vault API client")
}
app.client = client
return nil
}
var (
configPath = flag.String("config", "config.yml", "YAML Configuration file.")
)
func main() {
flag.Parse()
config, err := newConfig(*configPath)
if err != nil {
log.Fatalf("Error reading configuration: %+v", err)
}
app := newApp(config)
if err = setupVault(app); err != nil {
log.Fatalf("Error configuring Vault client: %+v", err)
}
kv := app.client.KVv2(config.VaultMount)
ctx := context.Background()
// Start a unix socket listener for each configured secret
for _, secretCfg := range config.Secrets {
go func(secret Secret) {
socketSecretListen(ctx, app.client, kv, app.config.SocketRoot, secret)
}(secretCfg)
}
// Register and handle interrupt signals to make sure we clean up
// the unix sockets nicely.
signalChan := make(chan os.Signal, 1)
done := make(chan struct{})
signal.Notify(signalChan, os.Interrupt)
go func() {
<-signalChan
log.Print("Received interrupt: cleaning up...")
for _, secret := range config.Secrets {
sockPath := app.config.SocketRoot + secret.SocketPath
err := os.Remove(sockPath)
if err != nil {
log.Print(err)
} else {
log.Printf("Removed socket %s", sockPath)
}
}
close(done)
}()
<-done
}