summaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
authorCorey Hulen <corey@hulen.com>2017-03-31 06:54:30 -0700
committerChristopher Speller <crspeller@gmail.com>2017-03-31 09:54:30 -0400
commit00bb4799899cbbcdf48b147639dee3cee7ea199b (patch)
tree74881150207ae980b1703644da265fed6886d65d /utils
parentfa40bfb9e6f3bcdfe631301be2ee3ce755cbef0e (diff)
downloadchat-00bb4799899cbbcdf48b147639dee3cee7ea199b.tar.gz
chat-00bb4799899cbbcdf48b147639dee3cee7ea199b.tar.bz2
chat-00bb4799899cbbcdf48b147639dee3cee7ea199b.zip
PLT-6090 adding ability to read license file from disk (#5895)
* PLT-6090 adding ability to read license file from disk * Fixing unit test that fails only sometimes * Fixing test that fails randomly
Diffstat (limited to 'utils')
-rw-r--r--utils/license.go27
-rw-r--r--utils/license_test.go28
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")
+ }
+}