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 ;
25+ import java .util .Optional ;
2426
27+ import static io .trino .filesystem .azure .AzureFileSystemConstants .EXTRA_SAS_TOKEN_PROPERTY_PREFIX ;
28+ import static io .trino .filesystem .azure .AzureFileSystemConstants .EXTRA_USE_VENDED_TOKEN ;
2529import static io .trino .filesystem .s3 .S3FileSystemConstants .EXTRA_CREDENTIALS_ACCESS_KEY_PROPERTY ;
2630import static io .trino .filesystem .s3 .S3FileSystemConstants .EXTRA_CREDENTIALS_SECRET_KEY_PROPERTY ;
2731import static io .trino .filesystem .s3 .S3FileSystemConstants .EXTRA_CREDENTIALS_SESSION_TOKEN_PROPERTY ;
@@ -34,6 +38,8 @@ public class IcebergRestCatalogFileSystemFactory
3438 private static final String VENDED_S3_SECRET_KEY = "s3.secret-access-key" ;
3539 private static final String VENDED_S3_SESSION_TOKEN = "s3.session-token" ;
3640
41+ private static final String VENDED_ADLS_SAS_TOKEN_PREFIX = "adls.sas-token." ;
42+
3743 private final TrinoFileSystemFactory fileSystemFactory ;
3844 private final boolean vendedCredentialsEnabled ;
3945
@@ -47,25 +53,56 @@ public IcebergRestCatalogFileSystemFactory(TrinoFileSystemFactory fileSystemFact
4753 @ Override
4854 public TrinoFileSystem create (ConnectorIdentity identity , Map <String , String > fileIoProperties )
4955 {
50- if (vendedCredentialsEnabled &&
51- fileIoProperties .containsKey (VENDED_S3_ACCESS_KEY ) &&
56+ if (vendedCredentialsEnabled ) {
57+ return fileSystemFactory .create (
58+ getVendedS3Identity (identity , fileIoProperties )
59+ .or (() -> getVendedAzureIdentity (identity , fileIoProperties ))
60+ .orElse (identity ));
61+ }
62+
63+ return fileSystemFactory .create (identity );
64+ }
65+
66+ private static Optional <ConnectorIdentity > getVendedS3Identity (ConnectorIdentity identity , Map <String , String > fileIoProperties )
67+ {
68+ if (fileIoProperties .containsKey (VENDED_S3_ACCESS_KEY ) &&
5269 fileIoProperties .containsKey (VENDED_S3_SECRET_KEY ) &&
5370 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 );
71+ return Optional .of (getVendedIdentity (identity , ImmutableMap .<String , String >builder ()
72+ .put (EXTRA_CREDENTIALS_ACCESS_KEY_PROPERTY , fileIoProperties .get (VENDED_S3_ACCESS_KEY ))
73+ .put (EXTRA_CREDENTIALS_SECRET_KEY_PROPERTY , fileIoProperties .get (VENDED_S3_SECRET_KEY ))
74+ .put (EXTRA_CREDENTIALS_SESSION_TOKEN_PROPERTY , fileIoProperties .get (VENDED_S3_SESSION_TOKEN ))
75+ .buildOrThrow ()));
6776 }
77+ return Optional .empty ();
78+ }
6879
69- return fileSystemFactory .create (identity );
80+ private static Optional <ConnectorIdentity > getVendedAzureIdentity (ConnectorIdentity identity , Map <String , String > fileIoProperties )
81+ {
82+ ImmutableMap .Builder <String , String > azureCredentialBuilder = ImmutableMap .builder ();
83+ PropertyUtil .propertiesWithPrefix (fileIoProperties , VENDED_ADLS_SAS_TOKEN_PREFIX )
84+ .forEach ((host , token ) -> {
85+ String storageAccount = host .contains ("." ) ? host .substring (0 , host .indexOf ('.' )) : host ;
86+
87+ if (!storageAccount .isEmpty () && !token .isEmpty ()) {
88+ azureCredentialBuilder .put (EXTRA_SAS_TOKEN_PROPERTY_PREFIX + storageAccount , token );
89+ azureCredentialBuilder .put (EXTRA_USE_VENDED_TOKEN , "true" );
90+ }
91+ });
92+
93+ Map <String , String > azureCredentials = azureCredentialBuilder .buildKeepingLast ();
94+ return azureCredentials .isEmpty () ? Optional .empty () : Optional .of (getVendedIdentity (identity , azureCredentials ));
95+ }
96+
97+ private static ConnectorIdentity getVendedIdentity (ConnectorIdentity identity , Map <String , String > extraCredentials )
98+ {
99+ // Do not include original credentials as they should not be used in vended mode
100+ return ConnectorIdentity .forUser (identity .getUser ())
101+ .withGroups (identity .getGroups ())
102+ .withPrincipal (identity .getPrincipal ())
103+ .withEnabledSystemRoles (identity .getEnabledSystemRoles ())
104+ .withConnectorRole (identity .getConnectorRole ())
105+ .withExtraCredentials (extraCredentials )
106+ .build ();
70107 }
71108}
0 commit comments