-
Notifications
You must be signed in to change notification settings - Fork 13
/
credential.go
848 lines (714 loc) · 26.1 KB
/
credential.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
package handler
import (
"context"
"encoding/base64"
"fmt"
"reflect"
"strings"
"time"
v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
defs "github.com/Axway/agent-sdk/pkg/apic/definitions"
prov "github.com/Axway/agent-sdk/pkg/apic/provisioning"
"github.com/Axway/agent-sdk/pkg/authz/oauth"
"github.com/Axway/agent-sdk/pkg/config"
"github.com/Axway/agent-sdk/pkg/util"
"github.com/Axway/agent-sdk/pkg/util/log"
"github.com/Axway/agent-sdk/pkg/watchmanager/proto"
)
const (
update = "update"
xAxwayEncrypted = "x-axway-encrypted"
crFinalizer = "agent.credential.provisioned"
)
type credProv interface {
CredentialProvision(credentialRequest prov.CredentialRequest) (status prov.RequestStatus, credentials prov.Credential)
CredentialDeprovision(credentialRequest prov.CredentialRequest) (status prov.RequestStatus)
CredentialUpdate(credentialRequest prov.CredentialRequest) (status prov.RequestStatus, credentials prov.Credential)
}
type credentials struct {
marketplaceHandler
prov credProv
client client
encryptSchema encryptSchemaFunc
idpProviderRegistry oauth.ProviderRegistry
}
// encryptSchemaFunc func signature for encryptSchema
type encryptSchemaFunc func(schema, credData map[string]interface{}, key, alg, hash string) (map[string]interface{}, error)
// NewCredentialHandler creates a Handler for Credentials
func NewCredentialHandler(prov credProv, client client, providerRegistry oauth.ProviderRegistry) Handler {
return &credentials{
prov: prov,
client: client,
encryptSchema: encryptSchema,
idpProviderRegistry: providerRegistry,
}
}
// Handle processes grpc events triggered for Credentials
func (h *credentials) Handle(ctx context.Context, meta *proto.EventMeta, resource *v1.ResourceInstance) error {
action := GetActionFromContext(ctx)
if resource.Kind != management.CredentialGVK().Kind || h.prov == nil || h.shouldIgnoreSubResourceUpdate(action, meta) {
return nil
}
logger := getLoggerFromContext(ctx).WithComponent("credentialHandler")
ctx = setLoggerInContext(ctx, logger)
cr := &management.Credential{}
err := cr.FromInstance(resource)
if err != nil {
logger.WithError(err).Error("could not handle credential request")
return nil
}
if ok := isStatusFound(cr.Status); !ok {
logger.Debugf("could not handle credential request as it did not have a status subresource")
return nil
}
if ok := h.shouldProcessDeleting(cr); ok {
logger.Trace("processing resource in deleting state")
h.onDeleting(ctx, cr)
return nil
}
var credential *management.Credential
if ok := h.shouldProcessPending(cr); ok {
log.Trace("processing resource in pending status")
credential = h.onPending(ctx, cr)
} else if actions := h.shouldProcessUpdating(cr); len(actions) != 0 {
log.Trace("processing resource in updating status")
credential = h.onUpdates(ctx, cr, actions)
}
if credential != nil {
err = h.client.CreateSubResource(cr.ResourceMeta, cr.SubResources)
if err != nil {
logger.WithError(err).Error("error creating subresources")
}
// update the status resource regardless of errors updating the other subresources
statusErr := h.client.CreateSubResource(credential.ResourceMeta, map[string]interface{}{"status": credential.Status})
if statusErr != nil {
logger.WithError(statusErr).Error("error creating status subresources")
return statusErr
}
}
return err
}
// shouldProcessDeleting
// Finalizers = has agent finalizer and
// (Spec.State.Name = Inactive, StateReason = Credential Expired, Status.Level = Pending) or
// (Metadata.State = Deleting)
func (h *credentials) shouldProcessDeleting(cr *management.Credential) bool {
if !hasAgentCredentialFinalizer(cr.Finalizers) {
return false
}
if cr.Spec.State.Name == v1.Inactive && cr.Spec.State.Reason == prov.CredExpDetail && cr.Status.Level == prov.Pending.String() {
// expired credential
return true
}
if cr.Metadata.State == v1.ResourceDeleting {
// don't process delete when error from agent
return !hasAgentCredentialError(cr.Status)
}
return false
}
// shouldProvision
// Status.Level = Pending and
// Metadata.State = !Deleting and
// Spec.State.Name = Active and
// Spec.State.Rotate = false and
// Finalizers = no agent finalizer
func (h *credentials) shouldProcessPending(cr *management.Credential) bool {
if h.marketplaceHandler.shouldProcessPending(cr.Status, cr.Metadata.State) {
return cr.Spec.State.Name == v1.Active && !cr.Spec.State.Rotate && !hasAgentCredentialFinalizer(cr.Finalizers)
}
return false
}
// shouldProcessUpdating
func (h *credentials) shouldProcessUpdating(cr *management.Credential) []prov.CredentialAction {
actions := []prov.CredentialAction{}
inter := reflect.TypeOf((*credProv)(nil)).Elem()
if !reflect.TypeOf(h.prov).Implements(inter) {
log.Debugf("credential updates not supported by agent")
return actions
}
if !hasAgentCredentialFinalizer(cr.Finalizers) || cr.Status.Level != prov.Pending.String() {
return actions
}
// suspend
if cr.Spec.State.Name == v1.Inactive && (cr.State.Name == v1.Active || cr.State.Name == "") {
actions = append(actions, prov.Suspend)
}
// rotate
if cr.Spec.State.Rotate {
actions = append(actions, prov.Rotate)
}
// enable
if cr.Spec.State.Name == v1.Active && cr.State.Name == v1.Inactive {
actions = append(actions, prov.Enable)
}
return actions
}
func (h *credentials) onDeleting(ctx context.Context, cred *management.Credential) {
logger := getLoggerFromContext(ctx)
provData := h.deprovisionPreProcess(ctx, cred)
crd, err := h.getCRD(ctx, cred)
if err != nil {
logger.WithError(err).Error("error getting credential request definition")
h.onError(ctx, cred, err)
return
}
app, err := h.getManagedApp(ctx, cred)
if err != nil {
logger.WithError(err).Error("error getting managed app")
h.onError(ctx, cred, err)
return
}
provCreds, err := h.newProvCreds(cred, util.GetAgentDetails(app), provData, 0, crd)
if err != nil {
logger.WithError(err).Error("error preparing credential request")
h.onError(ctx, cred, err)
return
}
status := h.prov.CredentialDeprovision(provCreds)
h.deprovisionPostProcess(status, provCreds, logger, ctx, cred)
}
func (*credentials) deprovisionPreProcess(ctx context.Context, cred *management.Credential) map[string]interface{} {
var provData map[string]interface{}
if cred.Data != nil {
if m, ok := cred.Data.(map[string]interface{}); ok {
provData = m
}
}
return provData
}
func (h *credentials) deprovisionPostProcess(status prov.RequestStatus, provCreds *provCreds, logger log.FieldLogger, ctx context.Context, cred *management.Credential) {
if status.GetStatus() == prov.Success {
if provCreds.IsIDPCredential() {
err := h.unregisterIDPClientCredential(provCreds)
if err != nil {
logger.WithError(err).Error("error deprovisioning credential request from IDP")
h.onError(ctx, cred, err)
return
}
}
ri, _ := cred.AsInstance()
h.client.UpdateResourceFinalizer(ri, crFinalizer, "", false)
// update sub resources when expire
if cred.Metadata.State != v1.ResourceDeleting {
cred.State.Name = v1.Inactive
cred.Status.Level = prov.Success.String()
cred.Status.Reasons = []v1.ResourceStatusReason{}
h.client.CreateSubResource(cred.ResourceMeta, map[string]interface{}{
"state": cred.State,
})
h.client.CreateSubResource(cred.ResourceMeta, map[string]interface{}{
"status": cred.Status,
})
}
} else {
err := fmt.Errorf(status.GetMessage())
logger.WithError(err).Error("request status was not Success, skipping")
h.onError(ctx, cred, err)
h.client.CreateSubResource(cred.ResourceMeta, cred.SubResources)
}
}
func (h *credentials) onPending(ctx context.Context, cred *management.Credential) *management.Credential {
// check the application status
logger := getLoggerFromContext(ctx)
app, crd, shouldReturn := h.provisionPreProcess(ctx, cred)
if shouldReturn {
return cred
}
provCreds, err := h.newProvCreds(cred, util.GetAgentDetails(app), nil, 0, crd)
if err != nil {
logger.WithError(err).Error("error preparing credential request")
h.onError(ctx, cred, err)
return cred
}
if provCreds.IsIDPCredential() {
err := h.registerIDPClientCredential(provCreds)
if err != nil {
logger.WithError(err).Error("error provisioning credential request with IDP")
h.onError(ctx, cred, err)
return cred
}
}
status, credentialData := h.prov.CredentialProvision(provCreds)
h.provisionPostProcess(status, credentialData, app, crd, provCreds, cred)
return cred
}
func (h *credentials) provisionPreProcess(ctx context.Context, cred *management.Credential) (*management.ManagedApplication, *management.CredentialRequestDefinition, bool) {
logger := getLoggerFromContext(ctx)
app, err := h.getManagedApp(ctx, cred)
if err != nil {
logger.WithError(err).Error("error getting managed app")
h.onError(ctx, cred, err)
return nil, nil, true
}
if app.Status.Level != prov.Success.String() {
err = fmt.Errorf("cannot handle credential when application is not yet successful")
h.onError(ctx, cred, err)
return nil, nil, true
}
crd, err := h.getCRD(ctx, cred)
if err != nil {
logger.WithError(err).Error("error getting credential request definition")
h.onError(ctx, cred, err)
return nil, nil, true
}
return app, crd, false
}
func (h *credentials) provisionPostProcess(status prov.RequestStatus, credentialData prov.Credential, app *management.ManagedApplication, crd *management.CredentialRequestDefinition, provCreds *provCreds, cred *management.Credential) {
var err error
data := map[string]interface{}{}
if status.GetStatus() == prov.Success {
credentialData := h.getProvisionedCredentialData(provCreds, credentialData)
if credentialData != nil {
sec := app.Spec.Security
d := credentialData.GetData()
if crd.Spec.Provision == nil {
data = d
} else if d != nil {
data, err = h.encryptSchema(
crd.Spec.Provision.Schema,
d,
sec.EncryptionKey, sec.EncryptionAlgorithm, sec.EncryptionHash,
)
}
if err != nil {
status = prov.NewRequestStatusBuilder().
SetMessage(fmt.Sprintf("error encrypting credential: %s", err.Error())).
SetCurrentStatusReasons(cred.Status.Reasons).
Failed()
}
}
}
cred.Data = data
cred.Status = prov.NewStatusReason(status)
// use the expiration time sent back with the data
if credentialData != nil && !credentialData.GetExpirationTime().IsZero() {
cred.Policies.Expiry = &management.CredentialPoliciesExpiry{
Timestamp: v1.Time(credentialData.GetExpirationTime()),
}
} else if provCreds.days != 0 {
// update the expiration timestamp
expTS := time.Now().AddDate(0, 0, provCreds.days)
cred.Policies.Expiry = &management.CredentialPoliciesExpiry{
Timestamp: v1.Time(expTS),
}
}
details := util.MergeMapStringString(util.GetAgentDetailStrings(cred), status.GetProperties())
util.SetAgentDetails(cred, util.MapStringStringToMapStringInterface(details))
h.processCredentialLevelSuccess(provCreds, cred)
cred.SubResources = map[string]interface{}{
defs.XAgentDetails: util.GetAgentDetails(cred),
"data": cred.Data,
"policies": cred.Policies,
"state": cred.State,
}
}
func (h *credentials) processCredentialLevelSuccess(provCreds *provCreds, cred *management.Credential) {
if cred.Status.Level == prov.Success.String() {
if !hasAgentCredentialFinalizer(cred.Finalizers) {
ri, _ := cred.AsInstance()
// only add finalizer on success
h.client.UpdateResourceFinalizer(ri, crFinalizer, "", true)
}
if provCreds.GetCredentialAction() != prov.Rotate {
// if this is not a rotate action update the state to the desired state
cred.State.Name = cred.Spec.State.Name
} else {
// if the action was rotate lets remove the rotate flag from spec
cred.Spec.State.Rotate = false
h.client.UpdateResourceInstance(cred)
}
} else if cred.State.Name == "" {
cred.State.Name = v1.Inactive
}
}
func (h *credentials) onUpdates(ctx context.Context, cred *management.Credential, actions []prov.CredentialAction) *management.Credential {
logger := getLoggerFromContext(ctx)
app, crd, shouldReturn := h.provisionPreProcess(ctx, cred)
provData := h.deprovisionPreProcess(ctx, cred)
if shouldReturn {
return cred
}
for _, action := range actions {
provCreds, err := h.newProvCreds(cred, util.GetAgentDetails(app), provData, action, crd)
if err != nil {
logger.WithError(err).Error("error preparing credential request")
h.onError(ctx, cred, err)
return cred
}
if action != prov.Suspend && provCreds.IsIDPCredential() {
err := h.registerIDPClientCredential(provCreds)
if err != nil {
logger.WithError(err).Error("error provisioning credential request with IDP")
h.onError(ctx, cred, err)
return cred
}
}
status, credentialData := h.prov.CredentialUpdate(provCreds)
h.provisionPostProcess(status, credentialData, app, crd, provCreds, cred)
}
return cred
}
// onError updates the AccessRequest with an error status
func (h *credentials) onError(_ context.Context, cred *management.Credential, err error) {
ps := prov.NewRequestStatusBuilder()
status := ps.SetMessage(fmt.Sprintf("Agent: %s", err.Error())).SetCurrentStatusReasons(cred.Status.Reasons).Failed()
cred.Status = prov.NewStatusReason(status)
cred.SubResources = map[string]interface{}{
"status": cred.Status,
}
}
func (h *credentials) getManagedApp(ctx context.Context, cred *management.Credential) (*management.ManagedApplication, error) {
app := management.NewManagedApplication(cred.Spec.ManagedApplication, cred.Metadata.Scope.Name)
ri, err := h.client.GetResource(app.GetSelfLink())
if err != nil {
return nil, err
}
app = &management.ManagedApplication{}
err = app.FromInstance(ri)
return app, err
}
func (h *credentials) getCRD(ctx context.Context, cred *management.Credential) (*management.CredentialRequestDefinition, error) {
crd := management.NewCredentialRequestDefinition(cred.Spec.CredentialRequestDefinition, cred.Metadata.Scope.Name)
ri, err := h.client.GetResource(crd.GetSelfLink())
if err != nil {
return nil, err
}
crd = &management.CredentialRequestDefinition{}
err = crd.FromInstance(ri)
return crd, err
}
func formattedJWKS(jwks string) string {
formattedJWKS := strings.ReplaceAll(jwks, "----- ", "-----\n")
return strings.ReplaceAll(formattedJWKS, " -----", "\n-----")
}
func (h *credentials) registerIDPClientCredential(cr *provCreds) error {
p := cr.GetIDPProvider()
idpCredData := cr.GetIDPCredentialData()
// prepare external client metadata from CRD data
builder := oauth.NewClientMetadataBuilder().
SetClientName(cr.GetName()).
SetScopes(idpCredData.GetScopes()).
SetGrantTypes(idpCredData.GetGrantTypes()).
SetTokenEndpointAuthMethod(idpCredData.GetTokenEndpointAuthMethod()).
SetResponseType(idpCredData.GetResponseTypes()).
SetRedirectURIs(idpCredData.GetRedirectURIs())
if idpCredData.GetTokenEndpointAuthMethod() == config.PrivateKeyJWT {
builder.SetJWKS([]byte(formattedJWKS(idpCredData.GetPublicKey()))).
SetJWKSURI(idpCredData.GetJwksURI())
}
if idpCredData.GetTokenEndpointAuthMethod() == config.TLSClientAuth || idpCredData.GetTokenEndpointAuthMethod() == config.SelfSignedTLSClientAuth {
builder.SetJWKS([]byte(formattedJWKS(idpCredData.GetCertificate()))).
SetCertificateMetadata(idpCredData.GetCertificateMetadata()).
SetTLSClientAuthSanDNS(idpCredData.GetTLSClientAuthSanDNS()).
SetTLSClientAuthSanEmail(idpCredData.GetTLSClientAuthSanEmail()).
SetTLSClientAuthSanIP(idpCredData.GetTLSClientAuthSanIP()).
SetTLSClientAuthSanURI(idpCredData.GetTLSClientAuthSanURI())
}
clientMetadata, err := builder.Build()
if err != nil {
return err
}
// provision external client
resClientMetadata, err := p.RegisterClient(clientMetadata)
if err != nil {
return err
}
cr.idpCredData.clientID = resClientMetadata.GetClientID()
cr.idpCredData.clientSecret = resClientMetadata.GetClientSecret()
return nil
}
func (h *credentials) unregisterIDPClientCredential(cr *provCreds) error {
p := cr.GetIDPProvider()
err := p.UnregisterClient(cr.idpCredData.GetClientID())
if err != nil {
return err
}
cr.idpCredData.clientID = cr.idpCredData.GetClientID()
return nil
}
func (h *credentials) getProvisionedCredentialData(provCreds *provCreds, credentialData prov.Credential) prov.Credential {
if provCreds.IsIDPCredential() {
return prov.NewCredentialBuilder().SetOAuthIDAndSecret(
provCreds.GetIDPCredentialData().GetClientID(),
provCreds.GetIDPCredentialData().GetClientSecret(),
)
}
return credentialData
}
func hasAgentCredentialError(status *v1.ResourceStatus) bool {
for _, r := range status.Reasons {
if strings.HasPrefix(r.Detail, "Agent:") {
return true
}
}
return false
}
func hasAgentCredentialFinalizer(finalizers []v1.Finalizer) bool {
for _, f := range finalizers {
if f.Name == crFinalizer {
return true
}
}
return false
}
type provCreds struct {
managedApp string
credType string
id string
name string
days int
credAction prov.CredentialAction
credData map[string]interface{}
credDetails map[string]interface{}
appDetails map[string]interface{}
idpProvider oauth.Provider
idpCredData *idpCredData
credSchema map[string]interface{}
credProvSchema map[string]interface{}
credSchemaDetails map[string]interface{}
}
type idpCredData struct {
clientID string
clientSecret string
scopes []string
grantTypes []string
tokenAuthMethod string
responseTypes []string
redirectURLs []string
jwksURI string
publicKey string
certificate string
certificateMetadata string
tlsClientAuthSanDNS string
tlsClientAuthSanEmail string
tlsClientAuthSanIP string
tlsClientAuthSanURI string
}
func (h *credentials) newProvCreds(cr *management.Credential, appDetails map[string]interface{}, provData map[string]interface{}, action prov.CredentialAction, crd *management.CredentialRequestDefinition) (*provCreds, error) {
credDetails := util.GetAgentDetails(cr)
provCred := &provCreds{
appDetails: appDetails,
credDetails: credDetails,
credType: cr.Spec.CredentialRequestDefinition,
credData: cr.Spec.Data,
managedApp: cr.Spec.ManagedApplication,
id: cr.Metadata.ID,
name: cr.Name,
credAction: action,
days: 0,
}
if crd != nil {
if crd.Spec.Provision != nil &&
crd.Spec.Provision.Policies.Expiry != nil {
provCred.days = int(crd.Spec.Provision.Policies.Expiry.Period)
}
credSchemaDetails := util.GetAgentDetails(crd)
provCred.credSchema = crd.Spec.Schema
if crd.Spec.Provision != nil {
provCred.credProvSchema = crd.Spec.Provision.Schema
}
provCred.credSchemaDetails = credSchemaDetails
}
// Setup external credential request data to be used for provisioning
if idpTokenURL, ok := provCred.credData[prov.IDPTokenURL].(string); ok && h.idpProviderRegistry != nil {
p, err := h.idpProviderRegistry.GetProviderByTokenEndpoint(idpTokenURL)
if err != nil {
return nil, fmt.Errorf("IDP provider not found for credential request")
}
provCred.idpProvider = p
provCred.idpCredData = newIDPCredData(p, provCred.credData, provData)
}
return provCred, nil
}
// newIDPCredData - reads the idp client metadata from credential request
func newIDPCredData(p oauth.Provider, credData, provData map[string]interface{}) *idpCredData {
cd := &idpCredData{}
if provData != nil {
cd.clientID = util.GetStringFromMapInterface(prov.OauthClientID, provData)
}
cd.scopes = util.GetStringArrayFromMapInterface(prov.OauthScopes, credData)
cd.grantTypes = []string{util.GetStringFromMapInterface(prov.OauthGrantType, credData)}
cd.redirectURLs = util.GetStringArrayFromMapInterface(prov.OauthRedirectURIs, credData)
cd.tokenAuthMethod = util.GetStringFromMapInterface(prov.OauthTokenAuthMethod, credData)
cd.publicKey = util.GetStringFromMapInterface(prov.OauthJwks, credData)
cd.jwksURI = util.GetStringFromMapInterface(prov.OauthJwksURI, credData)
cd.certificate = util.GetStringFromMapInterface(prov.OauthCertificate, credData)
cd.certificateMetadata = util.GetStringFromMapInterface(prov.OauthCertificateMetadata, credData)
cd.tlsClientAuthSanDNS = util.GetStringFromMapInterface(prov.OauthTLSAuthSANDNS, credData)
cd.tlsClientAuthSanEmail = util.GetStringFromMapInterface(prov.OauthTLSAuthSANEmail, credData)
cd.tlsClientAuthSanIP = util.GetStringFromMapInterface(prov.OauthTLSAuthSANIP, credData)
cd.tlsClientAuthSanURI = util.GetStringFromMapInterface(prov.OauthTLSAuthSANURI, credData)
return cd
}
// GetApplicationName gets the name of the managed application
func (c provCreds) GetApplicationName() string {
return c.managedApp
}
// GetID gets the id of the credential resource
func (c provCreds) GetID() string {
return c.id
}
// GetName gets the name of the credential resource
func (c provCreds) GetName() string {
return c.name
}
// GetCredentialType gets the type of the credential
func (c provCreds) GetCredentialType() string {
return c.credType
}
// GetCredentialData gets the data of the credential
func (c provCreds) GetCredentialData() map[string]interface{} {
return c.credData
}
// GetCredentialAction gets the data of the credential
func (c provCreds) GetCredentialAction() prov.CredentialAction {
return c.credAction
}
// GetID gets the id of the credential resource
func (c provCreds) GetCredentialExpirationDays() int {
return c.days
}
// GetCredentialSchema returns the schema for the credential request.
func (c provCreds) GetCredentialSchema() map[string]interface{} {
return c.credSchema
}
// GetCredentialProvisionSchema returns the provisioning schema for the credential request.
func (c provCreds) GetCredentialProvisionSchema() map[string]interface{} {
return c.credProvSchema
}
// GetCredentialSchemaDetailsValue returns a value found on the 'x-agent-details' sub resource of the crd.
func (c provCreds) GetCredentialSchemaDetailsValue(key string) interface{} {
if c.credSchemaDetails == nil {
return nil
}
return c.credSchemaDetails[key]
}
// IsIDPCredential returns boolean indicating if the credential request is for IDP provider
func (c provCreds) IsIDPCredential() bool {
return c.idpProvider != nil
}
// GetIDPProvider returns the interface for IDP provider if the credential request is for IDP provider
func (c provCreds) GetIDPProvider() oauth.Provider {
return c.idpProvider
}
// GetIDPCredentialData returns the credential data for IDP from the request
func (c provCreds) GetIDPCredentialData() prov.IDPCredentialData {
return c.idpCredData
}
// GetCredentialDetailsValue returns a value found on the 'x-agent-details' sub resource of the Credentials.
func (c provCreds) GetCredentialDetailsValue(key string) string {
if c.credDetails == nil {
return ""
}
return util.ToString(c.credDetails[key])
}
// GetApplicationDetailsValue returns a value found on the 'x-agent-details' sub resource of the ManagedApplication.
func (c provCreds) GetApplicationDetailsValue(key string) string {
if c.appDetails == nil {
return ""
}
return util.ToString(c.appDetails[key])
}
// GetClientID - returns client ID
func (c *idpCredData) GetClientID() string {
return c.clientID
}
// GetClientSecret - returns client secret
func (c *idpCredData) GetClientSecret() string {
return c.clientSecret
}
// GetScopes - returns client scopes
func (c *idpCredData) GetScopes() []string {
return c.scopes
}
// GetGrantTypes - returns grant types
func (c *idpCredData) GetGrantTypes() []string {
return c.grantTypes
}
// GetTokenEndpointAuthMethod - returns token auth method
func (c *idpCredData) GetTokenEndpointAuthMethod() string {
return c.tokenAuthMethod
}
// GetResponseTypes - returns token response type
func (c *idpCredData) GetResponseTypes() []string {
return c.responseTypes
}
// GetRedirectURIs - Returns redirect urls
func (c *idpCredData) GetRedirectURIs() []string {
return c.redirectURLs
}
// GetJwksURI - returns JWKS uri
func (c *idpCredData) GetJwksURI() string {
return c.jwksURI
}
// GetPublicKey - returns the public key
func (c *idpCredData) GetPublicKey() string {
return c.publicKey
}
// GetCertificate - returns the certificate
func (c *idpCredData) GetCertificate() string {
return c.certificate
}
// GetCertificateMetadata - returns the certificate metadata property
func (c *idpCredData) GetCertificateMetadata() string {
return c.certificateMetadata
}
// GetTLSClientAuthSanDNS - returns the value for tls_client_auth_san_dns
func (c *idpCredData) GetTLSClientAuthSanDNS() string {
return c.tlsClientAuthSanDNS
}
// GetTLSClientAuthSanDNS - returns the value for tls_client_auth_san_dns
func (c *idpCredData) GetTLSClientAuthSanEmail() string {
return c.tlsClientAuthSanEmail
}
// GetTLSClientAuthSanIP - returns the value for tls_client_auth_san_ip
func (c *idpCredData) GetTLSClientAuthSanIP() string {
return c.tlsClientAuthSanIP
}
// GetTLSClientAuthSanURI - returns the value for tls_client_auth_san_uri
func (c *idpCredData) GetTLSClientAuthSanURI() string {
return c.tlsClientAuthSanURI
}
// encryptSchema schema is the json schema. credData is the data that contains data to encrypt based on the key, alg and hash.
func encryptSchema(
schema, credData map[string]interface{}, key, alg, hash string,
) (map[string]interface{}, error) {
data := make(map[string]interface{})
enc, err := util.NewEncryptor(key, alg, hash)
if err != nil {
return data, err
}
schemaProps, ok := schema["properties"]
if !ok {
return data, fmt.Errorf("properties field not found on schema")
}
props, ok := schemaProps.(map[string]interface{})
if !ok {
props = make(map[string]interface{})
}
return encryptMap(enc, props, credData), nil
}
// encryptMap loops through all data and checks the value against the provisioning schema to see if it should be encrypted.
func encryptMap(enc util.Encryptor, schema, data map[string]interface{}) map[string]interface{} {
for key, value := range data {
schemaValue := schema[key]
v, ok := schemaValue.(map[string]interface{})
if !ok {
continue
}
if _, ok := v[xAxwayEncrypted]; ok {
v, ok := value.(string)
if !ok {
continue
}
str, err := enc.Encrypt(v)
if err != nil {
log.Error(err)
continue
}
data[key] = base64.StdEncoding.EncodeToString([]byte(str))
}
}
return data
}