/
clientmap.go
86 lines (78 loc) · 2.36 KB
/
clientmap.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
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2022 Wrangle Ltd
package utils
import (
"fmt"
"net/url"
"github.com/go-logr/logr"
"github.com/spf13/cobra"
apiclient "github.com/wrgl/wrgl/pkg/api/client"
"github.com/wrgl/wrgl/pkg/credentials"
"github.com/wrgl/wrgl/pkg/ref"
)
type ClientMap struct {
CredsStore *credentials.Store
clients map[string]*apiclient.Client
refs map[string]map[string][]byte
logger logr.Logger
}
func NewClientMap(cs *credentials.Store, logger logr.Logger) *ClientMap {
return &ClientMap{
CredsStore: cs,
logger: logger.WithName("ClientMap").V(1),
clients: map[string]*apiclient.Client{},
refs: map[string]map[string][]byte{},
}
}
func (m *ClientMap) GetClient(cmd *cobra.Command, remoteURI string, opts ...apiclient.ClientOption) (client *apiclient.Client, err error) {
if v, ok := m.clients[remoteURI]; ok {
m.logger.Info("found client", "remoteURI", remoteURI)
return v, nil
} else {
m.logger.Info("client not found", "remoteURI", remoteURI)
}
u, err := url.Parse(remoteURI)
if err != nil {
return
}
opts = append(opts, apiclient.WithUMATicketHandler(func(asURI, ticket, oldRPT string, logger logr.Logger) (rpt string, err error) {
return handleUMATicket(cmd, m.CredsStore, *u, asURI, ticket, oldRPT, logger)
}))
rpt := m.CredsStore.GetRPT(*u)
if rpt != "" {
m.logger.Info("rpt found", "remoteURI", remoteURI)
opts = append(opts, apiclient.WithRelyingPartyToken(rpt))
} else {
m.logger.Info("rpt not found", "remoteURI", remoteURI)
}
client, err = apiclient.NewClient(remoteURI, m.logger, opts...)
if err != nil {
return nil, fmt.Errorf("error creating new client: %w", err)
}
m.logger.Info("memoizing client", "remoteURI", remoteURI)
m.clients[remoteURI] = client
return
}
func (m *ClientMap) GetRefs(cmd *cobra.Command, remoteURI string, opts ...apiclient.ClientOption) (refs map[string][]byte, err error) {
if v, ok := m.refs[remoteURI]; ok {
return v, nil
}
client, err := m.GetClient(cmd, remoteURI, opts...)
if err != nil {
return
}
refs, err = client.GetRefs(nil, []string{ref.TransactionRefPrefix})
if err != nil {
return nil, HandleHTTPError(cmd, m.CredsStore, remoteURI, err)
}
m.refs[remoteURI] = refs
return
}
func (m *ClientMap) ResetCookies() error {
for _, c := range m.clients {
if err := c.ResetCookies(); err != nil {
return err
}
}
return nil
}