-
Notifications
You must be signed in to change notification settings - Fork 90
/
main.go
204 lines (170 loc) · 5.64 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import "C"
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
kotsscheme "github.com/replicatedhq/kots/kotskinds/client/kotsclientset/scheme"
kotslicense "github.com/replicatedhq/kots/pkg/license"
"github.com/replicatedhq/kots/pkg/pull"
"github.com/replicatedhq/kots/pkg/upstream"
"k8s.io/client-go/kubernetes/scheme"
)
//export UpdateCheck
func UpdateCheck(socket, fromArchivePath, namespace string) {
go func() {
var ffiResult *FFIResult
statusClient, err := connectToStatusServer(socket)
if err != nil {
fmt.Printf("failed to connect to status server: %s\n", err)
return
}
defer func() {
statusClient.end(ffiResult)
}()
tmpRoot, err := ioutil.TempDir("", "kots")
if err != nil {
fmt.Printf("failed to create temp path: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
defer os.RemoveAll(tmpRoot)
tarGz, err := extractArchive(tmpRoot, fromArchivePath)
if err != nil {
fmt.Printf("failed to extract archive: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
installationFilePath := filepath.Join(tmpRoot, "upstream", "userdata", "installation.yaml")
beforeCursor, err := readCursorFromPath(installationFilePath)
if err != nil {
fmt.Printf("failed to read cursor file: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
expectedLicenseFile := filepath.Join(tmpRoot, "upstream", "userdata", "license.yaml")
license, err := loadLicenseFromPath(expectedLicenseFile)
if err != nil {
fmt.Printf("failed to load license: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
pullOptions := pull.PullOptions{
LicenseFile: expectedLicenseFile,
Namespace: namespace,
ConfigFile: filepath.Join(tmpRoot, "upstream", "userdata", "config.yaml"),
InstallationFile: filepath.Join(tmpRoot, "upstream", "userdata", "installation.yaml"),
RootDir: tmpRoot,
ExcludeKotsKinds: true,
ExcludeAdminConsole: true,
CreateAppDir: false,
}
if _, err := pull.Pull(fmt.Sprintf("replicated://%s", license.Spec.AppSlug), pullOptions); err != nil {
fmt.Printf("failed to pull upstream: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
afterCursor, err := readCursorFromPath(installationFilePath)
if err != nil {
fmt.Printf("failed to read cursor file after update: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
fmt.Printf("Result of checking for updates for %s: Before: %s, After %s\n", license.Spec.AppSlug, beforeCursor, afterCursor)
isUpdateAvailable := !beforeCursor.Equal(afterCursor)
if !isUpdateAvailable {
ffiResult = NewFFIResult(0)
return
}
paths := []string{
filepath.Join(tmpRoot, "upstream"),
filepath.Join(tmpRoot, "base"),
filepath.Join(tmpRoot, "overlays"),
}
err = os.Remove(fromArchivePath)
if err != nil {
fmt.Printf("failed to delete archive to replace: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
if err := tarGz.Archive(paths, fromArchivePath); err != nil {
fmt.Printf("failed to write archive: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
ffiResult = NewFFIResult(1)
}()
}
//export GetLatestLicense
func GetLatestLicense(socket, licenseData string) {
go func() {
var ffiResult *FFIResult
statusClient, err := connectToStatusServer(socket)
if err != nil {
fmt.Printf("failed to connect to status server: %s\n", err)
return
}
defer func() {
statusClient.end(ffiResult)
}()
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(licenseData), nil, nil)
if err != nil {
fmt.Printf("failed to decode license data: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
license := obj.(*kotsv1beta1.License)
latestLicense, err := kotslicense.GetLatestLicense(license)
if err != nil {
fmt.Printf("failed to get latest license: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
marshalledLicense := upstream.MustMarshalLicense(latestLicense)
ffiResult = NewFFIResult(1).WithData(string(marshalledLicense))
}()
}
//export VerifyAirgapLicense
func VerifyAirgapLicense(licenseData string) *C.char {
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(licenseData), nil, nil)
if err != nil {
fmt.Printf("failed to decode license data: %s\n", err.Error())
return nil
}
license := obj.(*kotsv1beta1.License)
verifiedLicense, err := pull.VerifySignature(license)
if err != nil {
fmt.Printf("failed to verify airgap license signature: %s\n", err.Error())
return nil
}
marshalledLicense := upstream.MustMarshalLicense(verifiedLicense)
return C.CString(string(marshalledLicense))
}
func init() {
kotsscheme.AddToScheme(scheme.Scheme)
}
func parseConfigValuesFromFile(filename string) (*kotsv1beta1.ConfigValues, error) {
contents, err := ioutil.ReadFile(filename)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, errors.Wrap(err, "failed to read config values file")
}
decode := scheme.Codecs.UniversalDeserializer().Decode
decoded, gvk, err := decode(contents, nil, nil)
if err != nil {
return nil, errors.Wrap(err, "unable to decode config values file")
}
if gvk.Group != "kots.io" || gvk.Version != "v1beta1" || gvk.Kind != "ConfigValues" {
return nil, errors.New("not config values")
}
config := decoded.(*kotsv1beta1.ConfigValues)
return config, nil
}
func main() {}