Skip to content

Commit

Permalink
remove a number of unused ffi functions
Browse files Browse the repository at this point in the history
  • Loading branch information
laverya committed Aug 14, 2020
1 parent c249545 commit 70e0b96
Show file tree
Hide file tree
Showing 8 changed files with 0 additions and 1,429 deletions.
59 changes: 0 additions & 59 deletions ffi/archive.go

This file was deleted.

70 changes: 0 additions & 70 deletions ffi/installation.go

This file was deleted.

47 changes: 0 additions & 47 deletions ffi/license.go

This file was deleted.

195 changes: 0 additions & 195 deletions ffi/main.go
Expand Up @@ -3,207 +3,12 @@ package main
import "C"

import (
"fmt"
"io/ioutil"
"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"
"k8s.io/client-go/kubernetes/scheme"
)

//export UpdateCheck
func UpdateCheck(socket, fromArchivePath, namespace string) {
go func() {
var ffiResult *FFIResult

statusClient, err := connectToStatusServer(socket)
if err != nil {
fmt.Printf("failed to connect to status server: %s\n", err)
return
}
defer func() {
statusClient.end(ffiResult)
}()

tmpRoot, err := ioutil.TempDir("", "kots")
if err != nil {
fmt.Printf("failed to create temp path: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
defer os.RemoveAll(tmpRoot)

tarGz, err := extractArchive(tmpRoot, fromArchivePath)
if err != nil {
fmt.Printf("failed to extract archive: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

installationFilePath := filepath.Join(tmpRoot, "upstream", "userdata", "installation.yaml")
beforeCursor, err := readCursorFromPath(installationFilePath)
if err != nil {
fmt.Printf("failed to read cursor file: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

expectedLicenseFile := filepath.Join(tmpRoot, "upstream", "userdata", "license.yaml")
license, err := loadLicenseFromPath(expectedLicenseFile)
if err != nil {
fmt.Printf("failed to load license: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

pullOptions := pull.PullOptions{
LicenseFile: expectedLicenseFile,
Namespace: namespace,
ConfigFile: filepath.Join(tmpRoot, "upstream", "userdata", "config.yaml"),
InstallationFile: filepath.Join(tmpRoot, "upstream", "userdata", "installation.yaml"),
RootDir: tmpRoot,
ExcludeKotsKinds: true,
ExcludeAdminConsole: true,
CreateAppDir: false,
}

if _, err := pull.Pull(fmt.Sprintf("replicated://%s", license.Spec.AppSlug), pullOptions); err != nil {
fmt.Printf("failed to pull upstream: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

afterCursor, err := readCursorFromPath(installationFilePath)
if err != nil {
fmt.Printf("failed to read cursor file after update: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

fmt.Printf("Result of checking for updates for %s: Before: %s, After %s\n", license.Spec.AppSlug, beforeCursor, afterCursor)

isUpdateAvailable := !beforeCursor.Equal(afterCursor)
if !isUpdateAvailable {
ffiResult = NewFFIResult(0)
return
}

paths := []string{
filepath.Join(tmpRoot, "upstream"),
filepath.Join(tmpRoot, "base"),
filepath.Join(tmpRoot, "overlays"),
}

skippedFilesPath := filepath.Join(tmpRoot, "skippedFiles")
if _, err := os.Stat(skippedFilesPath); err == nil {
paths = append(paths, skippedFilesPath)
}

err = os.Remove(fromArchivePath)
if err != nil {
fmt.Printf("failed to delete archive to replace: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

if err := tarGz.Archive(paths, fromArchivePath); err != nil {
fmt.Printf("failed to write archive: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

ffiResult = NewFFIResult(1)
}()
}

//export GetLatestLicense
func GetLatestLicense(socket, licenseData string) {
go func() {
var ffiResult *FFIResult

statusClient, err := connectToStatusServer(socket)
if err != nil {
fmt.Printf("failed to connect to status server: %s\n", err)
return
}
defer func() {
statusClient.end(ffiResult)
}()

decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(licenseData), nil, nil)
if err != nil {
fmt.Printf("failed to decode license data: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}
license := obj.(*kotsv1beta1.License)

latestLicense, err := kotslicense.GetLatestLicense(license)
if err != nil {
fmt.Printf("failed to get latest license: %s\n", err.Error())
ffiResult = NewFFIResult(-1).WithError(err)
return
}

marshalledLicense := upstream.MustMarshalLicense(latestLicense)
ffiResult = NewFFIResult(1).WithData(string(marshalledLicense))
}()
}

//export VerifyAirgapLicense
func VerifyAirgapLicense(licenseData string) *C.char {
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(licenseData), nil, nil)
if err != nil {
fmt.Printf("failed to decode license data: %s\n", err.Error())
return nil
}
license := obj.(*kotsv1beta1.License)

verifiedLicense, err := pull.VerifySignature(license)
if err != nil {
fmt.Printf("failed to verify airgap license signature: %s\n", err.Error())
return nil
}

marshalledLicense := upstream.MustMarshalLicense(verifiedLicense)

return C.CString(string(marshalledLicense))
}

func init() {
kotsscheme.AddToScheme(scheme.Scheme)
}

func parseConfigValuesFromFile(filename string) (*kotsv1beta1.ConfigValues, error) {
contents, err := ioutil.ReadFile(filename)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, errors.Wrap(err, "failed to read config values file")
}

decode := scheme.Codecs.UniversalDeserializer().Decode
decoded, gvk, err := decode(contents, nil, nil)
if err != nil {
return nil, errors.Wrap(err, "unable to decode config values file")
}

if gvk.Group != "kots.io" || gvk.Version != "v1beta1" || gvk.Kind != "ConfigValues" {
return nil, errors.New("not config values")
}

config := decoded.(*kotsv1beta1.ConfigValues)

return config, nil
}

func main() {}

0 comments on commit 70e0b96

Please sign in to comment.