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

Mutations

76

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

78

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

80

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

87

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

88

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

89

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

107

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

108

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

109

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

111

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

112

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

113

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

116

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

119

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

123

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

128

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

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

151

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

159

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

161

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

162

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

165

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

172

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

175

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

195

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

199

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

203

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

207

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

211

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

215

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

223

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

228

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

233

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.16.1