Skip to content

Commit fae1531

Browse files
committed
KNOX-3438: validate configured default token TTL against admin API bounds at startup
1 parent 8ae65dc commit fae1531

5 files changed

Lines changed: 48 additions & 2 deletions

File tree

gateway-server/src/main/java/org/apache/knox/gateway/services/knoxidf/delegation/EmptyDelegationPolicyService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,12 @@ public DelegationPolicyList list(String actorAuthorityFilter) {
8585
public PolicyDecision evaluate(PolicyCheckRequest request) {
8686
return new PolicyDecision("service_not_configured", 0);
8787
}
88+
89+
@Override
90+
public int getConfiguredTokenTtlSec() {
91+
// This stub is only in play when the service role is not deployed (so the admin API is not
92+
// deployed either). Return the gateway-config default so any bounds check still sees an
93+
// in-range value rather than 0.
94+
return GatewayConfig.DELEGATION_SERVICE_TOKEN_TTL_SEC_DEFAULT;
95+
}
8896
}

gateway-server/src/main/java/org/apache/knox/gateway/services/knoxidf/delegation/JdbcDelegationPolicyService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ public DelegationPolicyList list(String actorAuthorityFilter) {
204204
}
205205
}
206206

207+
@Override
208+
public int getConfiguredTokenTtlSec() {
209+
return configuredKnoxTokenTtlSec;
210+
}
211+
207212
@Override
208213
public PolicyDecision evaluate(PolicyCheckRequest request) {
209214
// Step 1: look up registration

gateway-service-knoxidf/src/main/java/org/apache/knox/gateway/service/knoxidf/DelegationPolicyResource.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,24 @@ public void init() {
9696
final GatewayServices services = (GatewayServices)
9797
servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
9898
policyService = services.getService(ServiceType.DELEGATION_POLICY_SERVICE);
99+
configureTtlBounds();
100+
}
99101

102+
private void configureTtlBounds() {
100103
minTokenTtlSec = readPositiveIntParam(MIN_TOKEN_TTL_SEC_PARAM, DEFAULT_MIN_TOKEN_TTL_SEC);
101104
maxTokenTtlSec = readPositiveIntParam(MAX_TOKEN_TTL_SEC_PARAM, DEFAULT_MAX_TOKEN_TTL_SEC);
102105
if (minTokenTtlSec > maxTokenTtlSec) {
103106
throw new IllegalStateException("Invalid delegation policy TTL bounds: "
104-
+ MIN_TOKEN_TTL_SEC_PARAM + " (" + minTokenTtlSec + ") must not exceed "
105-
+ MAX_TOKEN_TTL_SEC_PARAM + " (" + maxTokenTtlSec + ")");
107+
+ MIN_TOKEN_TTL_SEC_PARAM + " (" + minTokenTtlSec + ") must not exceed "
108+
+ MAX_TOKEN_TTL_SEC_PARAM + " (" + maxTokenTtlSec + ")");
109+
}
110+
111+
final int configuredDefaultTtlSec = policyService.getConfiguredTokenTtlSec();
112+
if (configuredDefaultTtlSec < minTokenTtlSec || configuredDefaultTtlSec > maxTokenTtlSec) {
113+
throw new IllegalStateException("Configured default delegation token TTL ("
114+
+ configuredDefaultTtlSec + "s) is outside the enforced bounds [" + minTokenTtlSec + ", "
115+
+ maxTokenTtlSec + "]; policies without an explicit tokenTtlSec would receive an "
116+
+ "out-of-range effective TTL");
106117
}
107118
}
108119

gateway-service-knoxidf/src/test/java/org/apache/knox/gateway/service/knoxidf/DelegationPolicyResourceTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,7 @@ public void testInstantFieldsSerializeAsIso8601() throws Exception {
11861186
@Test
11871187
public void testInitWiresServiceFromGatewayServices() throws Exception {
11881188
final DelegationPolicyService svc = EasyMock.createNiceMock(DelegationPolicyService.class);
1189+
EasyMock.expect(svc.getConfiguredTokenTtlSec()).andReturn(3600).anyTimes();
11891190
EasyMock.replay(svc);
11901191

11911192
final GatewayServices gws = EasyMock.createNiceMock(GatewayServices.class);
@@ -1229,8 +1230,20 @@ public void testInitRejectsMinGreaterThanMax() throws Exception {
12291230
initResourceWithBounds("7200", "120");
12301231
}
12311232

1233+
@Test(expected = IllegalStateException.class)
1234+
public void testInitRejectsConfiguredDefaultOutsideBounds() throws Exception {
1235+
// Default bounds are [60, 86400]; a gateway-wide default above max would let policies without
1236+
// an explicit tokenTtlSec yield an out-of-range effective TTL, so init() must fail fast.
1237+
initResource(null, null, 100000);
1238+
}
1239+
12321240
private DelegationPolicyResource initResourceWithBounds(String min, String max) throws Exception {
1241+
return initResource(min, max, 3600);
1242+
}
1243+
1244+
private DelegationPolicyResource initResource(String min, String max, int configuredDefault) throws Exception {
12331245
final DelegationPolicyService svc = EasyMock.createNiceMock(DelegationPolicyService.class);
1246+
EasyMock.expect(svc.getConfiguredTokenTtlSec()).andReturn(configuredDefault).anyTimes();
12341247
EasyMock.replay(svc);
12351248

12361249
final GatewayServices gws = EasyMock.createNiceMock(GatewayServices.class);

gateway-spi/src/main/java/org/apache/knox/gateway/services/knoxidf/delegation/DelegationPolicyService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,13 @@ public interface DelegationPolicyService extends Service {
8080
DelegationPolicyList list(String actorAuthorityFilter);
8181

8282
PolicyDecision evaluate(PolicyCheckRequest request);
83+
84+
/**
85+
* @return the gateway-wide default token TTL (seconds) that {@link #evaluate} applies when a
86+
* policy does not pin its own tokenTtlSec. Exposed so the admin API can validate this
87+
* fallback against its configured [min, max] bounds at startup and fail fast on an
88+
* out-of-range value -- otherwise policies stored without an explicit tokenTtlSec would
89+
* yield an effective TTL outside the range the API enforces for explicit values.
90+
*/
91+
int getConfiguredTokenTtlSec();
8392
}

0 commit comments

Comments
 (0)