forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssl_config.go
46 lines (38 loc) · 999 Bytes
/
ssl_config.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
package auth
import (
"crypto/x509/pkix"
"github.com/cloudfoundry/bosh-agent/internal/github.com/cloudfoundry/bosh-utils/errors"
)
type SSLConfig struct {
CertFile string
KeyFile string
CACertPem string
PkixNames []pkix.Name
}
func NewSSLConfig(certFile, keyFile, certPem string, allowedNames []string) (SSLConfig, error) {
pkixNames, err := parseNames(allowedNames)
if err != nil {
return SSLConfig{}, err
}
return SSLConfig{
CertFile: certFile,
KeyFile: keyFile,
CACertPem: certPem,
PkixNames: pkixNames,
}, nil
}
func parseNames(allowedNames []string) ([]pkix.Name, error) {
if len(allowedNames) == 0 {
return nil, errors.Error("AllowedNames must be specified")
}
var pkixNames []pkix.Name
parser := NewDistinguishedNamesParser()
for _, dn := range allowedNames {
pkixName, err := parser.Parse(dn)
if err != nil {
return nil, errors.WrapError(err, "Invalid AllowedNames")
}
pkixNames = append(pkixNames, *pkixName)
}
return pkixNames, nil
}