mroosendaal
mroosendaal

Reputation: 113

Connect to Postgres DB with Kerberos from Java/Windows7

I've looked everywhere and asked loads of people but no one is able to help me so far. I'm trying to connect to a postgres (9.6) database on a remote machine from my windows (7) laptop through a Java (8) application. We use Kerberos for securing access but i have a valid Kerberos account and can create tickets via de Ticket Manager. I can also log on to other 'services' which require Kerberos authentication, although not through java but via a browser.

But whatever i try, i can't get my java program to work. Here's what i've got:

krb5.ini

[libdefaults]
default_realm = <domain>
forwardable = true
kdc_timesync = 1
ccache_type = 4
proxiable = true
dns_lookup_kdc = true
dns_lookup_realm = true

[realms]
<domain>.NET = {
    admin_server = <domain-server>
    default_domain = <domain>
}

[domain_realm]
.<domain> = <domain>
<domain>  = <domain>
.local.nl.<company>.com = <domain>
local.nl.<company>.com = <domain>
 [login]
krb4_convert = true
krb4_get_tickets = false

jaas.conf:

pgjdbc {
com.sun.security.auth.module.Krb5LoginModule required
refreshKrb5Config=true
doNotPrompt=false
useTicketCache=false
renewTGT=false
useKeyTab=true
keyTab="<location>/<filename>.keytab"
debug=true
client=true
principal="<username>@<domain>";
};

.keytab file

public class KerberosPostgresClient {
static {
        System.setProperty("java.security.krb5.conf","c:/tmp/krb5.ini");
        System.setProperty("java.security.krb5.realm","<domain>");
        System.setProperty("java.security.krb5.kdc","<domain>");
        System.setProperty("javax.security.auth.useSubjectCredsOnly","false");
        System.setProperty("java.security.auth.login.config","c:/tmp/jaas.conf"); }

@Test
public void test() throws Exception {
    String url = "jdbc:postgresql://<hostname>:<port>/<database>";
    Properties properties = new Properties();
    properties.setProperty("JAASConfigName", "pgjdbc");
    try (Connection conn = DriverManager.getConnection(url, connInfo)) {
        conn.createStatement();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

The very simple java code can find the keytab, jaas.conf. I created the keytab file on a different machine but with the same principal and password.

When i run the program i see:

Debug is  true storeKey false useTicketCache false useKeyTab true doNotPrompt false ticketCache is null isInitiator true KeyTab is c:/tmp/<username>.keytab refreshKrb5Config is true principal is <username>@<domain> tryFirstPass is false useFirstPass is false storePass is false clearPass is false
Refreshing Kerberos configuration

and after a short while i get an exception:

[Krb5LoginModule] authentication failed 
Receive timed out
org.postgresql.util.PSQLException: GSS Authentication failed
at org.postgresql.gss.MakeGSS.authenticate(MakeGSS.java:65)
....    
Caused by: java.net.SocketTimeoutException: Receive timed out
at java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.receive0(DualStackPlainDatagramSocketImpl.java:120)
at java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:144)
at java.net.DatagramSocket.receive(DatagramSocket.java:812)
at sun.security.krb5.internal.UDPClient.receive(NetClient.java:206)
at sun.security.krb5.KdcComm$KdcCommunication.run(KdcComm.java:411)
at sun.security.krb5.KdcComm$KdcCommunication.run(KdcComm.java:364)
at java.security.AccessController.doPrivileged(Native Method)
at sun.security.krb5.KdcComm.send(KdcComm.java:348)
at sun.security.krb5.KdcComm.sendIfPossible(KdcComm.java:253)
at sun.security.krb5.KdcComm.send(KdcComm.java:229)
at sun.security.krb5.KdcComm.send(KdcComm.java:200)
at sun.security.krb5.KrbAsReqBuilder.send(KrbAsReqBuilder.java:316)
at sun.security.krb5.KrbAsReqBuilder.action(KrbAsReqBuilder.java:361)
at com.sun.security.auth.module.Krb5LoginModule.attemptAuthentication(Krb5LoginModule.java:776)
... 45 more

I used to get other exceptions which indicated that it couldn't find the keytab file but with the above setup it seems to work. I can also ping the postgres database from my machine.

I found: Error connecting to PostgreSQL 9.4 with MIT Kerberos via JDBC vs CLI but has no solution

Upvotes: 4

Views: 5541

Answers (1)

mroosendaal
mroosendaal

Reputation: 113

i finally got it working with the following settings in my jaas.conf:

pgjdbc {
com.sun.security.auth.module.Krb5LoginModule required
refreshKrb5Config=true
doNotPrompt=true
useTicketCache=true
renewTGT=true
useKeyTab=true
keyTab="c:/<locationto>/<user>.keytab"
debug=true
client=true
principal="<user>@<domain>";
};

namely the combination of doNotPrompt, useTicketCache, renewTGT finally got it working

Upvotes: 7

Related Questions