Null safety via JSpecify spring-security-kerberos-test

Closes gh-18551
This commit is contained in:
Robert Winch 2026-01-21 17:53:12 -06:00
parent 91aee30906
commit 42e1e9fb67
No known key found for this signature in database
4 changed files with 47 additions and 9 deletions

View File

@ -1,6 +1,7 @@
plugins {
id 'io.spring.convention.spring-module'
id 'javadoc-warnings-error'
id 'security-nullability'
}
description = 'Spring Security Kerberos Test'

View File

@ -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;
}

View File

@ -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);

View File

@ -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;