2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-08-16 10:06:38 -04:00
|
|
|
package license
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
2022-08-16 10:06:38 -04:00
|
|
|
)
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// FromFile reads the license from fileHandler at path and returns it as a string.
|
2022-08-16 10:06:38 -04:00
|
|
|
func FromFile(fileHandler file.Handler, path string) (string, error) {
|
|
|
|
readBytes, err := fileHandler.Read(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("unable to read from '%s': %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
maxSize := base64.StdEncoding.DecodedLen(len(readBytes))
|
|
|
|
decodedLicense := make([]byte, maxSize)
|
|
|
|
n, err := base64.StdEncoding.Decode(decodedLicense, readBytes)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("unable to base64 decode license file: %w", err)
|
|
|
|
}
|
|
|
|
if n != 36 { // length of UUID
|
|
|
|
return "", fmt.Errorf("license file corrupt: wrong length")
|
|
|
|
}
|
|
|
|
decodedLicense = decodedLicense[:n]
|
|
|
|
|
|
|
|
return string(decodedLicense), nil
|
|
|
|
}
|