Search in sources :

Example 1 with ConfigurationContext

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

the class ExportTest method export.

public <T> String export(DataBoundConfigurator<T> configurator, T object) throws Exception {
    ConfigurationAsCode casc = ConfigurationAsCode.get();
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    final CNode config = configurator.describe(object, context);
    final Node valueNode = casc.toYaml(config);
    try (StringWriter writer = new StringWriter()) {
        ConfigurationAsCode.serializeYamlNode(valueNode, writer);
        return writer.toString();
    } catch (IOException e) {
        throw new YAMLException(e);
    }
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) StringWriter(java.io.StringWriter) CNode(io.jenkins.plugins.casc.model.CNode) Node(org.yaml.snakeyaml.nodes.Node) ConfigurationAsCode(io.jenkins.plugins.casc.ConfigurationAsCode) YAMLException(org.yaml.snakeyaml.error.YAMLException) IOException(java.io.IOException)

Example 2 with ConfigurationContext

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

the class AdminWhitelistRuleConfiguratorTest method checkA2MAccessControl_disable.

@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());
}
Also used : Jenkins(jenkins.model.Jenkins) CNode(io.jenkins.plugins.casc.model.CNode) ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) AdminWhitelistRule(jenkins.security.s2m.AdminWhitelistRule) Configurator(io.jenkins.plugins.casc.Configurator) MasterKillSwitchConfiguration(jenkins.security.s2m.MasterKillSwitchConfiguration) Mapping(io.jenkins.plugins.casc.model.Mapping) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 3 with ConfigurationContext

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

the class AdminWhitelistRuleConfiguratorTest method checkA2MAccessControl_enabled.

@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());
}
Also used : Jenkins(jenkins.model.Jenkins) CNode(io.jenkins.plugins.casc.model.CNode) ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) AdminWhitelistRule(jenkins.security.s2m.AdminWhitelistRule) Configurator(io.jenkins.plugins.casc.Configurator) MasterKillSwitchConfiguration(jenkins.security.s2m.MasterKillSwitchConfiguration) Mapping(io.jenkins.plugins.casc.model.Mapping) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 4 with ConfigurationContext

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

the class ProxyConfiguratorTest method shouldSetProxyWithAllFields.

@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"));
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) ProxyConfiguration(hudson.ProxyConfiguration) Configurator(io.jenkins.plugins.casc.Configurator) Mapping(io.jenkins.plugins.casc.model.Mapping) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 5 with ConfigurationContext

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

the class ProxyConfiguratorTest method describeProxyConfig.

@Test
@ConfiguredWithCode("Proxy.yml")
public void describeProxyConfig() throws Exception {
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    final CNode configNode = getProxyNode(context);
    Secret password = requireNonNull(Secret.decrypt(getProxyNode(context).getScalarValue("secretPassword")));
    final String yamlConfig = Util.toYamlString(configNode);
    assertEquals(String.join("\n", "name: \"proxyhost\"", "noProxyHost: \"externalhost\"", "port: 80", "secretPassword: \"" + password.getEncryptedValue() + "\"", "testUrl: \"http://google.com\"", "userName: \"login\"", ""), yamlConfig);
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) Secret(hudson.util.Secret) ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Aggregations

ConfigurationContext (io.jenkins.plugins.casc.ConfigurationContext)25 ConfiguratorRegistry (io.jenkins.plugins.casc.ConfiguratorRegistry)24 Test (org.junit.Test)22 Mapping (io.jenkins.plugins.casc.model.Mapping)19 CNode (io.jenkins.plugins.casc.model.CNode)13 Configurator (io.jenkins.plugins.casc.Configurator)8 ConfiguredWithCode (io.jenkins.plugins.casc.misc.ConfiguredWithCode)8 Issue (org.jvnet.hudson.test.Issue)5 ProxyConfiguration (hudson.ProxyConfiguration)2 ConfigurationAsCode (io.jenkins.plugins.casc.ConfigurationAsCode)2 ConfiguratorException (io.jenkins.plugins.casc.ConfiguratorException)2 Sequence (io.jenkins.plugins.casc.model.Sequence)2 Jenkins (jenkins.model.Jenkins)2 AdminWhitelistRule (jenkins.security.s2m.AdminWhitelistRule)2 MasterKillSwitchConfiguration (jenkins.security.s2m.MasterKillSwitchConfiguration)2 BeforeClass (org.junit.BeforeClass)2 UpdateCenter (hudson.model.UpdateCenter)1 UpdateSite (hudson.model.UpdateSite)1 Secret (hudson.util.Secret)1 ClassParametersAreNonnullByDefault (io.jenkins.plugins.casc.impl.configurators.nonnull.ClassParametersAreNonnullByDefault)1