|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.polaris.core.storage.cache; |
| 20 | + |
| 21 | +import static org.apache.polaris.core.storage.PolarisCredentialVendor.*; |
| 22 | + |
| 23 | +import java.util.EnumMap; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.Stream; |
| 26 | +import org.apache.iceberg.aws.s3.S3FileIOProperties; |
| 27 | +import org.apache.iceberg.azure.AzureProperties; |
| 28 | +import org.apache.iceberg.gcp.GCPProperties; |
| 29 | +import org.apache.polaris.core.persistence.*; |
| 30 | +import org.apache.polaris.core.storage.PolarisCredentialProperty; |
| 31 | +import org.assertj.core.api.Assertions; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.params.ParameterizedTest; |
| 34 | +import org.junit.jupiter.params.provider.Arguments; |
| 35 | +import org.junit.jupiter.params.provider.MethodSource; |
| 36 | + |
| 37 | +public class StorageCredentialCacheEntryTest { |
| 38 | + |
| 39 | + public StorageCredentialCacheEntryTest() {} |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testBadResult() { |
| 43 | + ScopedCredentialsResult badResult = |
| 44 | + new ScopedCredentialsResult( |
| 45 | + BaseResult.ReturnStatus.SUBSCOPE_CREDS_ERROR, "extra_error_info"); |
| 46 | + StorageCredentialCacheEntry cacheEntry = new StorageCredentialCacheEntry(badResult); |
| 47 | + Assertions.assertThatThrownBy(() -> cacheEntry.convertToMapOfString()) |
| 48 | + .isInstanceOf(NullPointerException.class) |
| 49 | + .hasMessageContaining("because \"this.credsMap\" is null"); |
| 50 | + } |
| 51 | + |
| 52 | + @ParameterizedTest |
| 53 | + @MethodSource("testConvertToMapOfStringParams") |
| 54 | + public void testConvertToMapOfString( |
| 55 | + Map<PolarisCredentialProperty, String> original, Map<String, String> expected) { |
| 56 | + EnumMap<PolarisCredentialProperty, String> props = |
| 57 | + new EnumMap<>(PolarisCredentialProperty.class); |
| 58 | + props.putAll(original); |
| 59 | + |
| 60 | + ScopedCredentialsResult goodResult = new ScopedCredentialsResult(props); |
| 61 | + StorageCredentialCacheEntry cacheEntry = new StorageCredentialCacheEntry(goodResult); |
| 62 | + Map<String, String> properties = cacheEntry.convertToMapOfString(); |
| 63 | + Assertions.assertThat(properties).isEqualTo(expected); |
| 64 | + System.out.println(properties); |
| 65 | + } |
| 66 | + |
| 67 | + private static Stream<Arguments> testConvertToMapOfStringParams() { |
| 68 | + return Stream.of( |
| 69 | + Arguments.of( |
| 70 | + Map.of( |
| 71 | + PolarisCredentialProperty.AWS_KEY_ID, |
| 72 | + "aws_key_id", |
| 73 | + PolarisCredentialProperty.AWS_SECRET_KEY, |
| 74 | + "aws_secret_access_key", |
| 75 | + PolarisCredentialProperty.AWS_TOKEN, |
| 76 | + "aws_session_token"), |
| 77 | + Map.of( |
| 78 | + S3FileIOProperties.ACCESS_KEY_ID, |
| 79 | + "aws_key_id", |
| 80 | + S3FileIOProperties.SECRET_ACCESS_KEY, |
| 81 | + "aws_secret_access_key", |
| 82 | + S3FileIOProperties.SESSION_TOKEN, |
| 83 | + "aws_session_token")), |
| 84 | + Arguments.of( |
| 85 | + Map.of( |
| 86 | + PolarisCredentialProperty.GCS_ACCESS_TOKEN, |
| 87 | + "gcs_oauth2_token", |
| 88 | + PolarisCredentialProperty.GCS_ACCESS_TOKEN_EXPIRES_AT, |
| 89 | + "gcs_token_expires_at"), |
| 90 | + Map.of( |
| 91 | + GCPProperties.GCS_OAUTH2_TOKEN, |
| 92 | + "gcs_oauth2_token", |
| 93 | + GCPProperties.GCS_OAUTH2_TOKEN_EXPIRES_AT, |
| 94 | + "gcs_token_expires_at")), |
| 95 | + Arguments.of( |
| 96 | + Map.of( |
| 97 | + PolarisCredentialProperty.AZURE_ACCOUNT_HOST, |
| 98 | + "azure_storage_account", |
| 99 | + PolarisCredentialProperty.AZURE_SAS_TOKEN, |
| 100 | + "azure_sas_token"), |
| 101 | + Map.of( |
| 102 | + AzureProperties.ADLS_SAS_TOKEN_PREFIX + "azure_storage_account", |
| 103 | + "azure_sas_token")), |
| 104 | + Arguments.of( |
| 105 | + Map.of(PolarisCredentialProperty.AZURE_ACCESS_TOKEN, "azure_access_token"), |
| 106 | + Map.of("", "azure_access_token")), |
| 107 | + Arguments.of( |
| 108 | + Map.of(PolarisCredentialProperty.EXPIRATION_TIME, "expiration_time"), |
| 109 | + Map.of("expiration-time", "expiration_time")), |
| 110 | + Arguments.of( |
| 111 | + Map.of( |
| 112 | + PolarisCredentialProperty.ADDITIONAL_STORAGE_CONFIG, |
| 113 | + "{\"s3.session-token-expires-at-ms\": \"expires_at\"}"), |
| 114 | + Map.of( |
| 115 | + "additional-storage-config", |
| 116 | + "{\"s3.session-token-expires-at-ms\": \"expires_at\"}")), |
| 117 | + Arguments.of(Map.of(), Map.of())); |
| 118 | + } |
| 119 | +} |
0 commit comments