1919import io .trino .filesystem .TrinoFileSystemFactory ;
2020import io .trino .plugin .iceberg .IcebergFileSystemFactory ;
2121import io .trino .spi .security .ConnectorIdentity ;
22+ import org .apache .iceberg .util .PropertyUtil ;
2223
2324import java .util .Map ;
2425
26+ import static io .trino .filesystem .azure .AzureFileSystemConstants .EXTRA_SAS_TOKEN_PROPERTY_PREFIX ;
2527import static io .trino .filesystem .s3 .S3FileSystemConstants .EXTRA_CREDENTIALS_ACCESS_KEY_PROPERTY ;
2628import static io .trino .filesystem .s3 .S3FileSystemConstants .EXTRA_CREDENTIALS_SECRET_KEY_PROPERTY ;
2729import static io .trino .filesystem .s3 .S3FileSystemConstants .EXTRA_CREDENTIALS_SESSION_TOKEN_PROPERTY ;
@@ -34,6 +36,8 @@ public class IcebergRestCatalogFileSystemFactory
3436 private static final String VENDED_S3_SECRET_KEY = "s3.secret-access-key" ;
3537 private static final String VENDED_S3_SESSION_TOKEN = "s3.session-token" ;
3638
39+ private static final String VENDED_ADLS_SAS_TOKEN_PREFIX = "adls.sas-token." ;
40+
3741 private final TrinoFileSystemFactory fileSystemFactory ;
3842 private final boolean vendedCredentialsEnabled ;
3943
@@ -47,25 +51,54 @@ public IcebergRestCatalogFileSystemFactory(TrinoFileSystemFactory fileSystemFact
4751 @ Override
4852 public TrinoFileSystem create (ConnectorIdentity identity , Map <String , String > fileIoProperties )
4953 {
50- if (vendedCredentialsEnabled &&
51- fileIoProperties .containsKey (VENDED_S3_ACCESS_KEY ) &&
52- fileIoProperties .containsKey (VENDED_S3_SECRET_KEY ) &&
53- fileIoProperties .containsKey (VENDED_S3_SESSION_TOKEN )) {
54- // Do not include original credentials as they should not be used in vended mode
55- ConnectorIdentity identityWithExtraCredentials = ConnectorIdentity .forUser (identity .getUser ())
56- .withGroups (identity .getGroups ())
57- .withPrincipal (identity .getPrincipal ())
58- .withEnabledSystemRoles (identity .getEnabledSystemRoles ())
59- .withConnectorRole (identity .getConnectorRole ())
60- .withExtraCredentials (ImmutableMap .<String , String >builder ()
61- .put (EXTRA_CREDENTIALS_ACCESS_KEY_PROPERTY , fileIoProperties .get (VENDED_S3_ACCESS_KEY ))
62- .put (EXTRA_CREDENTIALS_SECRET_KEY_PROPERTY , fileIoProperties .get (VENDED_S3_SECRET_KEY ))
63- .put (EXTRA_CREDENTIALS_SESSION_TOKEN_PROPERTY , fileIoProperties .get (VENDED_S3_SESSION_TOKEN ))
64- .buildOrThrow ())
65- .build ();
66- return fileSystemFactory .create (identityWithExtraCredentials );
54+ if (vendedCredentialsEnabled ) {
55+ ImmutableMap .Builder <String , String > overriddenCredentialsBuilder = ImmutableMap .builder ();
56+
57+ if (fileIoProperties .containsKey (VENDED_S3_ACCESS_KEY ) &&
58+ fileIoProperties .containsKey (VENDED_S3_SECRET_KEY ) &&
59+ fileIoProperties .containsKey (VENDED_S3_SESSION_TOKEN )) {
60+ // S3 vended credentials
61+ overriddenCredentialsBuilder
62+ .put (EXTRA_CREDENTIALS_ACCESS_KEY_PROPERTY , fileIoProperties .get (VENDED_S3_ACCESS_KEY ))
63+ .put (EXTRA_CREDENTIALS_SECRET_KEY_PROPERTY , fileIoProperties .get (VENDED_S3_SECRET_KEY ))
64+ .put (EXTRA_CREDENTIALS_SESSION_TOKEN_PROPERTY , fileIoProperties .get (VENDED_S3_SESSION_TOKEN ));
65+ }
66+ else {
67+ // Azure vended credentials
68+ overriddenCredentialsBuilder .putAll (getAzureCredentials (fileIoProperties ));
69+ }
70+
71+ Map <String , String > overriddenCredentials = overriddenCredentialsBuilder .buildOrThrow ();
72+ if (!overriddenCredentials .isEmpty ()) {
73+ // Do not include original credentials as they should not be used in vended mode
74+ ConnectorIdentity identityWithExtraCredentials = ConnectorIdentity
75+ .forUser (identity .getUser ())
76+ .withGroups (identity .getGroups ())
77+ .withPrincipal (identity .getPrincipal ())
78+ .withEnabledSystemRoles (identity .getEnabledSystemRoles ())
79+ .withConnectorRole (identity .getConnectorRole ())
80+ .withExtraCredentials (overriddenCredentials ).build ();
81+
82+ return fileSystemFactory .create (identityWithExtraCredentials );
83+ }
6784 }
6885
6986 return fileSystemFactory .create (identity );
7087 }
88+
89+ private static Map <String , String > getAzureCredentials (Map <String , String > fileIoProperties )
90+ {
91+ ImmutableMap .Builder <String , String > azureCredentialBuilder = ImmutableMap .builder ();
92+
93+ PropertyUtil .propertiesWithPrefix (fileIoProperties , VENDED_ADLS_SAS_TOKEN_PREFIX )
94+ .forEach ((host , token ) -> {
95+ String storageAccount = host .contains ("." ) ? host .substring (0 , host .indexOf ('.' )) : host ;
96+
97+ if (!storageAccount .isEmpty () && !token .isEmpty ()) {
98+ azureCredentialBuilder .put (EXTRA_SAS_TOKEN_PROPERTY_PREFIX + storageAccount , token );
99+ }
100+ });
101+
102+ return azureCredentialBuilder .build ();
103+ }
71104}
0 commit comments