You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/06/24 14:29:24 UTC

[GitHub] [shardingsphere] terrymanu commented on a change in pull request #10998: custom authority,schema can be assigned to proxy users

terrymanu commented on a change in pull request #10998:
URL: https://github.com/apache/shardingsphere/pull/10998#discussion_r657998686



##########
File path: shardingsphere-infra/shardingsphere-infra-authority/shardingsphere-infra-authority-common/src/main/java/org/apache/shardingsphere/authority/provider/schema/builder/SchemaPrivilegeBuilder.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.shardingsphere.authority.provider.schema.builder;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
+import org.apache.shardingsphere.authority.provider.schema.SchemaPrivilegesPermittedAuthorityProviderAlgorithm;
+import org.apache.shardingsphere.authority.provider.schema.model.privilege.SchemaPrivilegesPermittedShardingSpherePrivileges;
+import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
+
+import com.google.common.base.Preconditions;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class SchemaPrivilegeBuilder {
+    /**

Review comment:
       Please keep empty line between class and method definition

##########
File path: shardingsphere-infra/shardingsphere-infra-authority/shardingsphere-infra-authority-common/src/main/java/org/apache/shardingsphere/authority/provider/schema/builder/SchemaPrivilegeBuilder.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.shardingsphere.authority.provider.schema.builder;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
+import org.apache.shardingsphere.authority.provider.schema.SchemaPrivilegesPermittedAuthorityProviderAlgorithm;
+import org.apache.shardingsphere.authority.provider.schema.model.privilege.SchemaPrivilegesPermittedShardingSpherePrivileges;
+import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
+
+import com.google.common.base.Preconditions;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class SchemaPrivilegeBuilder {
+    /**
+     * Build privileges.
+     *
+     * @param users users
+     * @param props props
+     * @return privileges
+     */
+    public static Map<ShardingSphereUser, ShardingSpherePrivileges> build(final Collection<ShardingSphereUser> users, final Properties props) {
+        String mappingProp = props.getProperty(SchemaPrivilegesPermittedAuthorityProviderAlgorithm.PROP_USER_SCHEMA_MAPPINGS, "");
+        Preconditions.checkArgument(!"".equals(mappingProp), "user-schema-mappings configuration `%s` can not be null", mappingProp);
+        String[] mappings = mappingProp.split(",");
+        Arrays.asList(mappings).stream().forEach(each -> Preconditions.checkArgument(0 < each.indexOf("@") && 0 < each.indexOf("="), 
+                "user-schema-mappings configuration `%s` is invalid, the configuration format should be like `username@hostname=schema`", each));
+        Map<ShardingSphereUser, ShardingSpherePrivileges> result = new HashMap<>();
+        Map<ShardingSphereUser, Set<String>> userSchemaMappings = new HashMap<>();
+        Arrays.asList(mappings).stream().map(SchemaPrivilegeBuilder::convertSchemas).forEach(each -> merge(each, userSchemaMappings));
+        users.forEach(each -> result.put(each, new SchemaPrivilegesPermittedShardingSpherePrivileges(userSchemaMappings.getOrDefault(each, Collections.emptySet()))));
+        return result;
+    }
+    
+    /**
+     * convert schemas.
+     * @param userSchemaMapping user shcema mapping
+     * @return schemas
+     */
+    private static Map<ShardingSphereUser, String> convertSchemas(final String userSchemaMapping) {
+        String[] userSchemaPair = userSchemaMapping.trim().split("=");
+        Map<ShardingSphereUser, String> result = new HashMap<>();
+
+        String yamlUser = userSchemaPair[0];
+        String username = yamlUser.substring(0, yamlUser.indexOf("@"));
+        String hostname = yamlUser.substring(yamlUser.indexOf("@") + 1);
+
+        result.put(new ShardingSphereUser(username, "", hostname), userSchemaPair[1]);
+        return result;
+    }
+    
+    /**
+     * merge user schema to result.

Review comment:
       Please upper case the first letter of java doc

##########
File path: shardingsphere-infra/shardingsphere-infra-authority/shardingsphere-infra-authority-common/src/main/java/org/apache/shardingsphere/authority/provider/schema/builder/SchemaPrivilegeBuilder.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.shardingsphere.authority.provider.schema.builder;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
+import org.apache.shardingsphere.authority.provider.schema.SchemaPrivilegesPermittedAuthorityProviderAlgorithm;
+import org.apache.shardingsphere.authority.provider.schema.model.privilege.SchemaPrivilegesPermittedShardingSpherePrivileges;
+import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
+
+import com.google.common.base.Preconditions;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class SchemaPrivilegeBuilder {
+    /**
+     * Build privileges.
+     *
+     * @param users users
+     * @param props props
+     * @return privileges
+     */
+    public static Map<ShardingSphereUser, ShardingSpherePrivileges> build(final Collection<ShardingSphereUser> users, final Properties props) {
+        String mappingProp = props.getProperty(SchemaPrivilegesPermittedAuthorityProviderAlgorithm.PROP_USER_SCHEMA_MAPPINGS, "");
+        Preconditions.checkArgument(!"".equals(mappingProp), "user-schema-mappings configuration `%s` can not be null", mappingProp);
+        String[] mappings = mappingProp.split(",");
+        Arrays.asList(mappings).stream().forEach(each -> Preconditions.checkArgument(0 < each.indexOf("@") && 0 < each.indexOf("="), 
+                "user-schema-mappings configuration `%s` is invalid, the configuration format should be like `username@hostname=schema`", each));
+        Map<ShardingSphereUser, ShardingSpherePrivileges> result = new HashMap<>();
+        Map<ShardingSphereUser, Set<String>> userSchemaMappings = new HashMap<>();
+        Arrays.asList(mappings).stream().map(SchemaPrivilegeBuilder::convertSchemas).forEach(each -> merge(each, userSchemaMappings));
+        users.forEach(each -> result.put(each, new SchemaPrivilegesPermittedShardingSpherePrivileges(userSchemaMappings.getOrDefault(each, Collections.emptySet()))));
+        return result;
+    }
+    
+    /**
+     * convert schemas.

Review comment:
       Please upper case the first letter of java doc

##########
File path: shardingsphere-infra/shardingsphere-infra-authority/shardingsphere-infra-authority-common/src/main/java/org/apache/shardingsphere/authority/provider/schema/model/privilege/SchemaPrivilegesPermittedShardingSpherePrivileges.java
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.shardingsphere.authority.provider.schema.model.privilege;
+
+import java.util.Collection;
+import java.util.Set;
+
+import org.apache.shardingsphere.authority.model.AccessSubject;
+import org.apache.shardingsphere.authority.model.PrivilegeType;
+import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
+import org.apache.shardingsphere.authority.provider.natived.model.subject.SchemaAccessSubject;
+
+import lombok.AllArgsConstructor;
+
+@AllArgsConstructor
+public class SchemaPrivilegesPermittedShardingSpherePrivileges implements ShardingSpherePrivileges {
+
+    private Set<String> schemas;

Review comment:
       Can the attribute `schemas` to be final? 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org