Search in sources :

Example 1 with DataBoundConfigurator

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

the class ExportTest method shouldNotExportValuesWithSecretFields.

@Test
@Issue("SECURITY-1458")
public void shouldNotExportValuesWithSecretFields() throws Exception {
    DataBoundConfigurator<DataBoundSecretField> c = new DataBoundConfigurator<>(DataBoundSecretField.class);
    String res = export(c, new DataBoundSecretField("test"));
    assertThat(res, not(containsString("test")));
}
Also used : DataBoundConfigurator(io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 2 with DataBoundConfigurator

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

the class YamlExportTest method shouldDiscoverSecretsBasedOnTheAttributeType.

@Test
@Issue("SECURITY-1458")
public void shouldDiscoverSecretsBasedOnTheAttributeType() {
    DataBoundConfigurator c = new DataBoundConfigurator<>(AttributeTest.SecretRenamedFieldFithSecretConstructor.class);
    Set<Attribute> attributes = c.describe();
    assertThat(attributes.size(), equalTo(1));
    Attribute attr = attributes.iterator().next();
    assertTrue(attr.isSecret(null));
}
Also used : AttributeTest(io.jenkins.plugins.casc.AttributeTest) Attribute(io.jenkins.plugins.casc.Attribute) DataBoundConfigurator(io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test) AttributeTest(io.jenkins.plugins.casc.AttributeTest)

Example 3 with DataBoundConfigurator

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

the class DefaultConfiguratorRegistry method internalLookup.

private Configurator internalLookup(Type type) {
    Class clazz = Types.erasure(type);
    final Jenkins jenkins = Jenkins.get();
    final ExtensionList<Configurator> l = jenkins.getExtensionList(Configurator.class);
    for (Configurator c : l) {
        if (c.canConfigure(clazz)) {
            // this type has a dedicated Configurator implementation
            return c;
        }
    }
    // TODO: Only try to cast if we can actually get the parameterized type
    if (Collection.class.isAssignableFrom(clazz) && type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        Type actualType = pt.getActualTypeArguments()[0];
        if (actualType instanceof WildcardType) {
            actualType = ((WildcardType) actualType).getUpperBounds()[0];
        }
        if (actualType instanceof ParameterizedType) {
            actualType = ((ParameterizedType) actualType).getRawType();
        }
        if (!(actualType instanceof Class)) {
            throw new IllegalStateException("Can't handle " + type);
        }
        // cache is not reëntrant
        return internalLookup(actualType);
    }
    if (Configurable.class.isAssignableFrom(clazz)) {
        return new ConfigurableConfigurator(clazz);
    }
    if (Descriptor.class.isAssignableFrom(clazz)) {
        ExtensionList extensions = jenkins.getExtensionList(clazz);
        if (!extensions.isEmpty()) {
            return new DescriptorConfigurator((Descriptor) extensions.get(0));
        }
    }
    if (DataBoundConfigurator.getDataBoundConstructor(clazz) != null) {
        return new DataBoundConfigurator(clazz);
    }
    if (Modifier.isAbstract(clazz.getModifiers()) && Describable.class.isAssignableFrom(clazz)) {
        // this is a jenkins Describable component, with various implementations
        return new HeteroDescribableConfigurator(clazz);
    }
    if (Extension.class.isAssignableFrom(clazz)) {
        return new ExtensionConfigurator(clazz);
    }
    if (Stapler.lookupConverter(clazz) != null) {
        return new PrimitiveConfigurator(clazz);
    }
    if (clazz.isEnum()) {
        return new EnumConfigurator(clazz);
    }
    LOGGER.warning("Configuration-as-Code can't handle type " + type);
    return null;
}
Also used : ConfigurableConfigurator(io.jenkins.plugins.casc.impl.configurators.ConfigurableConfigurator) EnumConfigurator(io.jenkins.plugins.casc.impl.configurators.EnumConfigurator) Describable(hudson.model.Describable) DescriptorConfigurator(io.jenkins.plugins.casc.impl.configurators.DescriptorConfigurator) RootElementConfigurator(io.jenkins.plugins.casc.RootElementConfigurator) PrimitiveConfigurator(io.jenkins.plugins.casc.impl.configurators.PrimitiveConfigurator) HeteroDescribableConfigurator(io.jenkins.plugins.casc.impl.configurators.HeteroDescribableConfigurator) DataBoundConfigurator(io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator) Configurator(io.jenkins.plugins.casc.Configurator) EnumConfigurator(io.jenkins.plugins.casc.impl.configurators.EnumConfigurator) ConfigurableConfigurator(io.jenkins.plugins.casc.impl.configurators.ConfigurableConfigurator) ExtensionConfigurator(io.jenkins.plugins.casc.impl.configurators.ExtensionConfigurator) DataBoundConfigurator(io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator) DescriptorConfigurator(io.jenkins.plugins.casc.impl.configurators.DescriptorConfigurator) Jenkins(jenkins.model.Jenkins) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ExtensionConfigurator(io.jenkins.plugins.casc.impl.configurators.ExtensionConfigurator) HeteroDescribableConfigurator(io.jenkins.plugins.casc.impl.configurators.HeteroDescribableConfigurator) ExtensionList(hudson.ExtensionList) Collection(java.util.Collection) PrimitiveConfigurator(io.jenkins.plugins.casc.impl.configurators.PrimitiveConfigurator)

Example 4 with DataBoundConfigurator

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

the class Security1446Test method testExportWithEnvVar.

@Test
@Issue("SECURITY-1446")
public void testExportWithEnvVar() throws Exception {
    final String message = "Hello, world! PATH=${PATH} JAVA_HOME=^${JAVA_HOME}";
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    DataBoundConfigurator<UsernamePasswordCredentialsImpl> configurator = new DataBoundConfigurator<>(UsernamePasswordCredentialsImpl.class);
    UsernamePasswordCredentialsImpl creds = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "test", message, "foo", "bar");
    final CNode config = configurator.describe(creds, context);
    final Node valueNode = ConfigurationAsCode.get().toYaml(config);
    final String exported;
    try (StringWriter writer = new StringWriter()) {
        ConfigurationAsCode.serializeYamlNode(valueNode, writer);
        exported = writer.toString();
    } catch (IOException e) {
        throw new YAMLException(e);
    }
    assertThat("Message was not escaped", exported, not(containsString(message)));
    assertThat("Improper masking for PATH", exported, containsString("^${PATH}"));
    assertThat("Improper masking for JAVA_HOME", exported, containsString("^^${JAVA_HOME}"));
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) StringWriter(java.io.StringWriter) DataBoundConfigurator(io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator) CNode(io.jenkins.plugins.casc.model.CNode) Node(org.yaml.snakeyaml.nodes.Node) YAMLException(org.yaml.snakeyaml.error.YAMLException) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 5 with DataBoundConfigurator

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

the class ExportTest method shouldNotExportValuesWithSecretConstructors.

@Test
@Issue("SECURITY-1458")
public void shouldNotExportValuesWithSecretConstructors() throws Exception {
    DataBoundConfigurator<DataBoundSecretConstructor> c = new DataBoundConfigurator<>(DataBoundSecretConstructor.class);
    String res = export(c, new DataBoundSecretConstructor(Secret.fromString("test")));
    assertThat(res, not(containsString("test")));
}
Also used : DataBoundConfigurator(io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Aggregations

DataBoundConfigurator (io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator)6 Test (org.junit.Test)5 Issue (org.jvnet.hudson.test.Issue)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)1 ExtensionList (hudson.ExtensionList)1 Describable (hudson.model.Describable)1 Attribute (io.jenkins.plugins.casc.Attribute)1 AttributeTest (io.jenkins.plugins.casc.AttributeTest)1 Configurator (io.jenkins.plugins.casc.Configurator)1 RootElementConfigurator (io.jenkins.plugins.casc.RootElementConfigurator)1 ConfigurableConfigurator (io.jenkins.plugins.casc.impl.configurators.ConfigurableConfigurator)1 DescriptorConfigurator (io.jenkins.plugins.casc.impl.configurators.DescriptorConfigurator)1 EnumConfigurator (io.jenkins.plugins.casc.impl.configurators.EnumConfigurator)1 ExtensionConfigurator (io.jenkins.plugins.casc.impl.configurators.ExtensionConfigurator)1 HeteroDescribableConfigurator (io.jenkins.plugins.casc.impl.configurators.HeteroDescribableConfigurator)1 PrimitiveConfigurator (io.jenkins.plugins.casc.impl.configurators.PrimitiveConfigurator)1 CNode (io.jenkins.plugins.casc.model.CNode)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1