----初始化项目
This commit is contained in:
39
plugin/auth/pom.xml
Normal file
39
plugin/auth/pom.xml
Normal file
@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright 1999-2021 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.
|
||||
-->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>nacos-plugin</artifactId>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>nacos-auth-plugin</artifactId>
|
||||
<name>nacos-auth-plugin ${project.version}</name>
|
||||
<url>https://nacos.io</url>
|
||||
<description>Nacos auth plugin pom.xml file</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<artifactId>nacos-common</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.api;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Identity Context.
|
||||
*
|
||||
* @author Wuyfee
|
||||
*/
|
||||
public class IdentityContext {
|
||||
|
||||
/**
|
||||
* get context from request.
|
||||
*/
|
||||
private final Map<String, Object> param = new HashMap<>();
|
||||
|
||||
/**
|
||||
* get key from context.
|
||||
*
|
||||
* @param key key of request
|
||||
* @return value of param key
|
||||
*/
|
||||
public Object getParameter(String key) {
|
||||
return param.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get identity by key.
|
||||
*
|
||||
* @param key identity name
|
||||
* @param defaultValue default value when the value is {@code null} or the value is not expected class type
|
||||
* @param <T> classes type of identity value
|
||||
* @return identity value
|
||||
*/
|
||||
public <T> T getParameter(String key, T defaultValue) {
|
||||
if (null == defaultValue) {
|
||||
throw new IllegalArgumentException(
|
||||
"defaultValue can't be null. Please use #getParameter(String key) replace");
|
||||
}
|
||||
try {
|
||||
Object result = param.get(key);
|
||||
if (null != result) {
|
||||
return (T) defaultValue.getClass().cast(result);
|
||||
}
|
||||
return defaultValue;
|
||||
} catch (ClassCastException exception) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* put key and value to param.
|
||||
*
|
||||
* @param key key of request
|
||||
* @param value value of request's key
|
||||
*/
|
||||
public void setParameter(String key, Object value) {
|
||||
param.put(key, value);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.api;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Login identity context.
|
||||
*
|
||||
* @author Nacos
|
||||
*/
|
||||
public class LoginIdentityContext {
|
||||
|
||||
/**
|
||||
* get context from request.
|
||||
*/
|
||||
private final Map<String, String> param = new HashMap<>();
|
||||
|
||||
/**
|
||||
* get key from context.
|
||||
*
|
||||
* @param key key of request
|
||||
* @return value of param key
|
||||
*/
|
||||
public String getParameter(String key) {
|
||||
return param.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* put key and value to param.
|
||||
*
|
||||
* @param key key of request
|
||||
* @param value value of request's key
|
||||
*/
|
||||
public void setParameter(String key, String value) {
|
||||
param.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* put all parameters from Map.
|
||||
*
|
||||
* @param parameters map of parameters
|
||||
*/
|
||||
public void setParameters(Map<String, String> parameters) {
|
||||
param.putAll(parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* get all keys of param map.
|
||||
*
|
||||
* @return set all param keys.
|
||||
*/
|
||||
public Set<String> getAllKey() {
|
||||
return param.keySet();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.api;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Permission to auth.
|
||||
*
|
||||
* @author nkorange
|
||||
* @author mai.jh
|
||||
* @author xiweng.yy
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public class Permission implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -3583076254743606551L;
|
||||
|
||||
/**
|
||||
* An unique key of resource.
|
||||
*/
|
||||
private Resource resource;
|
||||
|
||||
/**
|
||||
* Action on resource, refer to class ActionTypes.
|
||||
*/
|
||||
private String action;
|
||||
|
||||
public Permission() {
|
||||
}
|
||||
|
||||
public Permission(Resource resource, String action) {
|
||||
this.resource = resource;
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public Resource getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
public void setResource(Resource resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Permission{" + "resource='" + resource + '\'' + ", action='" + action + '\'' + '}';
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.api;
|
||||
|
||||
import com.alibaba.nacos.plugin.auth.constant.SignType;
|
||||
|
||||
/**
|
||||
* Request resources.
|
||||
*
|
||||
* @author xiweng.yy
|
||||
*/
|
||||
public class RequestResource {
|
||||
|
||||
/**
|
||||
* Request type: naming or config.
|
||||
*/
|
||||
private String type;
|
||||
|
||||
private String namespace;
|
||||
|
||||
private String group;
|
||||
|
||||
/**
|
||||
* For type: naming, the resource should be service name.
|
||||
* For type: config, the resource should be config dataId.
|
||||
*/
|
||||
private String resource;
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
public void setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public void setGroup(String group) {
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
public String getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
public void setResource(String resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new naming request resource builder.
|
||||
*
|
||||
* @return naming request resource builder
|
||||
*/
|
||||
public static Builder namingBuilder() {
|
||||
Builder result = new Builder();
|
||||
result.setType(SignType.NAMING);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new config request resource builder.
|
||||
*
|
||||
* @return config request resource builder
|
||||
*/
|
||||
public static Builder configBuilder() {
|
||||
Builder result = new Builder();
|
||||
result.setType(SignType.CONFIG);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
private String type;
|
||||
|
||||
private String namespace;
|
||||
|
||||
private String group;
|
||||
|
||||
private String resource;
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Builder setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setGroup(String group) {
|
||||
this.group = group;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setResource(String resource) {
|
||||
this.resource = resource;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build request resource.
|
||||
*
|
||||
* @return request resource
|
||||
*/
|
||||
public RequestResource build() {
|
||||
RequestResource result = new RequestResource();
|
||||
result.setType(type);
|
||||
result.setNamespace(namespace);
|
||||
result.setGroup(group);
|
||||
result.setResource(resource);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.api;
|
||||
|
||||
import com.alibaba.nacos.common.utils.StringUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Resource used in authorization.
|
||||
*
|
||||
* @author nkorange
|
||||
* @author mai.jh
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public class Resource implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 925971662931204553L;
|
||||
|
||||
public static final Resource EMPTY_RESOURCE = new Resource(StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY,
|
||||
StringUtils.EMPTY, null);
|
||||
|
||||
private final String namespaceId;
|
||||
|
||||
private final String group;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final String type;
|
||||
|
||||
private final Properties properties;
|
||||
|
||||
public Resource(String namespaceId, String group, String name, String type, Properties properties) {
|
||||
this.namespaceId = namespaceId;
|
||||
this.group = group;
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public String getNamespaceId() {
|
||||
return namespaceId;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public Properties getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Resource{" + "namespaceId='" + namespaceId + '\'' + ", group='" + group + '\'' + ", name='" + name
|
||||
+ '\'' + ", type='" + type + '\'' + ", properties=" + properties + '}';
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.constant;
|
||||
|
||||
/**
|
||||
* Resource action type definitions.
|
||||
*
|
||||
* @author nkorange
|
||||
* @author mai.jh
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public enum ActionTypes {
|
||||
/**
|
||||
* Read.
|
||||
*/
|
||||
READ("r"),
|
||||
/**
|
||||
* Write.
|
||||
*/
|
||||
WRITE("w");
|
||||
|
||||
private final String action;
|
||||
|
||||
ActionTypes(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return action;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.constant;
|
||||
|
||||
/**
|
||||
* All the constants.
|
||||
*
|
||||
* @author onew
|
||||
*/
|
||||
public class Constants {
|
||||
|
||||
public static class Auth {
|
||||
|
||||
public static final String NACOS_CORE_AUTH_ENABLED = "nacos.core.auth.enabled";
|
||||
|
||||
public static final String NACOS_CORE_AUTH_SYSTEM_TYPE = "nacos.core.auth.system.type";
|
||||
|
||||
public static final String NACOS_CORE_AUTH_CACHING_ENABLED = "nacos.core.auth.caching.enabled";
|
||||
|
||||
public static final String NACOS_CORE_AUTH_SERVER_IDENTITY_KEY = "nacos.core.auth.server.identity.key";
|
||||
|
||||
public static final String NACOS_CORE_AUTH_SERVER_IDENTITY_VALUE = "nacos.core.auth.server.identity.value";
|
||||
|
||||
public static final String NACOS_CORE_AUTH_ENABLE_USER_AGENT_AUTH_WHITE = "nacos.core.auth.enable.userAgentAuthWhite";
|
||||
|
||||
}
|
||||
|
||||
public static class Resource {
|
||||
|
||||
public static final String SPLITTER = ":";
|
||||
|
||||
public static final String ANY = "*";
|
||||
|
||||
public static final String ACTION = "action";
|
||||
|
||||
public static final String REQUEST_CLASS = "requestClass";
|
||||
}
|
||||
|
||||
public static class Identity {
|
||||
|
||||
public static final String IDENTITY_ID = "identity_id";
|
||||
|
||||
public static final String X_REAL_IP = "X-Real-IP";
|
||||
|
||||
public static final String REMOTE_IP = "remote_ip";
|
||||
|
||||
public static final String IDENTITY_CONTEXT = "identity_context";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.constant;
|
||||
|
||||
/**
|
||||
* Auth sign type.
|
||||
*
|
||||
* @author xiweng.yy
|
||||
*/
|
||||
public class SignType {
|
||||
|
||||
public static final String NAMING = "naming";
|
||||
|
||||
public static final String CONFIG = "config";
|
||||
|
||||
public static final String CONSOLE = "console";
|
||||
|
||||
public static final String SPECIFIED = "specified";
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.exception;
|
||||
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
|
||||
/**
|
||||
* Exception to be thrown if authorization is failed.
|
||||
*
|
||||
* @author nkorange
|
||||
* @author mai.jh
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public class AccessException extends NacosException {
|
||||
|
||||
private static final long serialVersionUID = -2926344920552803270L;
|
||||
|
||||
public AccessException() {
|
||||
}
|
||||
|
||||
public AccessException(int code) {
|
||||
this.setErrCode(code);
|
||||
}
|
||||
|
||||
public AccessException(String msg) {
|
||||
this.setErrMsg(msg);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.spi.client;
|
||||
|
||||
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Client auth services SPI.
|
||||
*
|
||||
* @author Nacos
|
||||
*/
|
||||
public abstract class AbstractClientAuthService implements ClientAuthService {
|
||||
|
||||
protected List<String> serverList;
|
||||
|
||||
protected NacosRestTemplate nacosRestTemplate;
|
||||
|
||||
@Override
|
||||
public void setServerList(List<String> serverList) {
|
||||
this.serverList = serverList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNacosRestTemplate(NacosRestTemplate nacosRestTemplate) {
|
||||
this.nacosRestTemplate = nacosRestTemplate;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.spi.client;
|
||||
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
|
||||
import com.alibaba.nacos.common.lifecycle.Closeable;
|
||||
import com.alibaba.nacos.common.spi.NacosServiceLoader;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* ClientAuthService classLoader.
|
||||
*
|
||||
* @author wuyfee
|
||||
*/
|
||||
public class ClientAuthPluginManager implements Closeable {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ClientAuthPluginManager.class);
|
||||
|
||||
/**
|
||||
* The relationship of context type and {@link ClientAuthService}.
|
||||
*/
|
||||
private final Set<ClientAuthService> clientAuthServiceHashSet = new HashSet<>();
|
||||
|
||||
/**
|
||||
* init ClientAuthService.
|
||||
*/
|
||||
public void init(List<String> serverList, NacosRestTemplate nacosRestTemplate) {
|
||||
|
||||
Collection<AbstractClientAuthService> clientAuthServices = NacosServiceLoader
|
||||
.load(AbstractClientAuthService.class);
|
||||
for (ClientAuthService clientAuthService : clientAuthServices) {
|
||||
clientAuthService.setServerList(serverList);
|
||||
clientAuthService.setNacosRestTemplate(nacosRestTemplate);
|
||||
clientAuthServiceHashSet.add(clientAuthService);
|
||||
LOGGER.info("[ClientAuthPluginManager] Load ClientAuthService {} success.",
|
||||
clientAuthService.getClass().getCanonicalName());
|
||||
}
|
||||
if (clientAuthServiceHashSet.isEmpty()) {
|
||||
LOGGER.warn("[ClientAuthPluginManager] Load ClientAuthService fail, No ClientAuthService implements");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get all ClientAuthService instance.
|
||||
*
|
||||
* @return ClientAuthService Set.
|
||||
*/
|
||||
public Set<ClientAuthService> getAuthServiceSpiImplSet() {
|
||||
return clientAuthServiceHashSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() throws NacosException {
|
||||
for (ClientAuthService each : clientAuthServiceHashSet) {
|
||||
each.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.spi.client;
|
||||
|
||||
import com.alibaba.nacos.plugin.auth.api.LoginIdentityContext;
|
||||
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
|
||||
import com.alibaba.nacos.common.lifecycle.Closeable;
|
||||
import com.alibaba.nacos.plugin.auth.api.RequestResource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Client AuthService.
|
||||
*
|
||||
* @author wuyfee
|
||||
*/
|
||||
public interface ClientAuthService extends Closeable {
|
||||
|
||||
/**
|
||||
* login(request) to service and get response.
|
||||
*
|
||||
* @param properties login auth information.
|
||||
* @return boolean whether login success.
|
||||
*/
|
||||
Boolean login(Properties properties);
|
||||
|
||||
/**
|
||||
* set login serverList.
|
||||
*
|
||||
* @param serverList login server list;
|
||||
*/
|
||||
void setServerList(List<String> serverList);
|
||||
|
||||
/**
|
||||
* http request template.
|
||||
*
|
||||
* @param nacosRestTemplate nacos http request template.
|
||||
*/
|
||||
void setNacosRestTemplate(NacosRestTemplate nacosRestTemplate);
|
||||
|
||||
/**
|
||||
* get login identity context.
|
||||
*
|
||||
* @param resource resource for this request, some of plugin implementation will use this resource to generate their
|
||||
* identity context. If no need to use can ignore it.
|
||||
* @return LoginIdentityContext this plugin loginIdentityContext.
|
||||
*/
|
||||
LoginIdentityContext getLoginIdentityContext(RequestResource resource);
|
||||
|
||||
}
|
||||
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.spi.server;
|
||||
|
||||
import com.alibaba.nacos.common.spi.NacosServiceLoader;
|
||||
import com.alibaba.nacos.common.utils.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Load Plugins.
|
||||
*
|
||||
* @author Wuyfee
|
||||
* @author xiweng.yy
|
||||
*/
|
||||
public class AuthPluginManager {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AuthPluginManager.class);
|
||||
|
||||
private static final AuthPluginManager INSTANCE = new AuthPluginManager();
|
||||
|
||||
/**
|
||||
* The relationship of context type and {@link AuthPluginService}.
|
||||
*/
|
||||
private final Map<String, AuthPluginService> authServiceMap = new HashMap<>();
|
||||
|
||||
private AuthPluginManager() {
|
||||
initAuthServices();
|
||||
}
|
||||
|
||||
private void initAuthServices() {
|
||||
Collection<AuthPluginService> authPluginServices = NacosServiceLoader.load(AuthPluginService.class);
|
||||
for (AuthPluginService each : authPluginServices) {
|
||||
if (StringUtils.isEmpty(each.getAuthServiceName())) {
|
||||
LOGGER.warn(
|
||||
"[AuthPluginManager] Load AuthPluginService({}) AuthServiceName(null/empty) fail. Please Add AuthServiceName to resolve.",
|
||||
each.getClass());
|
||||
continue;
|
||||
}
|
||||
authServiceMap.put(each.getAuthServiceName(), each);
|
||||
LOGGER.info("[AuthPluginManager] Load AuthPluginService({}) AuthServiceName({}) successfully.",
|
||||
each.getClass(), each.getAuthServiceName());
|
||||
}
|
||||
}
|
||||
|
||||
public static AuthPluginManager getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* get AuthPluginService instance which AuthPluginService.getType() is type.
|
||||
*
|
||||
* @param authServiceName AuthServiceName, mark a AuthPluginService instance.
|
||||
* @return AuthPluginService instance.
|
||||
*/
|
||||
public Optional<AuthPluginService> findAuthServiceSpiImpl(String authServiceName) {
|
||||
return Optional.ofNullable(authServiceMap.get(authServiceName));
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.spi.server;
|
||||
|
||||
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
|
||||
import com.alibaba.nacos.plugin.auth.api.Permission;
|
||||
import com.alibaba.nacos.plugin.auth.api.Resource;
|
||||
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
|
||||
import com.alibaba.nacos.plugin.auth.exception.AccessException;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Auth service.
|
||||
*
|
||||
* @author Wuyfee
|
||||
* @author xiweng.yy
|
||||
*/
|
||||
public interface AuthPluginService {
|
||||
|
||||
/**
|
||||
* Define which identity information needed from request. e.q: username, password, accessToken.
|
||||
*
|
||||
* @return identity names
|
||||
*/
|
||||
Collection<String> identityNames();
|
||||
|
||||
/**
|
||||
* Judgement whether this plugin enable auth for this action and type.
|
||||
*
|
||||
* @param action action of request, see {@link ActionTypes}
|
||||
* @param type type of request, see {@link com.alibaba.nacos.plugin.auth.constant.SignType}
|
||||
* @return @return {@code true} if enable auth, otherwise {@code false}
|
||||
*/
|
||||
boolean enableAuth(ActionTypes action, String type);
|
||||
|
||||
/**
|
||||
* To validate whether the identity context from request is legal or illegal.
|
||||
*
|
||||
* @param identityContext where we can find the user information
|
||||
* @param resource resource about this user information
|
||||
* @return {@code true} if legal, otherwise {@code false}
|
||||
* @throws AccessException if authentication is failed
|
||||
*/
|
||||
boolean validateIdentity(IdentityContext identityContext, Resource resource) throws AccessException;
|
||||
|
||||
/**
|
||||
* Validate the identity whether has the resource authority.
|
||||
*
|
||||
* @param identityContext where we can find the user information.
|
||||
* @param permission permission to auth.
|
||||
* @return Boolean if the user has the resource authority.
|
||||
* @throws AccessException if authentication is failed
|
||||
*/
|
||||
Boolean validateAuthority(IdentityContext identityContext, Permission permission) throws AccessException;
|
||||
|
||||
/**
|
||||
* AuthPluginService Name which for conveniently find AuthPluginService instance.
|
||||
*
|
||||
* @return AuthServiceName mark a AuthPluginService instance.
|
||||
*/
|
||||
String getAuthServiceName();
|
||||
|
||||
/**
|
||||
* Is the plugin enable login.
|
||||
*
|
||||
* @return {@code true} if plugin need login, otherwise {@code false}
|
||||
* @since 2.2.2
|
||||
*/
|
||||
default boolean isLoginEnabled() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.api;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class IdentityContextTest {
|
||||
|
||||
private static final String TEST = "test";
|
||||
|
||||
private IdentityContext identityContext;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
identityContext = new IdentityContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetParameter() {
|
||||
assertNull(identityContext.getParameter(TEST));
|
||||
identityContext.setParameter(TEST, TEST);
|
||||
assertEquals(TEST, identityContext.getParameter(TEST));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetParameterWithDefaultValue() {
|
||||
assertEquals(TEST, identityContext.getParameter(TEST, TEST));
|
||||
identityContext.setParameter(TEST, TEST + "new");
|
||||
assertEquals(TEST + "new", identityContext.getParameter(TEST, TEST));
|
||||
long actual = identityContext.getParameter(TEST, 1L);
|
||||
assertEquals(1L, actual);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testGetParameterWithNullDefaultValue() {
|
||||
identityContext.getParameter(TEST, null);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.api;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class LoginIdentityContextTest {
|
||||
|
||||
private static final String TEST = "test";
|
||||
|
||||
private LoginIdentityContext loginIdentityContext;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
loginIdentityContext = new LoginIdentityContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetParameter() {
|
||||
assertNull(loginIdentityContext.getParameter(TEST));
|
||||
assertTrue(loginIdentityContext.getAllKey().isEmpty());
|
||||
loginIdentityContext.setParameter(TEST, TEST);
|
||||
assertEquals(TEST, loginIdentityContext.getParameter(TEST));
|
||||
assertEquals(1, loginIdentityContext.getAllKey().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetParameters() {
|
||||
assertNull(loginIdentityContext.getParameter(TEST));
|
||||
assertTrue(loginIdentityContext.getAllKey().isEmpty());
|
||||
Map<String, String> map = new HashMap<>(2);
|
||||
map.put(TEST, TEST);
|
||||
map.put(TEST + "2", TEST);
|
||||
loginIdentityContext.setParameters(map);
|
||||
assertEquals(TEST, loginIdentityContext.getParameter(TEST));
|
||||
assertEquals(TEST, loginIdentityContext.getParameter(TEST + "2"));
|
||||
assertEquals(2, loginIdentityContext.getAllKey().size());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.api;
|
||||
|
||||
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class PermissionTest {
|
||||
|
||||
private Permission permission;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
permission = new Permission(Resource.EMPTY_RESOURCE, ActionTypes.WRITE.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
assertEquals(
|
||||
"Permission{resource='Resource{namespaceId='', group='', name='', type='', properties=null}', action='w'}",
|
||||
permission.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetResource() {
|
||||
Permission permission = new Permission();
|
||||
Properties properties = new Properties();
|
||||
Resource resource = new Resource("NS", "G", "N", "TEST", properties);
|
||||
permission.setResource(resource);
|
||||
assertEquals("NS", permission.getResource().getNamespaceId());
|
||||
assertEquals("G", permission.getResource().getGroup());
|
||||
assertEquals("N", permission.getResource().getName());
|
||||
assertEquals("TEST", permission.getResource().getType());
|
||||
assertEquals(properties, permission.getResource().getProperties());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetAction() {
|
||||
Permission permission = new Permission();
|
||||
permission.setAction(ActionTypes.READ.toString());
|
||||
assertEquals(ActionTypes.READ.toString(), permission.getAction());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.api;
|
||||
|
||||
import com.alibaba.nacos.plugin.auth.constant.SignType;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class RequestResourceTest {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildNamingRequestResource() {
|
||||
RequestResource actual = RequestResource.namingBuilder().setNamespace("NS").setGroup("G").setResource("Service")
|
||||
.build();
|
||||
assertEquals(SignType.NAMING, actual.getType());
|
||||
assertEquals("NS", actual.getNamespace());
|
||||
assertEquals("G", actual.getGroup());
|
||||
assertEquals("Service", actual.getResource());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildConfigRequestResource() {
|
||||
RequestResource actual = RequestResource.configBuilder().setNamespace("NS").setGroup("G").setResource("dataId")
|
||||
.build();
|
||||
assertEquals(SignType.CONFIG, actual.getType());
|
||||
assertEquals("NS", actual.getNamespace());
|
||||
assertEquals("G", actual.getGroup());
|
||||
assertEquals("dataId", actual.getResource());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.constant;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ActionTypesTest {
|
||||
|
||||
@Test
|
||||
public void testToStringForRead() {
|
||||
ActionTypes actual = ActionTypes.valueOf("READ");
|
||||
assertEquals("r", actual.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToStringForWrite() {
|
||||
ActionTypes actual = ActionTypes.valueOf("WRITE");
|
||||
assertEquals("w", actual.toString());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.constant;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ConstantsTest {
|
||||
|
||||
@Test
|
||||
public void testConstantsForAuth() {
|
||||
assertEquals("nacos.core.auth.enabled", Constants.Auth.NACOS_CORE_AUTH_ENABLED);
|
||||
assertEquals("nacos.core.auth.system.type", Constants.Auth.NACOS_CORE_AUTH_SYSTEM_TYPE);
|
||||
assertEquals("nacos.core.auth.caching.enabled", Constants.Auth.NACOS_CORE_AUTH_CACHING_ENABLED);
|
||||
assertEquals("nacos.core.auth.server.identity.key", Constants.Auth.NACOS_CORE_AUTH_SERVER_IDENTITY_KEY);
|
||||
assertEquals("nacos.core.auth.server.identity.value", Constants.Auth.NACOS_CORE_AUTH_SERVER_IDENTITY_VALUE);
|
||||
assertEquals("nacos.core.auth.enable.userAgentAuthWhite",
|
||||
Constants.Auth.NACOS_CORE_AUTH_ENABLE_USER_AGENT_AUTH_WHITE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstantsForResource() {
|
||||
assertEquals(":", Constants.Resource.SPLITTER);
|
||||
assertEquals("*", Constants.Resource.ANY);
|
||||
assertEquals("action", Constants.Resource.ACTION);
|
||||
assertEquals("requestClass", Constants.Resource.REQUEST_CLASS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstantsForIdentity() {
|
||||
assertEquals("identity_id", Constants.Identity.IDENTITY_ID);
|
||||
assertEquals("X-Real-IP", Constants.Identity.X_REAL_IP);
|
||||
assertEquals("remote_ip", Constants.Identity.REMOTE_IP);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstantsForSignType() {
|
||||
assertEquals("naming", SignType.NAMING);
|
||||
assertEquals("config", SignType.CONFIG);
|
||||
assertEquals("console", SignType.CONSOLE);
|
||||
assertEquals("specified", SignType.SPECIFIED);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.exception;
|
||||
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class AccessExceptionTest {
|
||||
|
||||
@Test
|
||||
public void testNewAccessExceptionWithCode() {
|
||||
AccessException actual = new AccessException(403);
|
||||
assertEquals(403, actual.getErrCode());
|
||||
assertEquals(Constants.NULL, actual.getErrMsg());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNewAccessExceptionWithMsg() {
|
||||
AccessException actual = new AccessException("Test");
|
||||
assertEquals("Test", actual.getErrMsg());
|
||||
assertEquals(0, actual.getErrCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNewAccessExceptionWithNoArgs() {
|
||||
AccessException actual = new AccessException();
|
||||
assertEquals(Constants.NULL, actual.getErrMsg());
|
||||
assertEquals(0, actual.getErrCode());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.spi.client;
|
||||
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
|
||||
import com.alibaba.nacos.common.spi.NacosServiceLoader;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* {@link ClientAuthPluginManager} unit test.
|
||||
*
|
||||
* @author wuyfee
|
||||
* @date 2021-08-12 12:56
|
||||
*/
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ClientAuthPluginManagerTest {
|
||||
|
||||
private ClientAuthPluginManager clientAuthPluginManager;
|
||||
|
||||
@Mock
|
||||
private List<String> serverlist;
|
||||
|
||||
@Mock
|
||||
private NacosRestTemplate nacosRestTemplate;
|
||||
|
||||
@Before
|
||||
public void setUp() throws NoSuchFieldException, IllegalAccessException {
|
||||
clientAuthPluginManager = new ClientAuthPluginManager();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws NacosException, NoSuchFieldException, IllegalAccessException {
|
||||
getServiceLoaderMap().remove(AbstractClientAuthService.class);
|
||||
clientAuthPluginManager.shutdown();
|
||||
}
|
||||
|
||||
private Map<Class<?>, Collection<Class<?>>> getServiceLoaderMap()
|
||||
throws NoSuchFieldException, IllegalAccessException {
|
||||
Field servicesField = NacosServiceLoader.class.getDeclaredField("SERVICES");
|
||||
servicesField.setAccessible(true);
|
||||
return (Map<Class<?>, Collection<Class<?>>>) servicesField.get(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAuthServiceSpiImplSet() {
|
||||
clientAuthPluginManager.init(serverlist, nacosRestTemplate);
|
||||
Set<ClientAuthService> clientAuthServiceSet = clientAuthPluginManager.getAuthServiceSpiImplSet();
|
||||
Assert.assertFalse(clientAuthServiceSet.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAuthServiceSpiImplSetForEmpty() throws NoSuchFieldException, IllegalAccessException {
|
||||
getServiceLoaderMap().put(AbstractClientAuthService.class, Collections.emptyList());
|
||||
clientAuthPluginManager.init(serverlist, nacosRestTemplate);
|
||||
Set<ClientAuthService> clientAuthServiceSet = clientAuthPluginManager.getAuthServiceSpiImplSet();
|
||||
Assert.assertTrue(clientAuthServiceSet.isEmpty());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.spi.mock;
|
||||
|
||||
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
|
||||
import com.alibaba.nacos.plugin.auth.api.Permission;
|
||||
import com.alibaba.nacos.plugin.auth.api.Resource;
|
||||
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
|
||||
import com.alibaba.nacos.plugin.auth.exception.AccessException;
|
||||
import com.alibaba.nacos.plugin.auth.spi.server.AuthPluginService;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* Mock Server Auth Plugin Service.
|
||||
*
|
||||
* @author xiweng.yy
|
||||
*/
|
||||
public class MockAuthPluginService implements AuthPluginService {
|
||||
|
||||
@Override
|
||||
public Collection<String> identityNames() {
|
||||
return Collections.singletonList("mock");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean enableAuth(ActionTypes action, String type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validateIdentity(IdentityContext identityContext, Resource resource) throws AccessException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean validateAuthority(IdentityContext identityContext, Permission permission) throws AccessException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthServiceName() {
|
||||
return "mock";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.spi.mock;
|
||||
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.plugin.auth.api.LoginIdentityContext;
|
||||
import com.alibaba.nacos.plugin.auth.api.RequestResource;
|
||||
import com.alibaba.nacos.plugin.auth.spi.client.AbstractClientAuthService;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public class MockClientAuthService extends AbstractClientAuthService {
|
||||
|
||||
@Override
|
||||
public Boolean login(Properties properties) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginIdentityContext getLoginIdentityContext(RequestResource resource) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() throws NacosException {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.spi.mock;
|
||||
|
||||
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
|
||||
import com.alibaba.nacos.plugin.auth.api.Permission;
|
||||
import com.alibaba.nacos.plugin.auth.api.Resource;
|
||||
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
|
||||
import com.alibaba.nacos.plugin.auth.exception.AccessException;
|
||||
import com.alibaba.nacos.plugin.auth.spi.server.AuthPluginService;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Mock Server Auth Plugin Service.
|
||||
*
|
||||
* @author xiweng.yy
|
||||
*/
|
||||
public class MockEmptyNameAuthPluginService implements AuthPluginService {
|
||||
|
||||
@Override
|
||||
public Collection<String> identityNames() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean enableAuth(ActionTypes action, String type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validateIdentity(IdentityContext identityContext, Resource resource) throws AccessException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean validateAuthority(IdentityContext identityContext, Permission permission) throws AccessException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthServiceName() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 1999-2021 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.plugin.auth.spi.server;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* {@link AuthPluginManager} unit test.
|
||||
*
|
||||
* @author wuyfee
|
||||
* @date 2021-08-12 12:56
|
||||
*/
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AuthPluginManagerTest {
|
||||
|
||||
private AuthPluginManager authPluginManager;
|
||||
|
||||
@Mock
|
||||
private AuthPluginService authPluginService;
|
||||
|
||||
private static final String TYPE = "test";
|
||||
|
||||
@Before
|
||||
public void setUp() throws NoSuchFieldException, IllegalAccessException {
|
||||
authPluginManager = AuthPluginManager.getInstance();
|
||||
Class<AuthPluginManager> authPluginManagerClass = AuthPluginManager.class;
|
||||
Field authPlugins = authPluginManagerClass.getDeclaredField("authServiceMap");
|
||||
authPlugins.setAccessible(true);
|
||||
Map<String, AuthPluginService> authServiceMap = (Map<String, AuthPluginService>) authPlugins
|
||||
.get(authPluginManager);
|
||||
authServiceMap.put(TYPE, authPluginService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetInstance() {
|
||||
AuthPluginManager instance = AuthPluginManager.getInstance();
|
||||
|
||||
Assert.assertNotNull(instance);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAuthServiceSpiImpl() {
|
||||
Optional<AuthPluginService> authServiceImpl = authPluginManager.findAuthServiceSpiImpl(TYPE);
|
||||
Assert.assertTrue(authServiceImpl.isPresent());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
#
|
||||
# Copyright 1999-2021 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.
|
||||
#
|
||||
|
||||
com.alibaba.nacos.plugin.auth.spi.mock.MockClientAuthService
|
||||
@ -0,0 +1,18 @@
|
||||
#
|
||||
# Copyright 1999-2021 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.
|
||||
#
|
||||
|
||||
com.alibaba.nacos.plugin.auth.spi.mock.MockAuthPluginService
|
||||
com.alibaba.nacos.plugin.auth.spi.mock.MockEmptyNameAuthPluginService
|
||||
Reference in New Issue
Block a user