-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct the handling of access delegation mode (#211)
- Loading branch information
Showing
11 changed files
with
181 additions
and
31 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
polaris-service/src/main/java/org/apache/polaris/service/catalog/AccessDelegationMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.polaris.service.catalog; | ||
|
||
import com.google.common.base.Functions; | ||
import java.util.Arrays; | ||
import java.util.EnumSet; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Represents access mechanisms defined in the Iceberg REST API specification (values for the {@code | ||
* X-Iceberg-Access-Delegation} header). | ||
*/ | ||
public enum AccessDelegationMode { | ||
UNKNOWN("unknown"), | ||
VENDED_CREDENTIALS("vended-credentials"), | ||
REMOTE_SIGNING("remote-signing"), | ||
; | ||
|
||
AccessDelegationMode(String protocolValue) { | ||
this.protocolValue = protocolValue; | ||
} | ||
|
||
private final String protocolValue; | ||
|
||
public String protocolValue() { | ||
return protocolValue; | ||
} | ||
|
||
public static EnumSet<AccessDelegationMode> fromProtocolValuesList(String protocolValues) { | ||
if (protocolValues == null || protocolValues.isEmpty()) { | ||
return EnumSet.noneOf(AccessDelegationMode.class); | ||
} | ||
|
||
// Backward-compatibility case for old clients that still use the unofficial value of `true` to | ||
// request credential vending. Note that if the client requests `true` among other values it | ||
// will be parsed as `UNKNOWN` (by the code below this `if`) since the client submitting | ||
// multiple access modes is expected to be aware of the Iceberg REST API spec. | ||
if (protocolValues.trim().toLowerCase(Locale.ROOT).equals("true")) { | ||
return EnumSet.of(VENDED_CREDENTIALS); | ||
} | ||
|
||
EnumSet<AccessDelegationMode> set = EnumSet.noneOf(AccessDelegationMode.class); | ||
Arrays.stream(protocolValues.split(",")) // per Iceberg REST Catalog spec | ||
.map(String::trim) | ||
.map(n -> Mapper.byProtocolValue.getOrDefault(n, UNKNOWN)) | ||
.forEach(set::add); | ||
return set; | ||
} | ||
|
||
private static class Mapper { | ||
private static final Map<String, AccessDelegationMode> byProtocolValue = | ||
Arrays.stream(AccessDelegationMode.values()) | ||
.collect(Collectors.toMap(AccessDelegationMode::protocolValue, Functions.identity())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...is-service/src/test/java/org/apache/polaris/service/catalog/AccessDelegationModeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.polaris.service.catalog; | ||
|
||
import static org.apache.polaris.service.catalog.AccessDelegationMode.*; | ||
import static org.apache.polaris.service.catalog.AccessDelegationMode.REMOTE_SIGNING; | ||
import static org.apache.polaris.service.catalog.AccessDelegationMode.VENDED_CREDENTIALS; | ||
import static org.apache.polaris.service.catalog.AccessDelegationMode.fromProtocolValuesList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.EnumSet; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
|
||
class AccessDelegationModeTest { | ||
|
||
@ParameterizedTest | ||
@EnumSource(AccessDelegationMode.class) | ||
void testSingle(AccessDelegationMode mode) { | ||
assertThat(fromProtocolValuesList(mode.protocolValue())).isEqualTo(EnumSet.of(mode)); | ||
} | ||
|
||
@Test | ||
void testSeveral() { | ||
assertThat(fromProtocolValuesList("vended-credentials, remote-signing")) | ||
.isEqualTo(EnumSet.of(VENDED_CREDENTIALS, REMOTE_SIGNING)); | ||
} | ||
|
||
@Test | ||
void testEmpty() { | ||
assertThat(fromProtocolValuesList(null)).isEqualTo(EnumSet.noneOf(AccessDelegationMode.class)); | ||
assertThat(fromProtocolValuesList("")).isEqualTo(EnumSet.noneOf(AccessDelegationMode.class)); | ||
} | ||
|
||
@Test | ||
void testUnknown() { | ||
assertThat(fromProtocolValuesList("abc")).isEqualTo(EnumSet.of(UNKNOWN)); | ||
assertThat(fromProtocolValuesList("abc,def")).isEqualTo(EnumSet.of(UNKNOWN)); | ||
assertThat(fromProtocolValuesList("abc,remote-signing")) | ||
.isEqualTo(EnumSet.of(REMOTE_SIGNING, UNKNOWN)); | ||
} | ||
|
||
@Test | ||
void testLegacy() { | ||
assertThat(fromProtocolValuesList("true")).isEqualTo(EnumSet.of(VENDED_CREDENTIALS)); | ||
assertThat(fromProtocolValuesList("true, vended-credentials")) | ||
.isEqualTo(EnumSet.of(UNKNOWN, VENDED_CREDENTIALS)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.