Skip to content

Commit

Permalink
add pre-install hook that verifies dashboard licenses
Browse files Browse the repository at this point in the history
Signed-off-by: Burak Sekili <buraksekili@gmail.com>
  • Loading branch information
buraksekili committed Aug 30, 2023
1 parent 7a12edb commit 14262e1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 21 deletions.
18 changes: 0 additions & 18 deletions cmd/bootstrap-post/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"tyk/tyk/bootstrap/data"
"tyk/tyk/bootstrap/helpers"
"tyk/tyk/bootstrap/license"
"tyk/tyk/bootstrap/readiness"
)

Expand All @@ -18,23 +17,6 @@ func main() {
os.Exit(1)
}

dashboardLicenseKey, err := license.GetDashboardLicense()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

licenseIsValid, err := license.ValidateDashboardLicense(dashboardLicenseKey)
if err != nil {
fmt.Println(err)
}
if licenseIsValid {
fmt.Println("Provided license is valid")
} else {
fmt.Println("Provided license is invalid")
os.Exit(1)
}

err = readiness.CheckIfRequiredDeploymentsAreReady()
if err != nil {
fmt.Println(err)
Expand Down
17 changes: 17 additions & 0 deletions cmd/bootstrap-pre-install/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"os"
"tyk/tyk/bootstrap/license/preinstall"
)

func main() {
err := preinstall.PreHookInstall()
if err != nil {
fmt.Printf("Failed to run pre-hook job, err: %v", err)
os.Exit(1)
}

fmt.Println("Pre-Hook bootstrapping succeeded, the provided license is valid!")
}
1 change: 1 addition & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
TykDashboardProtoEnvVar = "TYK_DASHBOARD_PROTO"
TykDashboardSvcEnvVar = "TYK_DASHBOARD_SVC"
TykDashboardInsecureSkipVerify = "TYK_DASHBOARD_INSECURE_SKIP_VERIFY"
TykDashboardLicenseEnvVarName = "TYK_DB_LICENSEKEY"
TykDbListenport = "TYK_DB_LISTENPORT"
TykDbLicensekeyEnvVar = "TYK_DB_LICENSEKEY"
TykAdminSecretEnvVar = "TYK_ADMIN_SECRET"
Expand Down
5 changes: 2 additions & 3 deletions license/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import (
"strconv"
"strings"
"time"
"tyk/tyk/bootstrap/constants"
)

const TykDashboardLicenseEnvVarName = "TYK_DB_LICENSEKEY"

func GetDashboardLicense() (string, error) {
license, ok := os.LookupEnv(TykDashboardLicenseEnvVarName)
license, ok := os.LookupEnv(constants.TykDashboardLicenseEnvVarName)
if !ok {
return "", errors.New("license env var is not present")
}
Expand Down
28 changes: 28 additions & 0 deletions license/preinstall/preinstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Package preinstall exposes an API to run necessary operations required in pre-install hook job of bootstrapping.
// While bootstrapping Tyk Stack, users need to provide a valida Tyk License key.
// In the pre-hook installation, the helper functions defined in this package verifies the validity of the license.
package preinstall

import (
"errors"
"tyk/tyk/bootstrap/license"
)

// PreHookInstall runs all required License validity operations that are required in pre-install hook.
func PreHookInstall() error {
dashboardLicenseKey, err := license.GetDashboardLicense()
if err != nil {
return err
}

licenseIsValid, err := license.ValidateDashboardLicense(dashboardLicenseKey)
if err != nil {
return err
}

if !licenseIsValid {
return errors.New("provided license is invalid")
}

return nil
}

0 comments on commit 14262e1

Please sign in to comment.