Skip to content

Commit

Permalink
Support Group Provider
Browse files Browse the repository at this point in the history
Signed-off-by: HangyuanLiu <[email protected]>
  • Loading branch information
HangyuanLiu committed Feb 26, 2025
1 parent 0f8fc55 commit 2c3c29c
Show file tree
Hide file tree
Showing 27 changed files with 932 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,23 @@
import com.starrocks.mysql.MysqlPassword;
import com.starrocks.mysql.privilege.AuthPlugin;
import com.starrocks.persist.EditLog;
import com.starrocks.persist.GroupProviderLog;
import com.starrocks.persist.ImageWriter;
import com.starrocks.persist.OperationType;
import com.starrocks.persist.metablock.MapEntryConsumer;
import com.starrocks.persist.metablock.SRMetaBlockEOFException;
import com.starrocks.persist.metablock.SRMetaBlockException;
import com.starrocks.persist.metablock.SRMetaBlockID;
import com.starrocks.persist.metablock.SRMetaBlockReader;
import com.starrocks.persist.metablock.SRMetaBlockWriter;
import com.starrocks.qe.ConnectContext;
import com.starrocks.server.GlobalStateMgr;
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.CreateUserStmt;
import com.starrocks.sql.ast.DropUserStmt;
import com.starrocks.sql.ast.UserIdentity;
import com.starrocks.sql.ast.group.CreateGroupProviderStmt;
import com.starrocks.sql.ast.group.DropGroupProviderStmt;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -48,6 +53,7 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -71,6 +77,9 @@ public class AuthenticationMgr {
@SerializedName(value = "m")
private Map<String, UserProperty> userNameToProperty = new HashMap<>();

@SerializedName("gp")
protected Map<String, GroupProvider> nameToGroupProviderMap = new ConcurrentHashMap<>();

@SerializedName("sim")
protected Map<String, SecurityIntegration> nameToSecurityIntegrationMap = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -658,6 +667,17 @@ public void loadV2(SRMetaBlockReader reader) throws IOException, SRMetaBlockExce
this.isLoaded = true;
this.userNameToProperty = ret.userNameToProperty;
this.userToAuthenticationInfo = ret.userToAuthenticationInfo;

this.nameToSecurityIntegrationMap = ret.nameToSecurityIntegrationMap;
this.nameToGroupProviderMap = ret.nameToGroupProviderMap;

for (Map.Entry<String, GroupProvider> entry : nameToGroupProviderMap.entrySet()) {
try {
entry.getValue().init();
} catch (Exception e) {
LOG.error("failed to init group provider", e);
}
}
}

public UserProperty getUserProperty(String userName) {
Expand Down Expand Up @@ -757,4 +777,44 @@ public void replayDropSecurityIntegration(String name)
throws DdlException {
dropSecurityIntegration(name, true);
}

// ---------------------------------------- Group Provider Statement --------------------------------------

public void createGroupProviderStatement(CreateGroupProviderStmt stmt, ConnectContext context) throws DdlException {
GroupProvider groupProvider = GroupProviderFactory.createGroupProvider(stmt.getName(), stmt.getPropertyMap());
groupProvider.init();
this.nameToGroupProviderMap.put(stmt.getName(), groupProvider);

GlobalStateMgr.getCurrentState().getEditLog().logEdit(OperationType.OP_CREATE_GROUP_PROVIDER,
new GroupProviderLog(stmt.getName(), stmt.getPropertyMap()));
}

public void replayCreateGroupProvider(String name, Map<String, String> properties) {
GroupProvider groupProvider = GroupProviderFactory.createGroupProvider(name, properties);
try {
groupProvider.init();
this.nameToGroupProviderMap.put(name, groupProvider);
} catch (DdlException e) {
LOG.error("Failed to create group provider '{}'", name, e);
}
}

public void dropGroupProviderStatement(DropGroupProviderStmt stmt, ConnectContext context) {
this.nameToGroupProviderMap.remove(stmt.getName());

GlobalStateMgr.getCurrentState().getEditLog().logEdit(OperationType.OP_DROP_GROUP_PROVIDER,
new GroupProviderLog(stmt.getName(), null));
}

public void replayDropGroupProvider(String name) {
this.nameToGroupProviderMap.remove(name);
}

public List<GroupProvider> getAllGroupProviders() {
return new ArrayList<>(nameToGroupProviderMap.values());
}

public GroupProvider getGroupProvider(String name) {
return nameToGroupProviderMap.get(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// 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.authentication;

import com.starrocks.StarRocksFE;
import com.starrocks.common.DdlException;
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.UserIdentity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class FileGroupProvider extends GroupProvider {
private static final Logger LOG = LogManager.getLogger(FileGroupProvider.class);

public static final String TYPE = "file";

public static final String GROUP_FILE_URL = "group_file_url";

public static final Set<String> REQUIRED_PROPERTIES = new HashSet<>(List.of(
FileGroupProvider.GROUP_FILE_URL));

private final Map<String, Set<String>> userGroups;

public FileGroupProvider(String name, Map<String, String> properties) {
super(name, properties);
this.userGroups = new HashMap<>();
}

@Override
public void init() throws DdlException {
String groupFileUrl = properties.get(GROUP_FILE_URL);

try {
InputStream fileInputStream = null;
try {
if (groupFileUrl.startsWith("http://") || groupFileUrl.startsWith("https://")) {
fileInputStream = new URL(groupFileUrl).openStream();
} else {
String filePath = StarRocksFE.STARROCKS_HOME_DIR + "/conf/" + groupFileUrl;
fileInputStream = new FileInputStream(filePath);
}

String s = readInputStreamToString(fileInputStream, StandardCharsets.UTF_8);
for (String line : s.split("\r?\n")) {
if (line.trim().isEmpty()) {
continue;
}

String[] parts = line.split(":");
String groupName = parts[0];
String[] users = parts[1].split(",");

for (String user : users) {
user = user.trim();
userGroups.putIfAbsent(user, new HashSet<>());
userGroups.get(user).add(groupName);
}
}
} finally {
if (fileInputStream != null) {
fileInputStream.close();
}
}
} catch (IOException e) {
throw new DdlException(e.getMessage());
}
}

@Override
public Set<String> getGroup(UserIdentity userIdentity) {
return userGroups.getOrDefault(userIdentity.getUser(), new HashSet<>());
}

@Override
public void checkProperty() throws SemanticException {
REQUIRED_PROPERTIES.forEach(s -> {
if (!properties.containsKey(s)) {
throw new SemanticException("missing required property: " + s);
}
});
}

public static String readInputStreamToString(final InputStream stream, final Charset charset) throws IOException {
final int bufferSize = 1024;
final char[] buffer = new char[bufferSize];
final StringBuilder out = new StringBuilder();

try (Reader in = new InputStreamReader(stream, charset)) {
while (true) {
int rsz = in.read(buffer, 0, buffer.length);
if (rsz < 0) {
break;
}
out.append(buffer, 0, rsz);
}
return out.toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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.authentication;

import com.google.gson.annotations.SerializedName;
import com.starrocks.common.DdlException;
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.UserIdentity;

import java.util.Map;
import java.util.Set;

public abstract class GroupProvider {
public static final String GROUP_PROVIDER_PROPERTY_TYPE_KEY = "type";

@SerializedName(value = "n")
protected String name;
@SerializedName(value = "m")
protected Map<String, String> properties;

public GroupProvider(String name, Map<String, String> properties) {
this.name = name;
this.properties = properties;
}

public void init() throws DdlException {

}

public void destory() {

}

public String getName() {
return name;
}

public String getType() {
return properties.get("type");
}

public Map<String, String> getProperties() {
return properties;
}

public String getComment() {
return "";
}

public abstract Set<String> getGroup(UserIdentity userIdentity);

public abstract void checkProperty() throws SemanticException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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.authentication;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSortedSet;
import com.starrocks.sql.analyzer.SemanticException;

import java.util.Map;

public class GroupProviderFactory {

private static final ImmutableSortedSet<String> SUPPORT_GROUP_PROVIDER =
ImmutableSortedSet.orderedBy(String.CASE_INSENSITIVE_ORDER)
.add(UnixGroupProvider.TYPE)
.add(FileGroupProvider.TYPE)
.build();

public static void checkGroupProviderIsSupported(String groupProviderType) {
if (!SUPPORT_GROUP_PROVIDER.contains(groupProviderType)) {
throw new SemanticException("unsupported group provider type '" + groupProviderType + "'");
}
}
public static GroupProvider createGroupProvider(String name, Map<String, String> propertyMap) {
String type = propertyMap.get(GroupProvider.GROUP_PROVIDER_PROPERTY_TYPE_KEY);
checkGroupProviderIsSupported(type);

GroupProvider groupProvider = null;
if (type.equalsIgnoreCase(FileGroupProvider.TYPE)) {
groupProvider = new FileGroupProvider(name, propertyMap);
} else if (type.equalsIgnoreCase(UnixGroupProvider.TYPE)) {
groupProvider = new UnixGroupProvider(name, propertyMap);
}

Preconditions.checkNotNull(groupProvider);
return groupProvider;
}
}
20 changes: 13 additions & 7 deletions fe/fe-core/src/main/java/com/starrocks/authentication/JwkMgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@

public class JwkMgr {
public JWKSet getJwkSet(String jwksUrl) throws IOException, ParseException {
InputStream jwksInputStream;
if (jwksUrl.startsWith("http://") || jwksUrl.startsWith("https://")) {
jwksInputStream = new URL(jwksUrl).openStream();
} else {
String filePath = StarRocksFE.STARROCKS_HOME_DIR + "/conf/" + jwksUrl;
jwksInputStream = new FileInputStream(filePath);
InputStream jwksInputStream = null;
try {
if (jwksUrl.startsWith("http://") || jwksUrl.startsWith("https://")) {
jwksInputStream = new URL(jwksUrl).openStream();
} else {
String filePath = StarRocksFE.STARROCKS_HOME_DIR + "/conf/" + jwksUrl;
jwksInputStream = new FileInputStream(filePath);
}
return JWKSet.load(jwksInputStream);
} finally {
if (jwksInputStream != null) {
jwksInputStream.close();
}
}
return JWKSet.load(jwksInputStream);
}
}
Loading

0 comments on commit 2c3c29c

Please sign in to comment.