-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
7fe73a6
commit a2f742a
Showing
10 changed files
with
154 additions
and
35 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
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
37 changes: 37 additions & 0 deletions
37
...awei-governance/src/main/java/com/huaweicloud/governance/authentication/MatcherUtils.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,37 @@ | ||
/* | ||
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. 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 | ||
* | ||
* 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.huaweicloud.governance.authentication; | ||
|
||
public class MatcherUtils { | ||
public static boolean isPatternMatch(String value, String pattern) { | ||
if (pattern.startsWith("*") || pattern.startsWith("/*")) { | ||
int index = 0; | ||
for (int i = 0; i < pattern.length(); i++) { | ||
if (pattern.charAt(i) != '*' && pattern.charAt(i) != '/') { | ||
break; | ||
} | ||
index++; | ||
} | ||
return value.endsWith(pattern.substring(index)); | ||
} | ||
if (pattern.endsWith("*")) { | ||
return value.startsWith(pattern.substring(0, pattern.length() - 1)); | ||
} | ||
return value.equals(pattern); | ||
} | ||
} |
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
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
81 changes: 81 additions & 0 deletions
81
...com/huaweicloud/governance/authentication/securityPolicy/RSAProviderTokenManagerTest.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,81 @@ | ||
/* | ||
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. 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 | ||
* | ||
* 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.huaweicloud.governance.authentication.securityPolicy; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.core.env.Environment; | ||
|
||
import com.huaweicloud.governance.authentication.Const; | ||
import com.huaweicloud.governance.authentication.RSAProviderTokenManager; | ||
|
||
public class RSAProviderTokenManagerTest { | ||
private Environment environment = Mockito.mock(Environment.class); | ||
|
||
private RSAProviderTokenManager manager | ||
= new RSAProviderTokenManager(null, environment, null); | ||
|
||
@Test | ||
public void testCheckUriWhitelistPrefixMatch() { | ||
Mockito.when(environment.getProperty(Const.AUTH_API_PATH_WHITELIST, String.class, "")) | ||
.thenReturn("*/whitelist"); | ||
Assertions.assertTrue(manager.checkUriWhitelist("/api/check/whitelist")); | ||
} | ||
|
||
@Test | ||
public void testCheckUriWhitelistPrefixSlashMatch() { | ||
Mockito.when(environment.getProperty(Const.AUTH_API_PATH_WHITELIST, String.class, "")) | ||
.thenReturn("/*/whitelist"); | ||
Assertions.assertTrue(manager.checkUriWhitelist("/api/check/whitelist")); | ||
} | ||
@Test | ||
public void testCheckUriWhitelistPrefixNotMatch() { | ||
Mockito.when(environment.getProperty(Const.AUTH_API_PATH_WHITELIST, String.class, "")) | ||
.thenReturn("*/whitelist"); | ||
Assertions.assertFalse(manager.checkUriWhitelist("/api/check/query")); | ||
} | ||
|
||
@Test | ||
public void testCheckUriWhitelistSuffixMatch() { | ||
Mockito.when(environment.getProperty(Const.AUTH_API_PATH_WHITELIST, String.class, "")) | ||
.thenReturn("/api/*"); | ||
Assertions.assertTrue(manager.checkUriWhitelist("/api/check/whitelist")); | ||
} | ||
|
||
@Test | ||
public void testCheckUriWhitelistSuffixNotMatch() { | ||
Mockito.when(environment.getProperty(Const.AUTH_API_PATH_WHITELIST, String.class, "")) | ||
.thenReturn("/apg/*"); | ||
Assertions.assertFalse(manager.checkUriWhitelist("/api/check/whitelist")); | ||
} | ||
|
||
@Test | ||
public void testCheckUriWhitelistEqualMatch() { | ||
Mockito.when(environment.getProperty(Const.AUTH_API_PATH_WHITELIST, String.class, "")) | ||
.thenReturn("/api/check/whitelist"); | ||
Assertions.assertTrue(manager.checkUriWhitelist("/api/check/whitelist")); | ||
} | ||
|
||
@Test | ||
public void testCheckUriWhitelistEqualNotMatch() { | ||
Mockito.when(environment.getProperty(Const.AUTH_API_PATH_WHITELIST, String.class, "")) | ||
.thenReturn("/api/check/white"); | ||
Assertions.assertFalse(manager.checkUriWhitelist("/api/check/whitelist")); | ||
} | ||
} |