Skip to content

Commit a97167d

Browse files
authored
Merge pull request kmesh-net#1222 from yp969803/issue#214
feat: dump authorizationPolicy
2 parents 469c64e + 726da17 commit a97167d

File tree

7 files changed

+65
-4
lines changed

7 files changed

+65
-4
lines changed

pkg/auth/policy_store.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,15 @@ func (ps *policyStore) getByNamespace(namespace string) []string {
124124
}
125125
return nil
126126
}
127+
128+
// List returns a copied list of all policies
129+
func (p *policyStore) list() []*security.Authorization {
130+
p.rwLock.RLock()
131+
defer p.rwLock.RUnlock()
132+
out := make([]*security.Authorization, 0, len(p.byKey))
133+
for _, pol := range p.byKey {
134+
out = append(out, pol)
135+
}
136+
137+
return out
138+
}

pkg/auth/rbac.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,8 @@ func (r *Rbac) getIdentityByIp(ip []byte) Identity {
516516
serviceAccount: workload.GetServiceAccount(),
517517
}
518518
}
519+
520+
// List returns a copied list of all policies
521+
func (r *Rbac) PoliciesList() []*security.Authorization {
522+
return r.policyStore.list()
523+
}

pkg/controller/workload/workload_processor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,7 @@ func (p *Processor) handleAuthorizationTypeResponse(rsp *service_discovery_v3.De
909909
if err := rbac.UpdatePolicy(authPolicy); err != nil {
910910
return err
911911
}
912+
912913
policyKey := authPolicy.ResourceName()
913914
if err := maps_v2.AuthorizationUpdate(p.hashName.Hash(policyKey), authPolicy); err != nil {
914915
return fmt.Errorf("AuthorizationUpdate %s failed %v ", policyKey, err)

pkg/status/api.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net"
2121

2222
"kmesh.net/kmesh/api/v2/workloadapi"
23+
"kmesh.net/kmesh/api/v2/workloadapi/security"
2324
)
2425

2526
type Workload struct {
@@ -75,6 +76,14 @@ type Service struct {
7576
Waypoint *Waypoint `json:"waypoint"`
7677
}
7778

79+
type AuthorizationPolicy struct {
80+
Name string `json:"name"`
81+
Namespace string `json:"namespace"`
82+
Scope string `json:"scope"`
83+
Action string `json:"action"`
84+
Rules []*security.Rule `json:"rules"`
85+
}
86+
7887
type NetworkAddress struct {
7988
// Network represents the network this address is on.
8089
Network string
@@ -162,3 +171,15 @@ func ConvertService(s *workloadapi.Service) *Service {
162171

163172
return out
164173
}
174+
175+
func ConvertAuthorizationPolicy(p *security.Authorization) *AuthorizationPolicy {
176+
out := &AuthorizationPolicy{
177+
Name: p.GetName(),
178+
Namespace: p.GetNamespace(),
179+
Scope: p.GetScope().String(),
180+
Action: p.GetAction().String(),
181+
Rules: p.Rules,
182+
}
183+
184+
return out
185+
}

pkg/status/status_server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"google.golang.org/protobuf/encoding/protojson"
3232

3333
adminv2 "kmesh.net/kmesh/api/v2/admin"
34-
"kmesh.net/kmesh/api/v2/workloadapi/security"
3534
"kmesh.net/kmesh/daemon/options"
3635
"kmesh.net/kmesh/pkg/bpf"
3736
bpfads "kmesh.net/kmesh/pkg/bpf/ads"
@@ -461,8 +460,7 @@ func (s *Server) configDumpAds(w http.ResponseWriter, r *http.Request) {
461460
type WorkloadDump struct {
462461
Workloads []*Workload
463462
Services []*Service
464-
// TODO: add authorization
465-
Policies []*security.Authorization
463+
Policies []*AuthorizationPolicy
466464
}
467465

468466
func (s *Server) configDumpWorkload(w http.ResponseWriter, r *http.Request) {
@@ -474,16 +472,21 @@ func (s *Server) configDumpWorkload(w http.ResponseWriter, r *http.Request) {
474472

475473
workloads := client.WorkloadController.Processor.WorkloadCache.List()
476474
services := client.WorkloadController.Processor.ServiceCache.List()
475+
policies := client.WorkloadController.Rbac.PoliciesList()
477476
workloadDump := WorkloadDump{
478477
Workloads: make([]*Workload, 0, len(workloads)),
479478
Services: make([]*Service, 0, len(services)),
479+
Policies: make([]*AuthorizationPolicy, 0, len(policies)),
480480
}
481481
for _, w := range workloads {
482482
workloadDump.Workloads = append(workloadDump.Workloads, ConvertWorkload(w))
483483
}
484484
for _, s := range services {
485485
workloadDump.Services = append(workloadDump.Services, ConvertService(s))
486486
}
487+
for _, p := range policies {
488+
workloadDump.Policies = append(workloadDump.Policies, ConvertAuthorizationPolicy(p))
489+
}
487490
printWorkloadDump(w, workloadDump)
488491
}
489492

pkg/status/status_server_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ import (
3737
"kmesh.net/kmesh/api/v2/core"
3838
"kmesh.net/kmesh/api/v2/listener"
3939
"kmesh.net/kmesh/api/v2/workloadapi"
40+
"kmesh.net/kmesh/api/v2/workloadapi/security"
4041
"kmesh.net/kmesh/daemon/options"
42+
"kmesh.net/kmesh/pkg/auth"
4143
"kmesh.net/kmesh/pkg/bpf"
4244
maps_v2 "kmesh.net/kmesh/pkg/cache/v2/maps"
4345
"kmesh.net/kmesh/pkg/constants"
@@ -198,10 +200,18 @@ func TestServer_configDumpWorkload(t *testing.T) {
198200
},
199201
},
200202
}}
203+
policy := &security.Authorization{
204+
Name: "policy",
205+
Namespace: "ns",
206+
Scope: security.Scope_GLOBAL,
207+
Action: security.Action_ALLOW,
208+
}
201209
fakeWorkloadCache := cache.NewWorkloadCache()
202210
fakeServiceCache := cache.NewServiceCache()
203211
fakeWorkloadCache.AddOrUpdateWorkload(w1)
204212
fakeServiceCache.AddOrUpdateService(svc)
213+
fakeAuth := auth.NewRbac(fakeWorkloadCache)
214+
fakeAuth.UpdatePolicy(policy)
205215
// Create a new instance of the Server struct
206216
server := &Server{
207217
xdsClient: &controller.XdsClient{
@@ -210,6 +220,7 @@ func TestServer_configDumpWorkload(t *testing.T) {
210220
WorkloadCache: fakeWorkloadCache,
211221
ServiceCache: fakeServiceCache,
212222
},
223+
Rbac: fakeAuth,
213224
},
214225
},
215226
}

pkg/status/testdata/workload_configdump.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,13 @@
5151
}
5252
}
5353
],
54-
"Policies": null
54+
"Policies": [
55+
{
56+
"name": "policy",
57+
"namespace": "ns",
58+
"scope": "GLOBAL",
59+
"action": "ALLOW",
60+
"rules": null
61+
}
62+
]
5563
}

0 commit comments

Comments
 (0)