2021-09-27 08:36:28 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Encryption is the encryption parameter.
|
|
|
|
type Encryption int
|
|
|
|
|
|
|
|
// supported encryption policies.
|
|
|
|
const (
|
|
|
|
EncryptionNo Encryption = iota
|
|
|
|
EncryptionOptional
|
|
|
|
EncryptionStrict
|
|
|
|
)
|
|
|
|
|
2022-06-21 11:41:15 +00:00
|
|
|
// MarshalJSON implements json.Marshaler.
|
2021-09-27 08:36:28 +00:00
|
|
|
func (d Encryption) MarshalJSON() ([]byte, error) {
|
|
|
|
var out string
|
|
|
|
|
|
|
|
switch d {
|
|
|
|
case EncryptionNo:
|
|
|
|
out = "no"
|
|
|
|
|
|
|
|
case EncryptionOptional:
|
|
|
|
out = "optional"
|
|
|
|
|
2023-03-31 14:22:08 +00:00
|
|
|
case EncryptionStrict:
|
2021-09-27 08:36:28 +00:00
|
|
|
out = "strict"
|
2023-03-31 14:22:08 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid encryption: %v", d)
|
2021-09-27 08:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(out)
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:41:15 +00:00
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
2021-09-27 08:36:28 +00:00
|
|
|
func (d *Encryption) UnmarshalJSON(b []byte) error {
|
|
|
|
var in string
|
|
|
|
if err := json.Unmarshal(b, &in); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch in {
|
|
|
|
case "no", "false":
|
|
|
|
*d = EncryptionNo
|
|
|
|
|
|
|
|
case "optional":
|
|
|
|
*d = EncryptionOptional
|
|
|
|
|
|
|
|
case "strict", "yes", "true":
|
|
|
|
*d = EncryptionStrict
|
|
|
|
|
|
|
|
default:
|
2023-03-31 14:22:08 +00:00
|
|
|
return fmt.Errorf("invalid encryption: '%s'", in)
|
2021-09-27 08:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-11 09:46:40 +00:00
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
// UnmarshalEnv implements env.Unmarshaler.
|
|
|
|
func (d *Encryption) UnmarshalEnv(_ string, v string) error {
|
|
|
|
return d.UnmarshalJSON([]byte(`"` + v + `"`))
|
2021-10-11 09:46:40 +00:00
|
|
|
}
|