@@ -10,6 +10,7 @@ import (
10
10
"github.com/numberly/vault-db-injector/pkg/config"
11
11
"github.com/numberly/vault-db-injector/pkg/k8s"
12
12
promInjector "github.com/numberly/vault-db-injector/pkg/prometheus"
13
+ "golang.org/x/time/rate"
13
14
)
14
15
15
16
type KeyInformation struct {
@@ -119,24 +120,35 @@ func (c *Connector) ListKeyInformations(ctx context.Context, path, prefix string
119
120
var wg sync.WaitGroup
120
121
keyInformationsChan := make (chan * KeyInformation , len (keys ))
121
122
123
+ // Create a rate limiter
124
+ rateLimit := rate .Limit (c .VaultRateLimit ) // requests per second
125
+ limiter := rate .NewLimiter (rateLimit , 1 )
126
+
122
127
for _ , k := range keys {
123
128
wg .Add (1 )
124
129
go func (k interface {}) {
125
130
defer wg .Done ()
131
+
132
+ // Wait for the rate limiter
133
+ if err := limiter .Wait (ctx ); err != nil {
134
+ c .Log .Errorf ("Rate limiter error: %v" , err )
135
+ return
136
+ }
137
+
126
138
podName := strings .TrimSuffix (k .(string ), "/" )
127
139
128
140
// Utiliser le préfixe pour lire les données
129
141
dataPath := fmt .Sprintf ("%s/data/%s/%s" , path , prefix , podName )
130
142
podSecret , err := c .client .Logical ().ReadWithContext (ctx , dataPath )
131
143
if err != nil {
132
- c .Log .Errorf ("Error while trying to recover data informations for : %s: %v" , podName , err )
144
+ c .Log .Errorf ("Error while trying to recover data informations for: %s: %v" , podName , err )
133
145
return
134
146
}
135
147
136
148
if podSecret == nil || podSecret .Data == nil || podSecret .Data ["data" ] == nil {
137
149
status , err := c .DeleteData (ctx , podName , path , podName , "" , prefix )
138
150
if err != nil {
139
- c .Log .Errorf ("Data for %s can't be deleted : %s with error : %s" , podName , status , err .Error ())
151
+ c .Log .Errorf ("Data for %s can't be deleted: %s with error: %s" , podName , status , err .Error ())
140
152
}
141
153
return
142
154
}
@@ -189,61 +201,73 @@ func (c *Connector) HandleTokens(ctx context.Context, cfg *config.Config, keysIn
189
201
return false
190
202
}
191
203
192
- // Créer une map pour une recherche rapide des podsInformations
204
+ // Create a map for quick lookup of pod information
193
205
podInfoMap := make (map [string ]k8s.PodInformations )
194
206
for _ , pi := range podsInformations {
195
207
for _ , uuid := range pi .PodNameUUIDs {
196
208
podInfoMap [uuid ] = pi
197
209
}
198
-
199
210
}
200
211
201
212
var KubePolicies []string
202
213
KubePolicies = append (KubePolicies , c .authRole )
203
214
_ , err = c .CreateOrphanToken (ctx , "1h" , KubePolicies )
204
215
if err != nil {
205
- c .Log .Errorf ("Can't create orphan ticket : %v" , err )
216
+ c .Log .Errorf ("Can't create orphan ticket: %v" , err )
206
217
c .Log .Error ("Token renew has been cancelled" )
207
218
return false
208
219
}
220
+
221
+ // Create a rate limiter
222
+ rateLimit := rate .Limit (cfg .VaultRateLimit ) // requests per second
223
+ limiter := rate .NewLimiter (rateLimit , 1 )
224
+
209
225
var wg sync.WaitGroup
210
226
var isOk bool = true
227
+
211
228
for _ , ki := range keysInformations {
212
229
wg .Add (1 )
213
230
go func (ki * KeyInformation ) {
214
231
defer wg .Done ()
215
232
233
+ // Wait for the rate limiter
234
+ if err := limiter .Wait (ctx ); err != nil {
235
+ c .Log .Errorf ("Rate limiter error: %v" , err )
236
+ isOk = false
237
+ return
238
+ }
239
+
216
240
if _ , found := podInfoMap [ki .PodNameUID ]; found {
217
241
err := c .RenewToken (ctx , ki .TokenId , ki .PodNameUID , ki .Namespace , SyncTTLSecond )
218
242
if err != nil {
219
- c .Log .Errorf ("Can't renew Token with pod UUID : %s" , ki .PodNameUID )
243
+ c .Log .Errorf ("Can't renew Token with pod UUID: %s" , ki .PodNameUID )
220
244
isOk = false
221
245
return
222
246
}
223
- err = c .RenewLease (ctx , ki .LeaseId , 86400 * 5 , ki .PodNameUID , ki .Namespace ) // Renew for 1week
247
+ err = c .RenewLease (ctx , ki .LeaseId , 86400 * 5 , ki .PodNameUID , ki .Namespace ) // Renew for 1 week
224
248
if err != nil {
225
- c .Log .Errorf ("Can't renew Lease with pod UUID : %s" , ki .PodNameUID )
249
+ c .Log .Errorf ("Can't renew Lease with pod UUID: %s" , ki .PodNameUID )
226
250
isOk = false
227
251
return
228
252
}
229
253
} else {
230
254
leaseTooYoung , err := c .isLeaseTooYoung (ctx , ki .LeaseId )
231
255
if err != nil {
232
- c .Log .Debug ("error while trying to retrieve lease age, lease will be cleaned" )
256
+ c .Log .Debug ("Error while trying to retrieve lease age, lease will be cleaned" )
233
257
}
234
258
if leaseTooYoung {
235
- c .Log .Infof ("This lease : %s is too young to be cleaned up." , ki .LeaseId )
259
+ c .Log .Infof ("This lease: %s is too young to be cleaned up." , ki .LeaseId )
236
260
return
237
261
}
238
262
err = c .RevokeOrphanToken (ctx , ki .TokenId , ki .PodNameUID , ki .Namespace )
239
263
if err != nil {
240
- c .Log .Errorf ("Can't revok Token with UUID : %s" , ki .PodNameUID )
264
+ c .Log .Errorf ("Can't revoke Token with UUID: %s" , ki .PodNameUID )
241
265
isOk = false
242
266
return
243
267
}
244
268
status , err := c .DeleteData (ctx , ki .PodNameUID , secretName , ki .PodNameUID , ki .Namespace , prefix )
245
269
if err != nil {
246
- c .Log .Errorf ("Data for %s can't be deleted : %s with error : %s" , ki .PodNameUID , status , err .Error ())
270
+ c .Log .Errorf ("Data for %s can't be deleted: %s with error: %s" , ki .PodNameUID , status , err .Error ())
247
271
isOk = false
248
272
return
249
273
}
@@ -252,10 +276,11 @@ func (c *Connector) HandleTokens(ctx context.Context, cfg *config.Config, keysIn
252
276
promInjector .RenewLeaseCount .DeleteLabelValues (ki .PodNameUID , ki .Namespace )
253
277
promInjector .RenewTokenCount .DeleteLabelValues (ki .PodNameUID , ki .Namespace )
254
278
promInjector .DataDeletedCount .DeleteLabelValues (ki .PodNameUID , ki .Namespace )
255
- c .Log .Infof ("Token has been revoked and data deleted : %s" , status )
279
+ c .Log .Infof ("Token has been revoked and data deleted: %s" , status )
256
280
}
257
281
}(ki )
258
282
}
283
+
259
284
wg .Wait ()
260
285
c .RevokeSelfToken (ctx , c .client .Token (), "" , "" )
261
286
c .SetToken (c .K8sSaVaultToken )
0 commit comments