|
22 | 22 | import org.apache.iceberg.util.PropertyUtil; |
23 | 23 |
|
24 | 24 | import java.util.Map; |
| 25 | +import java.util.Optional; |
25 | 26 |
|
26 | 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; |
27 | 29 | import static io.trino.filesystem.s3.S3FileSystemConstants.EXTRA_CREDENTIALS_ACCESS_KEY_PROPERTY; |
28 | 30 | import static io.trino.filesystem.s3.S3FileSystemConstants.EXTRA_CREDENTIALS_SECRET_KEY_PROPERTY; |
29 | 31 | import static io.trino.filesystem.s3.S3FileSystemConstants.EXTRA_CREDENTIALS_SESSION_TOKEN_PROPERTY; |
@@ -52,53 +54,55 @@ public IcebergRestCatalogFileSystemFactory(TrinoFileSystemFactory fileSystemFact |
52 | 54 | public TrinoFileSystem create(ConnectorIdentity identity, Map<String, String> fileIoProperties) |
53 | 55 | { |
54 | 56 | 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 | | - } |
| 57 | + return fileSystemFactory.create( |
| 58 | + getVendedS3Identity(identity, fileIoProperties) |
| 59 | + .or(() -> getVendedAzureIdentity(identity, fileIoProperties)) |
| 60 | + .orElse(identity)); |
84 | 61 | } |
85 | 62 |
|
86 | 63 | return fileSystemFactory.create(identity); |
87 | 64 | } |
88 | 65 |
|
89 | | - private static Map<String, String> getAzureCredentials(Map<String, String> fileIoProperties) |
| 66 | + private static Optional<ConnectorIdentity> getVendedS3Identity(ConnectorIdentity identity, Map<String, String> fileIoProperties) |
90 | 67 | { |
91 | | - ImmutableMap.Builder<String, String> azureCredentialBuilder = ImmutableMap.builder(); |
| 68 | + if (fileIoProperties.containsKey(VENDED_S3_ACCESS_KEY) && |
| 69 | + fileIoProperties.containsKey(VENDED_S3_SECRET_KEY) && |
| 70 | + fileIoProperties.containsKey(VENDED_S3_SESSION_TOKEN)) { |
| 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())); |
| 76 | + } |
| 77 | + return Optional.empty(); |
| 78 | + } |
92 | 79 |
|
| 80 | + private static Optional<ConnectorIdentity> getVendedAzureIdentity(ConnectorIdentity identity, Map<String, String> fileIoProperties) |
| 81 | + { |
| 82 | + ImmutableMap.Builder<String, String> azureCredentialBuilder = ImmutableMap.builder(); |
93 | 83 | PropertyUtil.propertiesWithPrefix(fileIoProperties, VENDED_ADLS_SAS_TOKEN_PREFIX) |
94 | 84 | .forEach((host, token) -> { |
95 | 85 | String storageAccount = host.contains(".") ? host.substring(0, host.indexOf('.')) : host; |
96 | 86 |
|
97 | 87 | if (!storageAccount.isEmpty() && !token.isEmpty()) { |
98 | 88 | azureCredentialBuilder.put(EXTRA_SAS_TOKEN_PROPERTY_PREFIX + storageAccount, token); |
| 89 | + azureCredentialBuilder.put(EXTRA_USE_VENDED_TOKEN, "true"); |
99 | 90 | } |
100 | 91 | }); |
101 | 92 |
|
102 | | - return azureCredentialBuilder.build(); |
| 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(); |
103 | 107 | } |
104 | 108 | } |
0 commit comments