30
30
import java .security .cert .X509Certificate ;
31
31
import javax .net .ssl .X509TrustManager ;
32
32
33
- import org .neo4j .driver .v1 .Logger ;
34
33
import org .neo4j .driver .internal .util .BytePrinter ;
34
+ import org .neo4j .driver .v1 .Logger ;
35
35
36
+ import static java .lang .String .format ;
36
37
import static org .neo4j .driver .internal .util .CertificateTool .X509CertToString ;
37
38
38
39
/**
@@ -77,6 +78,8 @@ private void load() throws IOException
77
78
return ;
78
79
}
79
80
81
+ assertKnownHostFileReadable ();
82
+
80
83
BufferedReader reader = new BufferedReader ( new FileReader ( knownHosts ) );
81
84
String line ;
82
85
while ( (line = reader .readLine ()) != null )
@@ -107,12 +110,38 @@ private void saveTrustedHost( String fingerprint ) throws IOException
107
110
logger .warn ( "Adding %s as known and trusted certificate for %s." , fingerprint , serverId );
108
111
createKnownCertFileIfNotExists ();
109
112
113
+ assertKnownHostFileWritable ();
110
114
BufferedWriter writer = new BufferedWriter ( new FileWriter ( knownHosts , true ) );
111
115
writer .write ( serverId + " " + this .fingerprint );
112
116
writer .newLine ();
113
117
writer .close ();
114
118
}
115
119
120
+
121
+ private void assertKnownHostFileReadable () throws IOException
122
+ {
123
+ if ( !knownHosts .canRead () )
124
+ {
125
+ throw new IOException ( format (
126
+ "Failed to load certificates from file %s as you have no read permissions to it.\n " +
127
+ "Try configuring the Neo4j driver to use a file system location you do have read permissions to." ,
128
+ knownHosts .getAbsolutePath ()
129
+ ) );
130
+ }
131
+ }
132
+
133
+ private void assertKnownHostFileWritable () throws IOException
134
+ {
135
+ if ( !knownHosts .canWrite () )
136
+ {
137
+ throw new IOException ( format (
138
+ "Failed to write certificates to file %s as you have no write permissions to it.\n " +
139
+ "Try configuring the Neo4j driver to use a file system location you do have write permissions to." ,
140
+ knownHosts .getAbsolutePath ()
141
+ ) );
142
+ }
143
+ }
144
+
116
145
/*
117
146
* Disallow all client connection to this client
118
147
*/
@@ -140,7 +169,7 @@ public void checkServerTrusted( X509Certificate[] chain, String authType )
140
169
}
141
170
catch ( IOException e )
142
171
{
143
- throw new CertificateException ( String . format (
172
+ throw new CertificateException ( format (
144
173
"Failed to save the server ID and the certificate received from the server to file %s.\n " +
145
174
"Server ID: %s\n Received cert:\n %s" ,
146
175
knownHosts .getAbsolutePath (), serverId , X509CertToString ( cert ) ), e );
@@ -150,7 +179,7 @@ public void checkServerTrusted( X509Certificate[] chain, String authType )
150
179
{
151
180
if ( !this .fingerprint .equals ( cert ) )
152
181
{
153
- throw new CertificateException ( String . format (
182
+ throw new CertificateException ( format (
154
183
"Unable to connect to neo4j at `%s`, because the certificate the server uses has changed. " +
155
184
"This is a security feature to protect against man-in-the-middle attacks.\n " +
156
185
"If you trust the certificate the server uses now, simply remove the line that starts with " +
0 commit comments