-
Notifications
You must be signed in to change notification settings - Fork 464
/
inject.go
156 lines (136 loc) · 3.43 KB
/
inject.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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"github.com/autonomy/dianemo/src/initramfs/pkg/crypto/x509"
"github.com/autonomy/dianemo/src/initramfs/pkg/userdata"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
)
// injectCmd represents the inject command
var injectCmd = &cobra.Command{
Use: "inject",
Short: "inject data into fields in the user data.",
Long: ``,
}
// injectOSCmd represents the inject command
// nolint: dupl
var injectOSCmd = &cobra.Command{
Use: "os",
Short: "inject OS data.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if err := inject(args, crt, key, injectOSData); err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
},
}
// injectIdentityCmd represents the inject command
// nolint: dupl
var injectIdentityCmd = &cobra.Command{
Use: "identity",
Short: "inject identity data.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if err := inject(args, crt, key, injectIdentityData); err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
},
}
// injectKubernetesCmd represents the inject command
// nolint: dupl
var injectKubernetesCmd = &cobra.Command{
Use: "kubernetes",
Short: "inject Kubernetes data.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if err := inject(args, crt, key, injectKubernetesData); err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
},
}
// nolint: dupl
func injectOSData(u *userdata.UserData, crt, key string) (err error) {
if u.Security == nil {
u.Security = &userdata.Security{
OS: &userdata.OSSecurity{},
}
}
crtAndKey, err := x509.NewCertificateAndKeyFromFiles(crt, key)
if err != nil {
return
}
u.Security.OS.CA = crtAndKey
return nil
}
// nolint: dupl
func injectIdentityData(u *userdata.UserData, crt, key string) (err error) {
if u.Security == nil {
u.Security = &userdata.Security{
OS: &userdata.OSSecurity{},
}
}
crtAndKey, err := x509.NewCertificateAndKeyFromFiles(crt, key)
if err != nil {
return
}
u.Security.OS.Identity = crtAndKey
return nil
}
// nolint: dupl
func injectKubernetesData(u *userdata.UserData, crt, key string) (err error) {
if u.Security == nil {
u.Security = &userdata.Security{
Kubernetes: &userdata.KubernetesSecurity{},
}
}
crtAndKey, err := x509.NewCertificateAndKeyFromFiles(crt, key)
if err != nil {
return
}
u.Security.Kubernetes.CA = crtAndKey
return nil
}
func inject(args []string, crt, key string, f func(*userdata.UserData, string, string) error) (err error) {
if len(args) != 1 {
err = fmt.Errorf("expected 1 argument, got %d", len(args))
return
}
configBytes, err := ioutil.ReadFile(args[0])
if err != nil {
return
}
data := &userdata.UserData{}
if err = yaml.Unmarshal(configBytes, data); err != nil {
return
}
if err = f(data, crt, key); err != nil {
return
}
dataBytes, err := yaml.Marshal(data)
if err != nil {
return
}
if err = ioutil.WriteFile(args[0], dataBytes, 0600); err != nil {
return
}
return nil
}
func init() {
injectCmd.PersistentFlags().StringVar(&crt, "crt", "", "the path to the PKI certificate")
if err := injectCmd.MarkPersistentFlagRequired("crt"); err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
injectCmd.PersistentFlags().StringVar(&key, "key", "", "the path to the PKI key")
if err := injectCmd.MarkPersistentFlagRequired("key"); err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
injectCmd.AddCommand(injectOSCmd, injectIdentityCmd, injectKubernetesCmd)
rootCmd.AddCommand(injectCmd)
}