/
aisreq.go
164 lines (155 loc) · 3.85 KB
/
aisreq.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
// Package authn is authentication server for AIStore.
/*
* Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
*/
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"sync"
"time"
"github.com/NVIDIA/aistore/api/apc"
"github.com/NVIDIA/aistore/api/authn"
"github.com/NVIDIA/aistore/cmn"
"github.com/NVIDIA/aistore/cmn/cos"
"github.com/NVIDIA/aistore/cmn/nlog"
jsoniter "github.com/json-iterator/go"
)
const (
retryCount = 4
retrySleep = 3 * time.Second
retry503 = time.Minute
)
func (m *mgr) validateSecret(clu *authn.CluACL) (err error) {
const tag = "validate-secret"
var (
secret = Conf.Secret()
cksum = cos.NewCksumHash(cos.ChecksumSHA256)
)
cksum.H.Write([]byte(secret))
cksum.Finalize()
body := cos.MustMarshal(&authn.ServerConf{Secret: cksum.Val()})
for _, u := range clu.URLs {
if err = m.call(http.MethodPost, u, apc.Tokens, body, tag); err == nil {
return
}
err = fmt.Errorf("failed to %s with %s: %v", tag, clu, err)
}
return
}
// update list of revoked token on all clusters
func (m *mgr) broadcastRevoked(token string) {
tokenList := authn.TokenList{Tokens: []string{token}}
body := cos.MustMarshal(tokenList)
m.broadcast(http.MethodDelete, apc.Tokens, body, "broadcast-revoked")
}
// broadcast the request to all clusters. If a cluster has a few URLS,
// it sends to the first working one. Clusters are processed in parallel.
func (m *mgr) broadcast(method, path string, body []byte, tag string) {
clus, err := m.clus()
if err != nil {
nlog.Errorf("Failed to read cluster list: %v", err)
return
}
wg := &sync.WaitGroup{}
for _, clu := range clus {
wg.Add(1)
go func(clu *authn.CluACL) {
var err error
for _, u := range clu.URLs {
if err = m.call(method, u, path, body, tag); err == nil {
break
}
}
if err != nil {
nlog.Errorf("failed to %s with %s: %v", tag, clu, err)
}
wg.Done()
}(clu)
}
wg.Wait()
}
// Send valid and non-expired revoked token list to a cluster.
func (m *mgr) syncTokenList(clu *authn.CluACL) {
const tag = "sync-tokens"
tokenList, err := m.generateRevokedTokenList()
if err != nil {
nlog.Errorf("failed to sync token list with %q(%q): %v", clu.ID, clu.Alias, err)
return
}
if len(tokenList) == 0 {
return
}
body := cos.MustMarshal(authn.TokenList{Tokens: tokenList})
for _, u := range clu.URLs {
if err = m.call(http.MethodDelete, u, apc.Tokens, body, tag); err == nil {
break
}
err = fmt.Errorf("failed to %s with %s: %v", tag, clu, err)
}
if err != nil {
nlog.Errorln(err)
}
}
// TODO: reuse api/client.go reqParams.do()
func (m *mgr) call(method, proxyURL, path string, injson []byte, tag string) error {
var (
rerr error
msg []byte
retries = retryCount
sleep = retrySleep
url = proxyURL + cos.JoinWords(apc.Version, path)
client = m.clientH
)
if cos.IsHTTPS(proxyURL) {
client = m.clientTLS
}
// while cos.IsRetriableConnErr()
for i := 1; i <= retries; i++ {
req, nerr := http.NewRequest(method, url, bytes.NewBuffer(injson))
if nerr != nil {
return nerr
}
req.Header.Set(cos.HdrContentType, cos.ContentJSON)
resp, err := client.Do(req)
if resp != nil {
if resp.Body != nil {
msg, _ = io.ReadAll(resp.Body)
resp.Body.Close()
}
}
if err == nil {
if resp.StatusCode < http.StatusBadRequest {
return nil
}
} else {
if cos.IsRetriableConnErr(err) {
continue
}
if resp == nil {
return err
}
}
if resp.StatusCode == http.StatusServiceUnavailable {
if retries == retryCount {
retries = int(retry503 / retrySleep)
}
} else {
var herr *cmn.ErrHTTP
if jsonErr := jsoniter.Unmarshal(msg, &herr); jsonErr == nil {
return herr
}
}
if i < retries {
nlog.Warningf("failed to %q %s: %v - retrying...", tag, url, err)
time.Sleep(sleep)
if i > retries/2+1 && sleep == retrySleep {
sleep *= 2
}
}
rerr = err
}
return rerr
}