Search in sources :

Example 1 with Attribute

use of io.jenkins.plugins.casc.Attribute 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 2 with Attribute

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

the class DataBoundConfigurator method describe.

@CheckForNull
@Override
public CNode describe(T instance, ConfigurationContext context) throws Exception {
    // Here we assume a correctly designed DataBound Object will have required attributes set by DataBoundConstructor
    // and all others using DataBoundSetters. So constructor parameters for sure are part of the description, others
    // need to be compared with default values.
    // Build same object with only constructor parameters
    final Constructor constructor = getDataBoundConstructor();
    final Parameter[] parameters = constructor.getParameters();
    final String[] names = ClassDescriptor.loadParameterNames(constructor);
    final Attribute[] attributes = new Attribute[parameters.length];
    final Object[] args = new Object[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        final Parameter p = parameters[i];
        final Attribute a = createAttribute(names[i], TypePair.of(p));
        if (a != null) {
            Object value = a.getValue(instance);
            if (value != null) {
                Object converted = Stapler.CONVERT_UTILS.convert(value, a.getType());
                if (converted instanceof Collection || p.getType().isArray() || !a.isMultiple()) {
                    args[i] = converted;
                } else if (Set.class.isAssignableFrom(p.getType())) {
                    args[i] = Collections.singleton(converted);
                } else {
                    args[i] = Collections.singletonList(converted);
                }
            }
            if (args[i] == null && p.getType().isPrimitive()) {
                args[i] = defaultPrimitiveValue(p.getType());
            }
            attributes[i] = a;
        }
    }
    T ref = (T) constructor.newInstance(args);
    // compare instance with this "default" object
    Mapping mapping = compare(instance, ref, context);
    // add constructor parameters
    for (int i = 0; i < parameters.length; i++) {
        if (args[i] == null)
            continue;
        mapping.put(names[i], attributes[i].describe(instance, context));
    }
    return mapping;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Attribute(io.jenkins.plugins.casc.Attribute) DescribableAttribute(io.jenkins.plugins.casc.impl.attributes.DescribableAttribute) DataBoundConstructor(org.kohsuke.stapler.DataBoundConstructor) Constructor(java.lang.reflect.Constructor) Mapping(io.jenkins.plugins.casc.model.Mapping) Parameter(java.lang.reflect.Parameter) Collection(java.util.Collection) CheckForNull(javax.annotation.CheckForNull)

Example 3 with Attribute

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

the class DataBoundConfigurator method describe.

@NonNull
@Override
public Set<Attribute<T, ?>> describe() {
    final Set<Attribute<T, ?>> attributes = super.describe();
    final Constructor constructor = getDataBoundConstructor(target);
    if (constructor != null) {
        final Parameter[] parameters = constructor.getParameters();
        final String[] names = ClassDescriptor.loadParameterNames(constructor);
        for (int i = 0; i < parameters.length; i++) {
            final Parameter p = parameters[i];
            final Attribute a = createAttribute(names[i], TypePair.of(p));
            if (a == null)
                continue;
            attributes.add(a);
        }
    }
    return attributes;
}
Also used : Attribute(io.jenkins.plugins.casc.Attribute) DescribableAttribute(io.jenkins.plugins.casc.impl.attributes.DescribableAttribute) DataBoundConstructor(org.kohsuke.stapler.DataBoundConstructor) Constructor(java.lang.reflect.Constructor) Parameter(java.lang.reflect.Parameter) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 4 with Attribute

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

the class MavenConfigurator method describe.

@NonNull
@Override
public Set<Attribute<GlobalMavenConfig, ?>> describe() {
    final Set<Attribute<GlobalMavenConfig, ?>> attributes = super.describe();
    final Descriptor descriptor = Jenkins.get().getDescriptorOrDie(Maven.class);
    final Configurator<Descriptor> task = new DescriptorConfigurator(descriptor);
    for (Attribute attribute : task.describe()) {
        attributes.add(new Attribute<GlobalMavenConfig, Object>(attribute.getName(), attribute.getType()).multiple(attribute.isMultiple()).getter(g -> attribute.getValue(descriptor)).setter((g, v) -> attribute.setValue(descriptor, v)));
    }
    return attributes;
}
Also used : Restricted(org.kohsuke.accmod.Restricted) Maven(hudson.tasks.Maven) Descriptor(hudson.model.Descriptor) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) Jenkins(jenkins.model.Jenkins) Configurator(io.jenkins.plugins.casc.Configurator) Set(java.util.Set) GlobalMavenConfig(jenkins.mvn.GlobalMavenConfig) NoExternalUse(org.kohsuke.accmod.restrictions.NoExternalUse) CNode(io.jenkins.plugins.casc.model.CNode) Mapping(io.jenkins.plugins.casc.model.Mapping) BaseConfigurator(io.jenkins.plugins.casc.BaseConfigurator) DescriptorConfigurator(io.jenkins.plugins.casc.impl.configurators.DescriptorConfigurator) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Attribute(io.jenkins.plugins.casc.Attribute) Extension(hudson.Extension) CheckForNull(edu.umd.cs.findbugs.annotations.CheckForNull) GlobalConfiguration(jenkins.model.GlobalConfiguration) Attribute(io.jenkins.plugins.casc.Attribute) DescriptorConfigurator(io.jenkins.plugins.casc.impl.configurators.DescriptorConfigurator) Descriptor(hudson.model.Descriptor) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

Attribute (io.jenkins.plugins.casc.Attribute)4 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 DescribableAttribute (io.jenkins.plugins.casc.impl.attributes.DescribableAttribute)2 Mapping (io.jenkins.plugins.casc.model.Mapping)2 Constructor (java.lang.reflect.Constructor)2 Parameter (java.lang.reflect.Parameter)2 Set (java.util.Set)2 DataBoundConstructor (org.kohsuke.stapler.DataBoundConstructor)2 CheckForNull (edu.umd.cs.findbugs.annotations.CheckForNull)1 Extension (hudson.Extension)1 Descriptor (hudson.model.Descriptor)1 Maven (hudson.tasks.Maven)1 AttributeTest (io.jenkins.plugins.casc.AttributeTest)1 BaseConfigurator (io.jenkins.plugins.casc.BaseConfigurator)1 ConfigurationContext (io.jenkins.plugins.casc.ConfigurationContext)1 Configurator (io.jenkins.plugins.casc.Configurator)1 DataBoundConfigurator (io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator)1 DescriptorConfigurator (io.jenkins.plugins.casc.impl.configurators.DescriptorConfigurator)1 CNode (io.jenkins.plugins.casc.model.CNode)1 Collection (java.util.Collection)1