Skip to content

Commit

Permalink
Merge pull request #12 from TykTechnologies/feature/TT-5906/prehook-l…
Browse files Browse the repository at this point in the history
…icense-validation

[TT-5906] Add pre-install package that verifies dashboard license
  • Loading branch information
buraksekili committed Sep 4, 2023
2 parents 7a12edb + d541fb8 commit 2626b28
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .container/image/bootstrap-pre-install/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM scratch

ADD bootstrapapp-pre-install /app/bin/bootstrap-app-pre-install
63 changes: 62 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ builds:
- CGO_ENABLED=0
ldflags:
- -X main.version={{.Version}}
-
id: "preinstall"
main: ./cmd/bootstrap-pre-install
binary: bin/bootstrapapp-pre-install
goos:
- linux
goarch:
- amd64
- arm64
env:
- CGO_ENABLED=0
ldflags:
- -X main.version={{.Version}}


# https://goreleaser.com/customization/docker/
Expand Down Expand Up @@ -97,6 +110,34 @@ dockers:
use: buildx
build_flag_templates:
- "--platform=linux/arm64/v8"
-
ids:
- preinstall
image_templates:
- "tykio/tyk-k8s-bootstrap-pre-install:latest-amd64"
- "tykio/tyk-k8s-bootstrap-pre-install:v{{ .Major }}-amd64"
- "tykio/tyk-k8s-bootstrap-pre-install:v{{ .Major }}.{{ .Minor }}-amd64"
- "tykio/tyk-k8s-bootstrap-pre-install:{{ .Tag }}-amd64"
goos: linux
goarch: amd64
dockerfile: ".container/image/bootstrap-pre-install/Dockerfile"
use: buildx
build_flag_templates:
- "--platform=linux/amd64"
-
ids:
- preinstall
image_templates:
- "tykio/tyk-k8s-bootstrap-pre-install:latest-arm64v8"
- "tykio/tyk-k8s-bootstrap-pre-install:v{{ .Major }}-arm64v8"
- "tykio/tyk-k8s-bootstrap-pre-install:v{{ .Major }}.{{ .Minor }}-arm64v8"
- "tykio/tyk-k8s-bootstrap-pre-install:{{ .Tag }}-arm64v8"
goos: linux
goarch: arm64
dockerfile: ".container/image/bootstrap-pre-install/Dockerfile"
use: buildx
build_flag_templates:
- "--platform=linux/arm64/v8"

# https://goreleaser.com/customization/docker_manifest/
docker_manifests:
Expand Down Expand Up @@ -138,4 +179,24 @@ docker_manifests:
- name_template: "tykio/tyk-k8s-bootstrap-pre-delete:{{ .Tag }}"
image_templates:
- "tykio/tyk-k8s-bootstrap-pre-delete:{{ .Tag }}-amd64"
- "tykio/tyk-k8s-bootstrap-pre-delete:{{ .Tag }}-arm64v8"
- "tykio/tyk-k8s-bootstrap-pre-delete:{{ .Tag }}-arm64v8"

- name_template: "tykio/tyk-k8s-bootstrap-pre-install:latest"
image_templates:
- "tykio/tyk-k8s-bootstrap-pre-install:latest-amd64"
- "tykio/tyk-k8s-bootstrap-pre-install:latest-arm64v8"

- name_template: "tykio/tyk-k8s-bootstrap-pre-install:v{{ .Major }}"
image_templates:
- "tykio/tyk-k8s-bootstrap-pre-install:v{{ .Major }}-amd64"
- "tykio/tyk-k8s-bootstrap-pre-install:v{{ .Major }}-arm64v8"

- name_template: "tykio/tyk-k8s-bootstrap-pre-install:v{{ .Major }}.{{ .Minor }}"
image_templates:
- "tykio/tyk-k8s-bootstrap-pre-install:v{{ .Major }}.{{ .Minor }}-amd64"
- "tykio/tyk-k8s-bootstrap-pre-install:v{{ .Major }}.{{ .Minor }}-arm64v8"

- name_template: "tykio/tyk-k8s-bootstrap-pre-install:{{ .Tag }}"
image_templates:
- "tykio/tyk-k8s-bootstrap-pre-install:{{ .Tag }}-amd64"
- "tykio/tyk-k8s-bootstrap-pre-install:{{ .Tag }}-arm64v8"
17 changes: 12 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@ GO_ARG_LINUX=GOOS=linux GOARCH=amd64

SRC_PATH?=$(shell pwd)
BIN_PATH?=$(SRC_PATH)/bin

BOOTSTRAP_APP_PREINSTALL_NAME=bootstrapapp-pre-install
BOOTSTRAP_APP_POST_NAME=bootstrapapp-post
BOOTSTRAP_APP_PREDELETE_NAME=bootstrapapp-pre-delete

BOOTSTRAP_CMD_PREINSTALL_PATH=./cmd/bootstrap-pre-install
BOOTSTRAP_CMD_POST_PATH=./cmd/bootstrap-post
BOOTSTRAP_CMD_PREDELETE_PATH=./cmd/bootstrap-pre-delete

build-bootstrap-pre-install:
@echo "\n Building bootstrap-pre-install binary"
env $(GO_ARG_LINUX) CGO_ENABLED=0 go build -o "$(BIN_PATH)/$(BOOTSTRAP_APP_PREINSTALL_NAME)" -ldflags \
"-X main.version=$(MAIN_VERSION)" "$(BOOTSTRAP_CMD_PREINSTALL_PATH)"

build-bootstrap-post:
@echo "\n Building bootstrap binary"
env $(GO_ARG_LINUX) CGO_ENABLED=0 go build -v -o "$(BIN_PATH)/$(BOOTSTRAP_APP_PATH)$(BOOTSTRAP_APP_POST_NAME)" -ldflags \
@echo "\n Building bootstrapapp-post binary"
env $(GO_ARG_LINUX) CGO_ENABLED=0 go build -o "$(BIN_PATH)/$(BOOTSTRAP_APP_POST_NAME)" -ldflags \
"-X main.version=$(MAIN_VERSION)" "$(BOOTSTRAP_CMD_POST_PATH)"

build-bootstrap-pre-delete:
@echo "\n Building bootstrap binary"
env $(GO_ARG_LINUX) CGO_ENABLED=0 go build -v -o "$(BIN_PATH)/$(BOOTSTRAP_APP_PATH)$(BOOTSTRAP_APP_PREDELETE_NAME)" -ldflags \
@echo "\n Building bootstrapapp-pre-delete binary"
env $(GO_ARG_LINUX) CGO_ENABLED=0 go build -o "$(BIN_PATH)/$(BOOTSTRAP_APP_PREDELETE_NAME)" -ldflags \
"-X main.version=$(MAIN_VERSION)" "$(BOOTSTRAP_CMD_PREDELETE_PATH)"

build-all: build-bootstrap-post build-bootstrap-pre-delete
build-all: build-bootstrap-post build-bootstrap-pre-delete build-bootstrap-pre-install
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/preinstallation"
)

func main() {
err := preinstallation.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 preinstallation/preinstallation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Package preinstallation 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 preinstallation

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

// PreHookInstall runs all required license validation 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 2626b28

Please sign in to comment.