-
Notifications
You must be signed in to change notification settings - Fork 8
/
oracle_provider.go
382 lines (321 loc) · 11.2 KB
/
oracle_provider.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package oracle
import (
"crypto/rsa"
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
"github.com/oracle/oci-go-sdk/common/auth"
"path"
"github.com/fnproject/fn_go/client/version"
"github.com/fnproject/fn_go/clientv2"
"github.com/fnproject/fn_go/provider"
openapi "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
homedir "github.com/mitchellh/go-homedir"
"github.com/oracle/oci-go-sdk/common"
oci "github.com/oracle/oci-go-sdk/common"
)
const (
CfgTenancyID = "oracle.tenancy-id"
CfgUserID = "oracle.user-id"
CfgFingerprint = "oracle.fingerprint"
CfgKeyFile = "oracle.key-file"
CfgPassPhrase = "oracle.pass-phrase"
CfgProfile = "oracle.profile"
CfgCompartmentID = "oracle.compartment-id"
CfgDisableCerts = "oracle.disable-certs"
CompartmentMetadata = "http://169.254.169.254/opc/v1/instance/compartmentId"
)
// Provider : Oracle Authentication provider
type Provider struct {
// FnApiUrl is the endpoint to use for API interactions
FnApiUrl *url.URL
// FnCallUrl is the endpoint used for call interactions
FnCallUrl *url.URL
// The key provider can be a user or instance-principal-based one
KP oci.KeyProvider
//DisableCerts indicates if server certificates should be ignored
DisableCerts bool
//CompartmentID is the ocid of the functions compartment ID for a given function
CompartmentID string
}
func (op *Provider) APIClientv2() *clientv2.Fn {
runtime := openapi.New(op.FnApiUrl.Host, path.Join(op.FnApiUrl.Path, clientv2.DefaultBasePath), []string{op.FnApiUrl.Scheme})
runtime.Transport = op.WrapCallTransport(runtime.Transport)
return clientv2.New(runtime, strfmt.Default)
}
type Response struct {
Annotations Annotations `json:"annotations"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Name string `json:"name"`
}
type Annotations struct {
CompartmentID string `json:"oracle.com/oci/compartmentId"`
ShortCode string `json:"oracle.com/oci/appCode"`
}
func NewFromConfig(configSource provider.ConfigSource, passphraseSource provider.PassPhraseSource) (provider.Provider, error) {
apiUrl, err := provider.CanonicalFnAPIUrl(configSource.GetString(provider.CfgFnAPIURL))
if err != nil {
return nil, err
}
keyID, pKey, err := loadOracleConfig(configSource, passphraseSource)
if err != nil {
return nil, err
}
compartmentID := configSource.GetString(CfgCompartmentID)
if compartmentID == "" {
return nil, fmt.Errorf("no OCI compartment ID specified in config key %s ", CfgCompartmentID)
}
return &Provider{FnApiUrl: apiUrl,
KP: &ociKeyProvider{
ID: keyID,
key: pKey,
},
DisableCerts: configSource.GetBool(CfgDisableCerts),
CompartmentID: compartmentID,
}, nil
}
func NewIPProvider(configSource provider.ConfigSource, passphraseSource provider.PassPhraseSource) (provider.Provider, error) {
ip, err := auth.InstancePrincipalConfigurationProvider()
if err != nil {
return nil, err
}
cfgApiUrl := configSource.GetString(provider.CfgFnAPIURL)
if cfgApiUrl == "" {
region, err := ip.Region()
if err != nil {
return nil, err
}
// Construct the API endpoint from the "nearby" endpoint
cfgApiUrl = fmt.Sprintf("https://functions.%s.oraclecloud.com", region)
}
apiUrl, err := provider.CanonicalFnAPIUrl(cfgApiUrl)
if err != nil {
return nil, err
}
compartmentID := configSource.GetString(CfgCompartmentID)
if compartmentID == "" {
// Get the local compartment ID from the metadata endpoint
resp, err := http.DefaultClient.Get(CompartmentMetadata)
if err != nil {
return nil, fmt.Errorf("problem fetching compartment Id from metadata endpoint %s", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("problem fetching compartment Id from metadata endpoint %s", err)
}
compartmentID = string(body)
}
return &Provider{FnApiUrl: apiUrl,
KP: ip,
DisableCerts: configSource.GetBool(CfgDisableCerts),
CompartmentID: compartmentID,
}, nil
}
func (op *Provider) APIURL() *url.URL {
return op.FnApiUrl
}
func (p *Provider) UnavailableResources() []provider.FnResourceType {
return []provider.FnResourceType{provider.TriggerResourceType}
}
func (op *Provider) WrapCallTransport(roundTripper http.RoundTripper) http.RoundTripper {
if op.DisableCerts {
roundTripper = InsecureRoundTripper(roundTripper)
}
ociClient := common.RequestSigner(op.KP, []string{"host", "date", "(request-target)", "opc-compartment-id"}, []string{"content-length", "content-type", "x-content-sha256"})
signingRoundTrripper := ociSigningRoundTripper{
transport: roundTripper,
ociClient: ociClient,
}
roundTripper = compartmentIDRoundTripper{
transport: signingRoundTrripper,
compartmentID: op.CompartmentID,
}
roundTripper = requestIdRoundTripper{
transport: roundTripper,
}
return roundTripper
}
func (op *Provider) APIClient() *clientv2.Fn {
runtime := openapi.New(op.FnApiUrl.Host, path.Join(op.FnApiUrl.Path, clientv2.DefaultBasePath), []string{op.FnApiUrl.Scheme})
runtime.Transport = op.WrapCallTransport(runtime.Transport)
return clientv2.New(runtime, strfmt.Default)
}
func (op *Provider) VersionClient() *version.Client {
runtime := openapi.New(op.FnApiUrl.Host, op.FnApiUrl.Path, []string{op.FnApiUrl.Scheme})
runtime.Transport = op.WrapCallTransport(runtime.Transport)
return version.New(runtime, strfmt.Default)
}
type ociKeyProvider struct {
ID string
key *rsa.PrivateKey
}
func (kp ociKeyProvider) PrivateRSAKey() (*rsa.PrivateKey, error) {
return kp.key, nil
}
func (kp ociKeyProvider) KeyID() (string, error) {
return kp.ID, nil
}
type ociSigningRoundTripper struct {
ociClient common.HTTPRequestSigner
transport http.RoundTripper
}
func (t ociSigningRoundTripper) RoundTrip(request *http.Request) (response *http.Response, err error) {
if request.Header.Get("Date") == "" {
request.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
}
request, err = signRequest(t.ociClient, request)
if err != nil {
return
}
response, err = t.transport.RoundTrip(request)
return
}
// Add the necessary headers and sign the request
func signRequest(signer common.HTTPRequestSigner, request *http.Request) (signedRequest *http.Request, err error) {
// Check that a Date header is set, otherwise authentication will fail
if request.Header.Get("Date") == "" {
return nil, fmt.Errorf("Date header must be present and non-empty on request")
}
if request.Method == "POST" || request.Method == "PATCH" || request.Method == "PUT" {
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Content-Length", fmt.Sprintf("%d", request.ContentLength))
}
err = signer.Sign(request)
return request, err
}
// http.RoundTripper middleware that injects an opc-request-id header
type requestIdRoundTripper struct {
transport http.RoundTripper
}
func (t requestIdRoundTripper) RoundTrip(request *http.Request) (response *http.Response, e error) {
requestID := provider.GetRequestID(request.Context())
if requestID != "" {
request.Header.Set("Opc-Request-Id", requestID)
}
response, e = t.transport.RoundTrip(request)
return
}
// http.RoundTripper middleware that adds an opc-compartment-id header to all requests
type compartmentIDRoundTripper struct {
transport http.RoundTripper
compartmentID string
}
func (t compartmentIDRoundTripper) RoundTrip(request *http.Request) (response *http.Response, e error) {
request.Header.Set("opc-compartment-id", t.compartmentID)
response, e = t.transport.RoundTrip(request)
return
}
// Skip verification of insecure certs
func InsecureRoundTripper(roundTripper http.RoundTripper) http.RoundTripper {
transport := roundTripper.(*http.Transport)
if transport != nil {
if transport.TLSClientConfig != nil {
transport.TLSClientConfig.InsecureSkipVerify = true
} else {
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
}
return transport
}
func loadOracleConfig(config provider.ConfigSource, passphrase provider.PassPhraseSource) (string, *rsa.PrivateKey, error) {
var oracleProfile string
var err error
var cf oci.ConfigurationProvider
if oracleProfile = config.GetString(CfgProfile); oracleProfile == "" {
oracleProfile = "DEFAULT"
}
home, err := homedir.Dir()
if err != nil {
return "", nil, fmt.Errorf("error getting home directory %v", err)
}
path := filepath.Join(home, ".oci", "config")
if _, err := os.Stat(path); err == nil {
cf, err = oci.ConfigurationProviderFromFileWithProfile(path, oracleProfile, "")
if err != nil {
return "", nil, err
}
}
var tenancyID string
if tenancyID = config.GetString(CfgTenancyID); tenancyID == "" {
if cf == nil {
return "", nil, errors.New("unable to find tenancyID in configuration: oracle.tenancy-id is missing from current-context file and tenancy-id wasn't found in ~/.oci/config.")
}
tenancyID, err = cf.TenancyOCID()
if err != nil {
return "", nil, err
}
}
var userID string
if userID = config.GetString(CfgUserID); userID == "" {
if cf == nil {
return "", nil, errors.New("unable to find userID in configuration: oracle.tenancy-id is missing from current-context file and tenancy-id wasn't found in ~/.oci/config.")
}
userID, err = cf.UserOCID()
if err != nil {
return "", nil, err
}
}
var fingerprint string
if fingerprint = config.GetString(CfgFingerprint); fingerprint == "" {
if cf == nil {
return "", nil, errors.New("unable to find fingerprint in configuration: oracle.tenancy-id is missing from current-context file and tenancy-id wasn't found in ~/.oci/config.")
}
fingerprint, err = cf.KeyFingerprint()
if err != nil {
return "", nil, err
}
}
keyID := tenancyID + "/" + userID + "/" + fingerprint
var pKey *rsa.PrivateKey
if keyFile := config.GetString(CfgKeyFile); keyFile != "" {
pKey, err = privateKey(config, passphrase, keyFile)
if err != nil {
return "", nil, err
}
return keyID, pKey, nil
}
if cf == nil {
return "", nil, errors.New("unable to find private key in configuration: oracle.tenancy-id is missing from current-context file and tenancy-id wasn't found in ~/.oci/config.")
}
// Read private key for .oci file
pKey, err = cf.PrivateRSAKey()
if err != nil {
return "", nil, err
}
return keyID, pKey, nil
}
func privateKey(config provider.ConfigSource, passphrase provider.PassPhraseSource, pkeyFilePath string) (*rsa.PrivateKey, error) {
keyBytes, err := ioutil.ReadFile(pkeyFilePath)
if err != nil {
return nil, fmt.Errorf("Unable to load private key from file: %s. Error: %s \n", pkeyFilePath, err)
}
var key *rsa.PrivateKey
pKeyPword := config.GetString(CfgPassPhrase)
if !config.IsSet(CfgPassPhrase) {
if key, err = getPrivateKey(keyBytes, pKeyPword, pkeyFilePath); key == nil {
pKeyPword, err = passphrase.ChallengeForPassPhrase("oracle.privateKey", fmt.Sprintf("Enter passphrase for private key %s", pkeyFilePath))
if err != nil {
return nil, err
}
}
}
key, err = getPrivateKey(keyBytes, pKeyPword, pkeyFilePath)
return key, err
}
func getPrivateKey(keyBytes []byte, pKeyPword, pkeyFilePath string) (*rsa.PrivateKey, error) {
key, err := oci.PrivateKeyFromBytes(keyBytes, oci.String(pKeyPword))
if err != nil {
if pKeyPword != "" {
return nil, fmt.Errorf("Unable to load private key from file bytes: %s. Error: %s \n", pkeyFilePath, err)
}
}
return key, nil
}