-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CALCITE-5681] Support authorization via GRANT and REVOKE DDL commands #3726
Open
macroguo-ghy
wants to merge
1
commit into
apache:main
Choose a base branch
from
macroguo-ghy:auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
69 changes: 69 additions & 0 deletions
69
core/src/main/java/org/apache/calcite/access/CalcitePrincipal.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,69 @@ | ||
/* | ||
* 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.calcite.access; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
|
||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
import java.security.Principal; | ||
import java.util.Locale; | ||
|
||
import static org.apache.calcite.runtime.Utilities.hash; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* CalcitePrincipal is a simple implementation of {@link Principal} | ||
* that represents a user. CalcitePrincipal ignore the case of the | ||
* name when comparing two principals. | ||
*/ | ||
public class CalcitePrincipal implements Principal, Comparable<CalcitePrincipal> { | ||
|
||
private final String name; | ||
|
||
public CalcitePrincipal(String name) { | ||
this.name = requireNonNull(name, "name").toUpperCase(Locale.ROOT); | ||
} | ||
|
||
@Override public String getName() { | ||
return this.name; | ||
} | ||
|
||
@Override public String toString() { | ||
return this.name; | ||
} | ||
|
||
@Override public boolean equals(@Nullable Object o) { | ||
if (this == o) { | ||
return true; | ||
} else if (o != null && this.getClass() == o.getClass()) { | ||
CalcitePrincipal that = (CalcitePrincipal) o; | ||
return StringUtils.equalsIgnoreCase(this.name, that.name); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override public int hashCode() { | ||
return hash(this.name); | ||
} | ||
|
||
@Override public int compareTo(CalcitePrincipal o) { | ||
return String.CASE_INSENSITIVE_ORDER.compare(this.name, o.name); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
core/src/main/java/org/apache/calcite/access/CalcitePrincipalFactory.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,48 @@ | ||
/* | ||
* 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.calcite.access; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
|
||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
/** | ||
* Factory for {@link CalcitePrincipal}. | ||
*/ | ||
public class CalcitePrincipalFactory { | ||
|
||
public static final CalcitePrincipalFactory INSTANCE = new CalcitePrincipalFactory(); | ||
|
||
private final LoadingCache<String, CalcitePrincipal> cache = | ||
CacheBuilder.newBuilder().build(CacheLoader.from(CalcitePrincipalFactory::createInternal)); | ||
|
||
private CalcitePrincipalFactory() { | ||
} | ||
|
||
private static CalcitePrincipal createInternal(String name) { | ||
return new CalcitePrincipal(name); | ||
} | ||
|
||
public static @Nullable CalcitePrincipal create(@Nullable String name) { | ||
if (name == null) { | ||
return null; | ||
} | ||
return INSTANCE.cache.getUnchecked(name); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
core/src/main/java/org/apache/calcite/access/package-info.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,28 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Calcite Access Control. | ||
*/ | ||
@DefaultQualifier(value = NonNull.class, locations = TypeUseLocation.FIELD) | ||
@DefaultQualifier(value = NonNull.class, locations = TypeUseLocation.PARAMETER) | ||
@DefaultQualifier(value = NonNull.class, locations = TypeUseLocation.RETURN) | ||
package org.apache.calcite.access; | ||
|
||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.checkerframework.framework.qual.DefaultQualifier; | ||
import org.checkerframework.framework.qual.TypeUseLocation; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
import org.apache.calcite.schema.Wrapper; | ||
import org.apache.calcite.schema.impl.MaterializedViewTable; | ||
import org.apache.calcite.schema.impl.StarTable; | ||
import org.apache.calcite.sql.SqlAccessType; | ||
import org.apache.calcite.util.NameMap; | ||
import org.apache.calcite.util.NameMultimap; | ||
import org.apache.calcite.util.NameSet; | ||
|
@@ -40,8 +41,10 @@ | |
|
||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
import java.security.Principal; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.NavigableMap; | ||
|
@@ -71,6 +74,9 @@ public abstract class CalciteSchema { | |
protected final NameSet functionNames; | ||
protected final NameMap<FunctionEntry> nullaryFunctionMap; | ||
protected final NameMap<CalciteSchema> subSchemaMap; | ||
|
||
// tableName -> (user, access) | ||
protected final NameMap<Map<Principal, SqlAccessType>> privilegeMap; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whether to use a separate class to maintain the relationship between |
||
private @Nullable List<? extends List<String>> path; | ||
|
||
protected CalciteSchema(@Nullable CalciteSchema parent, Schema schema, | ||
|
@@ -82,6 +88,7 @@ protected CalciteSchema(@Nullable CalciteSchema parent, Schema schema, | |
@Nullable NameMultimap<FunctionEntry> functionMap, | ||
@Nullable NameSet functionNames, | ||
@Nullable NameMap<FunctionEntry> nullaryFunctionMap, | ||
@Nullable NameMap<Map<Principal, SqlAccessType>> privilegeMap, | ||
@Nullable List<? extends List<String>> path) { | ||
this.parent = parent; | ||
this.schema = schema; | ||
|
@@ -106,6 +113,7 @@ protected CalciteSchema(@Nullable CalciteSchema parent, Schema schema, | |
} else { | ||
this.typeMap = typeMap; | ||
} | ||
this.privilegeMap = privilegeMap != null ? privilegeMap : new NameMap<>(); | ||
this.path = path; | ||
} | ||
|
||
|
@@ -543,6 +551,47 @@ public boolean removeType(String name) { | |
return typeMap.remove(name) != null; | ||
} | ||
|
||
public void grant(String name, Principal p, SqlAccessType grant, boolean caseSensitive) { | ||
for (Map.Entry<String, Map<Principal, SqlAccessType>> entry | ||
: privilegeMap.range(name, caseSensitive).entrySet()) { | ||
final Map<Principal, SqlAccessType> tablePrivileges = entry.getValue(); | ||
final SqlAccessType accessType = | ||
tablePrivileges.getOrDefault(p, SqlAccessType.createNoneAccess()); | ||
accessType.add(grant); | ||
tablePrivileges.put(p, accessType); | ||
return; | ||
} | ||
|
||
final Map<Principal, SqlAccessType> tablePrivileges = new HashMap<>(); | ||
tablePrivileges.put(p, grant); | ||
privilegeMap.put(name, tablePrivileges); | ||
} | ||
|
||
public void revoke(String name, Principal p, SqlAccessType revoke, boolean caseSensitive) { | ||
for (Map.Entry<String, Map<Principal, SqlAccessType>> entry | ||
: privilegeMap.range(name, caseSensitive).entrySet()) { | ||
final Map<Principal, SqlAccessType> tablePrivileges = entry.getValue(); | ||
final SqlAccessType accessType = tablePrivileges.get(p); | ||
if (accessType == null) { | ||
return; | ||
} | ||
accessType.remove(revoke); | ||
return; | ||
} | ||
} | ||
|
||
public SqlAccessType getAccessType(String name, Principal p, boolean caseSensitive) { | ||
for (Map.Entry<String, Map<Principal, SqlAccessType>> entry | ||
: privilegeMap.range(name, caseSensitive).entrySet()) { | ||
final Map<Principal, SqlAccessType> tablePrivileges = entry.getValue(); | ||
final SqlAccessType accessType = tablePrivileges.get(p); | ||
if (accessType != null) { | ||
return accessType; | ||
} | ||
} | ||
return SqlAccessType.createNoneAccess(); | ||
} | ||
|
||
/** | ||
* Entry in a schema, such as a table or sub-schema. | ||
* | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To refer to a user can use
UserPrincipal