forked from influxdata/flux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dependencies.go
147 lines (125 loc) · 3.8 KB
/
dependencies.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package flux
import (
"context"
"net"
"syscall"
"time"
"github.com/InfluxCommunity/flux/codes"
"github.com/InfluxCommunity/flux/dependencies/filesystem"
"github.com/InfluxCommunity/flux/dependencies/http"
"github.com/InfluxCommunity/flux/dependencies/secret"
"github.com/InfluxCommunity/flux/dependencies/url"
"github.com/InfluxCommunity/flux/dependency"
"github.com/InfluxCommunity/flux/internal/errors"
)
var _ Dependencies = (*Deps)(nil)
type Dependency = dependency.Interface
type key int
const dependenciesKey key = iota
type Dependencies interface {
Dependency
HTTPClient() (http.Client, error)
PrivateHTTPClient() (http.Client, error)
FilesystemService() (filesystem.Service, error)
SecretService() (secret.Service, error)
URLValidator() (url.Validator, error)
}
// Deps implements Dependencies.
// Any deps which are nil will produce an explicit error.
type Deps struct {
Deps WrappedDeps
}
type WrappedDeps struct {
HTTPClient http.Client
FilesystemService filesystem.Service
SecretService secret.Service
URLValidator url.Validator
}
func (d Deps) HTTPClient() (http.Client, error) {
if d.Deps.HTTPClient != nil {
return d.Deps.HTTPClient, nil
}
return nil, errors.New(codes.Unimplemented, "http client uninitialized in dependencies")
}
func (d Deps) PrivateHTTPClient() (http.Client, error) {
c, err := d.HTTPClient()
if err != nil {
return nil, err
}
return http.NewPrivateClient(c), nil
}
func (d Deps) FilesystemService() (filesystem.Service, error) {
if d.Deps.FilesystemService != nil {
return d.Deps.FilesystemService, nil
}
return nil, errors.New(codes.Unimplemented, "filesystem service uninitialized in dependencies")
}
func (d Deps) SecretService() (secret.Service, error) {
if d.Deps.SecretService != nil {
return d.Deps.SecretService, nil
}
return nil, errors.New(codes.Unimplemented, "secret service uninitialized in dependencies")
}
func (d Deps) URLValidator() (url.Validator, error) {
if d.Deps.URLValidator != nil {
return d.Deps.URLValidator, nil
}
return nil, errors.New(codes.Unimplemented, "url validator uninitialized in dependencies")
}
func (d Deps) Inject(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, dependenciesKey, d)
if d.Deps.FilesystemService != nil {
ctx = filesystem.Inject(ctx, d.Deps.FilesystemService)
}
return ctx
}
func GetDependencies(ctx context.Context) Dependencies {
deps := ctx.Value(dependenciesKey)
if deps == nil {
return NewEmptyDependencies()
}
return deps.(Dependencies)
}
// NewDefaultDependencies produces a set of dependencies.
// Not all dependencies have valid defaults and will not be set.
func NewDefaultDependencies() Deps {
validator := url.PassValidator{}
return Deps{
Deps: WrappedDeps{
HTTPClient: http.NewLimitedDefaultClient(validator),
// Default to having no filesystem, no secrets, and no url validation (always pass).
FilesystemService: nil,
SecretService: secret.EmptySecretService{},
URLValidator: validator,
},
}
}
// NewEmptyDependencies produces an empty set of dependencies.
// Accessing any dependency will result in an error.
func NewEmptyDependencies() Deps {
return Deps{}
}
// GetDialer will return a net.Dialer using the injected dependencies
// within the context.Context.
func GetDialer(ctx context.Context) (*net.Dialer, error) {
deps := GetDependencies(ctx)
url, err := deps.URLValidator()
if err != nil {
return nil, err
}
// Control is called after DNS lookup, but before the
// network connection is initiated.
control := func(network, address string, c syscall.RawConn) error {
host, _, err := net.SplitHostPort(address)
if err != nil {
return err
}
ip := net.ParseIP(host)
return url.ValidateIP(ip)
}
return &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
Control: control,
}, nil
}