forked from allegro/mesos-executor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert.go
32 lines (28 loc) · 770 Bytes
/
cert.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
package executor
import (
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"strings"
)
// GetCertFromEnvVariables returns certificate stored in
// environment variables. If no certificate is found then
// empty string is returned
func GetCertFromEnvVariables(env []string) (*x509.Certificate, error) {
for _, value := range env {
if strings.HasPrefix(value, "CERTIFICATE=") {
pemEncoded := []byte(strings.TrimPrefix(value, "CERTIFICATE="))
p, _ := pem.Decode(pemEncoded)
if p == nil {
return nil, errors.New("missing certificate data")
}
cert, err := x509.ParseCertificate(p.Bytes)
if err != nil {
return nil, fmt.Errorf("certificate is invalid: %s", err)
}
return cert, nil
}
}
return nil, errors.New("missing certificate")
}