1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
package config
import (
"os"
"strconv"
"strings"
"github.com/spiral/errors"
)
// SSL defines https server configuration.
type SSL struct {
// Address to listen as HTTPS server, defaults to 0.0.0.0:443.
Address string
// Redirect when enabled forces all http connections to switch to https.
Redirect bool
// Key defined private server key.
Key string
// Cert is https certificate.
Cert string
// Root CA file
RootCA string
// internal
host string
Port int
}
func (s *SSL) Valid() error {
const op = errors.Op("ssl_valid")
parts := strings.Split(s.Address, ":")
switch len(parts) {
// :443 form
// localhost:443 form
// use 0.0.0.0 as host and 443 as port
case 2:
if parts[0] == "" {
s.host = "0.0.0.0"
} else {
s.host = parts[0]
}
port, err := strconv.Atoi(parts[1])
if err != nil {
return errors.E(op, err)
}
s.Port = port
default:
return errors.E(op, errors.Errorf("unknown format, accepted format is [:<port> or <host>:<port>], provided: %s", s.Address))
}
if _, err := os.Stat(s.Key); err != nil {
if os.IsNotExist(err) {
return errors.E(op, errors.Errorf("key file '%s' does not exists", s.Key))
}
return err
}
if _, err := os.Stat(s.Cert); err != nil {
if os.IsNotExist(err) {
return errors.E(op, errors.Errorf("cert file '%s' does not exists", s.Cert))
}
return err
}
// RootCA is optional, but if provided - check it
if s.RootCA != "" {
if _, err := os.Stat(s.RootCA); err != nil {
if os.IsNotExist(err) {
return errors.E(op, errors.Errorf("root ca path provided, but path '%s' does not exists", s.RootCA))
}
return err
}
}
return nil
}
|