You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2021/09/11 09:16:59 UTC

[servicecomb-java-chassis] branch master updated: #2568 The RBAC custom encryption method supports SPI loading. (#2572)

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 9f90bcd  #2568 The RBAC custom encryption method supports SPI loading. (#2572)
9f90bcd is described below

commit 9f90bcdfdc5d4f0dea72437dee72e55435c5564a
Author: zyl <72...@users.noreply.github.com>
AuthorDate: Sat Sep 11 17:16:54 2021 +0800

    #2568 The RBAC custom encryption method supports SPI loading. (#2572)
---
 .../serviceregistry/auth/RBACBootStrapService.java | 11 ++++---
 .../serviceregistry/auth/TestCipher.java           | 34 ++++++++++++++++++++
 .../auth/TestRBACBootStrapService.java             | 36 ++++++++++++++++++++++
 .../org.apache.servicecomb.foundation.auth.Cipher  | 18 +++++++++++
 4 files changed, 95 insertions(+), 4 deletions(-)

diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/auth/RBACBootStrapService.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/auth/RBACBootStrapService.java
index 4aa696a..c7a4914 100644
--- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/auth/RBACBootStrapService.java
+++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/auth/RBACBootStrapService.java
@@ -27,7 +27,7 @@ import java.util.Map;
 import org.apache.servicecomb.foundation.auth.Cipher;
 import org.apache.servicecomb.foundation.auth.DefaultCipher;
 import org.apache.servicecomb.foundation.bootstrap.BootStrapService;
-import org.apache.servicecomb.foundation.common.utils.BeanUtils;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.apache.servicecomb.foundation.ssl.SSLCustom;
 import org.apache.servicecomb.foundation.ssl.SSLOption;
 import org.apache.servicecomb.http.client.auth.DefaultRequestAuthHeaderProvider;
@@ -37,6 +37,8 @@ import org.apache.servicecomb.service.center.client.ServiceCenterClient;
 import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
 import org.springframework.core.env.Environment;
 
+import com.google.common.annotations.VisibleForTesting;
+
 public class RBACBootStrapService implements BootStrapService {
   private static final String RBAC_ADDRESS = "servicecomb.service.registry.address";
 
@@ -76,13 +78,14 @@ public class RBACBootStrapService implements BootStrapService {
         getCipher(getStringProperty(environment, DefaultCipher.CIPHER_NAME, CIPHER_KEY)));
   }
 
-  private Cipher getCipher(String cipherName) {
+  @VisibleForTesting
+   Cipher getCipher(String cipherName) {
     if (DefaultCipher.CIPHER_NAME.equals(cipherName)) {
       return DefaultCipher.getInstance();
     }
 
-    Map<String, Cipher> cipherBeans = BeanUtils.getBeansOfType(Cipher.class);
-    return cipherBeans.values().stream().filter(c -> c.name().equals(cipherName)).findFirst()
+    List<Cipher> ciphers = SPIServiceUtils.getOrLoadSortedService(Cipher.class);
+    return ciphers.stream().filter(c -> c.name().equals(cipherName)).findFirst()
         .orElseThrow(() -> new IllegalArgumentException("failed to find cipher named " + cipherName));
   }
 
diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/auth/TestCipher.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/auth/TestCipher.java
new file mode 100644
index 0000000..6dbd734
--- /dev/null
+++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/auth/TestCipher.java
@@ -0,0 +1,34 @@
+/*
+ * 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.servicecomb.serviceregistry.auth;
+
+import org.apache.servicecomb.foundation.auth.Cipher;
+
+public class TestCipher implements Cipher {
+  @Override
+  public String name() {
+    return "testCipher";
+  }
+
+  @Override
+  public char[] decrypt(char[] encrypted) {
+    String encryptedValue = String.valueOf(encrypted);
+    String decryptValue = encryptedValue.substring(0, encryptedValue.length() / 2);
+    return decryptValue.toCharArray();
+  }
+}
diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/auth/TestRBACBootStrapService.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/auth/TestRBACBootStrapService.java
new file mode 100644
index 0000000..af3776b
--- /dev/null
+++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/auth/TestRBACBootStrapService.java
@@ -0,0 +1,36 @@
+/*
+ * 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.servicecomb.serviceregistry.auth;
+
+import org.apache.servicecomb.foundation.auth.Cipher;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestRBACBootStrapService {
+  RBACBootStrapService rbacBootStrapService = new RBACBootStrapService();
+
+  @Test
+  public void testGetCipher() {
+    Cipher cipher = rbacBootStrapService.getCipher("testCipher");
+    Assert.assertSame(cipher.name(), "testCipher");
+
+    char[] encrypted = "testtest".toCharArray();
+    String result = String.valueOf(cipher.decrypt(encrypted));
+    Assert.assertEquals(result, "test");
+  }
+}
\ No newline at end of file
diff --git a/service-registry/registry-service-center/src/test/resources/META-INF/services/org.apache.servicecomb.foundation.auth.Cipher b/service-registry/registry-service-center/src/test/resources/META-INF/services/org.apache.servicecomb.foundation.auth.Cipher
new file mode 100644
index 0000000..a3fc09e
--- /dev/null
+++ b/service-registry/registry-service-center/src/test/resources/META-INF/services/org.apache.servicecomb.foundation.auth.Cipher
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.servicecomb.serviceregistry.auth.TestCipher
\ No newline at end of file