-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathsdk.go
More file actions
243 lines (209 loc) · 6.7 KB
/
Copy pathsdk.go
File metadata and controls
243 lines (209 loc) · 6.7 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package handlers
import (
"errors"
"log/slog"
"github.com/opentdf/platform/otdfctl/pkg/auth"
"github.com/opentdf/platform/otdfctl/pkg/profiles"
"github.com/opentdf/platform/otdfctl/pkg/utils"
"github.com/opentdf/platform/protocol/go/common"
"github.com/opentdf/platform/sdk"
)
var (
SDK *sdk.SDK
ErrUnauthenticated = errors.New("unauthenticated")
)
type Handler struct {
sdk *sdk.SDK
platformEndpoint string
}
type handlerOpts struct {
endpoint string
TLSNoVerify bool
profile *profiles.OtdfctlProfileStore
sdkOpts []sdk.Option
hooks []Hook
}
// HandlerOption configures a Handler during construction. Callers compose
// options by passing them to New; extension points that want to defer their
// SDK-option contribution until profile resolution is complete should use
// WithHook rather than WithSDKOpts.
type HandlerOption func(handlerOpts) handlerOpts
// Hook is the umbrella type for handler-construction hooks. Concrete hook
// variants (PreSDKHook, and any future point-specific hooks added later)
// implement Hook so callers can pass them uniformly through WithHook and
// common.NewHandler. New hook points are added by declaring a new named
// callback type, giving it an isHandlerHook receiver, and extending the
// applyHooks dispatch switch — no new exported registration function is
// needed.
type Hook interface {
isHandlerHook()
}
// PreSDKHookContext exposes the resolved handler configuration to a
// PreSDKHook callback so it can decide which SDK options to contribute
// (for example, an interceptor that only attaches for a specific endpoint
// or profile).
type PreSDKHookContext struct {
Endpoint string
TLSNoVerify bool
Profile *profiles.OtdfctlProfileStore
}
// PreSDKHook runs after every HandlerOption has been applied and before
// sdk.New is called. It returns additional SDK options that get appended to
// the final options list. Multiple PreSDKHooks compose in registration
// order and each sees the same fully-resolved PreSDKHookContext.
type PreSDKHook func(PreSDKHookContext) []sdk.Option
func (PreSDKHook) isHandlerHook() {}
func WithEndpoint(endpoint string, tlsNoVerify bool) HandlerOption {
return func(c handlerOpts) handlerOpts {
c.endpoint = endpoint
c.TLSNoVerify = tlsNoVerify
return c
}
}
func WithProfile(profile *profiles.OtdfctlProfileStore) HandlerOption {
return func(c handlerOpts) handlerOpts {
if profile == nil {
return c
}
c.profile = profile
c.endpoint = profile.GetEndpoint()
c.TLSNoVerify = profile.GetTLSNoVerify()
// get sdk opts
opts, err := auth.GetSDKAuthOptionFromProfile(profile)
if err != nil {
return c
}
c.sdkOpts = append(c.sdkOpts, opts)
return c
}
}
func WithSDKOpts(opts ...sdk.Option) HandlerOption {
return func(c handlerOpts) handlerOpts {
c.sdkOpts = opts
return c
}
}
// WithHook registers one or more Hooks that run during handler construction
// after every HandlerOption has been applied and before the SDK is built.
// Nil entries are ignored so a caller cannot accidentally register a hook
// that would panic on execution.
func WithHook(hooks ...Hook) HandlerOption {
return func(c handlerOpts) handlerOpts {
for _, h := range hooks {
if h == nil {
continue
}
c.hooks = append(c.hooks, h)
}
return c
}
}
// applyHooks dispatches every registered Hook to the callback attached to
// its concrete variant. Each variant gets its own context type derived from
// the resolved handler configuration, and any SDK options the hook returns
// are appended to o.sdkOpts. Broken out of New so the extension point is
// directly testable without touching the network. Unknown Hook variants
// are dropped so future variants added upstream do not panic older code
// paths that have not yet been updated to dispatch them.
func applyHooks(o handlerOpts) handlerOpts {
if len(o.hooks) == 0 {
return o
}
for _, h := range o.hooks {
// Switch anticipates future Hook variants (see Hook doc) even though
// only PreSDKHook exists today, so gocritic's single-case suggestion
// would force a rewrite the moment a second variant lands.
//nolint:gocritic // extensible dispatch, keep switch
switch v := h.(type) {
case PreSDKHook:
// A typed-nil function value stored in the Hook interface
// passes the untyped-nil filter inside WithHook, so guard
// per-variant here before invoking the callback.
if v == nil {
continue
}
ctx := PreSDKHookContext{
Endpoint: o.endpoint,
TLSNoVerify: o.TLSNoVerify,
Profile: o.profile,
}
o.sdkOpts = append(o.sdkOpts, v(ctx)...)
}
}
return o
}
// Creates a new handler wrapping the SDK, which is authenticated through the cached client-credentials flow tokens
func New(opts ...HandlerOption) (Handler, error) {
var o handlerOpts
for _, f := range opts {
o = f(o)
}
o = applyHooks(o)
u, err := utils.NormalizeEndpoint(o.endpoint)
if err != nil {
return Handler{}, err
}
// get auth
authSDKOpt, err := auth.GetSDKAuthOptionFromProfile(o.profile)
if err != nil {
return Handler{}, err
}
defaultSDKOpts := []sdk.Option{
authSDKOpt,
sdk.WithConnectionValidation(),
sdk.WithLogger(slog.Default()),
}
if o.TLSNoVerify {
defaultSDKOpts = append(defaultSDKOpts, sdk.WithInsecureSkipVerifyConn())
}
if u.Scheme == "http" {
defaultSDKOpts = append(defaultSDKOpts, sdk.WithInsecurePlaintextConn())
}
o.sdkOpts = append(defaultSDKOpts, o.sdkOpts...)
s, err := sdk.New(u.String(), o.sdkOpts...)
if err != nil {
return Handler{}, err
}
return Handler{
sdk: s,
platformEndpoint: o.endpoint,
}, nil
}
func (h Handler) Close() error {
return h.sdk.Close()
}
func (h Handler) Direct() *sdk.SDK {
return h.sdk
}
// Replace all labels in the metadata
func (h Handler) WithReplaceLabelsMetadata(metadata *common.MetadataMutable, labels map[string]string) func(*common.MetadataMutable) *common.MetadataMutable {
return func(*common.MetadataMutable) *common.MetadataMutable {
nextMetadata := &common.MetadataMutable{
Labels: labels,
}
return nextMetadata
}
}
// Append a label to the metadata
func (h Handler) WithLabelMetadata(metadata *common.MetadataMutable, key, value string) func(*common.MetadataMutable) *common.MetadataMutable {
return func(*common.MetadataMutable) *common.MetadataMutable {
labels := metadata.GetLabels()
labels[key] = value
nextMetadata := &common.MetadataMutable{
Labels: labels,
}
return nextMetadata
}
}
// func buildMetadata(metadata *common.MetadataMutable, fns ...func(*common.MetadataMutable) *common.MetadataMutable) *common.MetadataMutable {
// if metadata == nil {
// metadata = &common.MetadataMutable{}
// }
// if len(fns) == 0 {
// return metadata
// }
// for _, fn := range fns {
// metadata = fn(metadata)
// }
// return metadata
// }