-
Notifications
You must be signed in to change notification settings - Fork 90
/
license.go
67 lines (58 loc) · 1.78 KB
/
license.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
package license
import (
"time"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/store"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kotskinds/client/kotsclientset/scheme"
)
func ResolveExistingLicense(newLicense *kotsv1beta1.License) (bool, error) {
notInstalledApps, err := store.GetStore().ListFailedApps()
if err != nil {
logger.Error(errors.Wrap(err, "failed to list failed apps"))
return false, err
}
for _, app := range notInstalledApps {
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(app.License), nil, nil)
if err != nil {
continue
}
license := obj.(*kotsv1beta1.License)
if license.Spec.LicenseID != newLicense.Spec.LicenseID {
continue
}
if err := store.GetStore().RemoveApp(app.ID); err != nil {
return false, errors.Wrap(err, "failed to remove existing app record")
}
}
// check if license still exists
allLicenses, err := store.GetStore().GetAllAppLicenses()
if err != nil {
return false, errors.Wrap(err, "failed to get all app licenses")
}
for _, l := range allLicenses {
if l.Spec.LicenseID == newLicense.Spec.LicenseID {
return false, nil
}
}
return true, nil
}
func LicenseIsExpired(license *kotsv1beta1.License) (bool, error) {
val, found := license.Spec.Entitlements["expires_at"]
if !found {
return false, nil
}
if val.ValueType != "" && val.ValueType != "String" {
return false, errors.Errorf("expires_at must be type String: %s", val.ValueType)
}
if val.Value.StrVal == "" {
return false, nil
}
partsed, err := time.Parse(time.RFC3339, val.Value.StrVal)
if err != nil {
return false, errors.Wrap(err, "failed to parse expiration time")
}
return partsed.Before(time.Now()), nil
}