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