Java Code Examples for hudson.model.User#getById()

The following examples show how to use hudson.model.User#getById() . 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: HudsonPrivateSecurityRealmConfigurator.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
private static User createAccount(HudsonPrivateSecurityRealm target, UserWithPassword user)
    throws IOException {
    User updatedUser;
    if (StringUtils.isNotBlank(user.password)) {
        if (StringUtils.startsWith(user.password, HASHED_PASSWORD_PREFIX)) {
            try {
                updatedUser = target
                    .createAccountWithHashedPassword(user.id, user.password);
            } catch (IllegalArgumentException | IOException e) {
                logger.log(Level.WARNING,
                    "Failed to create user with presumed hashed password", e);
                // fallback, just create the account as is
                updatedUser = target.createAccount(user.id, user.password);
            }
        } else {
            updatedUser = target.createAccount(user.id, user.password);
        }
    } else {
        updatedUser = User.getById(user.id, true);
    }
    return updatedUser;
}
 
Example 2
Source File: HudsonPrivateSecurityRealmConfiguratorTest.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
@Test
    @ConfiguredWithReadme("embedded-userdatabase/README.md#1")
    public void configure_all_attributes() {
        final User admin = User.getById("admin", false);
        assertNotNull(admin);

        assertThat(admin.getFullName(), is("Admin"));
        assertThat(admin.getDescription(), is("Superwoman"));

        SlackUserProperty slackUserProperty = admin
            .getProperty(SlackUserProperty.class);
        assertThat(slackUserProperty.getUserId(), is("ABCDEFGH"));

        UserProperty mailerProperty = admin.getProperty(UserProperty.class);
        assertThat(mailerProperty.getEmailAddress(), is("[email protected]"));

// Pending https://github.com/jenkinsci/ssh-cli-auth-module/pull/16
//        UserPropertyImpl authorizedKeysProperty = admin.getProperty(UserPropertyImpl.class);
//        assertThat(authorizedKeysProperty.authorizedKeys, is("ssh-rsa some-key\n"));
    }
 
Example 3
Source File: ViewsTest.java    From jenkins-client-java with MIT License 5 votes vote down vote up
@Before
public void setup() throws URISyntaxException
{
    User user = User.getById("admin", true);

    assertNotNull(user);

    String token = ((ApiTokenProperty) user.getProperty(ApiTokenProperty.class)).getApiToken();

    assertNotNull(j.jenkins.getRootUrl());

    j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
    views = new Jenkins(new URI(j.jenkins.getRootUrl()), user.getId(), token).getViews();
}
 
Example 4
Source File: QueuesTest.java    From jenkins-client-java with MIT License 5 votes vote down vote up
@Before
public void init() throws URISyntaxException
{
    User user = User.getById("admin", true);

    assertNotNull(user);

    String token = ((ApiTokenProperty) user.getProperty(ApiTokenProperty.class)).getApiToken();

    assertNotNull(j.jenkins.getRootUrl());

    j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
    queues = new Jenkins(new URI(j.jenkins.getRootUrl()), user.getId(), token).getQueues();
}
 
Example 5
Source File: FoldersTest.java    From jenkins-client-java with MIT License 5 votes vote down vote up
@Before
public void init() throws URISyntaxException
{
    User user = User.getById("admin", true);

    assertNotNull(user);

    String token = ((ApiTokenProperty) user.getProperty(ApiTokenProperty.class)).getApiToken();

    assertNotNull(j.jenkins.getRootUrl());

    j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
    folders = new Jenkins(new URI(j.jenkins.getRootUrl()), user.getId(), token).getFolders();
}
 
Example 6
Source File: ComputersTest.java    From jenkins-client-java with MIT License 5 votes vote down vote up
@Before
public void init() throws URISyntaxException
{
    User user = User.getById("admin", true);

    assertNotNull(user);

    String token = ((ApiTokenProperty) user.getProperty(ApiTokenProperty.class)).getApiToken();

    assertNotNull(j.jenkins.getRootUrl());

    j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
    computers = new Jenkins(new URI(j.jenkins.getRootUrl()), user.getId(), token).getComputers();
}
 
Example 7
Source File: LabelsTest.java    From jenkins-client-java with MIT License 5 votes vote down vote up
@Before
public void init() throws URISyntaxException
{
    User user = User.getById("admin", true);

    assertNotNull(user);

    String token = ((ApiTokenProperty) user.getProperty(ApiTokenProperty.class)).getApiToken();

    assertNotNull(j.jenkins.getRootUrl());

    j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
    labels = new Jenkins(new URI(j.jenkins.getRootUrl()), user.getId(), token).getLabels();
}
 
Example 8
Source File: WorkflowsTest.java    From jenkins-client-java with MIT License 5 votes vote down vote up
@Before
public void init() throws URISyntaxException
{
    User user = User.getById("admin", true);

    assertNotNull(user);

    String token = ((ApiTokenProperty) user.getProperty(ApiTokenProperty.class)).getApiToken();

    assertNotNull(j.jenkins.getRootUrl());

    j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
    workflows = new Jenkins(new URI(j.jenkins.getRootUrl()), user.getId(), token).getWorkflows();
}
 
Example 9
Source File: BlueOceanTest.java    From jenkins-client-java with MIT License 5 votes vote down vote up
@Before
public void init() throws URISyntaxException
{
    User user = User.getById("admin", true);

    assertNotNull(user);

    String token = ((ApiTokenProperty) user.getProperty(ApiTokenProperty.class)).getApiToken();

    assertNotNull(j.jenkins.getRootUrl());

    j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
    blue = new Jenkins(new URI(j.jenkins.getRootUrl()), user.getId(), token).getBlueOcean();
}
 
Example 10
Source File: MissingConfiguratorTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@ConfiguredWithCode(value = "MissingConfiguratorTest.yml", expected = IllegalArgumentException.class,
        message = "No hudson.security.AuthorizationStrategy implementation found for globalMatrix")
@Test
public void testThrowsSuggestion() throws Exception {
    //No config check needed, should fail with IllegalArgumentException
    //We're purposely trying to configure a plugin for which there is no configurator
    //admin user should not be created due to IllegalArgumentException
    User user = User.getById("admin", false);
    assertThat(user, is(nullValue()));
}
 
Example 11
Source File: HudsonPrivateSecurityRealmConfiguratorTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
@ConfiguredWithReadme("embedded-userdatabase/README.md#1")
public void config_local_security_and_hashed_admin_user() {
    final User admin = User.getById("hashedadmin", false);
    assertNotNull(admin);
    final HudsonPrivateSecurityRealm.Details details = admin.getProperty(HudsonPrivateSecurityRealm.Details.class);
    assertTrue(details.isPasswordCorrect("password"));
}
 
Example 12
Source File: HudsonPrivateSecurityRealmConfiguratorTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
@ConfiguredWithReadme("embedded-userdatabase/README.md#0")
public void configure_local_security_and_admin_user() throws Exception {
    final Jenkins jenkins = Jenkins.get();
    final HudsonPrivateSecurityRealm securityRealm = (HudsonPrivateSecurityRealm) jenkins.getSecurityRealm();
    assertFalse(securityRealm.allowsSignup());
    final User admin = User.getById("admin", false);
    assertNotNull(admin);
    final HudsonPrivateSecurityRealm.Details details = admin.getProperty(HudsonPrivateSecurityRealm.Details.class);
    assertTrue(details.isPasswordCorrect("somethingsecret"));

    final FullControlOnceLoggedInAuthorizationStrategy authorizationStrategy = (FullControlOnceLoggedInAuthorizationStrategy) jenkins.getAuthorizationStrategy();
    assertTrue(authorizationStrategy.isAllowAnonymousRead());
}
 
Example 13
Source File: FolderBasedAuthorizationStrategyTest.java    From folder-auth-plugin with MIT License 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    Jenkins jenkins = jenkinsRule.jenkins;
    jenkins.setSecurityRealm(jenkinsRule.createDummySecurityRealm());

    FolderBasedAuthorizationStrategy strategy = new FolderBasedAuthorizationStrategy(Collections.emptySet(),
            Collections.emptySet(), Collections.emptySet());
    jenkins.setAuthorizationStrategy(strategy);

    final String adminRoleName = "adminRole";
    final String overallReadRoleName = "overallRead";

    FolderAuthorizationStrategyAPI.addGlobalRole(new GlobalRole(adminRoleName,
            wrapPermissions(FolderAuthorizationStrategyManagementLink.getSafePermissions(
                    new HashSet<>(PermissionGroup.getAll())))));

    FolderAuthorizationStrategyAPI.assignSidToGlobalRole("admin", adminRoleName);

    FolderAuthorizationStrategyAPI.addGlobalRole(new GlobalRole(overallReadRoleName, wrapPermissions(Permission.READ)));
    FolderAuthorizationStrategyAPI.assignSidToGlobalRole("authenticated", overallReadRoleName);

    FolderAuthorizationStrategyAPI.addFolderRole(new FolderRole("folderRole1", wrapPermissions(Item.READ),
            ImmutableSet.of("root")));
    FolderAuthorizationStrategyAPI.assignSidToFolderRole("user1", "folderRole1");
    FolderAuthorizationStrategyAPI.assignSidToFolderRole("user2", "folderRole1");

    FolderAuthorizationStrategyAPI.addFolderRole(new FolderRole("folderRole2", wrapPermissions(Item.CONFIGURE, Item.DELETE),
            ImmutableSet.of("root/child1")));
    FolderAuthorizationStrategyAPI.assignSidToFolderRole("user2", "folderRole2");

    /*
     * Folder hierarchy for the test
     *
     *             root
     *             /  \
     *        child1   child2
     *          /        \
     *        child3     job1
     *         /
     *        job2
     */

    root = jenkins.createProject(Folder.class, "root");
    child1 = root.createProject(Folder.class, "child1");
    child2 = root.createProject(Folder.class, "child2");
    child3 = child1.createProject(Folder.class, "child3");

    job1 = child2.createProject(FreeStyleProject.class, "job1");
    job2 = child3.createProject(FreeStyleProject.class, "job2");

    admin = User.getById("admin", true);
    user1 = User.getById("user1", true);
    user2 = User.getById("user2", true);
}
 
Example 14
Source File: RoleStrategyTest.java    From configuration-as-code-plugin with MIT License 4 votes vote down vote up
@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(RoleBasedAuthorizationStrategy.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);
}
 
Example 15
Source File: JenkinsRule.java    From jenkins-test-harness with MIT License 4 votes vote down vote up
/**
 * Retrieve the {@link ApiTokenProperty} from the associated user, derive credentials from it and place it in Basic authorization header
 * @see #withBasicApiToken(User)
 * @since 2.32
 */
public @Nonnull WebClient withBasicApiToken(@Nonnull String userId){
    User user = User.getById(userId, false);
    assertNotNull("The userId must correspond to an already created User", user);
    return withBasicApiToken(user);
}
 
Example 16
Source File: JenkinsRuleTest.java    From jenkins-test-harness with MIT License 4 votes vote down vote up
@Test
public void testTokenHelperMethods() throws Exception {
    j.jenkins.setSecurityRealm(j.createDummySecurityRealm());

    JenkinsRule.WebClient wc = j.createWebClient();

    User alice = User.getById("alice", true);
    User.getById("bob", true);
    User.getById("charlotte", true);

    makeRequestAndAssertLogin(wc, "anonymous");

    wc.login("alice");
    makeRequestAndAssertLogin(wc, "alice");

    wc = j.createWebClient();
    makeRequestAndAssertLogin(wc, "anonymous");

    wc.withBasicCredentials("alice", "alice");
    makeRequestAndAssertLogin(wc, "alice");

    wc = j.createWebClient();

    // if password is not provided, the login is used for both
    wc.withBasicCredentials("alice");
    makeRequestAndAssertLogin(wc, "alice");

    wc = j.createWebClient();
    makeRequestAndAssertLogin(wc, "anonymous");

    wc.withBasicCredentials("alice", alice.getProperty(ApiTokenProperty.class).getApiToken());
    makeRequestAndAssertLogin(wc, "alice");

    wc = j.createWebClient();
    makeRequestAndAssertLogin(wc, "anonymous");

    wc.withBasicApiToken("bob");
    makeRequestAndAssertLogin(wc, "bob");

    wc = j.createWebClient();
    wc.withBasicApiToken("charlotte");
    makeRequestAndAssertLogin(wc, "charlotte");
}
 
Example 17
Source File: PluginsTest.java    From jenkins-client-java with MIT License 3 votes vote down vote up
@Before
public void setup() throws Exception {
    User user = User.getById("admin", true);

    assertNotNull(user);

    String token = ((ApiTokenProperty) user.getProperty(ApiTokenProperty.class)).getApiToken();

    assertNotNull(j.jenkins.getRootUrl());

    j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
    plugins = new Jenkins(new URI(j.jenkins.getRootUrl()), user.getId(), token).getPlugins();
}
 
Example 18
Source File: CredentialsTest.java    From jenkins-client-java with MIT License 3 votes vote down vote up
@Before
public void init() throws URISyntaxException {
    User user = User.getById("admin", true);

    assertNotNull(user);

    String token = ((ApiTokenProperty) user.getProperty(ApiTokenProperty.class)).getApiToken();

    assertNotNull(j.jenkins.getRootUrl());

    j.jenkins.setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
    credentials = new Jenkins(new URI(j.jenkins.getRootUrl()), user.getId(), token).getCredentials();
}