From a177cc17ff66368987c0c1daaea6193ac9292fe0 Mon Sep 17 00:00:00 2001 From: Salah Al Saleh Date: Tue, 28 Jan 2020 09:22:50 -0800 Subject: [PATCH] Get latest license on app install (#280) * Get latest license on app install --- ffi/main.go | 41 ++---------------------- integration/replicated/generate.go | 2 +- integration/replicated/pull_test.go | 2 +- pkg/k8sdoc/types.go | 2 -- pkg/kotsadm/types/deployoptions.go | 4 --- pkg/license/license.go | 48 +++++++++++++++++++++++++++++ pkg/upstream/replicated.go | 7 ++++- 7 files changed, 59 insertions(+), 47 deletions(-) create mode 100644 pkg/license/license.go diff --git a/ffi/main.go b/ffi/main.go index 230d848925..9b72ff801e 100644 --- a/ffi/main.go +++ b/ffi/main.go @@ -3,19 +3,17 @@ package main import "C" import ( - "encoding/base64" "fmt" "io/ioutil" - "net/http" "os" "path/filepath" "github.com/pkg/errors" kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1" kotsscheme "github.com/replicatedhq/kots/kotskinds/client/kotsclientset/scheme" + kotslicense "github.com/replicatedhq/kots/pkg/license" "github.com/replicatedhq/kots/pkg/pull" "github.com/replicatedhq/kots/pkg/upstream" - "github.com/replicatedhq/kots/pkg/version" "k8s.io/client-go/kubernetes/scheme" ) @@ -142,45 +140,12 @@ func GetLatestLicense(socket, licenseData string) { } license := obj.(*kotsv1beta1.License) - url := fmt.Sprintf("%s/release/%s/license", license.Spec.Endpoint, license.Spec.AppSlug) - - req, err := http.NewRequest("GET", url, nil) - if err != nil { - fmt.Printf("failed to call newrequest: %s\n", err.Error()) - ffiResult = NewFFIResult(-1).WithError(err) - return - } - req.Header.Add("User-Agent", fmt.Sprintf("KOTS/%s", version.Version())) - req.Header.Set("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", license.Spec.LicenseID, license.Spec.LicenseID))))) - - resp, err := http.DefaultClient.Do(req) - if err != nil { - fmt.Printf("failed to execute get request: %s\n", err.Error()) - ffiResult = NewFFIResult(-1).WithError(err) - return - } - defer resp.Body.Close() - - if resp.StatusCode >= 400 { - fmt.Printf("unexpected result from get request: %d\n", resp.StatusCode) - ffiResult = NewFFIResult(-1).WithError(err) - return - } - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - fmt.Printf("failed to load response") - ffiResult = NewFFIResult(-1).WithError(err) - return - } - - obj, _, err = decode(body, nil, nil) + latestLicense, err := kotslicense.GetLatestLicense(license) if err != nil { - fmt.Printf("failed to decode latest license data: %s\n", err.Error()) + fmt.Printf("failed to get latest license: %s\n", err.Error()) ffiResult = NewFFIResult(-1).WithError(err) return } - latestLicense := obj.(*kotsv1beta1.License) marshalledLicense := upstream.MustMarshalLicense(latestLicense) ffiResult = NewFFIResult(1).WithData(string(marshalledLicense)) diff --git a/integration/replicated/generate.go b/integration/replicated/generate.go index 49334c1161..6f8e9cc3a0 100644 --- a/integration/replicated/generate.go +++ b/integration/replicated/generate.go @@ -114,7 +114,7 @@ func generateExpectedFilesystem(namespace, rawArchivePath string) ([]byte, error pullOptions := pull.PullOptions{ RootDir: tmpRootDir, LocalPath: rawArchivePath, - Namespace: namespace, + Namespace: namespace, ExcludeKotsKinds: true, ExcludeAdminConsole: true, CreateAppDir: false, diff --git a/integration/replicated/pull_test.go b/integration/replicated/pull_test.go index 68bf714875..b47b4edec6 100644 --- a/integration/replicated/pull_test.go +++ b/integration/replicated/pull_test.go @@ -61,7 +61,7 @@ func Test_PullReplicated(t *testing.T) { pullOptions := pull.PullOptions{ RootDir: actualDir, LicenseFile: path.Join(test.testDir, "license.yaml"), - Namespace: namespace, + Namespace: namespace, ExcludeAdminConsole: true, ExcludeKotsKinds: true, Silent: true, diff --git a/pkg/k8sdoc/types.go b/pkg/k8sdoc/types.go index 3cd1c3d871..9c24201bbb 100644 --- a/pkg/k8sdoc/types.go +++ b/pkg/k8sdoc/types.go @@ -1,7 +1,5 @@ package k8sdoc -import () - type Doc struct { APIVersion string `yaml:"apiVersion"` Kind string `yaml:"kind"` diff --git a/pkg/kotsadm/types/deployoptions.go b/pkg/kotsadm/types/deployoptions.go index 83fd7a14c4..05b999811d 100644 --- a/pkg/kotsadm/types/deployoptions.go +++ b/pkg/kotsadm/types/deployoptions.go @@ -1,9 +1,5 @@ package types -import ( - "k8s.io/api/core/v1" -) - type DeployOptions struct { Namespace string Kubeconfig string diff --git a/pkg/license/license.go b/pkg/license/license.go new file mode 100644 index 0000000000..95f876fd46 --- /dev/null +++ b/pkg/license/license.go @@ -0,0 +1,48 @@ +package license + +import ( + "encoding/base64" + "fmt" + "io/ioutil" + "net/http" + + "github.com/pkg/errors" + kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1" + "github.com/replicatedhq/kots/kotskinds/client/kotsclientset/scheme" + "github.com/replicatedhq/kots/pkg/version" +) + +func GetLatestLicense(license *kotsv1beta1.License) (*kotsv1beta1.License, error) { + url := fmt.Sprintf("%s/license/%s", license.Spec.Endpoint, license.Spec.AppSlug) + + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, errors.Wrap(err, "failed to call newrequest") + } + req.Header.Add("User-Agent", fmt.Sprintf("KOTS/%s", version.Version())) + req.Header.Set("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", license.Spec.LicenseID, license.Spec.LicenseID))))) + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, errors.Wrap(err, "failed to execute get request") + } + defer resp.Body.Close() + + if resp.StatusCode >= 400 { + return nil, errors.Wrap(err, "unexpected result from get request") + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, errors.Wrap(err, "failed to load response") + } + + decode := scheme.Codecs.UniversalDeserializer().Decode + obj, _, err := decode(body, nil, nil) + if err != nil { + return nil, errors.Wrap(err, "failed to decode latest license data") + } + latestLicense := obj.(*kotsv1beta1.License) + + return latestLicense, nil +} diff --git a/pkg/upstream/replicated.go b/pkg/upstream/replicated.go index 2d7498002f..79290fb093 100644 --- a/pkg/upstream/replicated.go +++ b/pkg/upstream/replicated.go @@ -20,6 +20,7 @@ import ( "github.com/pkg/errors" kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1" "github.com/replicatedhq/kots/pkg/crypto" + kotslicense "github.com/replicatedhq/kots/pkg/license" "github.com/replicatedhq/kots/pkg/template" "github.com/replicatedhq/kots/pkg/upstream/types" "github.com/replicatedhq/kots/pkg/util" @@ -181,7 +182,11 @@ func downloadReplicated(u *url.URL, localPath string, rootDir string, useAppDir // Add the license to the upstream, if one was propvided if license != nil { - release.Manifests["userdata/license.yaml"] = MustMarshalLicense(license) + latestLicense, err := kotslicense.GetLatestLicense(license) + if err != nil { + return nil, errors.Wrap(err, "failed to get latest license") + } + release.Manifests["userdata/license.yaml"] = MustMarshalLicense(latestLicense) } files, err := releaseToFiles(release)