Skip to content

Commit 388911c

Browse files
committed
Cache license by id for ace
Signed-off-by: Tamal Saha <[email protected]>
1 parent ddf7a59 commit 388911c

File tree

4 files changed

+41
-29
lines changed

4 files changed

+41
-29
lines changed

pkg/server/issuer.go

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func IssueEnterpriseLicense(fs blobfs.Interface, certs *certstore.CertStore, inf
6969

7070
// 1 yr domain license
7171
license := &ProductLicense{
72+
ID: info.ID,
7273
Domain: domain,
7374
Product: info.Product(),
7475
Agreement: &LicenseAgreement{
@@ -78,12 +79,12 @@ func IssueEnterpriseLicense(fs blobfs.Interface, certs *certstore.CertStore, inf
7879
}
7980

8081
var crtLicense []byte
81-
exists, err := fs.Exists(context.TODO(), LicenseCertPath(license.Domain, license.Product, info.Cluster))
82+
exists, err := fs.Exists(context.TODO(), license.LicenseCertPath(info.Cluster))
8283
if err != nil {
8384
return nil, nil, err
8485
}
8586
if exists {
86-
data, err := fs.ReadFile(context.TODO(), LicenseCertPath(license.Domain, license.Product, info.Cluster))
87+
data, err := fs.ReadFile(context.TODO(), license.LicenseCertPath(info.Cluster))
8788
if err != nil {
8889
return nil, nil, err
8990
}
@@ -92,7 +93,7 @@ func IssueEnterpriseLicense(fs blobfs.Interface, certs *certstore.CertStore, inf
9293
return nil, nil, err
9394
}
9495
if len(certs) > 1 {
95-
return nil, nil, fmt.Errorf("multiple certificates found in %s", LicenseCertPath(license.Domain, license.Product, info.Cluster))
96+
return nil, nil, fmt.Errorf("multiple certificates found in %s", license.LicenseCertPath(info.Cluster))
9697
}
9798

9899
if !certs[0].NotAfter.Before(license.Agreement.ExpiryDate.Time) {
@@ -113,26 +114,29 @@ func IssueEnterpriseLicense(fs blobfs.Interface, certs *certstore.CertStore, inf
113114
LicenseForm: info,
114115
Timestamp: timestamp,
115116
}
116-
{
117-
// record request
118-
data, err := json.MarshalIndent(accesslog, "", " ")
119-
if err != nil {
120-
return nil, nil, err
121-
}
122-
err = fs.WriteFile(context.TODO(), FullLicenseIssueLogPath(domain, info.Product(), info.Cluster, timestamp), data)
123-
if err != nil {
124-
return nil, nil, err
125-
}
126-
}
127-
128-
{
129-
// mark email as verified
130-
if exists, err := fs.Exists(context.TODO(), EmailVerifiedPath(domain, info.Email)); err == nil && !exists {
131-
err = fs.WriteFile(context.TODO(), EmailVerifiedPath(domain, info.Email), []byte(timestamp))
117+
// only log for https://appscode.com/issue-license/
118+
if license.ID <= 0 {
119+
{
120+
// record request
121+
data, err := json.MarshalIndent(accesslog, "", " ")
122+
if err != nil {
123+
return nil, nil, err
124+
}
125+
err = fs.WriteFile(context.TODO(), FullLicenseIssueLogPath(domain, info.Product(), info.Cluster, timestamp), data)
132126
if err != nil {
133127
return nil, nil, err
134128
}
135129
}
130+
131+
{
132+
// mark email as verified
133+
if exists, err := fs.Exists(context.TODO(), EmailVerifiedPath(domain, info.Email)); err == nil && !exists {
134+
err = fs.WriteFile(context.TODO(), EmailVerifiedPath(domain, info.Email), []byte(timestamp))
135+
if err != nil {
136+
return nil, nil, err
137+
}
138+
}
139+
}
136140
}
137141

138142
return crtLicense, &accesslog, nil
@@ -176,11 +180,11 @@ func CreateLicense(fs blobfs.Interface, certs *certstore.CertStore, info License
176180
return nil, errors.Wrap(err, "failed to generate client certificate")
177181
}
178182

179-
err = fs.WriteFile(context.TODO(), LicenseCertPath(license.Domain, license.Product, cluster), cert.EncodeCertPEM(crt))
183+
err = fs.WriteFile(context.TODO(), license.LicenseCertPath(cluster), cert.EncodeCertPEM(crt))
180184
if err != nil {
181185
return nil, err
182186
}
183-
err = fs.WriteFile(context.TODO(), LicenseKeyPath(license.Domain, license.Product, cluster), cert.EncodePrivateKeyPEM(key))
187+
err = fs.WriteFile(context.TODO(), license.LicenseKeyPath(cluster), cert.EncodePrivateKeyPEM(key))
184188
if err != nil {
185189
return nil, err
186190
}

pkg/server/paths.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,18 @@ func AgreementPath(domain, product string) string {
3838
return fmt.Sprintf("domains/%s/products/%s/agreement.json", domain, product)
3939
}
4040

41-
func LicenseCertPath(domain, product, cluster string) string {
42-
return fmt.Sprintf("domains/%s/products/%s/clusters/%s/tls.crt", domain, product, cluster)
41+
func (l ProductLicense) LicenseCertPath(cluster string) string {
42+
if l.ID > 0 {
43+
return fmt.Sprintf("id/%d/products/%s/clusters/%s/tls.crt", l.ID, l.Product, cluster)
44+
}
45+
return fmt.Sprintf("domains/%s/products/%s/clusters/%s/tls.crt", l.Domain, l.Product, cluster)
46+
}
47+
48+
func (l ProductLicense) LicenseKeyPath(cluster string) string {
49+
if l.ID > 0 {
50+
return fmt.Sprintf("id/%d/products/%s/clusters/%s/tls.key", l.ID, l.Product, cluster)
51+
}
52+
return fmt.Sprintf("domains/%s/products/%s/clusters/%s/tls.key", l.Domain, l.Product, cluster)
4353
}
4454

4555
func ProductAccessLogPath(domain, product, cluster, timestamp string) string {
@@ -53,7 +63,3 @@ func FullLicenseIssueLogPath(domain, product, cluster, timestamp string) string
5363
func EmailAccessLogPath(domain, email, product, timestamp string) string {
5464
return fmt.Sprintf("domains/%s/emails/%s/products/%s/accesslog/%s", domain, email, product, timestamp)
5565
}
56-
57-
func LicenseKeyPath(domain, product, cluster string) string {
58-
return fmt.Sprintf("domains/%s/products/%s/clusters/%s/tls.key", domain, product, cluster)
59-
}

pkg/server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,12 +678,12 @@ func (s *Server) GetDomainLicense(domain string, product string) (*ProductLicens
678678
func (s *Server) CreateOrRetrieveLicense(info LicenseForm, license ProductLicense, cluster string) ([]byte, error) {
679679
// Return existing license for enterprise products
680680
if IsEnterpriseProduct(license.Product) {
681-
exists, err := s.fs.Exists(context.TODO(), LicenseCertPath(license.Domain, license.Product, cluster))
681+
exists, err := s.fs.Exists(context.TODO(), license.LicenseCertPath(cluster))
682682
if err != nil {
683683
return nil, err
684684
}
685685
if exists {
686-
return s.fs.ReadFile(context.TODO(), LicenseCertPath(license.Domain, license.Product, cluster))
686+
return s.fs.ReadFile(context.TODO(), license.LicenseCertPath(cluster))
687687
}
688688
}
689689
return CreateLicense(s.fs, s.certs, info, license, cluster, nil)

pkg/server/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
)
2929

3030
type ProductLicense struct {
31+
ID int64 `json:"id"`
3132
Domain string `json:"domain"`
3233
Product string `json:"product"` // This is now called plan in a parsed LicenseInfo
3334
TTL *metav1.Duration `json:"ttl,omitempty"`
@@ -44,6 +45,7 @@ type RegisterRequest struct {
4445
}
4546

4647
type LicenseForm struct {
48+
ID int64 `form:"id" json:"id"`
4749
Name string `form:"name" binding:"Required" json:"name"`
4850
Email string `form:"email" binding:"Required;Email" json:"email"`
4951
CC string `form:"cc" json:"cc"`

0 commit comments

Comments
 (0)