@@ -73,6 +73,67 @@ Note: a non-empty string value for the `password` property must be set. While th
7373be ignored when connecting with the Cloud SQL Connector using IAM auth, leaving it empty will cause
7474driver-level validations to fail.
7575
76+ ## Service Account Delegation
77+
78+ The Java Connector supports service account impersonation with the
79+ ` TARGET_PRINCIPAL ` option. When enabled, all API requests are made impersonating
80+ the supplied service account. The IAM principal must have the
81+ iam.serviceAccounts.getAccessToken permission or the role
82+ roles/iam.serviceAccounts.serviceAccountTokenCreator.
83+
84+ ``` java
85+ // Set up ConnectionFactoryOptions
86+ ConnectionFactoryOptions options = ConnectionFactoryOptions . builder()
87+ .option(DRIVER , " gcp" )
88+ .option(PROTOCOL , " postgresql" )
89+ .option(PASSWORD , " password" )
90+ .option(USER , " postgres-iam-user@gmail.com" )
91+ .option(DATABASE , " my_db" )
92+ .option(HOST , " project:region:instance" )
93+ .option(ENABLE_IAM_AUTH , true )
94+ .option(TARGET_PRINCIPAL , " postgres-iam-user@gmail.com,db-service-account@iam.gooogle.com" )
95+ .build();
96+
97+ // Initialize connection pool
98+ ConnectionFactory connectionFactory = ConnectionFactories . get(options);
99+ ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration
100+ .builder(connectionFactory)
101+ .build();
102+
103+ this . connectionPool = new ConnectionPool (configuration);
104+ ```
105+
106+ In addition, the ` DELEGATES ` option controls impersonation delegation.
107+ The value is a comma-separated list of service accounts containing chained
108+ list of delegates required to grant the final access_token. If set,
109+ the sequence of identities must have "Service Account Token Creator" capability
110+ granted to the preceding identity. For example, if set to
111+ ` "serviceAccountB,serviceAccountC" ` , the application default credentials must
112+ have the Token Creator role on serviceAccountB. serviceAccountB must have
113+ the Token Creator on serviceAccountC. Finally, C must have Token Creator on
114+ target principal. If unset, the application default credential principal
115+ must "Service Account Token Creator" capability granted that role on the
116+ target principal service account.
117+
118+
119+ For example:
120+ ``` java
121+ options. option(TARGET_PRINCIPAL , " TARGET_SERVICE_ACCOUNT" );
122+ options. option(DELEGATES , " SERVICE_ACCOUNT_1,SERVICE_ACCOUNT_2" );
123+ ```
124+
125+ In this example, the environment's application default principal impersonates
126+ SERVICE_ACCOUNT_1 which impersonates SERVICE_ACCOUNT_2 which then
127+ impersonates the TARGET_SERVICE_ACCOUNT.
128+
129+ In addition, the ` DELEGATES ` option supports an impersonation delegation chain
130+ where the value is a comma-separated list of service accounts. The first service
131+ account in the list is the impersonation target. Each subsequent service
132+ account is a delegate to the previous service account. When delegation is
133+ used, each delegate must have the permissions named above on the service
134+ account it is delegating to.
135+
136+
76137## Examples
77138
78139Examples for using the Cloud SQL JDBC Connector for Postgres can be found by looking at the integration tests in this repository.
0 commit comments