Skip to content

Commit

Permalink
Get latest license on app install (#280)
Browse files Browse the repository at this point in the history
* Get latest license on app install
  • Loading branch information
sgalsaleh authored Jan 28, 2020
1 parent 2e072ef commit a177cc1
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 47 deletions.
41 changes: 3 additions & 38 deletions ffi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion integration/replicated/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion integration/replicated/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions pkg/k8sdoc/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package k8sdoc

import ()

type Doc struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Expand Down
4 changes: 0 additions & 4 deletions pkg/kotsadm/types/deployoptions.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package types

import (
"k8s.io/api/core/v1"
)

type DeployOptions struct {
Namespace string
Kubeconfig string
Expand Down
48 changes: 48 additions & 0 deletions pkg/license/license.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 6 additions & 1 deletion pkg/upstream/replicated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a177cc1

Please sign in to comment.