forked from kubernetes-retired/kube-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
121 lines (97 loc) · 2.87 KB
/
helper.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
package helper
import (
"fmt"
"io/ioutil"
"os"
)
const (
dummyKey = `-----BEGIN RSA PRIVATE KEY-----
ZHVtbXkK
-----END RSA PRIVATE KEY-----`
dummyCert = `-----BEGIN CERTIFICATE-----
MIIBvjCCAWgCCQDQ4pUwqdLIIDANBgkqhkiG9w0BAQsFADBlMQswCQYDVQQGEwJV
UzESMBAGA1UECAwJQW50YXJ0aWNhMRowGAYDVQQKDBFUZXN0IFdpZGdldHMgSW5j
LjERMA8GA1UECwwIVGVzdCBMYWIxEzARBgNVBAMMCmR1bW15LWNlcnQwIBcNMTgw
NDMwMDk1NDExWhgPMjUxNzEyMzAwOTU0MTFaMGUxCzAJBgNVBAYTAlVTMRIwEAYD
VQQIDAlBbnRhcnRpY2ExGjAYBgNVBAoMEVRlc3QgV2lkZ2V0cyBJbmMuMREwDwYD
VQQLDAhUZXN0IExhYjETMBEGA1UEAwwKZHVtbXktY2VydDBcMA0GCSqGSIb3DQEB
AQUAA0sAMEgCQQDgd2lsmEBDXMxZsaFUSwnC/FF3x/62SIb3/f8mrGrBtb6Vim11
s7T0zFCm9cWbTi63bzWRFs3gP2FwwU1MF5RDAgMBAAEwDQYJKoZIhvcNAQELBQAD
QQA0bLc3+5kpZuJaAK+C0XvTPZFz8Vx1nv8YnwoIJdEvvGOPGAqvrA8Y0Fvs7L11
Z3leoFbVQmybV7EcduIrOANA
-----END CERTIFICATE-----`
)
func WithTempDir(fn func(dir string)) {
dir, err := ioutil.TempDir("", "test-temp-dir")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
fn(dir)
}
func WithDummyCredentials(fn func(dir string)) {
withDummyCredentials(true, fn)
}
func WithDummyCredentialsButCAKey(fn func(dir string)) {
withDummyCredentials(false, fn)
}
func withDummyCredentials(alsoWriteCAKey bool, fn func(dir string)) {
dir, err := ioutil.TempDir("", "dummy-credentials")
if err != nil {
panic(err)
}
// Remove all the contents in the dir including *.pem.enc created by ReadOrUpdateCompactAssets()
// Otherwise we end up with a lot of garbage directories we failed to remove as they aren't empty in
// config/temp, nodepool/config/temp, test/integration/temp
defer os.RemoveAll(dir)
for _, pairName := range []string{"ca", "apiserver", "kube-controller-manager", "kube-scheduler", "worker", "admin", "etcd", "etcd-client", "kiam-agent", "kiam-server", "apiserver-aggregator"} {
certFile := fmt.Sprintf("%s/%s.pem", dir, pairName)
if err := ioutil.WriteFile(certFile, []byte(dummyCert), 0644); err != nil {
panic(err)
}
defer os.Remove(certFile)
if pairName != "ca" || alsoWriteCAKey {
keyFile := fmt.Sprintf("%s/%s-key.pem", dir, pairName)
if err := ioutil.WriteFile(keyFile, []byte(dummyKey), 0644); err != nil {
panic(err)
}
defer os.Remove(keyFile)
}
}
type symlink struct {
from string
to string
}
symlinks := []symlink{
{"ca.pem", "worker-ca.pem"},
{"ca.pem", "etcd-trusted-ca.pem"},
{"ca.pem", "kiam-ca.pem"},
}
if alsoWriteCAKey {
symlinks = append(symlinks, symlink{"ca-key.pem", "worker-ca-key.pem"})
}
wd, err := os.Getwd()
if err != nil {
panic(err)
}
if err := os.Chdir(dir); err != nil {
panic(err)
}
for _, sl := range symlinks {
from := sl.from
to := sl.to
if _, err := os.Lstat(to); err == nil {
if err := os.Remove(to); err != nil {
panic(err)
}
}
if err := os.Symlink(from, to); err != nil {
panic(err)
}
defer os.Remove(to)
}
if err := os.Chdir(wd); err != nil {
panic(err)
}
fn(dir)
}