@@ -16,6 +16,7 @@ import {
1616 TrafficDirection as PBTrafficDirection ,
1717 CiliumEventType as PBCiliumEventType ,
1818 Service as PBService ,
19+ Policy as Policy ,
1920} from '~backend/proto/flow/flow_pb' ;
2021
2122import {
@@ -47,6 +48,8 @@ import * as misc from '~/domain/misc';
4748import * as verdictHelpers from './verdict' ;
4849import { authTypeFromPb } from './auth-type' ;
4950
51+ const derivedFrom = 'reserved:io.cilium.policy.derived-from' ;
52+
5053export const hubbleFlowFromObj = ( obj : any ) : HubbleFlow | null => {
5154 obj = obj . flow != null ? obj . flow : obj ;
5255
@@ -84,6 +87,9 @@ export const hubbleFlowFromObj = (obj: any): HubbleFlow | null => {
8487 const trafficDirection = trafficDirectionFromStr ( obj . trafficDirection ) ;
8588 const authType = authTypeFromStr ( obj . authType ) ;
8689
90+ const egressAllowedBy = getNameFromPolicy ( obj . getEgressAllowedByList ) ;
91+ const ingressAllowedBy = getNameFromPolicy ( obj . getIngressAllowedByList ) ;
92+
8793 return {
8894 time,
8995 verdict,
@@ -105,6 +111,8 @@ export const hubbleFlowFromObj = (obj: any): HubbleFlow | null => {
105111 summary : obj . summary ,
106112 trafficDirection,
107113 authType,
114+ egressAllowedBy,
115+ ingressAllowedBy,
108116 } ;
109117} ;
110118
@@ -148,6 +156,9 @@ export const hubbleFlowFromPb = (flow: PBFlow): HubbleFlow => {
148156 const trafficDirection = trafficDirectionFromPb ( flow . getTrafficDirection ( ) ) ;
149157 const authType = authTypeFromPb ( flow . getAuthType ( ) ) ;
150158
159+ const egressAllowedBy = getNameFromPolicy ( flow . getEgressAllowedByList ( ) ) ;
160+ const ingressAllowedBy = getNameFromPolicy ( flow . getIngressAllowedByList ( ) ) ;
161+
151162 return {
152163 time,
153164 verdict,
@@ -169,6 +180,8 @@ export const hubbleFlowFromPb = (flow: PBFlow): HubbleFlow => {
169180 summary : flow . getSummary ( ) ,
170181 trafficDirection,
171182 authType,
183+ egressAllowedBy : egressAllowedBy ,
184+ ingressAllowedBy : ingressAllowedBy ,
172185 } ;
173186} ;
174187
@@ -512,3 +525,25 @@ export const icmpv4FromPb = (icmp: PBICMPv4): ICMPv4 => {
512525export const icmpv6FromPb = ( icmp : PBICMPv6 ) : ICMPv6 => {
513526 return icmpv4FromPb ( icmp ) ;
514527} ;
528+
529+ export const getNameFromPolicy = ( policies : Array < Policy > ) : Array < string > => {
530+ return policies . map ( policy => {
531+ if ( policy . getName ( ) === '' ) {
532+ const labelMap = new Map < string , string > ( ) ;
533+ policy . getLabelsList ( ) . forEach ( keyValueString => {
534+ const [ key , value ] = keyValueString . split ( '=' ) ;
535+ labelMap . set ( key , value ) ;
536+ } ) ;
537+
538+ // Note: We try to automatically derive the policy name if it is set.
539+ // This is set by the policy mapstate for certain known scenarios, like allowing localhost access.
540+ // See: https://github.com/cilium/cilium/blob/614f2ddcc8fe93aeaf463b4535dcc0f1dcc373a3/pkg/policy/mapstate.go#L42-L49
541+ if ( labelMap . has ( derivedFrom ) ) {
542+ return '<cilium-internal>/' + labelMap . get ( derivedFrom ) ;
543+ }
544+ return '<cilium-internal>/unknown' ;
545+ }
546+
547+ return policy . getNamespace ( ) + '/' + policy . getName ( ) ;
548+ } ) ;
549+ } ;
0 commit comments