|
| 1 | +import java.security.PrivilegedActionException; |
| 2 | +import java.security.PrivilegedExceptionAction; |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.DriverManager; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Properties; |
| 8 | + |
| 9 | +import javax.security.auth.Subject; |
| 10 | +import javax.security.auth.login.LoginException; |
| 11 | +import javax.security.auth.spi.LoginModule; |
| 12 | + |
| 13 | +import org.ietf.jgss.GSSCredential; |
| 14 | +import org.ietf.jgss.GSSException; |
| 15 | +import org.ietf.jgss.GSSManager; |
| 16 | +import org.ietf.jgss.GSSName; |
| 17 | +import org.ietf.jgss.Oid; |
| 18 | + |
| 19 | +import com.sun.security.jgss.ExtendedGSSCredential; |
| 20 | + |
| 21 | +/** |
| 22 | + * |
| 23 | + * Sample of constrained delegation connection. |
| 24 | + * |
| 25 | + * An intermediate service is necessary to impersonate the client. This service needs to be configured with the |
| 26 | + * options: |
| 27 | + * "Trust this user for delegation to specified services only" |
| 28 | + * "Use any authentication protocol" |
| 29 | + * |
| 30 | + */ |
| 31 | +public class ConstrainedSample { |
| 32 | + |
| 33 | + // Connection properties |
| 34 | + private static final String DRIVER_CLASS_NAME ="com.microsoft.sqlserver.jdbc.SQLServerDriver"; |
| 35 | + private static final String CONNECTION_URI = "jdbc:sqlserver:// URI of the SQLServer"; |
| 36 | + |
| 37 | + private static final String TARGET_USER_NAME = "User to be impersonated"; |
| 38 | + |
| 39 | + // Impersonation service properties |
| 40 | + private static final String SERVICE_PRINCIPAL = "SPN"; |
| 41 | + private static final String KEYTAB_ROUTE = "Route to the keytab file"; |
| 42 | + |
| 43 | + private static final Properties driverProperties; |
| 44 | + private static Oid krb5Oid; |
| 45 | + |
| 46 | + private static Subject serviceSubject; |
| 47 | + |
| 48 | + static { |
| 49 | + |
| 50 | + driverProperties = new Properties(); |
| 51 | + driverProperties.setProperty("integratedSecurity", "true"); |
| 52 | + driverProperties.setProperty("authenticationScheme", "JavaKerberos"); |
| 53 | + |
| 54 | + try { |
| 55 | + krb5Oid = new Oid("1.2.840.113554.1.2.2"); |
| 56 | + } catch (GSSException e) { |
| 57 | + System.out.println("Error creating Oid: " + e); |
| 58 | + System.exit(-1); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static void main(String... args) throws Exception { |
| 63 | + |
| 64 | + Class.forName(DRIVER_CLASS_NAME).getConstructor().newInstance(); |
| 65 | + System.out.println("Service subject: " + doInitialLogin()); |
| 66 | + |
| 67 | + // Get impersonated user credentials thanks S4U2self mechanism |
| 68 | + GSSCredential impersonatedUserCreds = impersonate(); |
| 69 | + System.out.println("Credentials for " + TARGET_USER_NAME + ": " + impersonatedUserCreds); |
| 70 | + |
| 71 | + // Create a connection for target service thanks S4U2proxy mechanism |
| 72 | + try (Connection con = createConnection(impersonatedUserCreds)) { |
| 73 | + System.out.println("Connection succesfully: " + con); |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * |
| 80 | + * Authenticate the intermediate server that is going to impersonate the client |
| 81 | + * |
| 82 | + * @return a subject for the intermediate server with the keytab credentials |
| 83 | + * @throws PrivilegedActionException in case of failure |
| 84 | + */ |
| 85 | + private static Subject doInitialLogin() throws PrivilegedActionException { |
| 86 | + serviceSubject = new Subject(); |
| 87 | + |
| 88 | + LoginModule krb5Module; |
| 89 | + try { |
| 90 | + krb5Module = (LoginModule) Class.forName("com.sun.security.auth.module.Krb5LoginModule").getConstructor() |
| 91 | + .newInstance(); |
| 92 | + } catch (Exception e) { |
| 93 | + System.out.print("Error loading Krb5LoginModule module: " + e); |
| 94 | + throw new PrivilegedActionException(e); |
| 95 | + } |
| 96 | + |
| 97 | + System.setProperty("sun.security.krb5.debug", String.valueOf(true)); |
| 98 | + |
| 99 | + Map<String, String> options = new HashMap<>(); |
| 100 | + options.put("useKeyTab", "true"); |
| 101 | + options.put("storeKey", "true"); |
| 102 | + options.put("doNotPrompt", "true"); |
| 103 | + options.put("keyTab", KEYTAB_ROUTE); |
| 104 | + options.put("principal", SERVICE_PRINCIPAL); |
| 105 | + options.put("debug", "true"); |
| 106 | + options.put("isInitiator", "true"); |
| 107 | + |
| 108 | + Map<String, String> sharedState = new HashMap<>(0); |
| 109 | + |
| 110 | + krb5Module.initialize(serviceSubject, null, sharedState, options); |
| 111 | + try { |
| 112 | + krb5Module.login(); |
| 113 | + krb5Module.commit(); |
| 114 | + } catch (LoginException e) { |
| 115 | + System.out.print("Error authenticating with Kerberos: " + e); |
| 116 | + try { |
| 117 | + krb5Module.abort(); |
| 118 | + } catch (LoginException e1) { |
| 119 | + System.out.print("Error aborting Kerberos authentication: " + e1); |
| 120 | + throw new PrivilegedActionException(e); |
| 121 | + } |
| 122 | + throw new PrivilegedActionException(e); |
| 123 | + } |
| 124 | + |
| 125 | + return serviceSubject; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Generate the impersonated user credentials thanks to the S4U2self mechanism |
| 130 | + * |
| 131 | + * @return the client impersonated GSSCredential |
| 132 | + * @throws PrivilegedActionException in case of failure |
| 133 | + */ |
| 134 | + private static GSSCredential impersonate() throws PrivilegedActionException { |
| 135 | + return Subject.doAs(serviceSubject, (PrivilegedExceptionAction<GSSCredential>) () -> { |
| 136 | + GSSManager manager = GSSManager.getInstance(); |
| 137 | + |
| 138 | + GSSCredential self = manager.createCredential(null, GSSCredential.DEFAULT_LIFETIME, krb5Oid, |
| 139 | + GSSCredential.INITIATE_ONLY); |
| 140 | + GSSName user = manager.createName(TARGET_USER_NAME, GSSName.NT_USER_NAME); |
| 141 | + return ((ExtendedGSSCredential) self).impersonate(user); |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Obtains a connection using an impersonated credential |
| 147 | + * |
| 148 | + * @param impersonatedUserCredential impersonated user credentials |
| 149 | + * @return a connection to the SQL Server opened using the given impersonated credential |
| 150 | + * @throws PrivilegedActionException in case of failure |
| 151 | + */ |
| 152 | + private static Connection createConnection(final GSSCredential impersonatedUserCredential) |
| 153 | + throws PrivilegedActionException { |
| 154 | + |
| 155 | + return Subject.doAs(new Subject(), (PrivilegedExceptionAction<Connection>) () -> { |
| 156 | + driverProperties.put("gsscredential", impersonatedUserCredential); |
| 157 | + return DriverManager.getConnection(CONNECTION_URI, driverProperties); |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments