Search in sources :

Example 6 with ConfiguredWithCode

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

the class LDAPSecurityRealmTest method export_ldap_no_secret.

@Test
@ConfiguredWithCode("LDAPSecurityRealmTestNoSecret.yml")
public void export_ldap_no_secret() throws Exception {
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    CNode yourAttribute = getJenkinsRoot(context).get("securityRealm").asMapping().get("ldap");
    String exported = toYamlString(yourAttribute);
    String expected = toStringFromYamlFile(this, "LDAPSecurityRealmTestNoSecretExpected.yml");
    assertThat(exported, is(expected));
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) Util.toYamlString(io.jenkins.plugins.casc.misc.Util.toYamlString) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 7 with ConfiguredWithCode

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

the class MailExtTest method shouldNotExportOrLogCredentials.

@Test
@ConfiguredWithCode("MailExtTest.yml")
@Issue("SECURITY-1404")
public void shouldNotExportOrLogCredentials() throws Exception {
    assertEquals(SMTP_PASSWORD, ExtendedEmailPublisher.descriptor().getSmtpPassword().getPlainText());
    assertLogContains(logging, "smtpPassword =");
    assertNotInLog(logging, SMTP_PASSWORD);
    // Verify that the password does not get exported
    String exportedConfig = j.exportToString(false);
    assertThat("No entry was exported for SMTP credentials", exportedConfig, containsString("smtpPassword"));
    assertThat("There should be no SMTP password in the exported YAML", exportedConfig, not(containsString(SMTP_PASSWORD)));
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 8 with ConfiguredWithCode

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

the class PropertiesSecretSourceTest method testReadingSecretsFromProperties.

@Test
@ConfiguredWithCode("PropertiesSecretSourceTest.yaml")
public void testReadingSecretsFromProperties() {
    List<UsernamePasswordCredentials> credentialList = CredentialsProvider.lookupCredentials(UsernamePasswordCredentials.class, Jenkins.getInstanceOrNull(), null, Collections.emptyList());
    assertEquals(1, credentialList.size());
    UsernamePasswordCredentials credentials = credentialList.get(0);
    // https://leahneukirchen.org/blog/archive/2019/10/ken-thompson-s-unix-password.html
    assertEquals(USERNAME_SECRET, credentials.getUsername());
    assertEquals("p/q2-q4!", credentials.getPassword().getPlainText());
}
Also used : UsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 9 with ConfiguredWithCode

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

the class RoleStrategyTest method shouldReadRolesCorrectly.

@Test
@Issue("Issue #48")
@ConfiguredWithCode("RoleStrategy1.yml")
public void shouldReadRolesCorrectly() throws Exception {
    j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
    User admin = User.getById("admin", false);
    User user1 = User.getById("user1", false);
    User user2 = User.getById("user2", true);
    Computer agent1 = j.jenkins.getComputer("agent1");
    Computer agent2 = j.jenkins.getComputer("agent2");
    Folder folderA = j.jenkins.createProject(Folder.class, "A");
    FreeStyleProject jobA1 = folderA.createProject(FreeStyleProject.class, "1");
    Folder folderB = j.jenkins.createProject(Folder.class, "B");
    folderB.createProject(FreeStyleProject.class, "2");
    AuthorizationStrategy s = j.jenkins.getAuthorizationStrategy();
    assertThat("Authorization Strategy has been read incorrectly", s, instanceOf(RoleBasedAuthorizationStrategy.class));
    RoleBasedAuthorizationStrategy rbas = (RoleBasedAuthorizationStrategy) s;
    Map<Role, Set<String>> globalRoles = rbas.getGrantedRoles(RoleType.Global);
    assertThat(globalRoles.size(), equalTo(2));
    // Admin has configuration access
    assertHasPermission(admin, j.jenkins, Jenkins.ADMINISTER, Jenkins.READ);
    assertHasPermission(user1, j.jenkins, Jenkins.READ);
    assertHasNoPermission(user1, j.jenkins, Jenkins.ADMINISTER);
    // Folder A is restricted to admin
    assertHasPermission(admin, folderA, Item.CONFIGURE);
    assertHasPermission(user1, folderA, Item.READ, Item.DISCOVER);
    assertHasNoPermission(user1, folderA, Item.CONFIGURE, Item.DELETE, Item.BUILD);
    // But they have access to jobs in Folder A
    assertHasPermission(admin, folderA, Item.CONFIGURE, Item.CANCEL);
    assertHasPermission(user1, jobA1, Item.READ, Item.DISCOVER, Item.CONFIGURE, Item.BUILD, Item.DELETE);
    assertHasPermission(user2, jobA1, Item.READ, Item.DISCOVER, Item.CONFIGURE, Item.BUILD, Item.DELETE);
    assertHasNoPermission(user1, folderA, Item.CANCEL);
    // FolderB is editable by user2, but he cannot delete it
    assertHasPermission(user2, folderB, Item.READ, Item.DISCOVER, Item.CONFIGURE, Item.BUILD);
    assertHasNoPermission(user2, folderB, Item.DELETE);
    assertHasNoPermission(user1, folderB, Item.CONFIGURE, Item.BUILD, Item.DELETE);
    // Only user1 can run on agent1, but he still cannot configure it
    assertHasPermission(admin, agent1, Computer.CONFIGURE, Computer.DELETE, Computer.BUILD);
    assertHasPermission(user1, agent1, Computer.BUILD);
    assertHasNoPermission(user1, agent1, Computer.CONFIGURE, Computer.DISCONNECT);
    // Same user still cannot build on agent2
    assertHasNoPermission(user1, agent2, Computer.BUILD);
}
Also used : Role(com.michelin.cio.hudson.plugins.rolestrategy.Role) AuthorizationStrategy(hudson.security.AuthorizationStrategy) RoleBasedAuthorizationStrategy(com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy) User(hudson.model.User) Set(java.util.Set) Computer(hudson.model.Computer) Folder(com.cloudbees.hudson.plugins.folder.Folder) FreeStyleProject(hudson.model.FreeStyleProject) RoleBasedAuthorizationStrategy(com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 10 with ConfiguredWithCode

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

the class SSHCredentialsTest method shouldSupportMultilineCertificates.

@Test
@ConfiguredWithCode("SSHCredentialsTest_Multiline_Key.yml")
@Issue("https://github.com/jenkinsci/configuration-as-code-plugin/issues/1189")
public void shouldSupportMultilineCertificates() {
    BasicSSHUserPrivateKey certKey = getCredentials(BasicSSHUserPrivateKey.class);
    assertThat("Private key roundtrip failed", certKey.getPrivateKey().trim(), equalTo(MySSHKeySecretSource.PRIVATE_SSH_KEY.trim()));
}
Also used : BasicSSHUserPrivateKey(com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Aggregations

ConfiguredWithCode (io.jenkins.plugins.casc.misc.ConfiguredWithCode)52 Test (org.junit.Test)51 Issue (org.jvnet.hudson.test.Issue)18 CNode (io.jenkins.plugins.casc.model.CNode)16 Jenkins (jenkins.model.Jenkins)15 ConfigurationContext (io.jenkins.plugins.casc.ConfigurationContext)8 ConfiguratorRegistry (io.jenkins.plugins.casc.ConfiguratorRegistry)8 Util.toYamlString (io.jenkins.plugins.casc.misc.Util.toYamlString)8 Mapping (io.jenkins.plugins.casc.model.Mapping)7 BasicSSHUserPrivateKey (com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey)5 Configurator (io.jenkins.plugins.casc.Configurator)5 ProxyConfiguration (hudson.ProxyConfiguration)4 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)3 UsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials)3 AdminWhitelistRule (jenkins.security.s2m.AdminWhitelistRule)3 CredentialsRootConfigurator (com.cloudbees.plugins.credentials.casc.CredentialsRootConfigurator)2 Role (com.michelin.cio.hudson.plugins.rolestrategy.Role)2 RoleBasedAuthorizationStrategy (com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy)2 FreeStyleProject (hudson.model.FreeStyleProject)2 User (hudson.model.User)2