Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Apache RocketMQ
Rocketmq
提交
020f4b4c
R
Rocketmq
项目概览
Apache RocketMQ
/
Rocketmq
上一次同步 大约 3 年
通知
267
Star
16139
Fork
68
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rocketmq
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
020f4b4c
编写于
12月 11, 2018
作者:
L
laohu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
clean code
上级
23a24c44
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
205 addition
and
155 deletion
+205
-155
acl/pom.xml
acl/pom.xml
+0
-12
acl/src/main/java/org/apache/rocketmq/acl/common/AclClientRPCHook.java
...java/org/apache/rocketmq/acl/common/AclClientRPCHook.java
+8
-8
acl/src/main/java/org/apache/rocketmq/acl/common/AclSigner.java
...c/main/java/org/apache/rocketmq/acl/common/AclSigner.java
+6
-6
acl/src/main/java/org/apache/rocketmq/acl/common/AclUtils.java
...rc/main/java/org/apache/rocketmq/acl/common/AclUtils.java
+1
-1
acl/src/main/java/org/apache/rocketmq/acl/common/Permission.java
.../main/java/org/apache/rocketmq/acl/common/Permission.java
+11
-10
acl/src/main/java/org/apache/rocketmq/acl/common/SessionCredentials.java
...va/org/apache/rocketmq/acl/common/SessionCredentials.java
+9
-9
acl/src/main/java/org/apache/rocketmq/acl/plain/PlainAccessResource.java
...va/org/apache/rocketmq/acl/plain/PlainAccessResource.java
+1
-1
acl/src/main/java/org/apache/rocketmq/acl/plain/PlainAccessValidator.java
...a/org/apache/rocketmq/acl/plain/PlainAccessValidator.java
+4
-5
acl/src/main/java/org/apache/rocketmq/acl/plain/PlainPermissionLoader.java
.../org/apache/rocketmq/acl/plain/PlainPermissionLoader.java
+98
-13
acl/src/test/java/org/apache/rocketmq/acl/common/PermissionTest.java
...t/java/org/apache/rocketmq/acl/common/PermissionTest.java
+5
-4
acl/src/test/java/org/apache/rocketmq/acl/plain/PlainAccessValidatorTest.java
...g/apache/rocketmq/acl/plain/PlainAccessValidatorTest.java
+1
-1
acl/src/test/java/org/apache/rocketmq/acl/plain/PlainPermissionLoaderTest.java
.../apache/rocketmq/acl/plain/PlainPermissionLoaderTest.java
+61
-85
未找到文件。
acl/pom.xml
浏览文件 @
020f4b4c
...
...
@@ -49,17 +49,5 @@
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
</dependency>
<dependency>
<groupId>
org.powermock
</groupId>
<artifactId>
powermock-module-junit4
</artifactId>
<version>
1.7.1
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.powermock
</groupId>
<artifactId>
powermock-api-mockito2
</artifactId>
<version>
1.7.1
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
</project>
acl/src/main/java/org/apache/rocketmq/acl/common/AclClientRPCHook.java
浏览文件 @
020f4b4c
...
...
@@ -24,9 +24,9 @@ import org.apache.rocketmq.remoting.CommandCustomHeader;
import
org.apache.rocketmq.remoting.RPCHook
;
import
org.apache.rocketmq.remoting.protocol.RemotingCommand
;
import
static
org
.
apache
.
rocketmq
.
acl
.
common
.
SessionCredentials
.
A
ccessKey
;
import
static
org
.
apache
.
rocketmq
.
acl
.
common
.
SessionCredentials
.
S
ecurityToken
;
import
static
org
.
apache
.
rocketmq
.
acl
.
common
.
SessionCredentials
.
S
ignature
;
import
static
org
.
apache
.
rocketmq
.
acl
.
common
.
SessionCredentials
.
A
CCESS_KEY
;
import
static
org
.
apache
.
rocketmq
.
acl
.
common
.
SessionCredentials
.
S
ECURITY_TOKEN
;
import
static
org
.
apache
.
rocketmq
.
acl
.
common
.
SessionCredentials
.
S
IGNATURE
;
public
class
AclClientRPCHook
implements
RPCHook
{
private
final
SessionCredentials
sessionCredentials
;
...
...
@@ -42,11 +42,11 @@ public class AclClientRPCHook implements RPCHook {
byte
[]
total
=
AclUtils
.
combineRequestContent
(
request
,
parseRequestContent
(
request
,
sessionCredentials
.
getAccessKey
(),
sessionCredentials
.
getSecurityToken
()));
String
signature
=
AclUtils
.
calSignature
(
total
,
sessionCredentials
.
getSecretKey
());
request
.
addExtField
(
S
ignature
,
signature
);
request
.
addExtField
(
A
ccessKey
,
sessionCredentials
.
getAccessKey
());
request
.
addExtField
(
S
IGNATURE
,
signature
);
request
.
addExtField
(
A
CCESS_KEY
,
sessionCredentials
.
getAccessKey
());
if
(
sessionCredentials
.
getSecurityToken
()
!=
null
)
{
request
.
addExtField
(
S
ecurityToken
,
sessionCredentials
.
getSecurityToken
());
request
.
addExtField
(
S
ECURITY_TOKEN
,
sessionCredentials
.
getSecurityToken
());
}
}
...
...
@@ -59,9 +59,9 @@ public class AclClientRPCHook implements RPCHook {
CommandCustomHeader
header
=
request
.
readCustomHeader
();
// sort property
SortedMap
<
String
,
String
>
map
=
new
TreeMap
<
String
,
String
>();
map
.
put
(
A
ccessKey
,
ak
);
map
.
put
(
A
CCESS_KEY
,
ak
);
if
(
securityToken
!=
null
)
{
map
.
put
(
S
ecurityToken
,
securityToken
);
map
.
put
(
S
ECURITY_TOKEN
,
securityToken
);
}
try
{
// add header properties
...
...
acl/src/main/java/org/apache/rocketmq/acl/common/AclSigner.java
浏览文件 @
020f4b4c
...
...
@@ -25,14 +25,14 @@ import org.apache.rocketmq.logging.InternalLogger;
import
org.apache.rocketmq.logging.InternalLoggerFactory
;
public
class
AclSigner
{
public
static
final
Charset
defaultCharset
=
Charset
.
forName
(
"UTF-8"
);
public
static
final
SigningAlgorithm
defaultAlgorithm
=
SigningAlgorithm
.
HmacSHA1
;
public
static
final
Charset
DEFAULT_CHARSET
=
Charset
.
forName
(
"UTF-8"
);
public
static
final
SigningAlgorithm
DEFAULT_ALGORITHM
=
SigningAlgorithm
.
HmacSHA1
;
private
static
final
InternalLogger
log
=
InternalLoggerFactory
.
getLogger
(
LoggerName
.
ROCKETMQ_AUTHORIZE_LOGGER_NAME
);
private
static
final
int
CAL_SIGNATURE_FAILED
=
10015
;
private
static
final
String
CAL_SIGNATURE_FAILED_MSG
=
"[%s:signature-failed] unable to calculate a request signature. error=%s"
;
public
static
String
calSignature
(
String
data
,
String
key
)
throws
AclException
{
return
calSignature
(
data
,
key
,
defaultAlgorithm
,
defaultCharset
);
return
calSignature
(
data
,
key
,
DEFAULT_ALGORITHM
,
DEFAULT_CHARSET
);
}
public
static
String
calSignature
(
String
data
,
String
key
,
SigningAlgorithm
algorithm
,
...
...
@@ -44,7 +44,7 @@ public class AclSigner {
throws
AclException
{
try
{
byte
[]
signature
=
sign
(
data
.
getBytes
(
charset
),
key
.
getBytes
(
charset
),
algorithm
);
return
new
String
(
Base64
.
encodeBase64
(
signature
),
defaultCharset
);
return
new
String
(
Base64
.
encodeBase64
(
signature
),
DEFAULT_CHARSET
);
}
catch
(
Exception
e
)
{
String
message
=
String
.
format
(
CAL_SIGNATURE_FAILED_MSG
,
CAL_SIGNATURE_FAILED
,
e
.
getMessage
());
log
.
error
(
message
,
e
);
...
...
@@ -65,7 +65,7 @@ public class AclSigner {
}
public
static
String
calSignature
(
byte
[]
data
,
String
key
)
throws
AclException
{
return
calSignature
(
data
,
key
,
defaultAlgorithm
,
defaultCharset
);
return
calSignature
(
data
,
key
,
DEFAULT_ALGORITHM
,
DEFAULT_CHARSET
);
}
public
static
String
calSignature
(
byte
[]
data
,
String
key
,
SigningAlgorithm
algorithm
,
...
...
@@ -77,7 +77,7 @@ public class AclSigner {
throws
AclException
{
try
{
byte
[]
signature
=
sign
(
data
,
key
.
getBytes
(
charset
),
algorithm
);
return
new
String
(
Base64
.
encodeBase64
(
signature
),
defaultCharset
);
return
new
String
(
Base64
.
encodeBase64
(
signature
),
DEFAULT_CHARSET
);
}
catch
(
Exception
e
)
{
String
message
=
String
.
format
(
CAL_SIGNATURE_FAILED_MSG
,
CAL_SIGNATURE_FAILED
,
e
.
getMessage
());
log
.
error
(
message
,
e
);
...
...
acl/src/main/java/org/apache/rocketmq/acl/common/AclUtils.java
浏览文件 @
020f4b4c
...
...
@@ -33,7 +33,7 @@ public class AclUtils {
try
{
StringBuilder
sb
=
new
StringBuilder
(
""
);
for
(
Map
.
Entry
<
String
,
String
>
entry
:
fieldsMap
.
entrySet
())
{
if
(!
SessionCredentials
.
S
ignature
.
equals
(
entry
.
getKey
()))
{
if
(!
SessionCredentials
.
S
IGNATURE
.
equals
(
entry
.
getKey
()))
{
sb
.
append
(
entry
.
getValue
());
}
}
...
...
acl/src/main/java/org/apache/rocketmq/acl/common/Permission.java
浏览文件 @
020f4b4c
...
...
@@ -16,11 +16,12 @@
*/
package
org.apache.rocketmq.acl.common
;
import
com.alibaba.fastjson.JSONArray
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Set
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.rocketmq.acl.plain.PlainAccessResource
;
import
org.apache.rocketmq.common.protocol.RequestCode
;
public
class
Permission
{
...
...
@@ -33,15 +34,15 @@ public class Permission {
static
{
// UPDATE_AND_CREATE_TOPIC
ADMIN_CODE
.
add
(
17
);
ADMIN_CODE
.
add
(
RequestCode
.
UPDATE_AND_CREATE_TOPIC
);
// UPDATE_BROKER_CONFIG
ADMIN_CODE
.
add
(
25
);
ADMIN_CODE
.
add
(
RequestCode
.
UPDATE_BROKER_CONFIG
);
// DELETE_TOPIC_IN_BROKER
ADMIN_CODE
.
add
(
215
);
ADMIN_CODE
.
add
(
RequestCode
.
DELETE_TOPIC_IN_BROKER
);
// UPDATE_AND_CREATE_SUBSCRIPTIONGROUP
ADMIN_CODE
.
add
(
200
);
ADMIN_CODE
.
add
(
RequestCode
.
UPDATE_AND_CREATE_SUBSCRIPTIONGROUP
);
// DELETE_SUBSCRIPTIONGROUP
ADMIN_CODE
.
add
(
207
);
ADMIN_CODE
.
add
(
RequestCode
.
DELETE_SUBSCRIPTIONGROUP
);
}
public
static
boolean
checkPermission
(
byte
neededPerm
,
byte
ownedPerm
)
{
...
...
@@ -76,16 +77,16 @@ public class Permission {
}
}
public
static
void
setTopicPerm
(
PlainAccessResource
plainAccessResource
,
Boolean
isTopic
,
JSONArray
topicArray
)
{
public
static
void
setTopicPerm
(
PlainAccessResource
plainAccessResource
,
Boolean
isTopic
,
List
<
String
>
topicArray
)
{
if
(
topicArray
==
null
||
topicArray
.
isEmpty
())
{
return
;
}
for
(
int
i
=
0
;
i
<
topicArray
.
size
();
i
++
)
{
String
[]
topicPrem
=
StringUtils
.
split
(
topic
Array
.
getString
(
i
)
,
"="
);
for
(
String
topic
:
topicArray
)
{
String
[]
topicPrem
=
StringUtils
.
split
(
topic
,
"="
);
if
(
topicPrem
.
length
==
2
)
{
plainAccessResource
.
addResourceAndPerm
(
isTopic
?
topicPrem
[
0
]
:
PlainAccessResource
.
getRetryTopic
(
topicPrem
[
0
]),
fromStringGetPermission
(
topicPrem
[
1
]));
}
else
{
throw
new
AclException
(
String
.
format
(
"%s Permission config erron %s"
,
isTopic
?
"topic"
:
"group"
,
topic
Array
.
getString
(
i
)
));
throw
new
AclException
(
String
.
format
(
"%s Permission config erron %s"
,
isTopic
?
"topic"
:
"group"
,
topic
));
}
}
}
...
...
acl/src/main/java/org/apache/rocketmq/acl/common/SessionCredentials.java
浏览文件 @
020f4b4c
...
...
@@ -24,12 +24,12 @@ import org.apache.rocketmq.common.MixAll;
public
class
SessionCredentials
{
public
static
final
Charset
CHARSET
=
Charset
.
forName
(
"UTF-8"
);
public
static
final
String
A
ccessKey
=
"AccessKey"
;
public
static
final
String
S
ecretKey
=
"SecretKey"
;
public
static
final
String
S
ignature
=
"Signature"
;
public
static
final
String
S
ecurityToken
=
"SecurityToken"
;
public
static
final
String
A
CCESS_KEY
=
"AccessKey"
;
public
static
final
String
S
ECRET_KEY
=
"SecretKey"
;
public
static
final
String
S
IGNATURE
=
"Signature"
;
public
static
final
String
S
ECURITY_TOKEN
=
"SecurityToken"
;
public
static
final
String
K
eyFile
=
System
.
getProperty
(
"rocketmq.client.keyFile"
,
public
static
final
String
K
EY_FILE
=
System
.
getProperty
(
"rocketmq.client.keyFile"
,
System
.
getProperty
(
"user.home"
)
+
File
.
separator
+
"onskey"
);
private
String
accessKey
;
...
...
@@ -40,7 +40,7 @@ public class SessionCredentials {
public
SessionCredentials
()
{
String
keyContent
=
null
;
try
{
keyContent
=
MixAll
.
file2String
(
K
eyFile
);
keyContent
=
MixAll
.
file2String
(
K
EY_FILE
);
}
catch
(
IOException
ignore
)
{
}
if
(
keyContent
!=
null
)
{
...
...
@@ -63,19 +63,19 @@ public class SessionCredentials {
public
void
updateContent
(
Properties
prop
)
{
{
String
value
=
prop
.
getProperty
(
A
ccessKey
);
String
value
=
prop
.
getProperty
(
A
CCESS_KEY
);
if
(
value
!=
null
)
{
this
.
accessKey
=
value
.
trim
();
}
}
{
String
value
=
prop
.
getProperty
(
S
ecretKey
);
String
value
=
prop
.
getProperty
(
S
ECRET_KEY
);
if
(
value
!=
null
)
{
this
.
secretKey
=
value
.
trim
();
}
}
{
String
value
=
prop
.
getProperty
(
S
ecurityToken
);
String
value
=
prop
.
getProperty
(
S
ECURITY_TOKEN
);
if
(
value
!=
null
)
{
this
.
securityToken
=
value
.
trim
();
}
...
...
acl/src/main/java/org/apache/rocketmq/acl/plain/PlainAccessResource.java
浏览文件 @
020f4b4c
...
...
@@ -56,7 +56,7 @@ public class PlainAccessResource implements AccessResource {
}
public
static
boolean
isRetryTopic
(
String
topic
)
{
return
(
null
!=
topic
&&
topic
.
startsWith
(
MixAll
.
RETRY_GROUP_TOPIC_PREFIX
)
);
return
null
!=
topic
&&
topic
.
startsWith
(
MixAll
.
RETRY_GROUP_TOPIC_PREFIX
);
}
public
static
String
getRetryTopic
(
String
group
)
{
...
...
acl/src/main/java/org/apache/rocketmq/acl/plain/PlainAccessValidator.java
浏览文件 @
020f4b4c
...
...
@@ -49,12 +49,11 @@ public class PlainAccessValidator implements AccessValidator {
PlainAccessResource
accessResource
=
new
PlainAccessResource
();
accessResource
.
setWhiteRemoteAddress
(
remoteAddr
);
accessResource
.
setRequestCode
(
request
.
getCode
());
accessResource
.
setAccessKey
(
request
.
getExtFields
().
get
(
SessionCredentials
.
A
ccessKey
));
accessResource
.
setSignature
(
request
.
getExtFields
().
get
(
SessionCredentials
.
S
ignature
));
accessResource
.
setSecretToken
(
request
.
getExtFields
().
get
(
SessionCredentials
.
S
ecurityToken
));
accessResource
.
setAccessKey
(
request
.
getExtFields
().
get
(
SessionCredentials
.
A
CCESS_KEY
));
accessResource
.
setSignature
(
request
.
getExtFields
().
get
(
SessionCredentials
.
S
IGNATURE
));
accessResource
.
setSecretToken
(
request
.
getExtFields
().
get
(
SessionCredentials
.
S
ECURITY_TOKEN
));
try
{
// resource 和 permission 转换
switch
(
request
.
getCode
())
{
case
RequestCode
.
SEND_MESSAGE
:
accessResource
.
addResourceAndPerm
(
request
.
getExtFields
().
get
(
"topic"
),
Permission
.
PUB
);
...
...
@@ -111,7 +110,7 @@ public class PlainAccessValidator implements AccessValidator {
// content
SortedMap
<
String
,
String
>
map
=
new
TreeMap
<
String
,
String
>();
for
(
Map
.
Entry
<
String
,
String
>
entry
:
request
.
getExtFields
().
entrySet
())
{
if
(!
SessionCredentials
.
S
ignature
.
equals
(
entry
.
getKey
()))
{
if
(!
SessionCredentials
.
S
IGNATURE
.
equals
(
entry
.
getKey
()))
{
map
.
put
(
entry
.
getKey
(),
entry
.
getValue
());
}
}
...
...
acl/src/main/java/org/apache/rocketmq/acl/plain/PlainPermissionLoader.java
浏览文件 @
020f4b4c
...
...
@@ -70,7 +70,7 @@ public class PlainPermissionLoader {
JSONObject
.
class
);
if
(
accessControlTransport
==
null
||
accessControlTransport
.
isEmpty
())
{
throw
new
AclException
(
"transport.yml file is not data"
);
throw
new
AclException
(
String
.
format
(
"%s file is not data"
,
fileHome
+
fileName
)
);
}
log
.
info
(
"BorkerAccessControlTransport data is : "
,
accessControlTransport
.
toString
());
JSONArray
globalWhiteRemoteAddressesList
=
accessControlTransport
.
getJSONArray
(
"globalWhiteRemoteAddresses"
);
...
...
@@ -81,9 +81,10 @@ public class PlainPermissionLoader {
}
JSONArray
accounts
=
accessControlTransport
.
getJSONArray
(
"accounts"
);
if
(
accounts
!=
null
&&
!
accounts
.
isEmpty
())
{
for
(
int
i
=
0
;
i
<
accounts
.
size
();
i
++)
{
this
.
setPlainAccessResource
(
getPlainAccessResource
(
accounts
.
getJSONObject
(
i
)));
List
<
PlainAccess
>
plainAccessList
=
accounts
.
toJavaList
(
PlainAccess
.
class
);
if
(
plainAccessList
!=
null
&&
!
plainAccessList
.
isEmpty
())
{
for
(
PlainAccess
plainAccess
:
plainAccessList
)
{
this
.
setPlainAccessResource
(
getPlainAccessResource
(
plainAccess
));
}
}
}
...
...
@@ -139,19 +140,19 @@ public class PlainPermissionLoader {
}
}
PlainAccessResource
getPlainAccessResource
(
JSONObject
account
)
{
PlainAccessResource
getPlainAccessResource
(
PlainAccess
plainAccess
)
{
PlainAccessResource
plainAccessResource
=
new
PlainAccessResource
();
plainAccessResource
.
setAccessKey
(
account
.
getString
(
"accessKey"
));
plainAccessResource
.
setSecretKey
(
account
.
getString
(
"secretKey"
));
plainAccessResource
.
setWhiteRemoteAddress
(
account
.
getString
(
"whiteRemoteAddress"
));
plainAccessResource
.
setAccessKey
(
plainAccess
.
getAccessKey
(
));
plainAccessResource
.
setSecretKey
(
plainAccess
.
getSecretKey
(
));
plainAccessResource
.
setWhiteRemoteAddress
(
plainAccess
.
getWhiteRemoteAddress
(
));
plainAccessResource
.
setAdmin
(
account
.
containsKey
(
"admin"
)
?
account
.
getBoolean
(
"admin"
)
:
false
);
plainAccessResource
.
setAdmin
(
plainAccess
.
isAdmin
()
);
plainAccessResource
.
setDefaultGroupPerm
(
Permission
.
fromStringGetPermission
(
account
.
getString
(
"defaultGroupPerm"
)));
plainAccessResource
.
setDefaultTopicPerm
(
Permission
.
fromStringGetPermission
(
account
.
getString
(
"defaultTopicPerm"
)));
plainAccessResource
.
setDefaultGroupPerm
(
Permission
.
fromStringGetPermission
(
plainAccess
.
getDefaultGroupPerm
(
)));
plainAccessResource
.
setDefaultTopicPerm
(
Permission
.
fromStringGetPermission
(
plainAccess
.
getDefaultTopicPerm
(
)));
Permission
.
setTopicPerm
(
plainAccessResource
,
true
,
account
.
getJSONArray
(
"groups"
));
Permission
.
setTopicPerm
(
plainAccessResource
,
true
,
account
.
getJSONArray
(
"topics"
));
Permission
.
setTopicPerm
(
plainAccessResource
,
false
,
plainAccess
.
getGroups
(
));
Permission
.
setTopicPerm
(
plainAccessResource
,
true
,
plainAccess
.
getTopics
(
));
return
plainAccessResource
;
}
...
...
@@ -250,4 +251,88 @@ public class PlainPermissionLoader {
return
isWatchStart
;
}
static
class
PlainAccess
{
private
String
accessKey
;
private
String
secretKey
;
private
String
whiteRemoteAddress
;
private
boolean
admin
;
private
String
defaultTopicPerm
;
private
String
defaultGroupPerm
;
private
List
<
String
>
topics
;
private
List
<
String
>
groups
;
public
String
getAccessKey
()
{
return
accessKey
;
}
public
void
setAccessKey
(
String
accessKey
)
{
this
.
accessKey
=
accessKey
;
}
public
String
getSecretKey
()
{
return
secretKey
;
}
public
void
setSecretKey
(
String
secretKey
)
{
this
.
secretKey
=
secretKey
;
}
public
String
getWhiteRemoteAddress
()
{
return
whiteRemoteAddress
;
}
public
void
setWhiteRemoteAddress
(
String
whiteRemoteAddress
)
{
this
.
whiteRemoteAddress
=
whiteRemoteAddress
;
}
public
boolean
isAdmin
()
{
return
admin
;
}
public
void
setAdmin
(
boolean
admin
)
{
this
.
admin
=
admin
;
}
public
String
getDefaultTopicPerm
()
{
return
defaultTopicPerm
;
}
public
void
setDefaultTopicPerm
(
String
defaultTopicPerm
)
{
this
.
defaultTopicPerm
=
defaultTopicPerm
;
}
public
String
getDefaultGroupPerm
()
{
return
defaultGroupPerm
;
}
public
void
setDefaultGroupPerm
(
String
defaultGroupPerm
)
{
this
.
defaultGroupPerm
=
defaultGroupPerm
;
}
public
List
<
String
>
getTopics
()
{
return
topics
;
}
public
void
setTopics
(
List
<
String
>
topics
)
{
this
.
topics
=
topics
;
}
public
List
<
String
>
getGroups
()
{
return
groups
;
}
public
void
setGroups
(
List
<
String
>
groups
)
{
this
.
groups
=
groups
;
}
}
}
acl/src/test/java/org/apache/rocketmq/acl/common/PermissionTest.java
浏览文件 @
020f4b4c
...
...
@@ -16,8 +16,9 @@
*/
package
org.apache.rocketmq.acl.common
;
import
com.alibaba.fastjson.JSONArray
;
import
java.util.ArrayList
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
org.apache.rocketmq.acl.plain.PlainAccessResource
;
...
...
@@ -93,7 +94,7 @@ public class PermissionTest {
Permission
.
setTopicPerm
(
plainAccessResource
,
false
,
null
);
Assert
.
assertNull
(
resourcePermMap
);
JSONArray
groups
=
new
JSONArray
();
List
<
String
>
groups
=
new
ArrayList
<>
();
Permission
.
setTopicPerm
(
plainAccessResource
,
false
,
groups
);
Assert
.
assertNull
(
resourcePermMap
);
...
...
@@ -112,7 +113,7 @@ public class PermissionTest {
perm
=
resourcePermMap
.
get
(
PlainAccessResource
.
getRetryTopic
(
"groupC"
));
Assert
.
assertEquals
(
perm
,
Permission
.
PUB
);
JSONArray
topics
=
new
JSONArray
();
List
<
String
>
topics
=
new
ArrayList
<>
();
topics
.
add
(
"topicA=DENY"
);
topics
.
add
(
"topicB=PUB|SUB"
);
topics
.
add
(
"topicC=PUB"
);
...
...
@@ -128,7 +129,7 @@ public class PermissionTest {
perm
=
resourcePermMap
.
get
(
"topicC"
);
Assert
.
assertEquals
(
perm
,
Permission
.
PUB
);
JSONArray
erron
=
new
JSONArray
();
List
<
String
>
erron
=
new
ArrayList
<>
();
erron
.
add
(
""
);
Permission
.
setTopicPerm
(
plainAccessResource
,
false
,
erron
);
}
...
...
acl/src/test/java/org/apache/rocketmq/acl/plain/PlainAccessValidatorTest.java
浏览文件 @
020f4b4c
...
...
@@ -70,7 +70,7 @@ public class PlainAccessValidatorTest {
AclClientRPCHook
aclClient
=
new
AclClientRPCHook
(
sessionCredentials
);
SendMessageRequestHeader
messageRequestHeader
=
new
SendMessageRequestHeader
();
messageRequestHeader
.
setTopic
(
"topic
A
"
);
messageRequestHeader
.
setTopic
(
"topic
B
"
);
RemotingCommand
remotingCommand
=
RemotingCommand
.
createRequestCommand
(
RequestCode
.
SEND_MESSAGE
,
messageRequestHeader
);
aclClient
.
doBeforeRequest
(
""
,
remotingCommand
);
...
...
acl/src/test/java/org/apache/rocketmq/acl/plain/PlainPermissionLoaderTest.java
浏览文件 @
020f4b4c
...
...
@@ -16,30 +16,22 @@
*/
package
org.apache.rocketmq.acl.plain
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
java.io.File
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
org.apache.commons.lang3.reflect.FieldUtils
;
import
org.apache.rocketmq.acl.common.AclException
;
import
org.apache.rocketmq.acl.common.AclUtils
;
import
org.apache.rocketmq.acl.common.Permission
;
import
org.apache.rocketmq.
common.MixAll
;
import
org.apache.rocketmq.
acl.plain.PlainPermissionLoader.PlainAccess
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.powermock.api.mockito.PowerMockito
;
import
org.powermock.core.classloader.annotations.PrepareForTest
;
import
org.powermock.modules.junit4.PowerMockRunner
;
@RunWith
(
PowerMockRunner
.
class
)
@PrepareForTest
({
AclUtils
.
class
})
public
class
PlainPermissionLoaderTest
{
PlainPermissionLoader
plainPermissionLoader
;
...
...
@@ -50,10 +42,6 @@ public class PlainPermissionLoaderTest {
PlainAccessResource
plainAccessResource
=
new
PlainAccessResource
();
PlainAccessResource
plainAccessResourceTwo
=
new
PlainAccessResource
();
Set
<
Integer
>
adminCode
=
new
HashSet
<>();
private
String
fileName
=
System
.
getProperty
(
"romcketmq.acl.plain.fileName"
,
"/conf/transport.yml"
);
private
Map
<
String
/** account **/
,
List
<
PlainAccessResource
>>
plainAccessResourceMap
;
private
List
<
RemoteAddressStrategy
>
globalWhiteRemoteAddressStrategy
;
@Before
public
void
init
()
throws
NoSuchFieldException
,
SecurityException
,
IOException
{
...
...
@@ -75,6 +63,7 @@ public class PlainPermissionLoaderTest {
System
.
setProperty
(
"java.version"
,
"1.6.11"
);
System
.
setProperty
(
"rocketmq.home.dir"
,
"src/test/resources"
);
System
.
setProperty
(
"romcketmq.acl.plain.fileName"
,
"/conf/transport.yml"
);
plainPermissionLoader
=
new
PlainPermissionLoader
();
}
...
...
@@ -98,85 +87,56 @@ public class PlainPermissionLoaderTest {
return
painAccessResource
;
}
@SuppressWarnings
(
"unchecked"
)
private
void
getField
(
PlainPermissionLoader
plainPermissionLoader
)
{
try
{
this
.
globalWhiteRemoteAddressStrategy
=
(
List
<
RemoteAddressStrategy
>)
FieldUtils
.
readDeclaredField
(
plainPermissionLoader
,
"globalWhiteRemoteAddressStrategy"
,
true
);
this
.
plainAccessResourceMap
=
(
Map
<
String
/** account **/
,
List
<
PlainAccessResource
>>)
FieldUtils
.
readDeclaredField
(
plainPermissionLoader
,
"plainAccessResourceMap"
,
true
);
}
catch
(
IllegalAccessException
e
)
{
e
.
printStackTrace
();
}
}
@Test
(
expected
=
AclException
.
class
)
public
void
initializeTest
()
{
System
.
setProperty
(
"romcketmq.acl.plain.fileName"
,
"/conf/transport-null.yml"
);
new
PlainPermissionLoader
();
}
@Test
public
void
initializeIngetYamlDataObject
()
{
String
fileHome
=
System
.
getProperty
(
MixAll
.
ROCKETMQ_HOME_PROPERTY
,
System
.
getenv
(
MixAll
.
ROCKETMQ_HOME_ENV
));
PowerMockito
.
mockStatic
(
AclUtils
.
class
);
JSONObject
json
=
new
JSONObject
();
json
.
put
(
""
,
""
);
PowerMockito
.
when
(
AclUtils
.
getYamlDataObject
(
fileHome
+
"/conf/transport.yml"
,
JSONObject
.
class
)).
thenReturn
(
json
);
PlainPermissionLoader
plainPermissionLoader
=
new
PlainPermissionLoader
();
getField
(
plainPermissionLoader
);
Assert
.
assertTrue
(
globalWhiteRemoteAddressStrategy
.
isEmpty
());
Assert
.
assertTrue
(
plainAccessResourceMap
.
isEmpty
());
}
@Test
public
void
getPlainAccessResourceTest
()
{
PlainAccessResource
plainAccessResource
=
new
PlainAccessResource
();
JSONObject
account
=
new
JSONObject
();
account
.
put
(
"accessKey"
,
"RocketMQ"
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
account
);
PlainAccess
plainAccess
=
new
PlainAccess
();
plainAccess
.
setAccessKey
(
"RocketMQ"
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
plainAccess
);
Assert
.
assertEquals
(
plainAccessResource
.
getAccessKey
(),
"RocketMQ"
);
account
.
put
(
"secretKey"
,
"12345678"
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
account
);
plainAccess
.
setSecretKey
(
"12345678"
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
plainAccess
);
Assert
.
assertEquals
(
plainAccessResource
.
getSecretKey
(),
"12345678"
);
account
.
put
(
"whiteRemoteAddress"
,
"127.0.0.1"
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
account
);
plainAccess
.
setWhiteRemoteAddress
(
"127.0.0.1"
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
plainAccess
);
Assert
.
assertEquals
(
plainAccessResource
.
getWhiteRemoteAddress
(),
"127.0.0.1"
);
account
.
put
(
"admin"
,
true
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
account
);
plainAccess
.
setAdmin
(
true
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
plainAccess
);
Assert
.
assertEquals
(
plainAccessResource
.
isAdmin
(),
true
);
account
.
put
(
"defaultGroupPerm"
,
"ANY"
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
account
);
plainAccess
.
setDefaultGroupPerm
(
"ANY"
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
plainAccess
);
Assert
.
assertEquals
(
plainAccessResource
.
getDefaultGroupPerm
(),
Permission
.
ANY
);
account
.
put
(
"defaultTopicPerm"
,
"ANY"
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
account
);
plainAccess
.
setDefaultTopicPerm
(
"ANY"
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
plainAccess
);
Assert
.
assertEquals
(
plainAccessResource
.
getDefaultTopicPerm
(),
Permission
.
ANY
);
JSONArray
groups
=
new
JSONArray
();
List
<
String
>
groups
=
new
ArrayList
<
String
>
();
groups
.
add
(
"groupA=DENY"
);
groups
.
add
(
"groupB=PUB|SUB"
);
groups
.
add
(
"groupC=PUB"
);
account
.
put
(
"groups"
,
groups
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
account
);
plainAccess
.
setGroups
(
groups
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
plainAccess
);
Map
<
String
,
Byte
>
resourcePermMap
=
plainAccessResource
.
getResourcePermMap
();
Assert
.
assertEquals
(
resourcePermMap
.
size
(),
3
);
Assert
.
assertEquals
(
resourcePermMap
.
get
(
"groupA"
).
byteValue
(),
Permission
.
DENY
);
Assert
.
assertEquals
(
resourcePermMap
.
get
(
"groupB"
).
byteValue
(),
Permission
.
ANY
);
Assert
.
assertEquals
(
resourcePermMap
.
get
(
"groupC"
).
byteValue
(),
Permission
.
PUB
);
Assert
.
assertEquals
(
resourcePermMap
.
get
(
PlainAccessResource
.
getRetryTopic
(
"groupA"
)
).
byteValue
(),
Permission
.
DENY
);
Assert
.
assertEquals
(
resourcePermMap
.
get
(
PlainAccessResource
.
getRetryTopic
(
"groupB"
)
).
byteValue
(),
Permission
.
ANY
);
Assert
.
assertEquals
(
resourcePermMap
.
get
(
PlainAccessResource
.
getRetryTopic
(
"groupC"
)
).
byteValue
(),
Permission
.
PUB
);
JSONArray
topics
=
new
JSONArray
();
List
<
String
>
topics
=
new
ArrayList
<
String
>
();
topics
.
add
(
"topicA=DENY"
);
topics
.
add
(
"topicB=PUB|SUB"
);
topics
.
add
(
"topicC=PUB"
);
account
.
put
(
"topics"
,
topics
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
account
);
plainAccess
.
setTopics
(
topics
);
plainAccessResource
=
plainPermissionLoader
.
getPlainAccessResource
(
plainAccess
);
resourcePermMap
=
plainAccessResource
.
getResourcePermMap
();
Assert
.
assertEquals
(
resourcePermMap
.
size
(),
3
);
Assert
.
assertEquals
(
resourcePermMap
.
size
(),
6
);
Assert
.
assertEquals
(
resourcePermMap
.
get
(
"topicA"
).
byteValue
(),
Permission
.
DENY
);
Assert
.
assertEquals
(
resourcePermMap
.
get
(
"topicB"
).
byteValue
(),
Permission
.
ANY
);
...
...
@@ -237,17 +197,21 @@ public class PlainPermissionLoaderTest {
new
PlainPermissionLoader
().
initialize
();
}
@SuppressWarnings
(
"unchecked"
)
@Test
public
void
cleanAuthenticationInfoTest
()
{
plainPermissionLoader
.
setPlainAccessResource
(
plainAccessResource
);
plainAccessResource
.
setRequestCode
(
202
);
plainPermissionLoader
.
eachCheckPlainAccessResource
(
plainAccessResource
);
public
void
cleanAuthenticationInfoTest
()
throws
IllegalAccessException
{
//plainPermissionLoader.setPlainAccessResource(plainAccessResource);
Map
<
String
,
List
<
PlainAccessResource
>>
plainAccessResourceMap
=
(
Map
<
String
,
List
<
PlainAccessResource
>>)
FieldUtils
.
readDeclaredField
(
plainPermissionLoader
,
"plainAccessResourceMap"
,
true
);
Assert
.
assertFalse
(
plainAccessResourceMap
.
isEmpty
());
plainPermissionLoader
.
cleanAuthenticationInfo
();
plainPermissionLoader
.
eachCheckPlainAccessResource
(
plainAccessResource
);
plainAccessResourceMap
=
(
Map
<
String
,
List
<
PlainAccessResource
>>)
FieldUtils
.
readDeclaredField
(
plainPermissionLoader
,
"plainAccessResourceMap"
,
true
);
Assert
.
assertTrue
(
plainAccessResourceMap
.
isEmpty
());
}
@Test
public
void
isWatchStartTest
()
{
System
.
setProperty
(
"java.version"
,
"1.7.11"
);
PlainPermissionLoader
plainPermissionLoader
=
new
PlainPermissionLoader
();
Assert
.
assertTrue
(
plainPermissionLoader
.
isWatchStart
());
System
.
setProperty
(
"java.version"
,
"1.6.11"
);
...
...
@@ -255,8 +219,10 @@ public class PlainPermissionLoaderTest {
Assert
.
assertFalse
(
plainPermissionLoader
.
isWatchStart
());
}
@SuppressWarnings
(
"unchecked"
)
@Test
public
void
watchTest
()
throws
IOException
{
public
void
watchTest
()
throws
IOException
,
IllegalAccessException
{
System
.
setProperty
(
"java.version"
,
"1.7.11"
);
System
.
setProperty
(
"rocketmq.home.dir"
,
"src/test/resources/watch"
);
File
file
=
new
File
(
"src/test/resources/watch/conf"
);
file
.
mkdirs
();
...
...
@@ -264,30 +230,33 @@ public class PlainPermissionLoaderTest {
transport
.
createNewFile
();
FileWriter
writer
=
new
FileWriter
(
transport
);
writer
.
write
(
"list:\r\n"
);
writer
.
write
(
"- account: rokcetmq\r\n"
);
writer
.
write
(
" password: aliyun11\r\n"
);
writer
.
write
(
" netaddress: 127.0.0.1\r\n"
);
writer
.
write
(
"accounts:\r\n"
);
writer
.
write
(
"- accessKey: rokcetmq\r\n"
);
writer
.
write
(
" secretKey: aliyun11\r\n"
);
writer
.
write
(
" whiteRemoteAddress: 127.0.0.1\r\n"
);
writer
.
write
(
" admin: true\r\n"
);
writer
.
flush
();
writer
.
close
();
PlainPermissionLoader
plainPermissionLoader
=
new
PlainPermissionLoader
();
plainAccessResource
.
setRequestCode
(
203
);
plainPermissionLoader
.
eachCheckPlainAccessResource
(
plainAccessResource
);
Map
<
String
,
List
<
PlainAccessResource
>>
plainAccessResourceMap
=
(
Map
<
String
,
List
<
PlainAccessResource
>>)
FieldUtils
.
readDeclaredField
(
plainPermissionLoader
,
"plainAccessResourceMap"
,
true
);
Assert
.
assertEquals
(
plainAccessResourceMap
.
get
(
"rokcetmq"
).
size
(),
1
);
writer
=
new
FileWriter
(
new
File
(
"src/test/resources/watch/conf/transport.yml"
),
true
);
writer
.
write
(
"- account: rokcet1\r\n"
);
writer
.
write
(
" password: aliyun1\r\n"
);
writer
.
write
(
" netaddress: 127.0.0.1\r\n"
);
writer
.
write
(
"- accessKey: rokcet1\r\n"
);
writer
.
write
(
" secretKey: aliyun1\r\n"
);
writer
.
write
(
" whiteRemoteAddress: 127.0.0.1\r\n"
);
writer
.
write
(
" admin: true\r\n"
);
writer
.
flush
();
writer
.
close
();
try
{
Thread
.
sleep
(
100
);
}
catch
(
InterruptedException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
plainAccessResource
Two
.
setRequestCode
(
203
);
plainPermissionLoader
.
eachCheckPlainAccessResource
(
plainAccessResourceTwo
);
plainAccessResource
Map
=
(
Map
<
String
,
List
<
PlainAccessResource
>>)
FieldUtils
.
readDeclaredField
(
plainPermissionLoader
,
"plainAccessResourceMap"
,
true
);
Assert
.
assertEquals
(
plainAccessResourceMap
.
get
(
"rokcet1"
).
size
(),
1
);
transport
.
delete
();
file
.
delete
();
...
...
@@ -296,4 +265,11 @@ public class PlainPermissionLoaderTest {
}
@Test
(
expected
=
AclException
.
class
)
public
void
initializeTest
()
{
System
.
setProperty
(
"romcketmq.acl.plain.fileName"
,
"/conf/transport-null.yml"
);
new
PlainPermissionLoader
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录