-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkpconfig.go
63 lines (52 loc) · 1.49 KB
/
kpconfig.go
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
package testcerts
import (
"errors"
"math/big"
"net"
)
var (
// ErrEmptyConfig is returned when a KeyPairConfig is empty.
ErrEmptyConfig = errors.New("empty KeyPairConfig")
// ErrInvalidIP is returned when an IP address is invalid.
ErrInvalidIP = errors.New("invalid IP address")
)
// KeyPairConfig is a configuration for generating an X.509 key pair.
type KeyPairConfig struct {
// Domains is a list of domains to include in the certificate as Subject
// Alternative Names.
Domains []string
// IPAddresses is a list of IP addresses to include in the certificate
// as Subject Alternative Names.
IPAddresses []string
// SerialNumber is the serial number to use for the certificate.
SerialNumber *big.Int
// CommonName is the Common Name to use for the certificate.
CommonName string
}
// Validate validates the KeyPairConfig ensuring that it is not empty and that
// provided values are valid.
func (c *KeyPairConfig) Validate() error {
// Check if the config is empty.
if len(c.Domains) == 0 && len(c.IPAddresses) == 0 {
return ErrEmptyConfig
}
// Validate IP addresses.
for _, ip := range c.IPAddresses {
if net.ParseIP(ip) == nil {
return ErrInvalidIP
}
}
return nil
}
// IPAddresses returns a list of IP addresses in Net.IP format.
func (c *KeyPairConfig) IPNetAddresses() ([]net.IP, error) {
var ips []net.IP
for _, ip := range c.IPAddresses {
parsed := net.ParseIP(ip)
if parsed == nil {
return nil, ErrInvalidIP
}
ips = append(ips, parsed)
}
return ips, nil
}