3030import java .security .cert .X509Certificate ;
3131import javax .net .ssl .X509TrustManager ;
3232
33- import org .neo4j .driver .v1 .Logger ;
3433import org .neo4j .driver .internal .util .BytePrinter ;
34+ import org .neo4j .driver .v1 .Logger ;
3535
36+ import static java .lang .String .format ;
3637import static org .neo4j .driver .internal .util .CertificateTool .X509CertToString ;
3738
3839/**
@@ -77,6 +78,8 @@ private void load() throws IOException
7778 return ;
7879 }
7980
81+ assertKnownHostFileReadable ();
82+
8083 BufferedReader reader = new BufferedReader ( new FileReader ( knownHosts ) );
8184 String line ;
8285 while ( (line = reader .readLine ()) != null )
@@ -107,12 +110,38 @@ private void saveTrustedHost( String fingerprint ) throws IOException
107110 logger .warn ( "Adding %s as known and trusted certificate for %s." , fingerprint , serverId );
108111 createKnownCertFileIfNotExists ();
109112
113+ assertKnownHostFileWritable ();
110114 BufferedWriter writer = new BufferedWriter ( new FileWriter ( knownHosts , true ) );
111115 writer .write ( serverId + " " + this .fingerprint );
112116 writer .newLine ();
113117 writer .close ();
114118 }
115119
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+
116145 /*
117146 * Disallow all client connection to this client
118147 */
@@ -140,7 +169,7 @@ public void checkServerTrusted( X509Certificate[] chain, String authType )
140169 }
141170 catch ( IOException e )
142171 {
143- throw new CertificateException ( String . format (
172+ throw new CertificateException ( format (
144173 "Failed to save the server ID and the certificate received from the server to file %s.\n " +
145174 "Server ID: %s\n Received cert:\n %s" ,
146175 knownHosts .getAbsolutePath (), serverId , X509CertToString ( cert ) ), e );
@@ -150,7 +179,7 @@ public void checkServerTrusted( X509Certificate[] chain, String authType )
150179 {
151180 if ( !this .fingerprint .equals ( cert ) )
152181 {
153- throw new CertificateException ( String . format (
182+ throw new CertificateException ( format (
154183 "Unable to connect to neo4j at `%s`, because the certificate the server uses has changed. " +
155184 "This is a security feature to protect against man-in-the-middle attacks.\n " +
156185 "If you trust the certificate the server uses now, simply remove the line that starts with " +
0 commit comments