View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.authority.rule;
19  
20  import lombok.Getter;
21  import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
22  import org.apache.shardingsphere.authority.constant.AuthorityOrder;
23  import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
24  import org.apache.shardingsphere.authority.spi.PrivilegeProvider;
25  import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
26  import org.apache.shardingsphere.infra.metadata.user.Grantee;
27  import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
28  import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
29  import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
30  
31  import java.util.Collection;
32  import java.util.LinkedHashMap;
33  import java.util.Map;
34  import java.util.Optional;
35  import java.util.stream.Collectors;
36  
37  /**
38   * Authority rule.
39   */
40  public final class AuthorityRule implements GlobalRule {
41      
42      @Getter
43      private final AuthorityRuleConfiguration configuration;
44      
45      private final Map<ShardingSphereUser, ShardingSpherePrivileges> privileges;
46      
47      public AuthorityRule(final AuthorityRuleConfiguration ruleConfig) {
48          configuration = ruleConfig;
49          Collection<ShardingSphereUser> users = ruleConfig.getUsers().stream()
50                  .map(each -> new ShardingSphereUser(each.getUsername(), each.getPassword(), each.getHostname(), each.getAuthenticationMethodName(), each.isAdmin())).collect(Collectors.toList());
51          privileges = users.stream().collect(Collectors.toMap(each -> each,
52                  each -> TypedSPILoader.getService(PrivilegeProvider.class, ruleConfig.getPrivilegeProvider().getType(), ruleConfig.getPrivilegeProvider().getProps())
53                          .build(ruleConfig, each.getGrantee()),
54                  (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
55      }
56      
57      /**
58       * Get authenticator type.
59       *
60       * @param user user
61       * @return authenticator type
62       */
63      public String getAuthenticatorType(final ShardingSphereUser user) {
64          if (configuration.getAuthenticators().containsKey(user.getAuthenticationMethodName())) {
65              return configuration.getAuthenticators().get(user.getAuthenticationMethodName()).getType();
66          }
67          if (configuration.getAuthenticators().containsKey(configuration.getDefaultAuthenticator())) {
68              return configuration.getAuthenticators().get(configuration.getDefaultAuthenticator()).getType();
69          }
70          return "";
71      }
72      
73      /**
74       * Get grantees.
75       *
76       * @return grantees
77       */
78      public Collection<Grantee> getGrantees() {
79          return privileges.keySet().stream().map(ShardingSphereUser::getGrantee).collect(Collectors.toList());
80      }
81      
82      /**
83       * Find user.
84       *
85       * @param grantee grantee user
86       * @return found user
87       */
88      @HighFrequencyInvocation
89      public Optional<ShardingSphereUser> findUser(final Grantee grantee) {
90          for (ShardingSphereUser each : privileges.keySet()) {
91              if (each.getGrantee().accept(grantee)) {
92                  return Optional.of(each);
93              }
94          }
95          return Optional.empty();
96      }
97      
98      /**
99       * Find privileges.
100      *
101      * @param grantee grantee
102      * @return found privileges
103      */
104     @HighFrequencyInvocation
105     public Optional<ShardingSpherePrivileges> findPrivileges(final Grantee grantee) {
106         for (ShardingSphereUser each : privileges.keySet()) {
107             if (each.getGrantee().accept(grantee)) {
108                 return Optional.of(each).map(privileges::get);
109             }
110         }
111         return Optional.empty();
112     }
113     
114     @Override
115     public int getOrder() {
116         return AuthorityOrder.ORDER;
117     }
118 }