-
Notifications
You must be signed in to change notification settings - Fork 51
/
transformer.go
80 lines (66 loc) · 1.96 KB
/
transformer.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package secretsproxy
import (
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"net/url"
"strconv"
"go.aporeto.io/trireme-lib/common"
)
// SecretsDriver is a generic interface that the secrets driver must implement.
type SecretsDriver interface {
Transport() http.RoundTripper
Transform(r *http.Request) error
}
// GenericSecretsDriver holds the configuration information for the driver and implements
// the SecretsDriver interface.
type GenericSecretsDriver struct {
transport *http.Transport
token string
targetURL *url.URL
}
// NewGenericSecretsDriver creates a new Kubernetes Secrets Driver. It
// always uses the incluster config to automatically derive all the
// necessary values.
func NewGenericSecretsDriver(ca []byte, token string, network *common.Service) (SecretsDriver, error) {
caPool := x509.NewCertPool()
if !caPool.AppendCertsFromPEM(ca) {
return nil, fmt.Errorf("No valid CA provided")
}
targetAddress := ""
if len(network.FQDNs) > 0 {
targetAddress = network.FQDNs[0]
} else if len(network.Addresses) > 0 {
targetAddress = network.Addresses[0].IP.String()
} else {
return nil, fmt.Errorf("No valid target")
}
if network.Ports.Min == 0 {
return nil, fmt.Errorf("Invalid port specification")
}
targetURL, err := url.Parse("https://" + targetAddress + ":" + strconv.Itoa(int(network.Ports.Min)))
if err != nil {
return nil, fmt.Errorf("Invalid URL for secrets service")
}
return &GenericSecretsDriver{
transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caPool,
},
},
token: token,
targetURL: targetURL,
}, nil
}
// Transport implements the transport interface of the SecretsDriver.
func (k *GenericSecretsDriver) Transport() http.RoundTripper {
return k.transport
}
// Transform transforms the request of the SecretsDriver
func (k *GenericSecretsDriver) Transform(r *http.Request) error {
r.Host = k.targetURL.Host
r.URL = k.targetURL
r.Header.Add("Authorization", "Bearer "+k.token)
return nil
}