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));
}
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")));
}
}
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));
}
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);
}
}
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);
}
}
Aggregations