diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/license.go | 27 | ||||
-rw-r--r-- | utils/license_test.go | 28 |
2 files changed, 55 insertions, 0 deletions
diff --git a/utils/license.go b/utils/license.go index 5ec94386d..f0763d741 100644 --- a/utils/license.go +++ b/utils/license.go @@ -12,6 +12,8 @@ import ( "encoding/base64" "encoding/pem" "fmt" + "io/ioutil" + "os" "strconv" "strings" @@ -112,6 +114,31 @@ func ValidateLicense(signed []byte) (bool, string) { return true, string(plaintext) } +func GetLicenseFileFromDisk(fileName string) []byte { + file, err := os.Open(fileName) + if err != nil { + l4g.Error("Failed to open license key from disk at %v err=%v", fileName, err.Error()) + return nil + } + defer file.Close() + + licenseBytes, err := ioutil.ReadAll(file) + if err != nil { + l4g.Error("Failed to read license key from disk at %v err=%v", fileName, err.Error()) + return nil + } + + return licenseBytes +} + +func GetLicenseFileLocation(fileLocation string) string { + if fileLocation == "" { + return FindDir("config") + "mattermost.mattermost-license" + } else { + return fileLocation + } +} + func getClientLicense(l *model.License) map[string]string { props := make(map[string]string) diff --git a/utils/license_test.go b/utils/license_test.go index 6508172d5..5c0122bc0 100644 --- a/utils/license_test.go +++ b/utils/license_test.go @@ -66,3 +66,31 @@ func TestClientLicenseEtag(t *testing.T) { t.Fatal("etags should not match") } } + +func TestGetLicenseFileLocation(t *testing.T) { + fileName := GetLicenseFileLocation("") + if len(fileName) == 0 { + t.Fatal("invalid default file name") + } + + fileName = GetLicenseFileLocation("mattermost.mattermost-license") + if fileName != "mattermost.mattermost-license" { + t.Fatal("invalid file name") + } +} + +func TestGetLicenseFileFromDisk(t *testing.T) { + fileBytes := GetLicenseFileFromDisk("thisfileshouldnotexist.mattermost-license") + if len(fileBytes) > 0 { + t.Fatal("invalid bytes") + } + + fileBytes = GetLicenseFileFromDisk(FindConfigFile("config.json")) + if len(fileBytes) == 0 { // a valid bytes but should be a fail license + t.Fatal("invalid bytes") + } + + if success, _ := ValidateLicense(fileBytes); success { + t.Fatal("should have been an invalid file") + } +} |