Skip to content

Commit

Permalink
[JENKINS-41759] PoC for reading from more places than the trusted scm…
Browse files Browse the repository at this point in the history
… branch
  • Loading branch information
rsandell committed Feb 17, 2017
1 parent fc74039 commit 5d78778
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 10 deletions.
Expand Up @@ -36,15 +36,15 @@
*/
public class TrustedProperties extends DeclarativeEnvironmentContributor<TrustedProperties> {

private final String path;
private final Object data;

@DataBoundConstructor
public TrustedProperties(String path) {
this.path = path;
public TrustedProperties(Object data) {
this.data = data;
}

public String getPath() {
return path;
public Object getData() {
return data;
}

@Extension @Symbol("properties")
Expand Down
Expand Up @@ -47,18 +47,45 @@ class TrustedPropertiesScript extends DeclarativeEnvironmentContributorScript<Tr
} else if (prefix == "_") {
prefix = ""
}
String content = script.readTrusted(describable.path)
return generateEnvStrings(prefix, content)

if (describable.data == null) {
return Collections.emptyList()
} else if (describable.data instanceof Map) {
return generateEnvStrings(prefix, (Map)describable.data)
} else {
String sData = "${describable.data}"
String content = null
if (sData.contains("=") && sData.contains("\n")) {
//It is the contents of a Properties file (have to have at least end with a new line)
content = sData
} else {
//treat it as a path
if (sData.startsWith("resources://")) { //TODO should be an extension
content = script.libraryResource(sData.drop(12))
} else if (sData.startsWith("scm://")) { //TODO should be an extension
content = script.readTrusted(sData.drop(6))
} else { //Hey, someone might have missed the switch to url?
content = script.readTrusted(sData)
}
}
return generateEnvStrings(prefix, content)
}
}

@NonCPS
List<String> generateEnvStrings(String prefix, String content) {
Properties p = new Properties()
p.load(new StringReader(content))
return generateEnvStrings(prefix, p)
}

@NonCPS
List<String> generateEnvStrings(String prefix, Map p) {
List<String> env = []

for (String suffix : p.keySet()) {
env.add("${prefix}${suffix.trim()}=${p.getProperty(suffix, "").trim()}")
String value = p[suffix]?.toString() ?: ""
env.add("${prefix}${suffix.trim()}=${value.trim()}")
}
return env
}
Expand Down
Expand Up @@ -25,10 +25,16 @@

package org.jenkinsci.plugins.pipeline.modeldefinition.environment.impl;

import jenkins.plugins.git.GitSCMSource;
import org.jenkinsci.plugins.pipeline.modeldefinition.AbstractModelDefTest;
import org.jenkinsci.plugins.workflow.libs.GlobalLibraries;
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration;
import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever;
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;

/**
* Tests {@link TrustedProperties}
*/
Expand All @@ -46,7 +52,18 @@ public void writeMarkers() throws Exception {

@Test
public void globalAndStage() throws Exception {
expect("environmentFromProperties")
expect("properties", "environmentFromProperties")
.logContains(
"FOO is BAR",
"PROP_NAME is Bobby",
"PROP_NUM is 1",
"P_NAME is Andrew",
"P_NUM is 0").go();
}

@Test
public void fromScmUrl() throws Exception {
expect("properties", "environmentFromSCM")
.logContains(
"FOO is BAR",
"PROP_NAME is Bobby",
Expand All @@ -57,10 +74,56 @@ public void globalAndStage() throws Exception {

@Test
public void emptyPrefixStageOverride() throws Exception {
expect("environmentFromPropertiesEmptyPrefix")
expect("properties", "environmentFromPropertiesEmptyPrefix")
.logContains(
"FOO is BAR",
"NAME is Andrew",
"NUM is 0").go();
}

@Test
public void fromMap() throws Exception {
expect("properties", "environmentFromCodedMap")
.logContains(
"FOO is BAR",
"PROP_NAME is Bobby",
"PROP_NUM is 5").go();
}

@Test
public void fromLibrary() throws Exception {
initGlobalLibraryResource();

expect("properties", "environmentFromLibraryResource")
.logContains(
"FOO is BAR",
"PROP_NAME is Liam",
"PROP_NUM is 10").go();
}

@Test
public void fromLibraryUrl() throws Exception {
initGlobalLibraryResource();

expect("properties", "environmentFromLibraryResourceUrl")
.logContains(
"FOO is BAR",
"PROP_NAME is Liam",
"PROP_NUM is 10").go();
}

private void initGlobalLibraryResource() throws Exception {
otherRepo.init();
otherRepo.write("vars/bar.groovy", "void call() { echo \"Hello\" } ");
otherRepo.write("resources/foo/bar.properties", "#Project build properties for ACME Inc.\n" +
"\n" +
"NAME=Liam\n" +
"NUM=10\n");
otherRepo.git("add", "vars");
otherRepo.git("add", "resources");
otherRepo.git("commit", "--message=init");
GlobalLibraries.get().setLibraries(Collections.singletonList(
new LibraryConfiguration("resources-stuff",
new SCMSourceRetriever(new GitSCMSource(null, otherRepo.toString(), "", "*", "", true)))));
}
}
@@ -0,0 +1,54 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* 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 properties


pipeline {
environment {
FOO = "BAZ"
PROP = properties([data: [NAME: "Bobby", NUM: 5]]) //A bit cheaty but need to workaround the argument unpacking
}
agent any

stages {
stage("foo") {
environment {
FOO = "BAR"
}

steps {
sh '''
echo "FOO is $FOO"
echo "PROP_NAME is $PROP_NAME"
echo "PROP_NUM is $PROP_NUM"
'''
}
}
}
}



@@ -0,0 +1,55 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* 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.
*
*/

import org.jenkinsci.plugins.workflow.libs.Library

@Library('resources-stuff@master') _

pipeline {
environment {
FOO = "BAZ"
PROP = properties(libraryResource("foo/bar.properties"))
}
agent any

stages {
stage("foo") {
environment {
FOO = "BAR"
}

steps {
sh '''
echo "FOO is $FOO"
echo "PROP_NAME is $PROP_NAME"
echo "PROP_NUM is $PROP_NUM"
'''
}
}
}
}



@@ -0,0 +1,55 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* 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.
*
*/

import org.jenkinsci.plugins.workflow.libs.Library

@Library('resources-stuff@master') _

pipeline {
environment {
FOO = "BAZ"
PROP = properties("resources://foo/bar.properties")
}
agent any

stages {
stage("foo") {
environment {
FOO = "BAR"
}

steps {
sh '''
echo "FOO is $FOO"
echo "PROP_NAME is $PROP_NAME"
echo "PROP_NUM is $PROP_NUM"
'''
}
}
}
}



Expand Up @@ -23,6 +23,9 @@
*
*/

package properties


pipeline {
environment {
FOO = "BAZ"
Expand Down
Expand Up @@ -23,6 +23,9 @@
*
*/

package properties


pipeline {
environment {
FOO = "BAZ"
Expand Down

0 comments on commit 5d78778

Please sign in to comment.