Skip to content

Commit 42e1e9f

Browse files
committed
Null safety via JSpecify spring-security-kerberos-test
Closes gh-18551
1 parent 91aee30 commit 42e1e9f

4 files changed

Lines changed: 47 additions & 9 deletions

File tree

kerberos/kerberos-test/spring-security-kerberos-test.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id 'io.spring.convention.spring-module'
33
id 'javadoc-warnings-error'
4+
id 'security-nullability'
45
}
56

67
description = 'Spring Security Kerberos Test'

kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/KerberosSecurityTestcase.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.File;
2020
import java.util.Properties;
2121

22+
import org.jspecify.annotations.Nullable;
2223
import org.junit.jupiter.api.AfterEach;
2324
import org.junit.jupiter.api.BeforeEach;
2425

@@ -35,17 +36,23 @@
3536
*/
3637
public class KerberosSecurityTestcase {
3738

38-
private MiniKdc kdc;
39+
private @Nullable MiniKdc kdc;
3940

40-
private File workDir;
41+
private @Nullable File workDir;
4142

42-
private Properties conf;
43+
private @Nullable Properties conf;
4344

4445
@BeforeEach
4546
public void startMiniKdc() throws Exception {
4647
createTestDir();
4748
createMiniKdcConf();
4849

50+
if (this.conf == null) {
51+
throw new IllegalStateException("conf must be initialized");
52+
}
53+
if (this.workDir == null) {
54+
throw new IllegalStateException("workDir must be initialized");
55+
}
4956
this.kdc = new MiniKdc(this.conf, this.workDir);
5057
this.kdc.start();
5158
}
@@ -73,15 +80,15 @@ public void stopMiniKdc() {
7380
}
7481
}
7582

76-
public MiniKdc getKdc() {
83+
public @Nullable MiniKdc getKdc() {
7784
return this.kdc;
7885
}
7986

80-
public File getWorkDir() {
87+
public @Nullable File getWorkDir() {
8188
return this.workDir;
8289
}
8390

84-
public Properties getConf() {
91+
public @Nullable Properties getConf() {
8592
return this.conf;
8693
}
8794

kerberos/kerberos-test/src/main/java/org/springframework/security/kerberos/test/MiniKdc.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.kerby.kerberos.kerb.server.SimpleKdcServer;
3535
import org.apache.kerby.util.IOUtil;
3636
import org.apache.kerby.util.NetworkUtil;
37+
import org.jspecify.annotations.Nullable;
3738
import org.slf4j.Logger;
3839
import org.slf4j.LoggerFactory;
3940

@@ -197,17 +198,17 @@ public static Properties createConf() {
197198

198199
private Properties conf;
199200

200-
private SimpleKdcServer simpleKdc;
201+
private @Nullable SimpleKdcServer simpleKdc;
201202

202203
private int port;
203204

204205
private String realm;
205206

206207
private File workDir;
207208

208-
private File krb5conf;
209+
private @Nullable File krb5conf;
209210

210-
private String transport;
211+
private @Nullable String transport;
211212

212213
private boolean krb5Debug;
213214

@@ -300,6 +301,9 @@ private void resetDefaultRealm() throws IOException {
300301

301302
private void prepareKdcServer() throws Exception {
302303
// transport
304+
if (this.simpleKdc == null) {
305+
throw new IllegalStateException("simpleKdc must be initialized");
306+
}
303307
this.simpleKdc.setWorkDir(this.workDir);
304308
this.simpleKdc.setKdcHost(getHost());
305309
this.simpleKdc.setKdcRealm(this.realm);
@@ -395,6 +399,9 @@ private void delete(File f) {
395399
* @throws Exception thrown if the principal could not be created.
396400
*/
397401
public synchronized void createPrincipal(String principal, String password) throws Exception {
402+
if (this.simpleKdc == null) {
403+
throw new IllegalStateException("MiniKdc must be started before creating principals");
404+
}
398405
this.simpleKdc.createPrincipal(principal, password);
399406
}
400407

@@ -405,6 +412,9 @@ public synchronized void createPrincipal(String principal, String password) thro
405412
* @throws Exception thrown if the principals or the keytab file could not be created.
406413
*/
407414
public synchronized void createPrincipal(File keytabFile, String... principals) throws Exception {
415+
if (this.simpleKdc == null) {
416+
throw new IllegalStateException("MiniKdc must be started before creating principals");
417+
}
408418
this.simpleKdc.createPrincipals(principals);
409419
if (keytabFile.exists() && !keytabFile.delete()) {
410420
LOG.error("Failed to delete keytab file: " + keytabFile);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2004-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@NullMarked
18+
package org.springframework.security.kerberos.test;
19+
20+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)