2023-10-21 15:12:08 +00:00
|
|
|
// Package customization contains the structure for the customization
|
|
|
|
// file to configure the OTS web- and command-line interface
|
|
|
|
package customization
|
2023-06-10 16:21:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2023-10-25 08:11:54 +00:00
|
|
|
// Frontend has a max attachment size of 64MiB as the base64 encoding
|
|
|
|
// will break afterwards. Therefore we use a maximum secret size of
|
|
|
|
// 65MiB and increase it by double base64 encoding:
|
|
|
|
//
|
|
|
|
// 65 MiB * 16/9 (twice 4/3 base64 size increase)
|
|
|
|
const defaultMaxSecretSize = 65 * 1024 * 1024 * (16 / 9) // = 115.6MiB
|
|
|
|
|
2023-06-10 16:21:38 +00:00
|
|
|
type (
|
2023-10-21 15:12:08 +00:00
|
|
|
// Customize holds the structure of the customization file
|
|
|
|
Customize struct {
|
2023-10-02 19:52:24 +00:00
|
|
|
AppIcon string `json:"appIcon,omitempty" yaml:"appIcon"`
|
2024-12-05 13:59:20 +00:00
|
|
|
AppIconDark string `json:"appIconDark,omitempty" yaml:"appIconDark"`
|
2023-10-02 19:52:24 +00:00
|
|
|
AppTitle string `json:"appTitle,omitempty" yaml:"appTitle"`
|
|
|
|
DisableAppTitle bool `json:"disableAppTitle,omitempty" yaml:"disableAppTitle"`
|
|
|
|
DisablePoweredBy bool `json:"disablePoweredBy,omitempty" yaml:"disablePoweredBy"`
|
|
|
|
DisableQRSupport bool `json:"disableQRSupport,omitempty" yaml:"disableQRSupport"`
|
|
|
|
DisableThemeSwitcher bool `json:"disableThemeSwitcher,omitempty" yaml:"disableThemeSwitcher"`
|
|
|
|
|
2023-06-26 21:01:06 +00:00
|
|
|
DisableExpiryOverride bool `json:"disableExpiryOverride,omitempty" yaml:"disableExpiryOverride"`
|
|
|
|
ExpiryChoices []int64 `json:"expiryChoices,omitempty" yaml:"expiryChoices"`
|
2023-10-02 19:52:24 +00:00
|
|
|
|
|
|
|
AcceptedFileTypes string `json:"acceptedFileTypes" yaml:"acceptedFileTypes"`
|
|
|
|
DisableFileAttachment bool `json:"disableFileAttachment" yaml:"disableFileAttachment"`
|
|
|
|
MaxAttachmentSizeTotal int64 `json:"maxAttachmentSizeTotal" yaml:"maxAttachmentSizeTotal"`
|
|
|
|
|
2023-10-23 12:05:20 +00:00
|
|
|
MaxSecretSize int64 `json:"-" yaml:"maxSecretSize"`
|
|
|
|
MetricsAllowedSubnets []string `json:"-" yaml:"metricsAllowedSubnets"`
|
|
|
|
OverlayFSPath string `json:"-" yaml:"overlayFSPath"`
|
|
|
|
UseFormalLanguage bool `json:"-" yaml:"useFormalLanguage"`
|
2024-09-22 10:55:11 +00:00
|
|
|
|
|
|
|
FooterLinks []FooterLink `json:"footerLinks,omitempty" yaml:"footerLinks"`
|
|
|
|
}
|
|
|
|
|
|
|
|
FooterLink struct {
|
|
|
|
Name string `json:"name" yaml:"name"`
|
|
|
|
URL string `json:"url" yaml:"url"`
|
2023-06-10 16:21:38 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-10-21 15:12:08 +00:00
|
|
|
// Load retrieves the Customization file from filesystem
|
|
|
|
func Load(filename string) (cust Customize, err error) {
|
2023-06-10 16:21:38 +00:00
|
|
|
if filename == "" {
|
|
|
|
// None given, take a shortcut
|
2023-08-10 22:57:30 +00:00
|
|
|
cust.applyFixes()
|
|
|
|
return cust, nil
|
2023-06-10 16:21:38 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 20:53:29 +00:00
|
|
|
cf, err := os.Open(filename) //#nosec:G304 // Loading a custom file is the intention here
|
2023-06-10 16:21:38 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
logrus.Warn("customize file given but not found")
|
|
|
|
return cust, nil
|
|
|
|
}
|
|
|
|
return cust, errors.Wrap(err, "opening customize file")
|
|
|
|
}
|
2023-10-04 20:53:29 +00:00
|
|
|
defer func() {
|
|
|
|
if err := cf.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing customize file (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2023-06-10 16:21:38 +00:00
|
|
|
|
2023-08-10 22:57:30 +00:00
|
|
|
if err = yaml.NewDecoder(cf).Decode(&cust); err != nil {
|
|
|
|
return cust, errors.Wrap(err, "decoding customize file")
|
|
|
|
}
|
|
|
|
|
|
|
|
cust.applyFixes()
|
|
|
|
|
|
|
|
return cust, nil
|
2023-06-10 16:21:38 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 15:12:08 +00:00
|
|
|
// ToJSON is a templating helper which returns the customization
|
|
|
|
// serialized as JSON in a string
|
|
|
|
func (c Customize) ToJSON() (string, error) {
|
2023-06-10 16:21:38 +00:00
|
|
|
j, err := json.Marshal(c)
|
|
|
|
return string(j), errors.Wrap(err, "marshalling JSON")
|
|
|
|
}
|
2023-08-10 22:57:30 +00:00
|
|
|
|
2023-10-21 15:12:08 +00:00
|
|
|
func (c *Customize) applyFixes() {
|
2023-08-10 22:57:30 +00:00
|
|
|
if len(c.AppTitle) == 0 {
|
|
|
|
c.AppTitle = "OTS - One Time Secrets"
|
|
|
|
}
|
2023-10-25 08:11:54 +00:00
|
|
|
|
|
|
|
if c.MaxSecretSize == 0 {
|
|
|
|
c.MaxSecretSize = defaultMaxSecretSize
|
|
|
|
}
|
2023-08-10 22:57:30 +00:00
|
|
|
}
|