Advanced connection information
Connection URI
The driver supports connection to URIs of the form
<SCHEME>://<HOST>[:<PORT>[?policy=<POLICY-NAME>]]
-
<SCHEME>
is one amongneo4j
,neo4j+s
,neo4j+ssc
,bolt
,bolt+s
,bolt+ssc
. -
<HOST>
is the host name where the Neo4j server is located. -
<PORT>
is optional, and denotes the port the Bolt protocol is available at. -
<POLICY-NAME>
is an optional server policy name. Server policies need to be set up prior to usage.
The driver does not support connection to a nested path, such as example.com/neo4j/ .
The server must be reachable from the domain root.
|
Connection protocols and security
Communication between the driver and the server is mediated by Bolt. The scheme of the server URI determines whether the connection is encrypted and, if so, what type of certificates are accepted.
URL scheme | Encryption | Comment |
---|---|---|
neo4j |
Default for local setups |
|
neo4j+s |
(only CA-signed certificates) |
Default for Aura |
neo4j+ssc |
(CA- and self-signed certificates) |
The driver receives a routing table from the server upon successful connection, regardless of whether the instance is a proper cluster environment or a single-machine environment.
The driver’s routing behavior works in tandem with Neo4j’s clustering by directing read/write transactions to appropriate cluster members.
If you want to target a specific machine, use the bolt , bolt+s , or bolt+ssc URI schemes instead.
|
The connection scheme to use is not your choice, but is rather determined by the server requirements. You must know the right server scheme upfront, as no metadata is exposed prior to connection. If you are unsure, ask the database administrator.
Authentication methods
Basic authentication
The basic authentication scheme relies on traditional username and password. These can either be the credentials for your local installation, or the ones provided with an Aura instance.
driver, err := neo4j.NewDriverWithContext(
dbUri,
neo4j.BasicAuth(dbUser, dbPassword, ""))
The basic authentication scheme can also be used to authenticate against an LDAP server (Enterprise Edition only).
Kerberos authentication
The Kerberos authentication scheme requires a base64-encoded ticket. It can only be used if the server has the Kerberos Add-on installed.
driver, err := neo4j.NewDriverWithContext(dbUri, neo4j.KerberosAuth(ticket))
Bearer authentication
The bearer authentication scheme requires a base64-encoded token provided by an Identity Provider through Neo4j’s Single Sign-On feature.
driver, err := neo4j.NewDriverWithContext(dbUri, neo4j.BearerAuth(token))
The bearer authentication scheme requires configuring Single Sign-On on the server. Once configured, clients can discover Neo4j’s configuration through the Discovery API. |
Custom authentication
Use the function CustomAuth
to log into a server having a custom authentication scheme.
No authentication
Use the function NoAuth
to access a server where authentication is disabled.
driver, err := neo4j.NewDriverWithContext(dbUri, neo4j.NoAuth())
Rotating authentication tokens
It is possible to rotate authentication tokens that are expected to expire (e.g. SSO).
You need to provide a TokenManager
instance when instantiating the Driver
, rather than a static authentication token.
The easiest way to get started is to use one of built-in implementations: BasicTokenManager
and BearerTokenManager
.
fetchAuthTokenFromProvider := func(ctx context.Context) (neo4j.AuthToken, *time.Time, error) {
// some way of getting a token
token, err := getSsoToken(ctx)
if err != nil {
return neo4j.AuthToken{}, nil, err
}
// assume we know tokens expire every 60 seconds
expiresIn := time.Now().Add(60 * time.Second)
// include a little buffer so that we fetch a new token before the old one expires
expiresIn = expiresIn.Add(-10 * time.Second)
// or return nil instead of `&expiresIn` if we don't expect it to expire
return token, &expiresIn, nil
}
// create a new driver with a bearer token manager
_, _ = neo4j.NewDriverWithContext(dbUri, auth.BearerTokenManager(fetchAuthTokenFromProvider))
This API must not be used for switching users. Auth managers must always return tokens for the same identity. You can switch users at both query level and session level. |
TokenManager implementations and providers must not interact with the driver in any way, as this can cause deadlocks and undefined behavior.
|
Mutual TLS (client-side certificates as 2FA)
Mutual TLS (mTLS) allows you to use a client certificate as second factor for authenticating with the server. The certificate can only be used together with an authentication token and is not a replacement of regular authentication, unless authentication is disabled on the server.
The client’s certificate and public key must be placed in the server’s <NEO4J_HOME>/certificates/bolt/trusted
directory. For more information on server setup, see Configure SSL over Bolt.
For mTLS to work, the driver’s connection with the server must be encrypted, i.e. the connection URI scheme must be either +s or +ssc (ex. neo4j+s://example.com:7687 ).
|
Use auth.NewStaticClientCertificateProvider()
for static certificates.
The method takes a ClientCertificate
instance.
certProvider, err := auth.NewStaticClientCertificateProvider(auth.ClientCertificate {
CertFile: "path/to/cert.pem",
KeyFile: "path/to/key.pem",
Password: "theCertPassword",
})
if err != nil {
log.Fatalf("Failed to load certificate: %v", err)
}
_, _ = neo4j.NewDriverWithContext(dbUri, neo4j.BasicAuth(dbUser, dbPassword, ""), func(config *config.Config) {
config.ClientCertificateProvider = certProvider
})
Use auth.NewRotatingClientCertificateProvider()
for rotating certificates.
The method takes a ClientCertificate
instance.
password := "theCertPassword"
certProvider, err := auth.NewRotatingClientCertificateProvider(auth.ClientCertificate {
CertFile: "path/to/cert.pem",
KeyFile: "path/to/key.pem",
Password: &password,
})
if err != nil {
log.Fatalf("Failed to load certificate: %v", err)
}
_, _ = neo4j.NewDriverWithContext(dbUri, neo4j.BasicAuth(dbUser, dbPassword, ""), func(config *config.Config) {
config.ClientCertificateProvider = certProvider
})
// use the driver a bit...
// when it's time to rotate the certificate...
err = provider.UpdateCertificate(auth.ClientCertificate {
CertFile: "path/to/new_cert.pem",
KeyFile: "path/to/new_key.pem",
Password: &password,
})
if err != nil {
log.Fatalf("Failed to update certificate: %v", err)
}
// use the driver again...
For more information, see API docs → ClientCertificateProvider
.
Custom address resolver
When creating a DriverWithContext
object, you can specify a resolver function to resolve the connection address the driver is initialized with.
Note that addresses that the driver receives in routing tables are not resolved with the custom resolver.
Your resolver function is called with a ServerAddress
object and should return a list of ServerAddress
objects.
example.com
on port 9999
is resolved to localhost
on port 7687
// import "github.com/neo4j/neo4j-go-driver/v5/neo4j/config"
driver, err := neo4j.NewDriverWithContext(
"neo4j://example.com:9999", neo4j.BasicAuth(dbUser, dbPassword, ""),
func(conf *config.Config) {
conf.AddressResolver = func(address config.ServerAddress) []config.ServerAddress {
return []config.ServerAddress{
neo4j.NewServerAddress("localhost", "7687"),
}
}
})
defer driver.Close(ctx)
Further connection parameters
You can find all DriverWithContext
configuration parameters in the API documentation → config package.
Glossary
- LTS
-
A Long Term Support release is one guaranteed to be supported for a number of years. Neo4j 4.4 is LTS, and Neo4j 5 will also have an LTS version.
- Aura
-
Aura is Neo4j’s fully managed cloud service. It comes with both free and paid plans.
- Cypher
-
Cypher is Neo4j’s graph query language that lets you retrieve data from the database. It is like SQL, but for graphs.
- APOC
-
Awesome Procedures On Cypher (APOC) is a library of (many) functions that can not be easily expressed in Cypher itself.
- Bolt
-
Bolt is the protocol used for interaction between Neo4j instances and drivers. It listens on port 7687 by default.
- ACID
-
Atomicity, Consistency, Isolation, Durability (ACID) are properties guaranteeing that database transactions are processed reliably. An ACID-compliant DBMS ensures that the data in the database remains accurate and consistent despite failures.
- eventual consistency
-
A database is eventually consistent if it provides the guarantee that all cluster members will, at some point in time, store the latest version of the data.
- causal consistency
-
A database is causally consistent if read and write queries are seen by every member of the cluster in the same order. This is stronger than eventual consistency.
- NULL
-
The null marker is not a type but a placeholder for absence of value. For more information, see Cypher → Working with
null
. - transaction
-
A transaction is a unit of work that is either committed in its entirety or rolled back on failure. An example is a bank transfer: it involves multiple steps, but they must all succeed or be reverted, to avoid money being subtracted from one account but not added to the other.
- backpressure
-
Backpressure is a force opposing the flow of data. It ensures that the client is not being overwhelmed by data faster than it can handle.
- transaction function
-
A transaction function is a callback executed by an
ExecuteRead
orExecuteWrite
call. The driver automatically re-executes the callback in case of server failure. - DriverWithContext
-
A
DriverWithContext
object holds the details required to establish connections with a Neo4j database.