constellation/internal/license/file.go
Thomas Tendyck bd63aa3c6b add license headers
sed -i '1i/*\nCopyright (c) Edgeless Systems GmbH\n\nSPDX-License-Identifier: AGPL-3.0-only\n*/\n' `grep -rL --include='*.go' 'DO NOT EDIT'`
gofumpt -w .
2022-09-05 09:17:25 +02:00

35 lines
823 B
Go

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package license
import (
"encoding/base64"
"fmt"
"github.com/edgelesssys/constellation/internal/file"
)
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
}