Skip to content

Commit 785c5a2

Browse files
authored
Merge pull request #250 from LalitDeore/license-1
Fix - licesing issues
2 parents 9e8df6a + 0447ae0 commit 785c5a2

File tree

3 files changed

+199
-29
lines changed

3 files changed

+199
-29
lines changed

db-connector.go

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13491,39 +13491,119 @@ func GetEsConfig(defaultCreds bool) *opensearch.Client {
1349113491
return es
1349213492
}
1349313493

13494-
func checkNoInternet() (bool, string) {
13494+
func checkNoInternet() OnpremLicense {
13495+
13496+
license := OnpremLicense{}
1349513497
licenseKey := os.Getenv("SHUFFLE_LICENSE")
1349613498
if len(licenseKey) == 0 {
13497-
return false, ""
13499+
return license
13500+
}
13501+
13502+
if len(licenseKey) < 32 {
13503+
log.Printf("[ERROR] License key is too short")
13504+
return license
1349813505
}
1349913506

13500-
// Month + Year -> when it runs out
13501-
sum := sha256.Sum256([]byte(licenseKey))
13502-
encodedString := hex.EncodeToString(sum[:])
13507+
// Split the license key into chunks of 32 characters
13508+
licenseParts := []string{}
13509+
for i := 0; i < len(licenseKey); i += 32 {
13510+
end := i + 32
13511+
13512+
if end > len(licenseKey) {
13513+
end = len(licenseKey)
13514+
}
13515+
licenseParts = append(licenseParts, licenseKey[i:end])
13516+
}
13517+
13518+
licenseKeyPart := licenseParts[0]
13519+
sum := sha256.Sum256([]byte(licenseKeyPart))
13520+
encodedString := hex.EncodeToString(sum[:])
13521+
13522+
tenantKey := ""
13523+
if len(licenseParts) > 1 {
13524+
tenantKey = licenseParts[1]
13525+
}
13526+
13527+
tenantHash := sha256.Sum256([]byte(tenantKey))
13528+
encodedTenant := hex.EncodeToString(tenantHash[:])
13529+
environmentKey := ""
13530+
if len(licenseParts) > 2 {
13531+
environmentKey = licenseParts[2]
13532+
}
13533+
13534+
environmentHash := sha256.Sum256([]byte(environmentKey))
13535+
encodedEnvironment := hex.EncodeToString(environmentHash[:])
13536+
13537+
branding := ""
13538+
if len(licenseParts) > 3 {
13539+
branding = licenseParts[3]
13540+
}
13541+
13542+
brandingHash := sha256.Sum256([]byte(branding))
13543+
encodedBranding := hex.EncodeToString(brandingHash[:])
1350313544

1350413545
// Returns a map[sha256]timeout string
13505-
onpremKeys := GetOnpremKeys()
13546+
onpremKeys := GetOnpremKeys()
1350613547
if timeout, ok := onpremKeys[encodedString]; ok {
13507-
// Check if current time is MORE than the encoded timeout. The timeout format
13548+
// Check if current time is MORE than the encoded timeout. The timeout format
1350813549
parsedTimeout, err := time.Parse("02-01-2006", timeout)
1350913550
if err != nil {
1351013551
log.Printf("[ERROR] Failed parsing license timeout: %s", err)
1351113552
} else {
1351213553
if time.Now().Before(parsedTimeout) {
13513-
if debug {
13554+
if debug {
1351413555
log.Printf("[DEBUG] License key is valid")
1351513556
}
1351613557

13517-
return true, timeout
13558+
license.Valid = true
13559+
license.Timeout = timeout
13560+
13561+
if len(tenantKey) > 0 && len(encodedTenant) > 0 {
13562+
amount := GetTenantAmount(encodedTenant)
13563+
license.Tenant.Limit = int64(amount)
13564+
if amount > 3 {
13565+
license.Tenant.Active = true
13566+
} else {
13567+
license.Tenant.Active = false
13568+
}
13569+
} else {
13570+
license.Tenant.Limit = 3
13571+
license.Tenant.Active = false
13572+
}
13573+
13574+
//check env limit
13575+
if len(environmentKey) > 0 && len(encodedEnvironment) > 0 {
13576+
amount := GetRuntimeLocationAmount(encodedEnvironment)
13577+
license.Environment.Limit = int64(amount)
13578+
if amount > 1 {
13579+
license.Environment.Active = true
13580+
} else {
13581+
license.Environment.Active = false
13582+
}
13583+
13584+
} else {
13585+
license.Environment.Limit = 1
13586+
license.Environment.Active = false
13587+
}
13588+
13589+
//check branding enable
13590+
if len(branding) > 0 && len(encodedBranding) > 0 {
13591+
branding := GetBrandingAvailable(encodedBranding)
13592+
license.Branding = branding
13593+
} else {
13594+
license.Branding = false
13595+
}
13596+
13597+
return license
1351813598
} else {
1351913599
log.Printf("[ERROR] License key has expired on %s", timeout)
13520-
return false, timeout
13600+
return license
1352113601
}
1352213602
}
13523-
}
13603+
}
1352413604

1352513605
log.Printf("[ERROR] No valid license key found based SHUFFLE_LICENSE %s", licenseKey)
13526-
return false, ""
13606+
return license
1352713607
}
1352813608

1352913609
func UploadAppSpecFiles(ctx context.Context, client *storage.Client, api WorkflowApp, parsed ParsedOpenApi) (WorkflowApp, error) {

shared.go

Lines changed: 92 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,13 @@ func HandleGetOrg(resp http.ResponseWriter, request *http.Request) {
11081108
// Not a suborg
11091109
cloudOrg := HandleCheckLicense(ctx, *org)
11101110
org = &cloudOrg
1111+
} else if len(org.CreatorOrg) > 0 && project.Environment == "onprem" {
1112+
parentOrg, err := GetOrg(ctx, org.CreatorOrg)
1113+
if err == nil && len(parentOrg.Subscriptions) > 0 {
1114+
licenseOrg := HandleCheckLicense(ctx, *parentOrg)
1115+
parentOrg = &licenseOrg
1116+
org.Subscriptions = parentOrg.Subscriptions
1117+
}
11111118
}
11121119

11131120
if len(org.CreatorOrg) == 0 && project.Environment == "onprem" {
@@ -3074,6 +3081,7 @@ func HandleGetEnvironments(resp http.ResponseWriter, request *http.Request) {
30743081
}
30753082

30763083
hideEnvs := false
3084+
parentOrgMain := Org{}
30773085
if project.Environment == "onprem" {
30783086
currentOrg, err := GetOrg(ctx, user.ActiveOrg.Id)
30793087
if err != nil {
@@ -3093,22 +3101,52 @@ func HandleGetEnvironments(resp http.ResponseWriter, request *http.Request) {
30933101
}
30943102

30953103
licenseOrg := HandleCheckLicense(ctx, *parentOrg)
3096-
if !licenseOrg.SyncFeatures.MultiEnv.Active && int64(len(environments)) > licenseOrg.SyncFeatures.MultiEnv.Limit {
3104+
parentOrgMain = licenseOrg
3105+
if int64(len(environments)) > licenseOrg.SyncFeatures.MultiEnv.Limit {
30973106
hideEnvs = true
30983107
}
30993108
}
31003109

31013110
newEnvironments := []Environment{}
3102-
for _, environment := range environments {
3103-
if len(environment.Id) == 0 {
3104-
environment.Id = uuid.NewV4().String()
3111+
sort.Slice(environments, func(i, j int) bool {
3112+
return environments[i].Created < environments[j].Created
3113+
})
3114+
3115+
filteredEnvironments := []Environment{}
3116+
if hideEnvs {
3117+
defaultEnvs := []Environment{}
3118+
nonDefaultEnvs := []Environment{}
3119+
for _, env := range environments {
3120+
if len(env.Id) == 0 {
3121+
env.Id = uuid.NewV4().String()
3122+
}
3123+
3124+
3125+
if env.Default {
3126+
defaultEnvs = append(defaultEnvs, env)
3127+
} else {
3128+
nonDefaultEnvs = append(nonDefaultEnvs, env)
3129+
}
31053130
}
31063131

3107-
// For onprem users without a valid license, only display default environments on the frontend.
3108-
if hideEnvs && !environment.Default {
3109-
continue
3132+
filteredEnvironments = append(filteredEnvironments, defaultEnvs...)
3133+
limit := int(parentOrgMain.SyncFeatures.MultiEnv.Limit)
3134+
for i, env := range nonDefaultEnvs {
3135+
if i < (limit - 1) {
3136+
filteredEnvironments = append(filteredEnvironments, env)
3137+
}
3138+
}
3139+
3140+
environments = filteredEnvironments
3141+
} else {
3142+
for i := range environments {
3143+
if len(environments[i].Id) == 0 {
3144+
environments[i].Id = uuid.NewV4().String()
3145+
}
31103146
}
3147+
}
31113148

3149+
for _, environment := range environments {
31123150
found := false
31133151
for _, oldEnv := range newEnvironments {
31143152
if oldEnv.Name == environment.Name {
@@ -11788,7 +11826,8 @@ func HandleCreateSubOrg(resp http.ResponseWriter, request *http.Request) {
1178811826
parentOrg.SyncUsage.MultiTenant.Counter = int64(len(childOrgs))
1178911827
}
1179011828

11791-
isLicensed, _ := checkNoInternet()
11829+
license := checkNoInternet()
11830+
isLicensed := license.Valid
1179211831
if !parentOrg.CloudSync && !isLicensed && len(childOrgs) >= 3 {
1179311832
log.Printf("[WARNING] Organization %s has exceeded the free plan limit of 3 sub-organizations. An enterprise license is required to create additional sub-organizations.", parentOrg.Id)
1179411833
resp.WriteHeader(400)
@@ -30775,11 +30814,33 @@ func HandleCheckLicense(ctx context.Context, org Org) Org {
3077530814
syncFeatures, err := GetCache(ctx, cacheKey)
3077630815
if err != nil {
3077730816
log.Printf("[ERROR] Failed to get cache in HandleCheckLicense: %v", err)
30817+
org.SyncFeatures.MultiEnv.Active = false
30818+
org.SyncFeatures.MultiEnv.Limit = 1
30819+
30820+
org.SyncFeatures.MultiTenant.Active = false
30821+
org.SyncFeatures.MultiTenant.Limit = 3
30822+
30823+
org.SyncFeatures.Branding.Active = false
30824+
org.Licensed = false
3077830825
return org
3077930826
}
3078030827
features := SyncFeatures{}
3078130828
if data, ok := syncFeatures.([]byte); ok {
3078230829
if err := json.Unmarshal(data, &features); err == nil {
30830+
licenseCacheKey := fmt.Sprintf("org_licensed_%s", org.Id)
30831+
licensed, err := GetCache(ctx, licenseCacheKey)
30832+
if err != nil {
30833+
org.Licensed = false
30834+
} else if data, ok := licensed.([]byte); ok {
30835+
licenseData := string(data)
30836+
if licenseData == "true" {
30837+
org.Licensed = true
30838+
} else {
30839+
org.Licensed = false
30840+
}
30841+
} else {
30842+
org.Licensed = false
30843+
}
3078330844

3078430845
org.SyncFeatures.MultiEnv.Active = features.MultiEnv.Active
3078530846
org.SyncFeatures.MultiEnv.Limit = features.MultiEnv.Limit
@@ -30860,14 +30921,17 @@ func HandleCheckLicense(ctx context.Context, org Org) Org {
3086030921

3086130922
} else if len(shuffleLicenseKey) > 0 {
3086230923

30863-
license, timeout := checkNoInternet()
30864-
if license == true {
30865-
org.SyncFeatures.MultiEnv.Limit = 100
30866-
org.SyncFeatures.MultiEnv.Active = true
30924+
license := checkNoInternet()
30925+
if license.Valid == true {
30926+
30927+
org.Licensed = true
3086730928

30868-
org.SyncFeatures.MultiTenant.Limit = 1000
30869-
org.SyncFeatures.MultiTenant.Active = true
30870-
org.SyncFeatures.Branding.Active = true
30929+
org.SyncFeatures.MultiEnv.Limit = license.Environment.Limit
30930+
org.SyncFeatures.MultiEnv.Active = license.Environment.Active
30931+
30932+
org.SyncFeatures.MultiTenant.Limit = license.Tenant.Limit
30933+
org.SyncFeatures.MultiTenant.Active = license.Tenant.Active
30934+
org.SyncFeatures.Branding.Active = license.Branding
3087130935

3087230936
org.SyncFeatures.AppExecutions.Active = true
3087330937
org.SyncFeatures.Webhook.Active = true
@@ -30885,6 +30949,15 @@ func HandleCheckLicense(ctx context.Context, org Org) Org {
3088530949
org.SyncFeatures.Schedule.Active = true
3088630950
org.SyncFeatures.Apps.Active = true
3088730951
org.SyncFeatures.ShuffleGPT.Active = true
30952+
} else {
30953+
org.Licensed = false
30954+
org.SyncFeatures.MultiEnv.Limit = 1
30955+
org.SyncFeatures.MultiEnv.Active = false
30956+
30957+
org.SyncFeatures.MultiTenant.Limit = 3
30958+
org.SyncFeatures.MultiTenant.Active = false
30959+
30960+
org.SyncFeatures.Branding.Active = false
3088830961
}
3088930962

3089030963
parsedEula := GetOnpremPaidEula()
@@ -30910,8 +30983,8 @@ func HandleCheckLicense(ctx context.Context, org Org) Org {
3091030983
"Custom Contract",
3091130984
}
3091230985

30913-
if license {
30914-
parsedTimeout, err := time.Parse("02-01-2006", timeout)
30986+
if license.Valid {
30987+
parsedTimeout, err := time.Parse("02-01-2006", license.Timeout)
3091530988
if err != nil {
3091630989
log.Printf("[ERROR] Failed parsing license timeout: %s", err)
3091730990
parsedTimeout = time.Now()
@@ -30947,6 +31020,8 @@ func HandleCheckLicense(ctx context.Context, org Org) Org {
3094731020

3094831021
} else {
3094931022
log.Printf("[WARNING] Org %v does not have an enterprise license. Please purchase an enterprise license to unlock production-ready features. Contact [email protected] for more information.", org.Id)
31023+
31024+
org.Licensed = false
3095031025
org.SyncFeatures.MultiEnv.Limit = 1
3095131026
org.SyncFeatures.MultiEnv.Active = false
3095231027

structs.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ type RetStruct struct {
146146
SessionKey string `json:"session_key"`
147147
IntervalSeconds int64 `json:"interval_seconds"`
148148
Subscriptions []PaymentSubscription `json:"subscriptions,omitempty"`
149+
Licensed bool `json:"licensed"`
149150
}
150151

151152
type AppMini struct {
@@ -996,6 +997,19 @@ type UsecaseInfo struct {
996997
Created int64 `datastore:"created" json:"created"`
997998
}
998999

1000+
type OnpremLimits struct {
1001+
Limit int64 `json:"limit" datastore:"limit"`
1002+
Active bool `json:"active" datastore:"active"`
1003+
}
1004+
1005+
type OnpremLicense struct {
1006+
Valid bool `json:"valid" datastore:"valid"`
1007+
Tenant OnpremLimits `json:"tenant" datastore:"tenant"`
1008+
Environment OnpremLimits `json:"environment" datastore:"environment"`
1009+
Timeout string `json:"timeout" datastore:"timeout"`
1010+
Branding bool `json:"branding" datastore:"branding"`
1011+
}
1012+
9991013
type Org struct {
10001014
Name string `json:"name" datastore:"name"`
10011015
Description string `json:"description" datastore:"description"`
@@ -1045,6 +1059,7 @@ type Org struct {
10451059
Billing Billing `json:"Billing" datastore:"Billing"`
10461060
CreatorOrg string `json:"creator_org" datastore:"creator_org"`
10471061
Branding OrgBranding `json:"branding" datastore:"branding"`
1062+
Licensed bool `json:"licensed" datastore:"licensed"` //Track onprem license
10481063
}
10491064

10501065
type Billing struct {

0 commit comments

Comments
 (0)