Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
LinuxSuRen
jenkins
提交
5751560f
J
jenkins
项目概览
LinuxSuRen
/
jenkins
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jenkins
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5751560f
编写于
7月 26, 2014
作者:
D
Daniel Beck
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1251 from daniel-beck/JENKINS-14583
[FIX JENKINS-14538] Improve builds dir input validation
上级
e8b1a500
50cbded7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
131 addition
and
10 deletion
+131
-10
core/src/main/java/jenkins/model/Jenkins.java
core/src/main/java/jenkins/model/Jenkins.java
+45
-10
core/src/test/java/jenkins/model/JenkinsDescriptorTest.java
core/src/test/java/jenkins/model/JenkinsDescriptorTest.java
+86
-0
未找到文件。
core/src/main/java/jenkins/model/Jenkins.java
浏览文件 @
5751560f
...
...
@@ -1753,14 +1753,43 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve
}
public
FormValidation
doCheckRawBuildsDir
(
@QueryParameter
String
value
)
{
if
(!
value
.
contains
(
"${"
))
{
File
d
=
new
File
(
value
);
if
(!
d
.
isDirectory
()
&&
(
d
.
getParentFile
()
==
null
||
!
d
.
getParentFile
().
canWrite
()))
{
// do essentially what expandVariablesForDirectory does, without an Item
String
replacedValue
=
expandVariablesForDirectory
(
value
,
"doCheckRawBuildsDir-Marker:foo"
,
Jenkins
.
getInstance
().
getRootDir
().
getPath
()
+
"/jobs/doCheckRawBuildsDir-Marker$foo"
);
File
replacedFile
=
new
File
(
replacedValue
);
if
(!
replacedFile
.
isAbsolute
())
{
return
FormValidation
.
error
(
value
+
" does not resolve to an absolute path"
);
}
if
(!
replacedValue
.
contains
(
"doCheckRawBuildsDir-Marker"
))
{
return
FormValidation
.
error
(
value
+
" does not contain ${ITEM_FULL_NAME} or ${ITEM_ROOTDIR}, cannot distinguish between projects"
);
}
if
(
replacedValue
.
contains
(
"doCheckRawBuildsDir-Marker:foo"
))
{
// make sure platform can handle colon
try
{
File
tmp
=
File
.
createTempFile
(
"Jenkins-doCheckRawBuildsDir"
,
"foo:bar"
);
tmp
.
delete
();
}
catch
(
IOException
e
)
{
return
FormValidation
.
error
(
value
+
" contains ${ITEM_FULLNAME} but your system does not support it (JENKINS-12251). Use ${ITEM_FULL_NAME} instead"
);
}
}
File
d
=
new
File
(
replacedValue
);
if
(!
d
.
isDirectory
())
{
// if dir does not exist (almost guaranteed) need to make sure nearest existing ancestor can be written to
d
=
d
.
getParentFile
();
while
(!
d
.
exists
())
{
d
=
d
.
getParentFile
();
}
if
(!
d
.
canWrite
())
{
return
FormValidation
.
error
(
value
+
" does not exist and probably cannot be created"
);
}
// TODO failure to use either ITEM_* variable might be an error too?
}
return
FormValidation
.
ok
();
// TODO assumes it will be OK after substitution, but can we be sure?
return
FormValidation
.
ok
();
}
// to route /descriptor/FQCN/xxx to getDescriptor(FQCN).xxx
...
...
@@ -1949,11 +1978,17 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve
}
private
File
expandVariablesForDirectory
(
String
base
,
Item
item
)
{
return
new
File
(
Util
.
replaceMacro
(
base
,
ImmutableMap
.
of
(
"JENKINS_HOME"
,
getRootDir
().
getPath
(),
"ITEM_ROOTDIR"
,
item
.
getRootDir
().
getPath
(),
"ITEM_FULLNAME"
,
item
.
getFullName
(),
// legacy, deprecated
"ITEM_FULL_NAME"
,
item
.
getFullName
().
replace
(
':'
,
'$'
))));
// safe, see JENKINS-12251
return
new
File
(
expandVariablesForDirectory
(
base
,
item
.
getFullName
(),
item
.
getRootDir
().
getPath
()));
}
@Restricted
(
NoExternalUse
.
class
)
static
String
expandVariablesForDirectory
(
String
base
,
String
itemFullName
,
String
itemRootDir
)
{
return
Util
.
replaceMacro
(
base
,
ImmutableMap
.
of
(
"JENKINS_HOME"
,
Jenkins
.
getInstance
().
getRootDir
().
getPath
(),
"ITEM_ROOTDIR"
,
itemRootDir
,
"ITEM_FULLNAME"
,
itemFullName
,
// legacy, deprecated
"ITEM_FULL_NAME"
,
itemFullName
.
replace
(
':'
,
'$'
)));
// safe, see JENKINS-12251
}
public
String
getRawWorkspaceDir
()
{
...
...
core/src/test/java/jenkins/model/JenkinsDescriptorTest.java
0 → 100644
浏览文件 @
5751560f
/*
* The MIT License
*
* Copyright (c) 2014 Daniel Beck
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package
jenkins.model
;
import
hudson.util.FormValidation
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
import
org.powermock.api.mockito.PowerMockito
;
import
org.powermock.core.classloader.annotations.PrepareForTest
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
java.io.File
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Matchers
.
anyString
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
PowerMockRunner
.
class
)
public
class
JenkinsDescriptorTest
{
@Mock
Jenkins
jenkins
;
@Before
public
void
setUp
()
throws
Exception
{
MockitoAnnotations
.
initMocks
(
this
);
}
private
FormValidation
checkBuildsDir
(
String
buildsDir
)
{
return
Jenkins
.
DescriptorImpl
.
INSTANCE
.
doCheckRawBuildsDir
(
buildsDir
);
}
private
boolean
isOK
(
String
buildsDir
)
{
return
checkBuildsDir
(
buildsDir
).
kind
==
FormValidation
.
Kind
.
OK
;
}
private
boolean
isError
(
String
buildsDir
)
{
return
checkBuildsDir
(
buildsDir
).
kind
==
FormValidation
.
Kind
.
ERROR
;
}
@Test
@PrepareForTest
(
Jenkins
.
class
)
public
void
testBuildDirValidation
()
{
PowerMockito
.
mockStatic
(
Jenkins
.
class
);
PowerMockito
.
when
(
Jenkins
.
getInstance
()).
thenReturn
(
jenkins
);
PowerMockito
.
when
(
Jenkins
.
expandVariablesForDirectory
(
anyString
(),
anyString
(),
anyString
())).
thenCallRealMethod
();
when
(
jenkins
.
getRootDir
()).
thenReturn
(
new
File
(
"."
).
getAbsoluteFile
());
assertTrue
(
isOK
(
"$JENKINS_HOME/foo/$ITEM_FULL_NAME"
));
assertTrue
(
isOK
(
"${ITEM_ROOTDIR}/builds"
));
assertTrue
(
isError
(
"$JENKINS_HOME"
));
assertTrue
(
isError
(
"$JENKINS_HOME/builds"
));
assertTrue
(
isError
(
"$ITEM_FULL_NAME"
));
assertTrue
(
isError
(
"/path/to/builds"
));
assertTrue
(
isError
(
"/invalid/$JENKINS_HOME"
));
assertTrue
(
isError
(
"relative/ITEM_FULL_NAME"
));
// TODO test literal absolute paths (e.g. /foo/$ITEM_FULL_NAME), ITEM_FULLNAME
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录