Skip to content

Commit b8d210a

Browse files
authored
Add Events for Policy Service APIs (#2479)
1 parent b174b9d commit b8d210a

File tree

3 files changed

+255
-22
lines changed

3 files changed

+255
-22
lines changed

runtime/service/src/main/java/org/apache/polaris/service/catalog/policy/CatalogPolicyEventServiceDelegator.java

Lines changed: 102 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@
2626
import jakarta.ws.rs.core.Response;
2727
import jakarta.ws.rs.core.SecurityContext;
2828
import org.apache.polaris.core.context.RealmContext;
29+
import org.apache.polaris.service.catalog.CatalogPrefixParser;
2930
import org.apache.polaris.service.catalog.api.PolarisCatalogPolicyApiService;
3031
import org.apache.polaris.service.catalog.common.CatalogAdapter;
32+
import org.apache.polaris.service.events.CatalogPolicyServiceEvents;
33+
import org.apache.polaris.service.events.listeners.PolarisEventListener;
3134
import org.apache.polaris.service.types.AttachPolicyRequest;
3235
import org.apache.polaris.service.types.CreatePolicyRequest;
3336
import org.apache.polaris.service.types.DetachPolicyRequest;
37+
import org.apache.polaris.service.types.GetApplicablePoliciesResponse;
38+
import org.apache.polaris.service.types.LoadPolicyResponse;
3439
import org.apache.polaris.service.types.UpdatePolicyRequest;
3540

3641
@Decorator
@@ -39,6 +44,8 @@ public class CatalogPolicyEventServiceDelegator
3944
implements PolarisCatalogPolicyApiService, CatalogAdapter {
4045

4146
@Inject @Delegate PolicyCatalogAdapter delegate;
47+
@Inject PolarisEventListener polarisEventListener;
48+
@Inject CatalogPrefixParser prefixParser;
4249

4350
@Override
4451
public Response createPolicy(
@@ -47,8 +54,17 @@ public Response createPolicy(
4754
CreatePolicyRequest createPolicyRequest,
4855
RealmContext realmContext,
4956
SecurityContext securityContext) {
50-
return delegate.createPolicy(
51-
prefix, namespace, createPolicyRequest, realmContext, securityContext);
57+
String catalogName = prefixParser.prefixToCatalogName(realmContext, prefix);
58+
polarisEventListener.onBeforeCreatePolicy(
59+
new CatalogPolicyServiceEvents.BeforeCreatePolicyEvent(
60+
catalogName, namespace, createPolicyRequest));
61+
Response resp =
62+
delegate.createPolicy(
63+
prefix, namespace, createPolicyRequest, realmContext, securityContext);
64+
polarisEventListener.onAfterCreatePolicy(
65+
new CatalogPolicyServiceEvents.AfterCreatePolicyEvent(
66+
catalogName, namespace, (LoadPolicyResponse) resp.getEntity()));
67+
return resp;
5268
}
5369

5470
@Override
@@ -60,8 +76,15 @@ public Response listPolicies(
6076
String policyType,
6177
RealmContext realmContext,
6278
SecurityContext securityContext) {
63-
return delegate.listPolicies(
64-
prefix, namespace, pageToken, pageSize, policyType, realmContext, securityContext);
79+
String catalogName = prefixParser.prefixToCatalogName(realmContext, prefix);
80+
polarisEventListener.onBeforeListPolicies(
81+
new CatalogPolicyServiceEvents.BeforeListPoliciesEvent(catalogName, namespace, policyType));
82+
Response resp =
83+
delegate.listPolicies(
84+
prefix, namespace, pageToken, pageSize, policyType, realmContext, securityContext);
85+
polarisEventListener.onAfterListPolicies(
86+
new CatalogPolicyServiceEvents.AfterListPoliciesEvent(catalogName, namespace, policyType));
87+
return resp;
6588
}
6689

6790
@Override
@@ -71,7 +94,15 @@ public Response loadPolicy(
7194
String policyName,
7295
RealmContext realmContext,
7396
SecurityContext securityContext) {
74-
return delegate.loadPolicy(prefix, namespace, policyName, realmContext, securityContext);
97+
String catalogName = prefixParser.prefixToCatalogName(realmContext, prefix);
98+
polarisEventListener.onBeforeLoadPolicy(
99+
new CatalogPolicyServiceEvents.BeforeLoadPolicyEvent(catalogName, namespace, policyName));
100+
Response resp =
101+
delegate.loadPolicy(prefix, namespace, policyName, realmContext, securityContext);
102+
polarisEventListener.onAfterLoadPolicy(
103+
new CatalogPolicyServiceEvents.AfterLoadPolicyEvent(
104+
catalogName, namespace, (LoadPolicyResponse) resp.getEntity()));
105+
return resp;
75106
}
76107

77108
@Override
@@ -82,8 +113,17 @@ public Response updatePolicy(
82113
UpdatePolicyRequest updatePolicyRequest,
83114
RealmContext realmContext,
84115
SecurityContext securityContext) {
85-
return delegate.updatePolicy(
86-
prefix, namespace, policyName, updatePolicyRequest, realmContext, securityContext);
116+
String catalogName = prefixParser.prefixToCatalogName(realmContext, prefix);
117+
polarisEventListener.onBeforeUpdatePolicy(
118+
new CatalogPolicyServiceEvents.BeforeUpdatePolicyEvent(
119+
catalogName, namespace, policyName, updatePolicyRequest));
120+
Response resp =
121+
delegate.updatePolicy(
122+
prefix, namespace, policyName, updatePolicyRequest, realmContext, securityContext);
123+
polarisEventListener.onAfterUpdatePolicy(
124+
new CatalogPolicyServiceEvents.AfterUpdatePolicyEvent(
125+
catalogName, namespace, (LoadPolicyResponse) resp.getEntity()));
126+
return resp;
87127
}
88128

89129
@Override
@@ -94,8 +134,17 @@ public Response dropPolicy(
94134
Boolean detachAll,
95135
RealmContext realmContext,
96136
SecurityContext securityContext) {
97-
return delegate.dropPolicy(
98-
prefix, namespace, policyName, detachAll, realmContext, securityContext);
137+
String catalogName = prefixParser.prefixToCatalogName(realmContext, prefix);
138+
polarisEventListener.onBeforeDropPolicy(
139+
new CatalogPolicyServiceEvents.BeforeDropPolicyEvent(
140+
catalogName, namespace, policyName, detachAll));
141+
Response resp =
142+
delegate.dropPolicy(
143+
prefix, namespace, policyName, detachAll, realmContext, securityContext);
144+
polarisEventListener.onAfterDropPolicy(
145+
new CatalogPolicyServiceEvents.AfterDropPolicyEvent(
146+
catalogName, namespace, policyName, detachAll));
147+
return resp;
99148
}
100149

101150
@Override
@@ -106,8 +155,17 @@ public Response attachPolicy(
106155
AttachPolicyRequest attachPolicyRequest,
107156
RealmContext realmContext,
108157
SecurityContext securityContext) {
109-
return delegate.attachPolicy(
110-
prefix, namespace, policyName, attachPolicyRequest, realmContext, securityContext);
158+
String catalogName = prefixParser.prefixToCatalogName(realmContext, prefix);
159+
polarisEventListener.onBeforeAttachPolicy(
160+
new CatalogPolicyServiceEvents.BeforeAttachPolicyEvent(
161+
catalogName, namespace, policyName, attachPolicyRequest));
162+
Response resp =
163+
delegate.attachPolicy(
164+
prefix, namespace, policyName, attachPolicyRequest, realmContext, securityContext);
165+
polarisEventListener.onAfterAttachPolicy(
166+
new CatalogPolicyServiceEvents.AfterAttachPolicyEvent(
167+
catalogName, namespace, policyName, attachPolicyRequest));
168+
return resp;
111169
}
112170

113171
@Override
@@ -118,8 +176,17 @@ public Response detachPolicy(
118176
DetachPolicyRequest detachPolicyRequest,
119177
RealmContext realmContext,
120178
SecurityContext securityContext) {
121-
return delegate.detachPolicy(
122-
prefix, namespace, policyName, detachPolicyRequest, realmContext, securityContext);
179+
String catalogName = prefixParser.prefixToCatalogName(realmContext, prefix);
180+
polarisEventListener.onBeforeDetachPolicy(
181+
new CatalogPolicyServiceEvents.BeforeDetachPolicyEvent(
182+
catalogName, namespace, policyName, detachPolicyRequest));
183+
Response resp =
184+
delegate.detachPolicy(
185+
prefix, namespace, policyName, detachPolicyRequest, realmContext, securityContext);
186+
polarisEventListener.onAfterDetachPolicy(
187+
new CatalogPolicyServiceEvents.AfterDetachPolicyEvent(
188+
catalogName, namespace, policyName, detachPolicyRequest));
189+
return resp;
123190
}
124191

125192
@Override
@@ -132,14 +199,27 @@ public Response getApplicablePolicies(
132199
String policyType,
133200
RealmContext realmContext,
134201
SecurityContext securityContext) {
135-
return delegate.getApplicablePolicies(
136-
prefix,
137-
pageToken,
138-
pageSize,
139-
namespace,
140-
targetName,
141-
policyType,
142-
realmContext,
143-
securityContext);
202+
String catalogName = prefixParser.prefixToCatalogName(realmContext, prefix);
203+
polarisEventListener.onBeforeGetApplicablePolicies(
204+
new CatalogPolicyServiceEvents.BeforeGetApplicablePoliciesEvent(
205+
catalogName, namespace, targetName, policyType));
206+
Response resp =
207+
delegate.getApplicablePolicies(
208+
prefix,
209+
pageToken,
210+
pageSize,
211+
namespace,
212+
targetName,
213+
policyType,
214+
realmContext,
215+
securityContext);
216+
polarisEventListener.onAfterGetApplicablePolicies(
217+
new CatalogPolicyServiceEvents.AfterGetApplicablePoliciesEvent(
218+
catalogName,
219+
namespace,
220+
targetName,
221+
policyType,
222+
(GetApplicablePoliciesResponse) resp.getEntity()));
223+
return resp;
144224
}
145225
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.polaris.service.events;
21+
22+
import org.apache.polaris.service.types.AttachPolicyRequest;
23+
import org.apache.polaris.service.types.CreatePolicyRequest;
24+
import org.apache.polaris.service.types.DetachPolicyRequest;
25+
import org.apache.polaris.service.types.GetApplicablePoliciesResponse;
26+
import org.apache.polaris.service.types.LoadPolicyResponse;
27+
import org.apache.polaris.service.types.UpdatePolicyRequest;
28+
29+
/**
30+
* Event records for Catalog Policy operations. Each operation has corresponding "Before" and
31+
* "After" event records.
32+
*/
33+
public class CatalogPolicyServiceEvents {
34+
35+
// Policy CRUD Events
36+
public record BeforeCreatePolicyEvent(
37+
String catalogName, String namespace, CreatePolicyRequest createPolicyRequest) {}
38+
39+
public record AfterCreatePolicyEvent(
40+
String catalogName, String namespace, LoadPolicyResponse loadPolicyResponse) {}
41+
42+
public record BeforeListPoliciesEvent(String catalogName, String namespace, String policyType) {}
43+
44+
public record AfterListPoliciesEvent(String catalogName, String namespace, String policyType) {}
45+
46+
public record BeforeLoadPolicyEvent(String catalogName, String namespace, String policyName) {}
47+
48+
public record AfterLoadPolicyEvent(
49+
String catalogName, String namespace, LoadPolicyResponse loadPolicyResponse) {}
50+
51+
public record BeforeUpdatePolicyEvent(
52+
String catalogName,
53+
String namespace,
54+
String policyName,
55+
UpdatePolicyRequest updatePolicyRequest) {}
56+
57+
public record AfterUpdatePolicyEvent(
58+
String catalogName, String namespace, LoadPolicyResponse loadPolicyResponse) {}
59+
60+
public record BeforeDropPolicyEvent(
61+
String catalogName, String namespace, String policyName, Boolean detachAll) {}
62+
63+
public record AfterDropPolicyEvent(
64+
String catalogName, String namespace, String policyName, Boolean detachAll) {}
65+
66+
// Policy Attachment Events
67+
public record BeforeAttachPolicyEvent(
68+
String catalogName,
69+
String namespace,
70+
String policyName,
71+
AttachPolicyRequest attachPolicyRequest) {}
72+
73+
public record AfterAttachPolicyEvent(
74+
String catalogName,
75+
String namespace,
76+
String policyName,
77+
AttachPolicyRequest attachPolicyRequest) {}
78+
79+
public record BeforeDetachPolicyEvent(
80+
String catalogName,
81+
String namespace,
82+
String policyName,
83+
DetachPolicyRequest detachPolicyRequest) {}
84+
85+
public record AfterDetachPolicyEvent(
86+
String catalogName,
87+
String namespace,
88+
String policyName,
89+
DetachPolicyRequest detachPolicyRequest) {}
90+
91+
// Policy Query Events
92+
public record BeforeGetApplicablePoliciesEvent(
93+
String catalogName, String namespace, String targetName, String policyType) {}
94+
95+
public record AfterGetApplicablePoliciesEvent(
96+
String catalogName,
97+
String namespace,
98+
String targetName,
99+
String policyType,
100+
GetApplicablePoliciesResponse getApplicablePoliciesResponse) {}
101+
}

runtime/service/src/main/java/org/apache/polaris/service/events/listeners/PolarisEventListener.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.polaris.service.events.BeforeViewCommitedEvent;
3333
import org.apache.polaris.service.events.BeforeViewRefreshedEvent;
3434
import org.apache.polaris.service.events.CatalogGenericTableServiceEvents;
35+
import org.apache.polaris.service.events.CatalogPolicyServiceEvents;
3536

3637
/**
3738
* Represents an event listener that can respond to notable moments during Polaris's execution.
@@ -78,6 +79,57 @@ public void onAfterTableCreated(AfterTableCreatedEvent event) {}
7879
/** {@link AfterCatalogCreatedEvent} */
7980
public void onAfterCatalogCreated(AfterCatalogCreatedEvent event) {}
8081

82+
// Catalog Policy Service Events
83+
/** {@link CatalogPolicyServiceEvents.BeforeCreatePolicyEvent} */
84+
public void onBeforeCreatePolicy(CatalogPolicyServiceEvents.BeforeCreatePolicyEvent event) {}
85+
86+
/** {@link CatalogPolicyServiceEvents.AfterCreatePolicyEvent} */
87+
public void onAfterCreatePolicy(CatalogPolicyServiceEvents.AfterCreatePolicyEvent event) {}
88+
89+
/** {@link CatalogPolicyServiceEvents.BeforeListPoliciesEvent} */
90+
public void onBeforeListPolicies(CatalogPolicyServiceEvents.BeforeListPoliciesEvent event) {}
91+
92+
/** {@link CatalogPolicyServiceEvents.AfterListPoliciesEvent} */
93+
public void onAfterListPolicies(CatalogPolicyServiceEvents.AfterListPoliciesEvent event) {}
94+
95+
/** {@link CatalogPolicyServiceEvents.BeforeLoadPolicyEvent} */
96+
public void onBeforeLoadPolicy(CatalogPolicyServiceEvents.BeforeLoadPolicyEvent event) {}
97+
98+
/** {@link CatalogPolicyServiceEvents.AfterLoadPolicyEvent} */
99+
public void onAfterLoadPolicy(CatalogPolicyServiceEvents.AfterLoadPolicyEvent event) {}
100+
101+
/** {@link CatalogPolicyServiceEvents.BeforeUpdatePolicyEvent} */
102+
public void onBeforeUpdatePolicy(CatalogPolicyServiceEvents.BeforeUpdatePolicyEvent event) {}
103+
104+
/** {@link CatalogPolicyServiceEvents.AfterUpdatePolicyEvent} */
105+
public void onAfterUpdatePolicy(CatalogPolicyServiceEvents.AfterUpdatePolicyEvent event) {}
106+
107+
/** {@link CatalogPolicyServiceEvents.BeforeDropPolicyEvent} */
108+
public void onBeforeDropPolicy(CatalogPolicyServiceEvents.BeforeDropPolicyEvent event) {}
109+
110+
/** {@link CatalogPolicyServiceEvents.AfterDropPolicyEvent} */
111+
public void onAfterDropPolicy(CatalogPolicyServiceEvents.AfterDropPolicyEvent event) {}
112+
113+
/** {@link CatalogPolicyServiceEvents.BeforeAttachPolicyEvent} */
114+
public void onBeforeAttachPolicy(CatalogPolicyServiceEvents.BeforeAttachPolicyEvent event) {}
115+
116+
/** {@link CatalogPolicyServiceEvents.AfterAttachPolicyEvent} */
117+
public void onAfterAttachPolicy(CatalogPolicyServiceEvents.AfterAttachPolicyEvent event) {}
118+
119+
/** {@link CatalogPolicyServiceEvents.BeforeDetachPolicyEvent} */
120+
public void onBeforeDetachPolicy(CatalogPolicyServiceEvents.BeforeDetachPolicyEvent event) {}
121+
122+
/** {@link CatalogPolicyServiceEvents.AfterDetachPolicyEvent} */
123+
public void onAfterDetachPolicy(CatalogPolicyServiceEvents.AfterDetachPolicyEvent event) {}
124+
125+
/** {@link CatalogPolicyServiceEvents.BeforeGetApplicablePoliciesEvent} */
126+
public void onBeforeGetApplicablePolicies(
127+
CatalogPolicyServiceEvents.BeforeGetApplicablePoliciesEvent event) {}
128+
129+
/** {@link CatalogPolicyServiceEvents.AfterGetApplicablePoliciesEvent} */
130+
public void onAfterGetApplicablePolicies(
131+
CatalogPolicyServiceEvents.AfterGetApplicablePoliciesEvent event) {}
132+
81133
/** {@link CatalogGenericTableServiceEvents.BeforeCreateGenericTableEvent} */
82134
public void onBeforeCreateGenericTable(
83135
CatalogGenericTableServiceEvents.BeforeCreateGenericTableEvent event) {}

0 commit comments

Comments
 (0)