Search in sources :

Example 6 with CNode

use of io.jenkins.plugins.casc.model.CNode in project configuration-as-code-plugin by jenkinsci.

the class TerraformTest method export_terraform_tool.

@Test
public void export_terraform_tool() throws Exception {
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    CNode yourAttribute = getToolRoot(context).get("terraform");
    String exported = toYamlString(yourAttribute);
    String expected = toStringFromYamlFile(this, "TerraformConfiguratorTestExpected.yml");
    assertThat(exported, is(expected));
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) Util.toYamlString(io.jenkins.plugins.casc.misc.Util.toYamlString) Test(org.junit.Test)

Example 7 with CNode

use of io.jenkins.plugins.casc.model.CNode in project configuration-as-code-plugin by jenkinsci.

the class SystemCredentialsTest method configure_system_credentials.

@Test
@ConfiguredWithCode("SystemCredentialsTest.yml")
public void configure_system_credentials() throws Exception {
    Jenkins jenkins = Jenkins.get();
    List<UsernamePasswordCredentials> ups = CredentialsProvider.lookupCredentials(UsernamePasswordCredentials.class, jenkins, ACL.SYSTEM, Collections.emptyList());
    assertThat(ups, hasSize(1));
    final UsernamePasswordCredentials up = ups.get(0);
    assertThat(up.getPassword().getPlainText(), equalTo("1234"));
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    final ConfigurationContext context = new ConfigurationContext(registry);
    final CNode node = context.lookup(up.getClass()).describe(up, context);
    assertThat(node.asMapping().getScalarValue("password"), not(equals("1234")));
    List<CertificateCredentials> certs = CredentialsProvider.lookupCredentials(CertificateCredentials.class, jenkins, ACL.SYSTEM, Collections.emptyList());
    assertThat(certs, hasSize(0));
    // TODO: add test for uploaded certificate
    // assertThat(certs.get(0).getPassword().getPlainText(), equalTo("ABCD"));
    List<BasicSSHUserPrivateKey> sshPrivateKeys = CredentialsProvider.lookupCredentials(BasicSSHUserPrivateKey.class, jenkins, ACL.SYSTEM, Collections.emptyList());
    assertThat(sshPrivateKeys, hasSize(1));
    final BasicSSHUserPrivateKey ssh_with_passphrase = sshPrivateKeys.get(0);
    assertThat(ssh_with_passphrase.getPassphrase().getPlainText(), equalTo("ABCD"));
    final DirectEntryPrivateKeySource source = (DirectEntryPrivateKeySource) ssh_with_passphrase.getPrivateKeySource();
    assertThat(source.getPrivateKey().getPlainText(), equalTo("s3cr3t"));
    // credentials should not appear in plain text in log
    for (LogRecord logRecord : log.getRecords()) {
        assertThat(logRecord.getMessage(), not(containsString("1234")));
        assertThat(logRecord.getMessage(), not(containsString("ABCD")));
    }
}
Also used : Jenkins(jenkins.model.Jenkins) CNode(io.jenkins.plugins.casc.model.CNode) LogRecord(java.util.logging.LogRecord) CertificateCredentials(com.cloudbees.plugins.credentials.common.CertificateCredentials) DirectEntryPrivateKeySource(com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.DirectEntryPrivateKeySource) BasicSSHUserPrivateKey(com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey) UsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 8 with CNode

use of io.jenkins.plugins.casc.model.CNode in project configuration-as-code-plugin by jenkinsci.

the class ToolDefaultPropertiesExportIgnoreListTest method export_tool_configuration.

@Test
@Issue("JENKINS-57122")
@ConfiguredWithCode("ToolDefaultPropertiesExportIgnoreList.yml")
public void export_tool_configuration() throws Exception {
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    CNode yourAttribute = getToolRoot(context);
    String exported = toYamlString(yourAttribute).replaceAll("git\\.exe", "git");
    String expected = toStringFromYamlFile(this, "ToolDefaultPropertiesExportIgnoreListExpected.yml");
    assertThat(exported, is(expected));
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) Util.toYamlString(io.jenkins.plugins.casc.misc.Util.toYamlString) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 9 with CNode

use of io.jenkins.plugins.casc.model.CNode in project configuration-as-code-plugin by jenkinsci.

the class ConfigurationAsCode method toYaml.

@CheckForNull
// for testing only
@Restricted(NoExternalUse.class)
public Node toYaml(CNode config) throws ConfiguratorException {
    if (config == null)
        return null;
    switch(config.getType()) {
        case MAPPING:
            final Mapping mapping = config.asMapping();
            final List<NodeTuple> tuples = new ArrayList<>();
            final List<Map.Entry<String, CNode>> entries = new ArrayList<>(mapping.entrySet());
            entries.sort(Map.Entry.comparingByKey());
            for (Map.Entry<String, CNode> entry : entries) {
                final Node valueNode = toYaml(entry.getValue());
                if (valueNode == null)
                    continue;
                tuples.add(new NodeTuple(new ScalarNode(Tag.STR, entry.getKey(), null, null, PLAIN), valueNode));
            }
            if (tuples.isEmpty())
                return null;
            return new MappingNode(Tag.MAP, tuples, BLOCK);
        case SEQUENCE:
            final Sequence sequence = config.asSequence();
            List<Node> nodes = new ArrayList<>();
            for (CNode cNode : sequence) {
                final Node valueNode = toYaml(cNode);
                if (valueNode == null)
                    continue;
                nodes.add(valueNode);
            }
            if (nodes.isEmpty())
                return null;
            return new SequenceNode(Tag.SEQ, nodes, BLOCK);
        case SCALAR:
        default:
            final Scalar scalar = config.asScalar();
            final String value = scalar.getValue();
            if (value == null || value.length() == 0)
                return null;
            final DumperOptions.ScalarStyle style;
            if (scalar.getFormat().equals(Format.MULTILINESTRING) && !scalar.isRaw()) {
                style = LITERAL;
            } else if (scalar.isRaw()) {
                style = PLAIN;
            } else {
                style = DOUBLE_QUOTED;
            }
            return new ScalarNode(getTag(scalar.getFormat()), value, null, null, style);
    }
}
Also used : ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) CNode(io.jenkins.plugins.casc.model.CNode) Node(org.yaml.snakeyaml.nodes.Node) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) ArrayList(java.util.ArrayList) Mapping(io.jenkins.plugins.casc.model.Mapping) Sequence(io.jenkins.plugins.casc.model.Sequence) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) Scalar(io.jenkins.plugins.casc.model.Scalar) CNode(io.jenkins.plugins.casc.model.CNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) DumperOptions(org.yaml.snakeyaml.DumperOptions) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) Map(java.util.Map) HashMap(java.util.HashMap) Restricted(org.kohsuke.accmod.Restricted) CheckForNull(edu.umd.cs.findbugs.annotations.CheckForNull)

Example 10 with CNode

use of io.jenkins.plugins.casc.model.CNode in project configuration-as-code-plugin by jenkinsci.

the class ConfigurationAsCode method export.

@Restricted(NoExternalUse.class)
public void export(OutputStream out) throws Exception {
    final List<NodeTuple> tuples = new ArrayList<>();
    final ConfigurationContext context = new ConfigurationContext(registry);
    for (RootElementConfigurator root : RootElementConfigurator.all()) {
        final CNode config = root.describe(root.getTargetComponent(context), context);
        final Node valueNode = toYaml(config);
        if (valueNode == null)
            continue;
        tuples.add(new NodeTuple(new ScalarNode(Tag.STR, root.getName(), null, null, PLAIN), valueNode));
    }
    MappingNode root = new MappingNode(Tag.MAP, tuples, BLOCK);
    try (Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
        serializeYamlNode(root, writer);
    } catch (IOException e) {
        throw new YAMLException(e);
    }
}
Also used : ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) CNode(io.jenkins.plugins.casc.model.CNode) Node(org.yaml.snakeyaml.nodes.Node) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) ArrayList(java.util.ArrayList) YAMLException(org.yaml.snakeyaml.error.YAMLException) IOException(java.io.IOException) CNode(io.jenkins.plugins.casc.model.CNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) OutputStreamWriter(java.io.OutputStreamWriter) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Restricted(org.kohsuke.accmod.Restricted)

Aggregations

CNode (io.jenkins.plugins.casc.model.CNode)36 Test (org.junit.Test)30 ConfiguredWithCode (io.jenkins.plugins.casc.misc.ConfiguredWithCode)16 Util.toYamlString (io.jenkins.plugins.casc.misc.Util.toYamlString)15 ConfigurationContext (io.jenkins.plugins.casc.ConfigurationContext)13 ConfiguratorRegistry (io.jenkins.plugins.casc.ConfiguratorRegistry)13 Mapping (io.jenkins.plugins.casc.model.Mapping)12 Configurator (io.jenkins.plugins.casc.Configurator)9 Issue (org.jvnet.hudson.test.Issue)6 Node (org.yaml.snakeyaml.nodes.Node)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ConfiguredWithReadme (io.jenkins.plugins.casc.misc.ConfiguredWithReadme)3 StringWriter (java.io.StringWriter)3 Jenkins (jenkins.model.Jenkins)3 YAMLException (org.yaml.snakeyaml.error.YAMLException)3 CredentialsRootConfigurator (com.cloudbees.plugins.credentials.casc.CredentialsRootConfigurator)2 ProxyConfiguration (hudson.ProxyConfiguration)2 Secret (hudson.util.Secret)2 ConfigurationAsCode (io.jenkins.plugins.casc.ConfigurationAsCode)2