config_json.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // +build json
  2. package tls
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "io/ioutil"
  7. )
  8. func (this *Config) UnmarshalJSON(data []byte) error {
  9. type JSONCertConfig struct {
  10. CertFile string `json:"certificateFile"`
  11. KeyFile string `json:"keyFile"`
  12. }
  13. type JSONConfig struct {
  14. Insecure bool `json:"allowInsecure"`
  15. Certs []*JSONCertConfig `json:"certificates"`
  16. }
  17. jsonConfig := new(JSONConfig)
  18. if err := json.Unmarshal(data, jsonConfig); err != nil {
  19. return err
  20. }
  21. this.Certificate = make([]*Certificate, len(jsonConfig.Certs))
  22. for idx, certConf := range jsonConfig.Certs {
  23. cert, err := ioutil.ReadFile(certConf.CertFile)
  24. if err != nil {
  25. return errors.New("TLS: Failed to load certificate file: " + err.Error())
  26. }
  27. key, err := ioutil.ReadFile(certConf.KeyFile)
  28. if err != nil {
  29. return errors.New("TLS: Failed to load key file: " + err.Error())
  30. }
  31. this.Certificate[idx] = &Certificate{
  32. Key: key,
  33. Certificate: cert,
  34. }
  35. }
  36. this.AllowInsecure = jsonConfig.Insecure
  37. return nil
  38. }