From 42e1e9fb67fe0226e0105dbb322b780f8ba6d93d Mon Sep 17 00:00:00 2001 From: Robert Winch <362503+rwinch@users.noreply.github.com> Date: Wed, 21 Jan 2026 17:53:12 -0600 Subject: [PATCH] Null safety via JSpecify spring-security-kerberos-test Closes gh-18551 --- .../spring-security-kerberos-test.gradle | 1 + .../test/KerberosSecurityTestcase.java | 19 ++++++++++++------ .../security/kerberos/test/MiniKdc.java | 16 ++++++++++++--- .../security/kerberos/test/package-info.java | 20 +++++++++++++++++++ 4 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/package-info.java diff --git a/kerberos/kerberos-test/spring-security-kerberos-test.gradle b/kerberos/kerberos-test/spring-security-kerberos-test.gradle index 5e7022783b..9d220c6c9b 100644 --- a/kerberos/kerberos-test/spring-security-kerberos-test.gradle +++ b/kerberos/kerberos-test/spring-security-kerberos-test.gradle @@ -1,6 +1,7 @@ plugins { id 'io.spring.convention.spring-module' id 'javadoc-warnings-error' + id 'security-nullability' } description = 'Spring Security Kerberos Test' diff --git a/kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/KerberosSecurityTestcase.java b/kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/KerberosSecurityTestcase.java index 885df14fec..3af19c6981 100644 --- a/kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/KerberosSecurityTestcase.java +++ b/kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/KerberosSecurityTestcase.java @@ -19,6 +19,7 @@ package org.springframework.security.kerberos.test; import java.io.File; import java.util.Properties; +import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -35,17 +36,23 @@ import org.junit.jupiter.api.BeforeEach; */ public class KerberosSecurityTestcase { - private MiniKdc kdc; + private @Nullable MiniKdc kdc; - private File workDir; + private @Nullable File workDir; - private Properties conf; + private @Nullable Properties conf; @BeforeEach public void startMiniKdc() throws Exception { createTestDir(); createMiniKdcConf(); + if (this.conf == null) { + throw new IllegalStateException("conf must be initialized"); + } + if (this.workDir == null) { + throw new IllegalStateException("workDir must be initialized"); + } this.kdc = new MiniKdc(this.conf, this.workDir); this.kdc.start(); } @@ -73,15 +80,15 @@ public class KerberosSecurityTestcase { } } - public MiniKdc getKdc() { + public @Nullable MiniKdc getKdc() { return this.kdc; } - public File getWorkDir() { + public @Nullable File getWorkDir() { return this.workDir; } - public Properties getConf() { + public @Nullable Properties getConf() { return this.conf; } diff --git a/kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/MiniKdc.java b/kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/MiniKdc.java index 924abd3b21..7d4242b2c9 100644 --- a/kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/MiniKdc.java +++ b/kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/MiniKdc.java @@ -34,6 +34,7 @@ import org.apache.kerby.kerberos.kerb.server.KdcConfigKey; import org.apache.kerby.kerberos.kerb.server.SimpleKdcServer; import org.apache.kerby.util.IOUtil; import org.apache.kerby.util.NetworkUtil; +import org.jspecify.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -197,7 +198,7 @@ public class MiniKdc { private Properties conf; - private SimpleKdcServer simpleKdc; + private @Nullable SimpleKdcServer simpleKdc; private int port; @@ -205,9 +206,9 @@ public class MiniKdc { private File workDir; - private File krb5conf; + private @Nullable File krb5conf; - private String transport; + private @Nullable String transport; private boolean krb5Debug; @@ -300,6 +301,9 @@ public class MiniKdc { private void prepareKdcServer() throws Exception { // transport + if (this.simpleKdc == null) { + throw new IllegalStateException("simpleKdc must be initialized"); + } this.simpleKdc.setWorkDir(this.workDir); this.simpleKdc.setKdcHost(getHost()); this.simpleKdc.setKdcRealm(this.realm); @@ -395,6 +399,9 @@ public class MiniKdc { * @throws Exception thrown if the principal could not be created. */ public synchronized void createPrincipal(String principal, String password) throws Exception { + if (this.simpleKdc == null) { + throw new IllegalStateException("MiniKdc must be started before creating principals"); + } this.simpleKdc.createPrincipal(principal, password); } @@ -405,6 +412,9 @@ public class MiniKdc { * @throws Exception thrown if the principals or the keytab file could not be created. */ public synchronized void createPrincipal(File keytabFile, String... principals) throws Exception { + if (this.simpleKdc == null) { + throw new IllegalStateException("MiniKdc must be started before creating principals"); + } this.simpleKdc.createPrincipals(principals); if (keytabFile.exists() && !keytabFile.delete()) { LOG.error("Failed to delete keytab file: " + keytabFile); diff --git a/kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/package-info.java b/kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/package-info.java new file mode 100644 index 0000000000..0c5db5bac7 --- /dev/null +++ b/kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2004-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@NullMarked +package org.springframework.security.kerberos.test; + +import org.jspecify.annotations.NullMarked;