AuthenticationUtil.java

1
package com.jsql.util;
2
3
import com.jsql.model.InjectionModel;
4
import org.apache.commons.lang3.StringUtils;
5
import org.apache.logging.log4j.LogManager;
6
import org.apache.logging.log4j.Logger;
7
8
import java.io.File;
9
import java.net.Authenticator;
10
import java.net.PasswordAuthentication;
11
import java.util.prefs.Preferences;
12
13
/**
14
 * Manage authentication protocols Basic, Digest, NTLM and Kerberos.
15
 * Java class Authenticator processes Basic, Digest and NTLM, library spnego
16
 * processes kerberos.
17
 */
18
public class AuthenticationUtil {
19
    
20
    private static final Logger LOGGER = LogManager.getRootLogger();
21
    
22
    /**
23
     * True if standard authentication Basic, Digest, NTLM is activated.
24
     */
25
    private boolean isAuthentication = false;
26
27
    /**
28
     * Login for standard authentication.
29
     */
30
    private String usernameAuthentication;
31
32
    /**
33
     * Pass for standard authentication.
34
     */
35
    private String passwordAuthentication;
36
    
37
    /**
38
     * True if kerberos authentication is activated.
39
     */
40
    private boolean isKerberos = false;
41
42
    /**
43
     * Path to the kerberos file login.
44
     */
45
    private String pathKerberosLogin;
46
47
    /**
48
     * Path to the kerberos file krb5.
49
     */
50
    private String pathKerberosKrb5;
51
52
    /**
53
     * Get new authentication settings from the view, update the utility class,
54
     * persist settings to the JVM and apply changes to the system.
55
     * @param isAuthentication true if non-kerberos authentication is activated
56
     * @param usernameAuthentication login for standard authentication
57
     * @param passwordAuthentication pass for standard authentication
58
     * @param isKerberos true if krb authentication is activated
59
     * @param kerberosKrb5Conf path to the file krb5
60
     * @param kerberosLoginConf path to the file login
61
     */
62
    public boolean set(
63
        boolean isAuthentication,
64
        String usernameAuthentication,
65
        String passwordAuthentication,
66
        boolean isKerberos,
67
        String kerberosKrb5Conf,
68
        String kerberosLoginConf
69
    ) {
70
        boolean isRestartRequired = this.initKerberos(isKerberos, kerberosKrb5Conf, kerberosLoginConf);
71 1 1. set : removed call to com/jsql/util/AuthenticationUtil::initSimpleAuthorization → NO_COVERAGE
        this.initSimpleAuthorization(isAuthentication, usernameAuthentication, passwordAuthentication);
72 1 1. set : removed call to com/jsql/util/AuthenticationUtil::setAuthentication → NO_COVERAGE
        this.setAuthentication();
73 2 1. set : replaced boolean return with true for com/jsql/util/AuthenticationUtil::set → NO_COVERAGE
2. set : replaced boolean return with false for com/jsql/util/AuthenticationUtil::set → NO_COVERAGE
        return isRestartRequired;
74
    }
75
76
    public void initSimpleAuthorization(boolean isAuthentication, String usernameAuthentication, String passwordAuthentication) {
77
        var preferences = Preferences.userRoot().node(InjectionModel.class.getName());
78 1 1. initSimpleAuthorization : removed call to java/util/prefs/Preferences::putBoolean → NO_COVERAGE
        preferences.putBoolean("isAuthentication", isAuthentication);
79 1 1. initSimpleAuthorization : removed call to java/util/prefs/Preferences::put → NO_COVERAGE
        preferences.put("usernameAuthentication", usernameAuthentication);
80 1 1. initSimpleAuthorization : removed call to java/util/prefs/Preferences::put → NO_COVERAGE
        preferences.put("passwordAuthentication", passwordAuthentication);
81
        // Define proxy settings
82
        this.isAuthentication = isAuthentication;
83
        this.usernameAuthentication = usernameAuthentication;
84
        this.passwordAuthentication = passwordAuthentication;
85
    }
86
87
    private boolean initKerberos(boolean isKerberos, String kerberosKrb5Conf, String kerberosLoginConf) {
88
        // Persist to JVM
89
        var preferences = Preferences.userRoot().node(InjectionModel.class.getName());
90
        
91
        this.isKerberos = isKerberos;
92
        this.pathKerberosKrb5 = kerberosKrb5Conf;
93
        this.pathKerberosLogin = kerberosLoginConf;
94
        
95
        // Check if krb file has changed
96 1 1. initKerberos : negated conditional → NO_COVERAGE
        boolean isRestartRequired = this.isKerberos
97 1 1. initKerberos : negated conditional → NO_COVERAGE
            && !new File(this.pathKerberosKrb5).exists()
98 1 1. initKerberos : negated conditional → NO_COVERAGE
            && !kerberosKrb5Conf.equals(this.pathKerberosKrb5);
99
        
100 1 1. initKerberos : removed call to java/util/prefs/Preferences::putBoolean → NO_COVERAGE
        preferences.putBoolean("enableKerberos", this.isKerberos);
101 1 1. initKerberos : removed call to java/util/prefs/Preferences::put → NO_COVERAGE
        preferences.put("kerberosKrb5Conf", this.pathKerberosKrb5);
102 1 1. initKerberos : removed call to java/util/prefs/Preferences::put → NO_COVERAGE
        preferences.put("kerberosLoginConf", this.pathKerberosLogin);
103
        
104
        // Check krb integrity
105 1 1. initKerberos : negated conditional → NO_COVERAGE
        if (this.isKerberos) {
106
            // Fix #23877: NoClassDefFoundError on java/nio/file/Paths
107 1 1. initKerberos : negated conditional → NO_COVERAGE
            if (!new File(this.pathKerberosKrb5).exists()) {
108
                LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Krb5 file not found: {}", this.pathKerberosKrb5);
109
            }
110 1 1. initKerberos : negated conditional → NO_COVERAGE
            if (!new File(this.pathKerberosLogin).exists()) {
111
                LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Login file not found: {}", this.pathKerberosLogin);
112
            }
113
        }
114 2 1. initKerberos : replaced boolean return with false for com/jsql/util/AuthenticationUtil::initKerberos → NO_COVERAGE
2. initKerberos : replaced boolean return with true for com/jsql/util/AuthenticationUtil::initKerberos → NO_COVERAGE
        return isRestartRequired;
115
    }
116
    
117
    /**
118
     * Initialize the utility class with preferences from the JVM
119
     * and apply environment settings.
120
     */
121
    public void setKerberosCifs() {
122
        // Use Preferences API to persist proxy configuration
123
        var preferences = Preferences.userRoot().node(InjectionModel.class.getName());
124
125
        // Default proxy disabled
126
        this.isAuthentication = preferences.getBoolean("isAuthentication", false);
127
128
        // Default TOR config
129
        this.usernameAuthentication = preferences.get("usernameAuthentication", StringUtils.EMPTY);
130
        this.passwordAuthentication = preferences.get("passwordAuthentication", StringUtils.EMPTY);
131
        
132
        this.isKerberos = preferences.getBoolean("enableKerberos", false);
133
        this.pathKerberosKrb5 = preferences.get("kerberosKrb5Conf", StringUtils.EMPTY);
134
        this.pathKerberosLogin = preferences.get("kerberosLoginConf", StringUtils.EMPTY);
135
        
136 1 1. setKerberosCifs : removed call to com/jsql/util/AuthenticationUtil::setAuthentication → NO_COVERAGE
        this.setAuthentication();
137
    }
138
    
139
    /**
140
     * Apply kerberos authentication to the JVM.
141
     */
142
    public void setAuthentication() {
143 1 1. setAuthentication : removed call to java/net/Authenticator::setDefault → NO_COVERAGE
        Authenticator.setDefault(null);
144 1 1. setAuthentication : negated conditional → NO_COVERAGE
        if (this.isAuthentication) {
145 1 1. setAuthentication : removed call to java/net/Authenticator::setDefault → NO_COVERAGE
            Authenticator.setDefault(new Authenticator() {
146
                @Override
147
                protected PasswordAuthentication getPasswordAuthentication() {
148 1 1. getPasswordAuthentication : replaced return value with null for com/jsql/util/AuthenticationUtil$1::getPasswordAuthentication → NO_COVERAGE
                    return new PasswordAuthentication (
149
                        AuthenticationUtil.this.usernameAuthentication,
150
                        AuthenticationUtil.this.passwordAuthentication.toCharArray()
151
                    );
152
                }
153
            });
154
        } else {
155 1 1. setAuthentication : removed call to java/net/Authenticator::setDefault → NO_COVERAGE
            Authenticator.setDefault(null);
156
        }
157 1 1. setAuthentication : negated conditional → NO_COVERAGE
        if (this.isKerberos) {
158
            System.setProperty("java.security.krb5.conf", this.pathKerberosKrb5);
159
            System.setProperty("java.security.auth.login.config", this.pathKerberosLogin);
160
            System.setProperty("spnego.krb5.conf", this.pathKerberosKrb5);
161
            System.setProperty("spnego.login.conf", this.pathKerberosLogin);
162
        } else {
163
            System.setProperty("java.security.krb5.conf", StringUtils.EMPTY);
164
            System.setProperty("java.security.auth.login.config", StringUtils.EMPTY);
165
            System.setProperty("spnego.krb5.conf", StringUtils.EMPTY);
166
            System.setProperty("spnego.login.conf", StringUtils.EMPTY);
167
        }
168
    }
169
    
170
    
171
    // Getters and setters
172
173
    public boolean isAuthentEnabled() {
174 2 1. isAuthentEnabled : replaced boolean return with false for com/jsql/util/AuthenticationUtil::isAuthentEnabled → NO_COVERAGE
2. isAuthentEnabled : replaced boolean return with true for com/jsql/util/AuthenticationUtil::isAuthentEnabled → NO_COVERAGE
        return this.isAuthentication;
175
    }
176
177
    public String getPathKerberosLogin() {
178 1 1. getPathKerberosLogin : replaced return value with "" for com/jsql/util/AuthenticationUtil::getPathKerberosLogin → NO_COVERAGE
        return this.pathKerberosLogin;
179
    }
180
181
    public String getPathKerberosKrb5() {
182 1 1. getPathKerberosKrb5 : replaced return value with "" for com/jsql/util/AuthenticationUtil::getPathKerberosKrb5 → NO_COVERAGE
        return this.pathKerberosKrb5;
183
    }
184
185
    public boolean isKerberos() {
186 2 1. isKerberos : replaced boolean return with true for com/jsql/util/AuthenticationUtil::isKerberos → NO_COVERAGE
2. isKerberos : replaced boolean return with false for com/jsql/util/AuthenticationUtil::isKerberos → NO_COVERAGE
        return this.isKerberos;
187
    }
188
189
    public String getUsernameAuthentication() {
190 1 1. getUsernameAuthentication : replaced return value with "" for com/jsql/util/AuthenticationUtil::getUsernameAuthentication → NO_COVERAGE
        return this.usernameAuthentication;
191
    }
192
193
    public String getPasswordAuthentication() {
194 1 1. getPasswordAuthentication : replaced return value with "" for com/jsql/util/AuthenticationUtil::getPasswordAuthentication → NO_COVERAGE
        return this.passwordAuthentication;
195
    }
196
    
197
    
198
    // Builder
199
    
200
    public AuthenticationUtil withAuthenticationEnabled() {
201
        this.isAuthentication = true;
202 1 1. withAuthenticationEnabled : replaced return value with null for com/jsql/util/AuthenticationUtil::withAuthenticationEnabled → NO_COVERAGE
        return this;
203
    }
204
    
205
    public AuthenticationUtil withUsernameAuthentication(String usernameAuthentication) {
206
        this.usernameAuthentication = usernameAuthentication;
207 1 1. withUsernameAuthentication : replaced return value with null for com/jsql/util/AuthenticationUtil::withUsernameAuthentication → NO_COVERAGE
        return this;
208
    }
209
    
210
    public AuthenticationUtil withPasswordAuthentication(String passwordAuthentication) {
211
        this.passwordAuthentication = passwordAuthentication;
212 1 1. withPasswordAuthentication : replaced return value with null for com/jsql/util/AuthenticationUtil::withPasswordAuthentication → NO_COVERAGE
        return this;
213
    }
214
}

Mutations

71

1.1
Location : set
Killed by : none
removed call to com/jsql/util/AuthenticationUtil::initSimpleAuthorization → NO_COVERAGE

72

1.1
Location : set
Killed by : none
removed call to com/jsql/util/AuthenticationUtil::setAuthentication → NO_COVERAGE

73

1.1
Location : set
Killed by : none
replaced boolean return with true for com/jsql/util/AuthenticationUtil::set → NO_COVERAGE

2.2
Location : set
Killed by : none
replaced boolean return with false for com/jsql/util/AuthenticationUtil::set → NO_COVERAGE

78

1.1
Location : initSimpleAuthorization
Killed by : none
removed call to java/util/prefs/Preferences::putBoolean → NO_COVERAGE

79

1.1
Location : initSimpleAuthorization
Killed by : none
removed call to java/util/prefs/Preferences::put → NO_COVERAGE

80

1.1
Location : initSimpleAuthorization
Killed by : none
removed call to java/util/prefs/Preferences::put → NO_COVERAGE

96

1.1
Location : initKerberos
Killed by : none
negated conditional → NO_COVERAGE

97

1.1
Location : initKerberos
Killed by : none
negated conditional → NO_COVERAGE

98

1.1
Location : initKerberos
Killed by : none
negated conditional → NO_COVERAGE

100

1.1
Location : initKerberos
Killed by : none
removed call to java/util/prefs/Preferences::putBoolean → NO_COVERAGE

101

1.1
Location : initKerberos
Killed by : none
removed call to java/util/prefs/Preferences::put → NO_COVERAGE

102

1.1
Location : initKerberos
Killed by : none
removed call to java/util/prefs/Preferences::put → NO_COVERAGE

105

1.1
Location : initKerberos
Killed by : none
negated conditional → NO_COVERAGE

107

1.1
Location : initKerberos
Killed by : none
negated conditional → NO_COVERAGE

110

1.1
Location : initKerberos
Killed by : none
negated conditional → NO_COVERAGE

114

1.1
Location : initKerberos
Killed by : none
replaced boolean return with false for com/jsql/util/AuthenticationUtil::initKerberos → NO_COVERAGE

2.2
Location : initKerberos
Killed by : none
replaced boolean return with true for com/jsql/util/AuthenticationUtil::initKerberos → NO_COVERAGE

136

1.1
Location : setKerberosCifs
Killed by : none
removed call to com/jsql/util/AuthenticationUtil::setAuthentication → NO_COVERAGE

143

1.1
Location : setAuthentication
Killed by : none
removed call to java/net/Authenticator::setDefault → NO_COVERAGE

144

1.1
Location : setAuthentication
Killed by : none
negated conditional → NO_COVERAGE

145

1.1
Location : setAuthentication
Killed by : none
removed call to java/net/Authenticator::setDefault → NO_COVERAGE

148

1.1
Location : getPasswordAuthentication
Killed by : none
replaced return value with null for com/jsql/util/AuthenticationUtil$1::getPasswordAuthentication → NO_COVERAGE

155

1.1
Location : setAuthentication
Killed by : none
removed call to java/net/Authenticator::setDefault → NO_COVERAGE

157

1.1
Location : setAuthentication
Killed by : none
negated conditional → NO_COVERAGE

174

1.1
Location : isAuthentEnabled
Killed by : none
replaced boolean return with false for com/jsql/util/AuthenticationUtil::isAuthentEnabled → NO_COVERAGE

2.2
Location : isAuthentEnabled
Killed by : none
replaced boolean return with true for com/jsql/util/AuthenticationUtil::isAuthentEnabled → NO_COVERAGE

178

1.1
Location : getPathKerberosLogin
Killed by : none
replaced return value with "" for com/jsql/util/AuthenticationUtil::getPathKerberosLogin → NO_COVERAGE

182

1.1
Location : getPathKerberosKrb5
Killed by : none
replaced return value with "" for com/jsql/util/AuthenticationUtil::getPathKerberosKrb5 → NO_COVERAGE

186

1.1
Location : isKerberos
Killed by : none
replaced boolean return with true for com/jsql/util/AuthenticationUtil::isKerberos → NO_COVERAGE

2.2
Location : isKerberos
Killed by : none
replaced boolean return with false for com/jsql/util/AuthenticationUtil::isKerberos → NO_COVERAGE

190

1.1
Location : getUsernameAuthentication
Killed by : none
replaced return value with "" for com/jsql/util/AuthenticationUtil::getUsernameAuthentication → NO_COVERAGE

194

1.1
Location : getPasswordAuthentication
Killed by : none
replaced return value with "" for com/jsql/util/AuthenticationUtil::getPasswordAuthentication → NO_COVERAGE

202

1.1
Location : withAuthenticationEnabled
Killed by : none
replaced return value with null for com/jsql/util/AuthenticationUtil::withAuthenticationEnabled → NO_COVERAGE

207

1.1
Location : withUsernameAuthentication
Killed by : none
replaced return value with null for com/jsql/util/AuthenticationUtil::withUsernameAuthentication → NO_COVERAGE

212

1.1
Location : withPasswordAuthentication
Killed by : none
replaced return value with null for com/jsql/util/AuthenticationUtil::withPasswordAuthentication → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.19.1