1414package io .trino .filesystem .gcs ;
1515
1616import com .google .auth .Credentials ;
17+ import com .google .auth .oauth2 .AccessToken ;
18+ import com .google .auth .oauth2 .GoogleCredentials ;
1719import com .google .cloud .NoCredentials ;
1820import com .google .cloud .storage .Storage ;
21+ import com .google .common .collect .ImmutableMap ;
1922import io .trino .spi .security .ConnectorIdentity ;
2023import org .junit .jupiter .api .Test ;
2124
2225import static io .trino .filesystem .gcs .GcsFileSystemConfig .AuthType ;
26+ import static io .trino .filesystem .gcs .GcsFileSystemConstants .EXTRA_CREDENTIALS_GCS_NO_AUTH_PROPERTY ;
27+ import static io .trino .filesystem .gcs .GcsFileSystemConstants .EXTRA_CREDENTIALS_GCS_OAUTH_TOKEN_EXPIRES_AT_PROPERTY ;
28+ import static io .trino .filesystem .gcs .GcsFileSystemConstants .EXTRA_CREDENTIALS_GCS_OAUTH_TOKEN_PROPERTY ;
29+ import static io .trino .filesystem .gcs .GcsFileSystemConstants .EXTRA_CREDENTIALS_GCS_PROJECT_ID_PROPERTY ;
30+ import static io .trino .filesystem .gcs .GcsFileSystemConstants .EXTRA_CREDENTIALS_GCS_SERVICE_HOST_PROPERTY ;
31+ import static io .trino .filesystem .gcs .GcsFileSystemConstants .EXTRA_CREDENTIALS_GCS_USER_PROJECT_PROPERTY ;
2332import static org .assertj .core .api .Assertions .assertThat ;
2433
2534final class TestGcsStorageFactory
@@ -38,4 +47,159 @@ void testApplicationDefaultCredentials()
3847
3948 assertThat (actualCredentials ).isEqualTo (NoCredentials .getInstance ());
4049 }
50+
51+ @ Test
52+ void testVendedOAuthToken ()
53+ throws Exception
54+ {
55+ GcsFileSystemConfig config = new GcsFileSystemConfig ().setAuthType (AuthType .APPLICATION_DEFAULT );
56+ GcsStorageFactory storageFactory = new GcsStorageFactory (config , new ApplicationDefaultAuth ());
57+
58+ ConnectorIdentity identity = ConnectorIdentity .forUser ("test" )
59+ .withExtraCredentials (ImmutableMap .of (
60+ EXTRA_CREDENTIALS_GCS_OAUTH_TOKEN_PROPERTY , "ya29.test-token" ))
61+ .build ();
62+
63+ try (Storage storage = storageFactory .create (identity )) {
64+ Credentials credentials = storage .getOptions ().getCredentials ();
65+ assertThat (credentials ).isInstanceOf (GoogleCredentials .class );
66+ GoogleCredentials googleCredentials = (GoogleCredentials ) credentials ;
67+ AccessToken accessToken = googleCredentials .getAccessToken ();
68+ assertThat (accessToken ).isNotNull ();
69+ assertThat (accessToken .getTokenValue ()).isEqualTo ("ya29.test-token" );
70+ }
71+ }
72+
73+ @ Test
74+ void testVendedOAuthTokenWithExpiration ()
75+ throws Exception
76+ {
77+ GcsFileSystemConfig config = new GcsFileSystemConfig ().setAuthType (AuthType .APPLICATION_DEFAULT );
78+ GcsStorageFactory storageFactory = new GcsStorageFactory (config , new ApplicationDefaultAuth ());
79+
80+ ConnectorIdentity identity = ConnectorIdentity .forUser ("test" )
81+ .withExtraCredentials (ImmutableMap .of (
82+ EXTRA_CREDENTIALS_GCS_OAUTH_TOKEN_PROPERTY , "ya29.test-token" ,
83+ EXTRA_CREDENTIALS_GCS_OAUTH_TOKEN_EXPIRES_AT_PROPERTY , "1700000000000" ))
84+ .build ();
85+
86+ try (Storage storage = storageFactory .create (identity )) {
87+ Credentials credentials = storage .getOptions ().getCredentials ();
88+ assertThat (credentials ).isInstanceOf (GoogleCredentials .class );
89+ GoogleCredentials googleCredentials = (GoogleCredentials ) credentials ;
90+ AccessToken accessToken = googleCredentials .getAccessToken ();
91+ assertThat (accessToken ).isNotNull ();
92+ assertThat (accessToken .getTokenValue ()).isEqualTo ("ya29.test-token" );
93+ assertThat (accessToken .getExpirationTime ()).isNotNull ();
94+ assertThat (accessToken .getExpirationTime ().getTime ()).isEqualTo (1700000000000L );
95+ }
96+ }
97+
98+ @ Test
99+ void testVendedProjectId ()
100+ throws Exception
101+ {
102+ GcsFileSystemConfig config = new GcsFileSystemConfig ()
103+ .setAuthType (AuthType .APPLICATION_DEFAULT )
104+ .setProjectId ("static-project" );
105+ GcsStorageFactory storageFactory = new GcsStorageFactory (config , new ApplicationDefaultAuth ());
106+
107+ ConnectorIdentity identity = ConnectorIdentity .forUser ("test" )
108+ .withExtraCredentials (ImmutableMap .of (
109+ EXTRA_CREDENTIALS_GCS_OAUTH_TOKEN_PROPERTY , "ya29.test-token" ,
110+ EXTRA_CREDENTIALS_GCS_PROJECT_ID_PROPERTY , "vended-project" ))
111+ .build ();
112+
113+ try (Storage storage = storageFactory .create (identity )) {
114+ assertThat (storage .getOptions ().getProjectId ()).isEqualTo ("vended-project" );
115+ }
116+ }
117+
118+ @ Test
119+ void testVendedServiceHost ()
120+ throws Exception
121+ {
122+ GcsFileSystemConfig config = new GcsFileSystemConfig ()
123+ .setAuthType (AuthType .APPLICATION_DEFAULT );
124+ GcsStorageFactory storageFactory = new GcsStorageFactory (config , new ApplicationDefaultAuth ());
125+
126+ ConnectorIdentity identity = ConnectorIdentity .forUser ("test" )
127+ .withExtraCredentials (ImmutableMap .of (
128+ EXTRA_CREDENTIALS_GCS_OAUTH_TOKEN_PROPERTY , "ya29.test-token" ,
129+ EXTRA_CREDENTIALS_GCS_SERVICE_HOST_PROPERTY , "https://custom-storage.googleapis.com" ))
130+ .build ();
131+
132+ try (Storage storage = storageFactory .create (identity )) {
133+ assertThat (storage .getOptions ().getHost ()).isEqualTo ("https://custom-storage.googleapis.com" );
134+ }
135+ }
136+
137+ @ Test
138+ void testVendedNoAuth ()
139+ throws Exception
140+ {
141+ GcsFileSystemConfig config = new GcsFileSystemConfig ().setAuthType (AuthType .APPLICATION_DEFAULT );
142+ GcsStorageFactory storageFactory = new GcsStorageFactory (config , new ApplicationDefaultAuth ());
143+
144+ ConnectorIdentity identity = ConnectorIdentity .forUser ("test" )
145+ .withExtraCredentials (ImmutableMap .of (
146+ EXTRA_CREDENTIALS_GCS_NO_AUTH_PROPERTY , "true" ))
147+ .build ();
148+
149+ try (Storage storage = storageFactory .create (identity )) {
150+ assertThat (storage .getOptions ().getCredentials ()).isEqualTo (NoCredentials .getInstance ());
151+ }
152+ }
153+
154+ @ Test
155+ void testNoAuthTakesPriorityOverOAuthToken ()
156+ throws Exception
157+ {
158+ GcsFileSystemConfig config = new GcsFileSystemConfig ().setAuthType (AuthType .APPLICATION_DEFAULT );
159+ GcsStorageFactory storageFactory = new GcsStorageFactory (config , new ApplicationDefaultAuth ());
160+
161+ ConnectorIdentity identity = ConnectorIdentity .forUser ("test" )
162+ .withExtraCredentials (ImmutableMap .of (
163+ EXTRA_CREDENTIALS_GCS_NO_AUTH_PROPERTY , "true" ,
164+ EXTRA_CREDENTIALS_GCS_OAUTH_TOKEN_PROPERTY , "ya29.test-token" ))
165+ .build ();
166+
167+ try (Storage storage = storageFactory .create (identity )) {
168+ assertThat (storage .getOptions ().getCredentials ()).isEqualTo (NoCredentials .getInstance ());
169+ }
170+ }
171+
172+ @ Test
173+ void testVendedUserProject ()
174+ throws Exception
175+ {
176+ GcsFileSystemConfig config = new GcsFileSystemConfig ()
177+ .setAuthType (AuthType .APPLICATION_DEFAULT );
178+ GcsStorageFactory storageFactory = new GcsStorageFactory (config , new ApplicationDefaultAuth ());
179+
180+ ConnectorIdentity identity = ConnectorIdentity .forUser ("test" )
181+ .withExtraCredentials (ImmutableMap .of (
182+ EXTRA_CREDENTIALS_GCS_OAUTH_TOKEN_PROPERTY , "ya29.test-token" ,
183+ EXTRA_CREDENTIALS_GCS_USER_PROJECT_PROPERTY , "billing-project" ))
184+ .build ();
185+
186+ try (Storage storage = storageFactory .create (identity )) {
187+ assertThat (storage .getOptions ().getQuotaProjectId ()).isEqualTo ("billing-project" );
188+ }
189+ }
190+
191+ @ Test
192+ void testStaticConfigUsedWithoutVendedCredentials ()
193+ throws Exception
194+ {
195+ GcsFileSystemConfig config = new GcsFileSystemConfig ()
196+ .setAuthType (AuthType .APPLICATION_DEFAULT )
197+ .setProjectId ("static-project" );
198+ GcsStorageFactory storageFactory = new GcsStorageFactory (config , new ApplicationDefaultAuth ());
199+
200+ try (Storage storage = storageFactory .create (ConnectorIdentity .ofUser ("test" ))) {
201+ assertThat (storage .getOptions ().getProjectId ()).isEqualTo ("static-project" );
202+ assertThat (storage .getOptions ().getCredentials ()).isEqualTo (NoCredentials .getInstance ());
203+ }
204+ }
41205}
0 commit comments