3434import org .neo4j .driver .v1 .Logger ;
3535import org .neo4j .driver .internal .util .BytePrinter ;
3636
37+ import static java .lang .String .format ;
3738import static org .neo4j .driver .internal .util .CertificateTool .X509CertToString ;
3839
3940/**
@@ -78,6 +79,8 @@ private void load() throws IOException
7879 return ;
7980 }
8081
82+ assertKnownHostFileReadable ();
83+
8184 BufferedReader reader = new BufferedReader ( new FileReader ( knownHosts ) );
8285 String line ;
8386 while ( (line = reader .readLine ()) != null )
@@ -108,12 +111,38 @@ private void saveTrustedHost( String fingerprint ) throws IOException
108111 logger .warn ( "Adding %s as known and trusted certificate for %s." , fingerprint , serverId );
109112 createKnownCertFileIfNotExists ();
110113
114+ assertKnownHostFileWritable ();
111115 BufferedWriter writer = new BufferedWriter ( new FileWriter ( knownHosts , true ) );
112116 writer .write ( serverId + " " + this .fingerprint );
113117 writer .newLine ();
114118 writer .close ();
115119 }
116120
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+
117146 /*
118147 * Disallow all client connection to this client
119148 */
@@ -141,7 +170,7 @@ public void checkServerTrusted( X509Certificate[] chain, String authType )
141170 }
142171 catch ( IOException e )
143172 {
144- throw new CertificateException ( String . format (
173+ throw new CertificateException ( format (
145174 "Failed to save the server ID and the certificate received from the server to file %s.\n " +
146175 "Server ID: %s\n Received cert:\n %s" ,
147176 knownHosts .getAbsolutePath (), serverId , X509CertToString ( cert ) ), e );
@@ -151,7 +180,7 @@ public void checkServerTrusted( X509Certificate[] chain, String authType )
151180 {
152181 if ( !this .fingerprint .equals ( cert ) )
153182 {
154- throw new CertificateException ( String . format (
183+ throw new CertificateException ( format (
155184 "Unable to connect to neo4j at `%s`, because the certificate the server uses has changed. " +
156185 "This is a security feature to protect against man-in-the-middle attacks.\n " +
157186 "If you trust the certificate the server uses now, simply remove the line that starts with " +
0 commit comments