forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'alibaba:develop' into develop
- Loading branch information
Showing
28 changed files
with
756 additions
and
125 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
43 changes: 43 additions & 0 deletions
43
auth/src/main/java/com/alibaba/nacos/auth/serveridentity/DefaultChecker.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,43 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.auth.serveridentity; | ||
|
||
import com.alibaba.nacos.auth.annotation.Secured; | ||
import com.alibaba.nacos.auth.config.AuthConfigs; | ||
|
||
/** | ||
* Nacos default server identity checker. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public class DefaultChecker implements ServerIdentityChecker { | ||
|
||
private AuthConfigs authConfigs; | ||
|
||
@Override | ||
public void init(AuthConfigs authConfigs) { | ||
this.authConfigs = authConfigs; | ||
} | ||
|
||
@Override | ||
public ServerIdentityResult check(ServerIdentity serverIdentity, Secured secured) { | ||
if (authConfigs.getServerIdentityValue().equals(serverIdentity.getIdentityValue())) { | ||
return ServerIdentityResult.success(); | ||
} | ||
return ServerIdentityResult.noMatched(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
auth/src/main/java/com/alibaba/nacos/auth/serveridentity/ServerIdentity.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,42 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.auth.serveridentity; | ||
|
||
/** | ||
* Nacos server identity. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public class ServerIdentity { | ||
|
||
private final String identityKey; | ||
|
||
private final String identityValue; | ||
|
||
public ServerIdentity(String identityKey, String identityValue) { | ||
this.identityKey = identityKey; | ||
this.identityValue = identityValue; | ||
} | ||
|
||
public String getIdentityKey() { | ||
return identityKey; | ||
} | ||
|
||
public String getIdentityValue() { | ||
return identityValue; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
auth/src/main/java/com/alibaba/nacos/auth/serveridentity/ServerIdentityChecker.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,44 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.auth.serveridentity; | ||
|
||
import com.alibaba.nacos.auth.annotation.Secured; | ||
import com.alibaba.nacos.auth.config.AuthConfigs; | ||
|
||
/** | ||
* Nacos server identity checker for nacos inner/admin API identity check. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public interface ServerIdentityChecker { | ||
|
||
/** | ||
* Do init checker. | ||
* | ||
* @param authConfigs config for nacos auth. | ||
*/ | ||
void init(AuthConfigs authConfigs); | ||
|
||
/** | ||
* Do check nacos server identity. | ||
* | ||
* @param serverIdentity server identity | ||
* @param secured secured api secured annotation | ||
* @return result of checking server identity | ||
*/ | ||
ServerIdentityResult check(ServerIdentity serverIdentity, Secured secured); | ||
} |
74 changes: 74 additions & 0 deletions
74
auth/src/main/java/com/alibaba/nacos/auth/serveridentity/ServerIdentityCheckerHolder.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,74 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.auth.serveridentity; | ||
|
||
import com.alibaba.nacos.common.spi.NacosServiceLoader; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Server Identity Checker SPI holder. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public class ServerIdentityCheckerHolder { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ServerIdentityCheckerHolder.class); | ||
|
||
private static final ServerIdentityCheckerHolder INSTANCE = new ServerIdentityCheckerHolder(); | ||
|
||
private ServerIdentityChecker checker; | ||
|
||
private ServerIdentityCheckerHolder() { | ||
tryGetCheckerBySpi(); | ||
} | ||
|
||
public static ServerIdentityCheckerHolder getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
public ServerIdentityChecker getChecker() { | ||
return checker; | ||
} | ||
|
||
private synchronized void tryGetCheckerBySpi() { | ||
Collection<ServerIdentityChecker> checkers = NacosServiceLoader.load(ServerIdentityChecker.class); | ||
if (checkers.isEmpty()) { | ||
checker = new DefaultChecker(); | ||
LOGGER.info("Not found ServerIdentityChecker implementation from SPI, use default."); | ||
return; | ||
} | ||
if (checkers.size() > 1) { | ||
checker = showAllImplementations(checkers); | ||
return; | ||
} | ||
checker = checkers.iterator().next(); | ||
LOGGER.info("Found ServerIdentityChecker implementation {}", checker.getClass().getCanonicalName()); | ||
} | ||
|
||
private ServerIdentityChecker showAllImplementations(Collection<ServerIdentityChecker> checkers) { | ||
ServerIdentityChecker result = checkers.iterator().next(); | ||
for (ServerIdentityChecker each : checkers) { | ||
LOGGER.warn("Found ServerIdentityChecker implementation {}", each.getClass().getCanonicalName()); | ||
} | ||
LOGGER.warn("Found more than one ServerIdentityChecker implementation from SPI, use the first one {}.", | ||
result.getClass().getCanonicalName()); | ||
return result; | ||
} | ||
} |
Oops, something went wrong.