Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-40062] Add nodes config.xml files to the bundle
  • Loading branch information
amuniz committed Dec 7, 2016
1 parent c8df4a8 commit 48dcdaf
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
@@ -0,0 +1,92 @@
/*
* The MIT License
*
* Copyright (c) 2016, 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 com.cloudbees.jenkins.support.configfiles;

import com.cloudbees.jenkins.support.api.Component;
import com.cloudbees.jenkins.support.api.Container;
import com.cloudbees.jenkins.support.api.TemporaryFileContent;
import com.cloudbees.jenkins.support.util.Helper;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.security.Permission;
import jenkins.model.Jenkins;
import org.xml.sax.SAXException;

import javax.xml.transform.TransformerException;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
* Adds nodes config.xml files to the bundle.
* Any secret string is redacted in the xml file.
*
* Unselected by default.
*/
@Extension
public class AgentsConfigFile extends Component {

@Override
public boolean isSelectedByDefault() {
return false;
}

@NonNull
@Override
public Set<Permission> getRequiredPermissions() {
return Collections.singleton(Jenkins.ADMINISTER);
}

@NonNull
@Override
public String getDisplayName() {
return Messages.AgentsConfigFile_displayName();
}

@Override
public void addContents(@NonNull Container container) {
File[] agentDirs = new File(Helper.getActiveInstance().getRootDir(), "nodes").listFiles();
if (agentDirs == null) {
return;
}
for(File agentDir : agentDirs) {
File config = new File(agentDir, "config.xml");
try {
File patchedConfig = SecretHandler.findSecrets(config);
container.add(new TemporaryFileContent("nodes/slave/" + agentDir.getName() + "/config.xml", patchedConfig));
} catch (IOException | SAXException | TransformerException e) {
LogRecord record = new LogRecord(Level.WARNING, "Could not add the {0} configuration file to the support bundle because of: {1}");
record.setParameters(new Object[]{ config.getAbsolutePath(), e });
record.setThrown(e);
LOGGER.log(record);
}
}
}

private static final Logger LOGGER = Logger.getLogger(AgentsConfigFile.class.getName());
}
@@ -0,0 +1,25 @@
#
# The MIT License
#
# Copyright (c) 2016, 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.
#

AgentsConfigFile.displayName=Agents config files (Encrypted secrets are redacted)
@@ -0,0 +1,61 @@
/*
* The MIT License
*
* Copyright (c) 2016, 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 com.cloudbees.jenkins.support.configfiles;

import com.cloudbees.jenkins.support.api.Container;
import com.cloudbees.jenkins.support.api.Content;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.EnvVars;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class AgentsConfigFileTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void agentsConfigFile() throws Exception {
j.createSlave("node1", "node1", new EnvVars());
AgentsConfigFile comp = new AgentsConfigFile();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
comp.addContents(new Container() {
@Override
public void add(@CheckForNull Content content) {
try {
content.writeTo(baos);
} catch (IOException e) {
Assert.fail(e.getMessage());
}
}
});
String fileContent = baos.toString();
Assert.assertTrue(fileContent.contains("<name>node1</name>"));
}
}

0 comments on commit 48dcdaf

Please sign in to comment.