-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (158 loc) · 4.68 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"context"
"fmt"
"log"
"os"
"os/user"
"strings"
"git.cloud.cluster.fun/AverageMarcus/kube-1password-secrets/internal/onepassword"
apiv1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
)
const (
fieldManagerName = "kube-1password-secrets"
idAnnotation = "kube-1password"
vaultAnnotation = "kube-1password/vault"
usernameAnnotation = "kube-1password/username-key"
passwordAnnotation = "kube-1password/password-key"
secretTextAnnotation = "kube-1password/secret-text-key"
secretTextParseAnnotation = "kube-1password/secret-text-parse"
)
var (
opClient *onepassword.Client
clientset *kubernetes.Clientset
)
func main() {
var err error
opClient, err = buildOpClient()
if err != nil {
panic(err.Error())
}
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
stopper := make(chan struct{})
defer close(stopper)
factory := informers.NewSharedInformerFactory(clientset, 0)
secretInformer := factory.Core().V1().Secrets()
informer := secretInformer.Informer()
defer runtime.HandleCrash()
go factory.Start(stopper)
if !cache.WaitForCacheSync(stopper, informer.HasSynced) {
runtime.HandleError(fmt.Errorf("Timed out waiting for caches to sync"))
return
}
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { processSecret(obj.(*apiv1.Secret)) },
UpdateFunc: func(old interface{}, new interface{}) {
managedFields := new.(*apiv1.Secret).GetManagedFields()
if len(managedFields) == 0 || managedFields[len(managedFields)-1].Manager != fieldManagerName {
processSecret(new.(*apiv1.Secret))
}
},
DeleteFunc: func(interface{}) {},
})
lister := secretInformer.Lister().Secrets((v1.NamespaceAll))
secrets, err := lister.List(labels.Everything())
for _, s := range secrets {
processSecret(s)
}
<-stopper
}
func processSecret(s *apiv1.Secret) {
if passwordID, exists := s.ObjectMeta.Annotations[idAnnotation]; exists {
log.Printf("[INFO] Syncing secret %s with 1Password secret %s\n", s.GetName(), passwordID)
keys := parseAnnotations(s.ObjectMeta.Annotations)
vault := keys["vault"]
item, err := opClient.GetSecret(vault, passwordID)
if err != nil {
if strings.Contains(err.Error(), "session expired") {
opClient, err = buildOpClient()
if err != nil {
panic(err.Error())
}
item, err = opClient.GetSecret(vault, passwordID)
if err != nil {
log.Println("[ERROR] Could not get secret", err)
return
}
} else {
log.Println("[ERROR] Could not get secret", err)
return
}
}
s.Data = make(map[string][]byte)
if item.Username != "" {
s.Data[keys["username"]] = []byte(parseNewlines(item.Username))
}
if item.Password != "" {
s.Data[keys["password"]] = []byte(parseNewlines(item.Password))
}
if item.SecretText != "" {
if s.ObjectMeta.Annotations[secretTextParseAnnotation] != "" {
// Parse secret text as individual secrets
lines := strings.Split(item.SecretText, "\n")
for _, line := range lines {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
s.Data[parts[0]] = []byte(parseNewlines(parts[1]))
}
}
} else {
s.Data[keys["secretText"]] = []byte(parseNewlines(item.SecretText))
}
}
_, err = clientset.CoreV1().Secrets(s.GetNamespace()).Update(context.Background(), s, metav1.UpdateOptions{FieldManager: fieldManagerName})
if err != nil {
log.Println("[ERROR] Could not update secret value", err)
}
}
}
func buildOpClient() (*onepassword.Client, error) {
usr, _ := user.Current()
err := os.Chmod(usr.HomeDir+"/.op", 0700)
if err != nil {
panic(err.Error())
}
if _, ok := os.LookupEnv("OP_SERVICE_ACCOUNT_TOKEN"); !ok {
return nil, fmt.Errorf("OP_SERVICE_ACCOUNT_TOKEN not specified")
}
return onepassword.New()
}
func parseAnnotations(annotations map[string]string) map[string]string {
keys := map[string]string{
"username": "username",
"password": "password",
"secretText": "secretText",
"vault": os.Getenv("OP_VAULT"),
}
for k, v := range annotations {
switch k {
case vaultAnnotation:
keys["vault"] = v
case usernameAnnotation:
keys["username"] = v
case passwordAnnotation:
keys["password"] = v
case secretTextAnnotation:
keys["secretText"] = v
}
}
return keys
}
func parseNewlines(in string) string {
return strings.ReplaceAll(in, "\\n", "\n")
}