Java Code Examples for io.jenkins.plugins.casc.model.CNode#asMapping()

The following examples show how to use io.jenkins.plugins.casc.model.CNode#asMapping() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseConfigurator.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
@NonNull
@Override
public T configure(CNode c, ConfigurationContext context) throws ConfiguratorException {
    final Mapping mapping = (c != null ? c.asMapping() : Mapping.EMPTY);
    final T instance = instance(mapping, context);
    if (instance instanceof Saveable) {
        try (BulkChange bc = new BulkChange((Saveable) instance) ){
            configure(mapping, instance, false, context);
            bc.commit();
        } catch (IOException e) {
            throw new ConfiguratorException("Failed to save "+instance, e);
        }
    } else {
        configure(mapping, instance, false, context);
    }

    return instance;
}
 
Example 2
Source File: CredentialsTest.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
@Test
@ConfiguredWithCode("GlobalCredentials.yml")
public void testExportFileCredentials() throws Exception {
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    CredentialsRootConfigurator root = ExtensionList.lookupSingleton(CredentialsRootConfigurator.class);

    CNode node = root.describe(root.getTargetComponent(context), context);
    assertNotNull(node);
    final Mapping mapping = node.asMapping();

    Mapping fileCredential = mapping.get("system")
        .asMapping()
        .get("domainCredentials")
        .asSequence().get(0)
        .asMapping().get("credentials")
        .asSequence().get(2)
        .asMapping().get("file").asMapping();

    assertThat(fileCredential.getScalarValue("scope"), is("GLOBAL"));
    assertThat(fileCredential.getScalarValue("id"), is("secret-file"));
    assertThat(fileCredential.getScalarValue("fileName"), is("mysecretfile.txt"));
    assertThat(fileCredential.getScalarValue("secretBytes"), not("WJjZAo="));
}
 
Example 3
Source File: ProxyConfiguratorTest.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
@Test
@ConfiguredWithCode("Proxy.yml")
public void shouldSetProxyWithAllFields() throws Exception {
    ProxyConfiguration proxy = j.jenkins.proxy;
    assertEquals(proxy.name, "proxyhost");
    assertEquals(proxy.port, 80);

    assertEquals(proxy.getUserName(), "login");
    assertThat(proxy.getSecretPassword(), hasPlainText("password"));
    assertEquals(proxy.noProxyHost, "externalhost");
    assertEquals(proxy.getTestUrl(), "http://google.com");

    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    final Configurator c = context.lookupOrFail(ProxyConfiguration.class);
    final CNode node = c.describe(proxy, context);
    assertNotNull(node);
    Mapping mapping = node.asMapping();
    assertEquals(6, mapping.size());
    assertEquals("proxyhost", mapping.getScalarValue("name"));
}
 
Example 4
Source File: ProxyConfiguratorTest.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
@Test
@ConfiguredWithCode("ProxyMinimal.yml")
public void shouldSetProxyWithMinimumFields() throws Exception {
    ProxyConfiguration proxy = j.jenkins.proxy;
    assertEquals(proxy.name, "proxyhost");
    assertEquals(proxy.port, 80);

    assertNull(proxy.getUserName());
    assertNull(proxy.getTestUrl());

    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    final Configurator c = context.lookupOrFail(ProxyConfiguration.class);
    final CNode node = c.describe(proxy, context);
    assertNotNull(node);
    Mapping mapping = node.asMapping();
    assertEquals(2, node.asMapping().size());
    assertEquals("proxyhost", mapping.getScalarValue("name"));
    assertEquals("80", mapping.getScalarValue("port"));
}
 
Example 5
Source File: GitToolConfiguratorJenkinsRuleTest.java    From git-client-plugin with MIT License 6 votes vote down vote up
@Test
public void testDescribeGitToolEmptyProperties() throws Exception {
    String gitName = "git-2.19.1-name";
    String gitHome = "/opt/git-2.19.1/bin/git";

    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);

    // Intentionally empty properties passed to GitTool constructor
    List<ToolProperty<ToolInstallation>> gitToolProperties = new ArrayList<>();

    GitTool gitTool = new GitTool(gitName, gitHome, gitToolProperties);

    CNode cNode = gitToolConfigurator.describe(gitTool, context);

    assertThat(cNode, is(notNullValue()));
    assertThat(cNode.getType(), is(CNode.Type.MAPPING));
    Mapping cNodeMapping = cNode.asMapping();
    assertThat(cNodeMapping.getScalarValue("name"), is(gitName));
    assertThat(cNodeMapping.getScalarValue("home"), is(gitHome));
}
 
Example 6
Source File: BaseConfigurator.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Override
public T check(CNode c, ConfigurationContext context) throws ConfiguratorException {
    final Mapping mapping = (c != null ? c.asMapping() : Mapping.EMPTY);
    final T instance = instance(mapping, context);
    configure(mapping, instance, true, context);
    return instance;
}
 
Example 7
Source File: CredentialsTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@ConfiguredWithCode("GlobalCredentials.yml")
@Test
public void testExportSSHCredentials() throws Exception {
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    CredentialsRootConfigurator root = ExtensionList.lookupSingleton(CredentialsRootConfigurator.class);

    CNode node = root.describe(root.getTargetComponent(context), context);
    assertNotNull(node);
    final Mapping mapping = node.asMapping();

    Mapping sshCredential = mapping.get("system")
            .asMapping()
            .get("domainCredentials")
            .asSequence().get(0)
            .asMapping().get("credentials")
            .asSequence().get(1)
            .asMapping().get("basicSSHUserPrivateKey").asMapping();

    assertThat(sshCredential.getScalarValue("scope"), is("SYSTEM"));
    assertThat(sshCredential.getScalarValue("id"), is("agent-private-key"));
    assertThat(sshCredential.getScalarValue("username"), is("agentuser"));

    String passphrase = sshCredential.getScalarValue("passphrase");
    assertThat(passphrase, not("password"));
    assertThat(requireNonNull(Secret.decrypt(passphrase), "Failed to decrypt the password from " + passphrase)
            .getPlainText(), is("password"));

    String sshKeyExported = sshCredential.get("privateKeySource")
            .asMapping()
            .get("directEntry")
            .asMapping()
            .get("privateKey")
            .asScalar()
            .getValue();

    assertThat(sshKeyExported, not("sp0ds9d+skkfjf"));
    assertThat(requireNonNull(Secret.decrypt(sshKeyExported)).getPlainText(), is("sp0ds9d+skkfjf"));
}
 
Example 8
Source File: AdminWhitelistRuleConfiguratorTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
@Issue("Issue #172")
@ConfiguredWithCode("AdminWhitelistRuleConfigurator/Agent2MasterSecurityKillSwitch_enabled.yml")
public void checkA2MAccessControl_enabled() throws Exception {
    final Jenkins jenkins = Jenkins.get();
    MasterKillSwitchConfiguration config = jenkins.getDescriptorByType(MasterKillSwitchConfiguration.class);
    Assert.assertTrue("Agent → Master Access Control should be enabled", config.getMasterToSlaveAccessControl());
    AdminWhitelistRule rule = jenkins.getInjector().getInstance(AdminWhitelistRule.class);
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    final Configurator c = context.lookupOrFail(AdminWhitelistRule.class);
    final CNode node = c.describe(rule, context);
    final Mapping agent = node.asMapping();
    assertEquals("true", agent.get("enabled").toString());
}
 
Example 9
Source File: AdminWhitelistRuleConfiguratorTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
@Issue("Issue #172")
@ConfiguredWithCode("AdminWhitelistRuleConfigurator/Agent2MasterSecurityKillSwitch_disabled.yml")
public void checkA2MAccessControl_disable() throws Exception {
    final Jenkins jenkins = Jenkins.get();
    MasterKillSwitchConfiguration config = jenkins.getDescriptorByType(MasterKillSwitchConfiguration.class);
    Assert.assertFalse("Agent → Master Access Control should be disabled", config.getMasterToSlaveAccessControl());
    AdminWhitelistRule rule = jenkins.getInjector().getInstance(AdminWhitelistRule.class);
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    final Configurator c = context.lookupOrFail(AdminWhitelistRule.class);
    final CNode node = c.describe(rule, context);
    final Mapping agent = node.asMapping();
    assertEquals("false", agent.get("enabled").toString());
}
 
Example 10
Source File: GitToolConfiguratorJenkinsRuleTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testDescribeGitTool() throws Exception {
    String gitName = "git-2.19.1-name";
    String gitHome = "/opt/git-2.19.1/bin/git";

    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);

    List<ToolProperty<ToolInstallation>> gitToolProperties = new ArrayList<>();
    List<ToolInstaller> toolInstallers = new ArrayList<>();
    ToolInstaller toolInstaller = new ZipExtractionInstaller("tool-label", "tool-url", "tool-subdir");
    toolInstallers.add(toolInstaller);
    InstallSourceProperty installSourceProperty = new InstallSourceProperty(toolInstallers);
    gitToolProperties.add(installSourceProperty);

    GitTool gitTool = new GitTool(gitName, gitHome, gitToolProperties);

    CNode cNode = gitToolConfigurator.describe(gitTool, context);

    assertThat(cNode, is(notNullValue()));
    assertThat(cNode.getType(), is(CNode.Type.MAPPING));
    Mapping cNodeMapping = cNode.asMapping();
    assertThat(cNodeMapping.getScalarValue("name"), is(gitName));
    assertThat(cNodeMapping.getScalarValue("home"), is(gitHome));

    //         properties:
    //          - installSource:
    //              installers:
    //                - zip:
    //                    label: "tool-label"
    //                    subdir: "tool-subdir"
    //                    url: "tool-url"
    Sequence propertiesSequence = cNodeMapping.get("properties").asSequence();                             // properties:
    Mapping installSourceMapping = propertiesSequence.get(0).asMapping().get("installSource").asMapping(); //  - installSource:
    Sequence installersSequence = installSourceMapping.get("installers").asSequence();                     //      installers:
    Mapping zipMapping = installersSequence.get(0).asMapping().get("zip").asMapping();                     //        - zip:
    assertThat(zipMapping.getScalarValue("label"), is("tool-label"));
    assertThat(zipMapping.getScalarValue("subdir"), is("tool-subdir"));
    assertThat(zipMapping.getScalarValue("url"), is("tool-url"));
}
 
Example 11
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testDescribeJGitTool() throws Exception {
    GitTool gitTool = new JGitTool();
    CNode cNode = gitToolConfigurator.describe(gitTool, NULL_CONFIGURATION_CONTEXT);
    assertThat(cNode, is(notNullValue()));
    assertThat(cNode.getType(), is(CNode.Type.MAPPING));
    Mapping cNodeMapping = cNode.asMapping();
    assertThat(cNodeMapping.getScalarValue("name"), is(JGitTool.MAGIC_EXENAME));
}
 
Example 12
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testDescribeJGitApacheTool() throws Exception {
    GitTool gitTool = new JGitApacheTool();
    CNode cNode = gitToolConfigurator.describe(gitTool, NULL_CONFIGURATION_CONTEXT);
    assertThat(cNode, is(notNullValue()));
    assertThat(cNode.getType(), is(CNode.Type.MAPPING));
    Mapping cNodeMapping = cNode.asMapping();
    assertThat(cNodeMapping.getScalarValue("name"), is(JGitApacheTool.MAGIC_EXENAME));
}
 
Example 13
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testDescribeGitToolWithoutProperties() throws Exception {
    String gitName = "git-name";
    String gitHome = "/opt/git-2.23.0/bin/git";
    GitTool gitTool = new GitTool(gitName, gitHome, null);
    CNode cNode = gitToolConfigurator.describe(gitTool, NULL_CONFIGURATION_CONTEXT);
    assertThat(cNode, is(notNullValue()));
    assertThat(cNode.getType(), is(CNode.Type.MAPPING));
    Mapping cNodeMapping = cNode.asMapping();
    assertThat(cNodeMapping.getScalarValue("name"), is(gitName));
    assertThat(cNodeMapping.getScalarValue("home"), is(gitHome));
}