-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement query rules in file based system access control
- Loading branch information
Showing
7 changed files
with
289 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"catalogs": [ | ||
{ | ||
"allow": true | ||
} | ||
], | ||
"queries": [ | ||
{ | ||
"user": "admin", | ||
"allow": ["execute", "kill", "view"] | ||
}, | ||
{ | ||
"user": "alice", | ||
"allow": ["execute", "kill"] | ||
}, | ||
{ | ||
"allow": ["execute"] | ||
} | ||
] | ||
} |
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
100 changes: 100 additions & 0 deletions
100
presto-plugin-toolkit/src/main/java/io/prestosql/plugin/base/security/QueryAccessRule.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,100 @@ | ||
/* | ||
* Licensed 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 io.prestosql.plugin.base.security; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
import com.google.common.collect.ImmutableSet; | ||
|
||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
|
||
import static com.google.common.base.MoreObjects.toStringHelper; | ||
import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
import static java.util.Arrays.stream; | ||
import static java.util.Objects.requireNonNull; | ||
import static java.util.function.Function.identity; | ||
|
||
public class QueryAccessRule | ||
{ | ||
private final Set<AccessMode> allow; | ||
private final Optional<Pattern> userRegex; | ||
|
||
@JsonCreator | ||
public QueryAccessRule( | ||
@JsonProperty("allow") Set<AccessMode> allow, | ||
@JsonProperty("user") Optional<Pattern> userRegex) | ||
{ | ||
this.allow = ImmutableSet.copyOf(requireNonNull(allow, "allow is null")); | ||
this.userRegex = requireNonNull(userRegex, "userRegex is null"); | ||
} | ||
|
||
public Optional<Set<AccessMode>> match(String user) | ||
{ | ||
if (userRegex.map(regex -> regex.matcher(user).matches()).orElse(true)) { | ||
return Optional.of(allow); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return toStringHelper(this) | ||
.omitNullValues() | ||
.add("allow", allow) | ||
.add("userRegex", userRegex.orElse(null)) | ||
.toString(); | ||
} | ||
|
||
public enum AccessMode | ||
{ | ||
EXECUTE("execute"), | ||
VIEW("view"), | ||
KILL("kill"); | ||
|
||
private static final Map<String, AccessMode> modeByName = stream(AccessMode.values()).collect(toImmutableMap(AccessMode::toString, identity())); | ||
|
||
private final String stringValue; | ||
|
||
AccessMode(String stringValue) | ||
{ | ||
this.stringValue = requireNonNull(stringValue, "stringValue is null"); | ||
} | ||
|
||
@JsonValue | ||
@Override | ||
public String toString() | ||
{ | ||
return stringValue; | ||
} | ||
|
||
@JsonCreator | ||
public static AccessMode fromJson(Object value) | ||
{ | ||
if (value instanceof String) { | ||
AccessMode accessMode = modeByName.get(((String) value).toLowerCase(Locale.US)); | ||
if (accessMode != null) { | ||
return accessMode; | ||
} | ||
} | ||
|
||
throw new IllegalArgumentException("Unknown " + AccessMode.class.getSimpleName() + ": " + value); | ||
} | ||
} | ||
} |
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.