forked from StarRocks/starrocks
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for configurable external authorizer
- Loading branch information
1 parent
97fa5e9
commit 7c63e80
Showing
17 changed files
with
1,093 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
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
20 changes: 20 additions & 0 deletions
20
fe/fe-core/src/main/java/com/starrocks/privilege/external/AccessTypeConverter.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,20 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// https://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 com.starrocks.privilege.external; | ||
|
||
import com.starrocks.privilege.PrivilegeType; | ||
|
||
public interface AccessTypeConverter { | ||
String convertToAccessType(PrivilegeType privilegeType); | ||
} |
62 changes: 62 additions & 0 deletions
62
.../src/main/java/com/starrocks/privilege/external/ConfigurableExternalAccessController.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,62 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// https://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 com.starrocks.privilege.external; | ||
|
||
import com.starrocks.common.Config; | ||
import com.starrocks.privilege.AccessDeniedException; | ||
import com.starrocks.privilege.ExternalAccessController; | ||
import com.starrocks.privilege.PrivilegeType; | ||
import com.starrocks.privilege.external.ExternalAccessResourceImpl; | ||
import com.starrocks.privilege.external.ExternalStarRocksAccessRequest; | ||
import com.starrocks.sql.ast.UserIdentity; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public abstract class ConfigurableExternalAccessController extends ExternalAccessController implements AccessTypeConverter { | ||
private static final Logger LOG = LoggerFactory.getLogger(ConfigurableExternalAccessController.class); | ||
protected ExternalAuthorizer authorizer; | ||
|
||
public ConfigurableExternalAccessController() { | ||
super(false); | ||
authorizer = initExternalAuthorizer(); | ||
} | ||
|
||
private ExternalAuthorizer initExternalAuthorizer() { | ||
String externalAuthorizationClassName = Config.external_authorization_class_name; | ||
try { | ||
ExternalAuthorizer authorizer = Class.forName(externalAuthorizationClassName) | ||
.asSubclass(ExternalAuthorizer.class).newInstance(); | ||
authorizer.init(); | ||
return authorizer; | ||
} catch (Exception e) { | ||
LOG.error("Failed to create external authorizer", e); | ||
} | ||
return null; | ||
} | ||
|
||
protected void hasPermission(ExternalAccessResourceImpl resource, UserIdentity user, PrivilegeType privilegeType) | ||
throws AccessDeniedException { | ||
|
||
if (authorizer == null) { | ||
throw new AccessDeniedException("External authorizer is not initialized"); | ||
} | ||
|
||
String accessType = convertToAccessType(privilegeType); | ||
ExternalStarRocksAccessRequest request = new ExternalStarRocksAccessRequest(resource, user, accessType); | ||
|
||
if (!authorizer.authorize(request)) { | ||
throw new AccessDeniedException("Access denied for user " + user + " on " + resource + " with " + privilegeType); | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
fe/fe-core/src/main/java/com/starrocks/privilege/external/ExternalAccessResourceBuilder.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,99 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// https://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 com.starrocks.privilege.external; | ||
|
||
import com.starrocks.privilege.ObjectType; | ||
import com.starrocks.privilege.external.ExternalAccessResourceImpl; | ||
|
||
public abstract class ExternalAccessResourceBuilder implements ObjectTypeConverter { | ||
ExternalAccessResourceImpl externalAccessResource; | ||
|
||
public ExternalAccessResourceImpl build() { | ||
return externalAccessResource; | ||
} | ||
|
||
protected ExternalAccessResourceBuilder(ExternalAccessResourceImpl externalAccessResource) { | ||
this.externalAccessResource = externalAccessResource; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setSystem() { | ||
externalAccessResource.setValue(convertToExternalObjectType(ObjectType.SYSTEM), "*"); | ||
return this; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setUser(String user) { | ||
externalAccessResource.setValue(convertToExternalObjectType(ObjectType.USER), user); | ||
return this; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setCatalog(String catalog) { | ||
externalAccessResource.setValue(convertToExternalObjectType(ObjectType.CATALOG), catalog); | ||
return this; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setDatabase(String database) { | ||
externalAccessResource.setValue(convertToExternalObjectType(ObjectType.DATABASE), database); | ||
return this; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setTable(String table) { | ||
externalAccessResource.setValue(convertToExternalObjectType(ObjectType.TABLE), table); | ||
return this; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setColumn(String column) { | ||
externalAccessResource.setValue("column", column); | ||
return this; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setView(String view) { | ||
externalAccessResource.setValue(convertToExternalObjectType(ObjectType.VIEW), view); | ||
return this; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setMaterializedView(String materializedView) { | ||
externalAccessResource.setValue(convertToExternalObjectType(ObjectType.MATERIALIZED_VIEW), materializedView); | ||
return this; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setFunction(String function) { | ||
externalAccessResource.setValue(convertToExternalObjectType(ObjectType.FUNCTION), function); | ||
return this; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setGlobalFunction(String globalFunction) { | ||
externalAccessResource.setValue(convertToExternalObjectType(ObjectType.GLOBAL_FUNCTION), globalFunction); | ||
return this; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setResource(String resource) { | ||
externalAccessResource.setValue(convertToExternalObjectType(ObjectType.RESOURCE), resource); | ||
return this; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setResourceGroup(String resourceGroup) { | ||
externalAccessResource.setValue(convertToExternalObjectType(ObjectType.RESOURCE_GROUP), resourceGroup); | ||
return this; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setStorageVolume(String storageVolume) { | ||
externalAccessResource.setValue(convertToExternalObjectType(ObjectType.STORAGE_VOLUME), storageVolume); | ||
return this; | ||
} | ||
|
||
public ExternalAccessResourceBuilder setPipe(String pipe) { | ||
externalAccessResource.setValue(convertToExternalObjectType(ObjectType.PIPE), pipe); | ||
return this; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
fe/fe-core/src/main/java/com/starrocks/privilege/external/ExternalAccessResourceImpl.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,58 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// https://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 com.starrocks.privilege.external; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class ExternalAccessResourceImpl { | ||
private final Map<String, String> resourceMap; | ||
|
||
public ExternalAccessResourceImpl() { | ||
this.resourceMap = new HashMap<>(); | ||
} | ||
|
||
public ExternalAccessResourceImpl(Map<String, String> resourceMap) { | ||
this.resourceMap = new HashMap<>(resourceMap); | ||
} | ||
|
||
public Map<String, String> getResourceMap() { | ||
return Collections.unmodifiableMap(resourceMap); | ||
} | ||
|
||
public void setValue(String key, String value) { | ||
resourceMap.put(key, value); | ||
} | ||
|
||
public String getValue(String key) { | ||
return resourceMap.get(key); | ||
} | ||
|
||
public Set<String> getKeys() { | ||
return resourceMap.keySet(); | ||
} | ||
|
||
public boolean containsKey(String key) { | ||
return resourceMap.containsKey(key); | ||
} | ||
|
||
public String toString() { | ||
return "ExternalAccessResourceImpl{" + | ||
"resourceMap=" + resourceMap + | ||
'}'; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
fe/fe-core/src/main/java/com/starrocks/privilege/external/ExternalAuthorizer.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,26 @@ | ||
// 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 com.starrocks.privilege.external; | ||
|
||
import com.starrocks.privilege.external.ExternalStarRocksAccessRequest; | ||
|
||
public interface ExternalAuthorizer { | ||
public void init() throws Exception; | ||
|
||
public boolean authorize(ExternalStarRocksAccessRequest request); | ||
} |
Oops, something went wrong.