-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
authmap_writer.go
111 lines (95 loc) · 2.88 KB
/
authmap_writer.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package auth
import (
"errors"
"fmt"
"github.com/cilium/ebpf"
"github.com/sirupsen/logrus"
"github.com/cilium/cilium/pkg/datapath/linux/utime"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/maps/authmap"
"github.com/cilium/cilium/pkg/policy"
)
type authMapWriter struct {
logger logrus.FieldLogger
authMap authmap.Map
}
func newAuthMapWriter(logger logrus.FieldLogger, authMap authmap.Map) *authMapWriter {
return &authMapWriter{
logger: logger,
authMap: authMap,
}
}
func (r *authMapWriter) All() (map[authKey]authInfo, error) {
result := map[authKey]authInfo{}
if err := r.authMap.IterateWithCallback(func(key *authmap.AuthKey, info *authmap.AuthInfo) {
result[authKey{
localIdentity: identity.NumericIdentity(key.LocalIdentity),
remoteIdentity: identity.NumericIdentity(key.RemoteIdentity),
remoteNodeID: key.RemoteNodeID,
authType: policy.AuthType(key.AuthType),
}] = authInfo{
expiration: info.Expiration.Time(),
}
}); err != nil {
return nil, fmt.Errorf("failed to get all entries from auth map: %w", err)
}
return result, nil
}
func (r *authMapWriter) Get(key authKey) (authInfo, error) {
lookup, err := r.authMap.Lookup(authmap.AuthKey{
LocalIdentity: key.localIdentity.Uint32(),
RemoteIdentity: key.remoteIdentity.Uint32(),
RemoteNodeID: key.remoteNodeID,
AuthType: key.authType.Uint8(),
})
if err != nil {
return authInfo{}, fmt.Errorf("failed to lookup authkey: %w", err)
}
return authInfo{
expiration: lookup.Expiration.Time(),
}, nil
}
func (r *authMapWriter) Update(key authKey, info authInfo) error {
return r.authMap.Update(authmap.AuthKey{
LocalIdentity: key.localIdentity.Uint32(),
RemoteIdentity: key.remoteIdentity.Uint32(),
RemoteNodeID: key.remoteNodeID,
AuthType: key.authType.Uint8(),
}, utime.TimeToUTime(info.expiration))
}
func (r *authMapWriter) Delete(key authKey) error {
if err := r.authMap.Delete(authmap.AuthKey{
LocalIdentity: key.localIdentity.Uint32(),
RemoteIdentity: key.remoteIdentity.Uint32(),
RemoteNodeID: key.remoteNodeID,
AuthType: key.authType.Uint8(),
}); err != nil {
return fmt.Errorf("failed to delete entry from auth map: %w", err)
}
return nil
}
func (r *authMapWriter) DeleteIf(predicate func(key authKey, info authInfo) bool) error {
all, err := r.All()
if err != nil {
return fmt.Errorf("failed to get all entries: %w", err)
}
for k, v := range all {
if predicate(k, v) {
if err := r.Delete(k); err != nil {
if errors.Is(err, ebpf.ErrKeyNotExist) {
r.logger.
WithField("key", k).
Debug("Failed to delete already deleted auth entry")
continue
}
return fmt.Errorf("failed to delete auth entry from map: %w", err)
}
}
}
return nil
}
func (r *authMapWriter) MaxEntries() uint32 {
return r.authMap.MaxEntries()
}