Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhangjian1949
apollo
提交
e955bacf
apollo
项目概览
zhangjian1949
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e955bacf
编写于
7月 11, 2020
作者:
J
Jason Song
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
restrict use of custom yaml types
上级
f34231b2
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
151 addition
and
7 deletion
+151
-7
apollo-client/src/main/java/com/ctrip/framework/apollo/util/yaml/YamlParser.java
...java/com/ctrip/framework/apollo/util/yaml/YamlParser.java
+5
-2
apollo-client/src/test/java/com/ctrip/framework/apollo/util/yaml/YamlParserTest.java
.../com/ctrip/framework/apollo/util/yaml/YamlParserTest.java
+6
-0
apollo-client/src/test/resources/yaml/case9.yaml
apollo-client/src/test/resources/yaml/case9.yaml
+5
-0
apollo-portal/pom.xml
apollo-portal/pom.xml
+9
-3
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ItemController.java
...ip/framework/apollo/portal/controller/ItemController.java
+16
-2
apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/controller/ItemControllerTest.java
...ramework/apollo/portal/controller/ItemControllerTest.java
+76
-0
apollo-portal/src/test/resources/yaml/case1.yaml
apollo-portal/src/test/resources/yaml/case1.yaml
+25
-0
apollo-portal/src/test/resources/yaml/case2.yaml
apollo-portal/src/test/resources/yaml/case2.yaml
+4
-0
apollo-portal/src/test/resources/yaml/case3.yaml
apollo-portal/src/test/resources/yaml/case3.yaml
+5
-0
未找到文件。
apollo-client/src/main/java/com/ctrip/framework/apollo/util/yaml/YamlParser.java
浏览文件 @
e955bacf
...
@@ -13,7 +13,7 @@ import java.util.Set;
...
@@ -13,7 +13,7 @@ import java.util.Set;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.yaml.snakeyaml.Yaml
;
import
org.yaml.snakeyaml.Yaml
;
import
org.yaml.snakeyaml.constructor.Constructor
;
import
org.yaml.snakeyaml.constructor.
Safe
Constructor
;
import
org.yaml.snakeyaml.nodes.MappingNode
;
import
org.yaml.snakeyaml.nodes.MappingNode
;
import
org.yaml.snakeyaml.parser.ParserException
;
import
org.yaml.snakeyaml.parser.ParserException
;
...
@@ -147,7 +147,10 @@ public class YamlParser {
...
@@ -147,7 +147,10 @@ public class YamlParser {
void
process
(
Properties
properties
,
Map
<
String
,
Object
>
map
);
void
process
(
Properties
properties
,
Map
<
String
,
Object
>
map
);
}
}
private
static
class
StrictMapAppenderConstructor
extends
Constructor
{
/**
* A specialized {@link SafeConstructor} that checks for duplicate keys.
*/
private
static
class
StrictMapAppenderConstructor
extends
SafeConstructor
{
// Declared as public for use in subclasses
// Declared as public for use in subclasses
StrictMapAppenderConstructor
()
{
StrictMapAppenderConstructor
()
{
...
...
apollo-client/src/test/java/com/ctrip/framework/apollo/util/yaml/YamlParserTest.java
浏览文件 @
e955bacf
...
@@ -18,6 +18,7 @@ import org.mockito.invocation.InvocationOnMock;
...
@@ -18,6 +18,7 @@ import org.mockito.invocation.InvocationOnMock;
import
org.mockito.stubbing.Answer
;
import
org.mockito.stubbing.Answer
;
import
org.springframework.beans.factory.config.YamlPropertiesFactoryBean
;
import
org.springframework.beans.factory.config.YamlPropertiesFactoryBean
;
import
org.springframework.core.io.ByteArrayResource
;
import
org.springframework.core.io.ByteArrayResource
;
import
org.yaml.snakeyaml.constructor.ConstructorException
;
import
org.yaml.snakeyaml.parser.ParserException
;
import
org.yaml.snakeyaml.parser.ParserException
;
public
class
YamlParserTest
{
public
class
YamlParserTest
{
...
@@ -44,6 +45,11 @@ public class YamlParserTest {
...
@@ -44,6 +45,11 @@ public class YamlParserTest {
testInvalid
(
"case8.yaml"
);
testInvalid
(
"case8.yaml"
);
}
}
@Test
(
expected
=
ConstructorException
.
class
)
public
void
testcase9
()
throws
Exception
{
testInvalid
(
"case9.yaml"
);
}
@Test
@Test
public
void
testOrderProperties
()
throws
IOException
{
public
void
testOrderProperties
()
throws
IOException
{
String
yamlContent
=
loadYaml
(
"orderedcase.yaml"
);
String
yamlContent
=
loadYaml
(
"orderedcase.yaml"
);
...
...
apollo-client/src/test/resources/yaml/case9.yaml
0 → 100644
浏览文件 @
e955bacf
!!javax.script.ScriptEngineManager
[
!!java.net.URLClassLoader
[[
!!java.net.URL
[
"
http://localhost"
]
]]
]
\ No newline at end of file
apollo-portal/pom.xml
浏览文件 @
e955bacf
...
@@ -27,11 +27,12 @@
...
@@ -27,11 +27,12 @@
<groupId>
com.ctrip.framework.apollo
</groupId>
<groupId>
com.ctrip.framework.apollo
</groupId>
<artifactId>
apollo-openapi
</artifactId>
<artifactId>
apollo-openapi
</artifactId>
</dependency>
</dependency>
<!-- yml processing -->
<dependency>
<dependency>
<groupId>
com.h2database
</groupId>
<groupId>
org.yaml
</groupId>
<artifactId>
h2
</artifactId>
<artifactId>
snakeyaml
</artifactId>
<scope>
test
</scope>
</dependency>
</dependency>
<!-- end of yml processing -->
<!-- JDK 1.8+ -->
<!-- JDK 1.8+ -->
<dependency>
<dependency>
<groupId>
javax.xml.bind
</groupId>
<groupId>
javax.xml.bind
</groupId>
...
@@ -58,6 +59,11 @@
...
@@ -58,6 +59,11 @@
<!-- end of JDK 11+ -->
<!-- end of JDK 11+ -->
<!-- test -->
<!-- test -->
<dependency>
<groupId>
com.h2database
</groupId>
<artifactId>
h2
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<dependency>
<groupId>
org.eclipse.jetty
</groupId>
<groupId>
org.eclipse.jetty
</groupId>
<artifactId>
jetty-server
</artifactId>
<artifactId>
jetty-server
</artifactId>
...
...
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/ItemController.java
浏览文件 @
e955bacf
...
@@ -33,6 +33,11 @@ import org.springframework.web.bind.annotation.RestController;
...
@@ -33,6 +33,11 @@ import org.springframework.web.bind.annotation.RestController;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.Objects
;
import
org.yaml.snakeyaml.DumperOptions
;
import
org.yaml.snakeyaml.LoaderOptions
;
import
org.yaml.snakeyaml.Yaml
;
import
org.yaml.snakeyaml.constructor.SafeConstructor
;
import
org.yaml.snakeyaml.representer.Representer
;
import
static
com
.
ctrip
.
framework
.
apollo
.
common
.
utils
.
RequestPrecondition
.
checkModel
;
import
static
com
.
ctrip
.
framework
.
apollo
.
common
.
utils
.
RequestPrecondition
.
checkModel
;
...
@@ -208,7 +213,7 @@ public class ItemController {
...
@@ -208,7 +213,7 @@ public class ItemController {
return
ResponseEntity
.
ok
().
build
();
return
ResponseEntity
.
ok
().
build
();
}
}
private
void
doSyntaxCheck
(
NamespaceTextModel
model
)
{
void
doSyntaxCheck
(
NamespaceTextModel
model
)
{
if
(
StringUtils
.
isBlank
(
model
.
getConfigText
()))
{
if
(
StringUtils
.
isBlank
(
model
.
getConfigText
()))
{
return
;
return
;
}
}
...
@@ -219,7 +224,7 @@ public class ItemController {
...
@@ -219,7 +224,7 @@ public class ItemController {
}
}
// use YamlPropertiesFactoryBean to check the yaml syntax
// use YamlPropertiesFactoryBean to check the yaml syntax
YamlPropertiesFactoryBean
yamlPropertiesFactoryBean
=
new
YamlPropertiesFactoryBean
();
TypeLimitedYamlPropertiesFactoryBean
yamlPropertiesFactoryBean
=
new
TypeLimited
YamlPropertiesFactoryBean
();
yamlPropertiesFactoryBean
.
setResources
(
new
ByteArrayResource
(
model
.
getConfigText
().
getBytes
()));
yamlPropertiesFactoryBean
.
setResources
(
new
ByteArrayResource
(
model
.
getConfigText
().
getBytes
()));
// this call converts yaml to properties and will throw exception if the conversion fails
// this call converts yaml to properties and will throw exception if the conversion fails
yamlPropertiesFactoryBean
.
getObject
();
yamlPropertiesFactoryBean
.
getObject
();
...
@@ -229,5 +234,14 @@ public class ItemController {
...
@@ -229,5 +234,14 @@ public class ItemController {
return
Objects
.
nonNull
(
item
)
&&
!
StringUtils
.
isContainEmpty
(
item
.
getKey
());
return
Objects
.
nonNull
(
item
)
&&
!
StringUtils
.
isContainEmpty
(
item
.
getKey
());
}
}
private
static
class
TypeLimitedYamlPropertiesFactoryBean
extends
YamlPropertiesFactoryBean
{
@Override
protected
Yaml
createYaml
()
{
LoaderOptions
loaderOptions
=
new
LoaderOptions
();
loaderOptions
.
setAllowDuplicateKeys
(
false
);
return
new
Yaml
(
new
SafeConstructor
(),
new
Representer
(),
new
DumperOptions
(),
loaderOptions
);
}
}
}
}
apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/controller/ItemControllerTest.java
0 → 100644
浏览文件 @
e955bacf
package
com.ctrip.framework.apollo.portal.controller
;
import
com.ctrip.framework.apollo.core.enums.ConfigFileFormat
;
import
com.ctrip.framework.apollo.portal.component.PermissionValidator
;
import
com.ctrip.framework.apollo.portal.entity.model.NamespaceTextModel
;
import
com.ctrip.framework.apollo.portal.service.ItemService
;
import
com.ctrip.framework.apollo.portal.service.NamespaceService
;
import
com.ctrip.framework.apollo.portal.spi.UserInfoHolder
;
import
com.google.common.base.Charsets
;
import
com.google.common.io.Files
;
import
java.io.File
;
import
java.io.IOException
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.mockito.junit.MockitoJUnitRunner
;
import
org.yaml.snakeyaml.constructor.ConstructorException
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
ItemControllerTest
{
@Mock
private
ItemService
configService
;
@Mock
private
NamespaceService
namespaceService
;
@Mock
private
UserInfoHolder
userInfoHolder
;
@Mock
private
PermissionValidator
permissionValidator
;
@InjectMocks
private
ItemController
itemController
;
@Before
public
void
setUp
()
throws
Exception
{
itemController
=
new
ItemController
(
configService
,
userInfoHolder
,
permissionValidator
,
namespaceService
);
}
@Test
public
void
yamlSyntaxCheckOK
()
throws
Exception
{
String
yaml
=
loadYaml
(
"case1.yaml"
);
itemController
.
doSyntaxCheck
(
assemble
(
ConfigFileFormat
.
YAML
.
getValue
(),
yaml
));
}
@Test
(
expected
=
IllegalStateException
.
class
)
public
void
yamlSyntaxCheckWithDuplicatedValue
()
throws
Exception
{
String
yaml
=
loadYaml
(
"case2.yaml"
);
itemController
.
doSyntaxCheck
(
assemble
(
ConfigFileFormat
.
YAML
.
getValue
(),
yaml
));
}
@Test
(
expected
=
ConstructorException
.
class
)
public
void
yamlSyntaxCheckWithUnsupportedType
()
throws
Exception
{
String
yaml
=
loadYaml
(
"case3.yaml"
);
itemController
.
doSyntaxCheck
(
assemble
(
ConfigFileFormat
.
YAML
.
getValue
(),
yaml
));
}
private
NamespaceTextModel
assemble
(
String
format
,
String
content
)
{
NamespaceTextModel
model
=
new
NamespaceTextModel
();
model
.
setFormat
(
format
);
model
.
setConfigText
(
content
);
return
model
;
}
private
String
loadYaml
(
String
caseName
)
throws
IOException
{
File
file
=
new
File
(
"src/test/resources/yaml/"
+
caseName
);
return
Files
.
toString
(
file
,
Charsets
.
UTF_8
);
}
}
\ No newline at end of file
apollo-portal/src/test/resources/yaml/case1.yaml
0 → 100644
浏览文件 @
e955bacf
root
:
key1
:
"
someValue"
key2
:
100
key3
:
key4
:
key5
:
'
(%sender%)
%message%'
key6
:
'
*
%sender%
%message%'
# commented: "xxx"
list
:
-
'
item
1'
-
'
item
2'
intList
:
-
100
-
200
listOfMap
:
-
key
:
'
#mychannel'
value
:
'
'
-
key
:
'
#myprivatechannel'
value
:
'
mypassword'
listOfList
:
-
-
'
a1'
-
'
a2'
-
-
'
b1'
-
'
b2'
listOfList2
:
[
[
'
a1'
,
'
a2'
],
[
'
b1'
,
'
b2'
]
]
apollo-portal/src/test/resources/yaml/case2.yaml
0 → 100644
浏览文件 @
e955bacf
root
:
key1
:
"
someValue"
key2
:
100
key1
:
"
anotherValue"
apollo-portal/src/test/resources/yaml/case3.yaml
0 → 100644
浏览文件 @
e955bacf
!!javax.script.ScriptEngineManager
[
!!java.net.URLClassLoader
[[
!!java.net.URL
[
"
http://localhost"
]
]]
]
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录