Search in sources :

Example 1 with StandardUsernameCredentials

use of com.cloudbees.plugins.credentials.common.StandardUsernameCredentials in project nodejs-plugin by jenkinsci.

the class NpmrcFileSupplyTest method test_supply_npmrc_with_registry.

@Test
public void test_supply_npmrc_with_registry() throws Exception {
    StandardUsernameCredentials user = createUser("test-user-id", "myuser", "mypassword");
    NPMRegistry privateRegistry = new NPMRegistry("https://private.organization.com/", user.getId(), null);
    NPMRegistry officalRegistry = new NPMRegistry("https://registry.npmjs.org/", null, "@user1 user2");
    Config config = createSetting("mytest", "email=guest@example.com", Arrays.asList(privateRegistry, officalRegistry));
    FreeStyleBuild build = new MockBuild(j.createFreeStyleProject(), folder.newFolder());
    FilePath npmrcFile = ConfigFileManager.provisionConfigFile(new ConfigFile(config.id, null, true), null, build, build.getWorkspace(), j.createTaskListener(), new ArrayList<String>(1));
    assertTrue(npmrcFile.exists());
    assertTrue(npmrcFile.length() > 0);
    Npmrc npmrc = Npmrc.load(new File(npmrcFile.getRemote()));
    assertTrue("Missing setting email", npmrc.contains("email"));
    assertEquals("Unexpected value from settings email", "guest@example.com", npmrc.get("email"));
}
Also used : FilePath(hudson.FilePath) Npmrc(jenkins.plugins.nodejs.configfiles.Npmrc) NPMRegistry(jenkins.plugins.nodejs.configfiles.NPMRegistry) ConfigFile(org.jenkinsci.lib.configprovider.model.ConfigFile) Config(org.jenkinsci.lib.configprovider.model.Config) NPMConfig(jenkins.plugins.nodejs.configfiles.NPMConfig) StandardUsernameCredentials(com.cloudbees.plugins.credentials.common.StandardUsernameCredentials) FreeStyleBuild(hudson.model.FreeStyleBuild) ConfigFile(org.jenkinsci.lib.configprovider.model.ConfigFile) File(java.io.File) Test(org.junit.Test)

Example 2 with StandardUsernameCredentials

use of com.cloudbees.plugins.credentials.common.StandardUsernameCredentials in project nodejs-plugin by jenkinsci.

the class RegistryHelper method resolveCredentials.

/**
 * Resolves all registry credentials and returns a map paring registry URL
 * to credential.
 *
 * @param build a build being run
 * @return map of registry URL - credential
 */
public Map<String, StandardUsernameCredentials> resolveCredentials(Run<?, ?> build) {
    Map<String, StandardUsernameCredentials> registry2credential = new HashMap<>();
    for (NPMRegistry registry : registries) {
        String credentialsId = registry.getCredentialsId();
        if (credentialsId != null) {
            // create a domain filter based on registry URL
            final URL registryURL = toURL(registry.getUrl());
            List<DomainRequirement> domainRequirements = Collections.emptyList();
            if (registryURL != null) {
                domainRequirements = Collections.<DomainRequirement>singletonList(new HostnameRequirement(registryURL.getHost()));
            }
            StandardUsernameCredentials c = CredentialsProvider.findCredentialById(credentialsId, StandardUsernameCredentials.class, build, domainRequirements);
            if (c != null) {
                registry2credential.put(registry.getUrl(), c);
            }
        }
    }
    return registry2credential;
}
Also used : HashMap(java.util.HashMap) DomainRequirement(com.cloudbees.plugins.credentials.domains.DomainRequirement) HostnameRequirement(com.cloudbees.plugins.credentials.domains.HostnameRequirement) StandardUsernameCredentials(com.cloudbees.plugins.credentials.common.StandardUsernameCredentials) URL(java.net.URL)

Example 3 with StandardUsernameCredentials

use of com.cloudbees.plugins.credentials.common.StandardUsernameCredentials in project nodejs-plugin by jenkinsci.

the class RegistryHelperTest method test_registry_credentials_resolution.

@Test
public void test_registry_credentials_resolution() throws Exception {
    NPMRegistry privateRegistry = new NPMRegistry("https://private.organization.com/", user.getId(), null);
    NPMRegistry officalRegistry = new NPMRegistry("https://registry.npmjs.org/", null, "@user1 user2");
    FreeStyleBuild build = j.createFreeStyleProject().createExecutable();
    RegistryHelper helper = new RegistryHelper(Arrays.asList(privateRegistry, officalRegistry));
    Map<String, StandardUsernameCredentials> resolvedCredentials = helper.resolveCredentials(build);
    assertFalse(resolvedCredentials.isEmpty());
    assertEquals(1, resolvedCredentials.size());
    assertThat(resolvedCredentials.keySet(), hasItem(privateRegistry.getUrl()));
    assertThat(resolvedCredentials.get(privateRegistry.getUrl()), equalTo(user));
}
Also used : StandardUsernameCredentials(com.cloudbees.plugins.credentials.common.StandardUsernameCredentials) FreeStyleBuild(hudson.model.FreeStyleBuild) Test(org.junit.Test)

Example 4 with StandardUsernameCredentials

use of com.cloudbees.plugins.credentials.common.StandardUsernameCredentials in project blueocean-plugin by jenkinsci.

the class BlueOceanCredentialsProviderTest method getCredentialsWhenUserExistedButNotAccessible.

@Test
@Issue("JENKINS-53188")
public void getCredentialsWhenUserExistedButNotAccessible() {
    PowerMockito.mockStatic(Jenkins.class);
    PowerMockito.when(Jenkins.get()).thenReturn(jenkins);
    PowerMockito.when(Jenkins.get()).thenReturn(jenkins);
    PowerMockito.when(Jenkins.get()).thenReturn(jenkins);
    when(jenkins.getSecurityRealm()).thenReturn(SecurityRealm.NO_AUTHENTICATION);
    when(jenkins.getSecretKey()).thenReturn("xxx");
    PowerMockito.mockStatic(User.class);
    // Make sure we return a user, cause it did once exist
    PowerMockito.when(User.get(anyString(), anyBoolean(), any())).thenReturn(user);
    Domain domain = BlueOceanCredentialsProvider.createDomain("api.github.com");
    BlueOceanCredentialsProvider blueOceanCredentialsProvider = new BlueOceanCredentialsProvider();
    BlueOceanCredentialsProvider.FolderPropertyImpl prop = new BlueOceanCredentialsProvider.FolderPropertyImpl("halkeye", "halkeye", domain);
    when(folder.getProperties()).thenReturn(describableList);
    when(describableList.get(BlueOceanCredentialsProvider.FolderPropertyImpl.class)).thenReturn(prop);
    // Should be empty when trying to impersonate and grab credentials though
    List<StandardUsernameCredentials> credentials = blueOceanCredentialsProvider.getCredentials(StandardUsernameCredentials.class, (ItemGroup) folder, ACL.SYSTEM, new ArrayList<>(Arrays.asList(new SchemeRequirement("https"), new HostnameRequirement("api.github.com"), new PathRequirement("/"))));
    assertEquals(Collections.emptyList(), credentials);
    List<Credentials> storeCredentials = prop.getStore().getCredentials(domain);
    assertEquals(Collections.emptyList(), storeCredentials);
}
Also used : HostnameRequirement(com.cloudbees.plugins.credentials.domains.HostnameRequirement) PathRequirement(com.cloudbees.plugins.credentials.domains.PathRequirement) StandardUsernameCredentials(com.cloudbees.plugins.credentials.common.StandardUsernameCredentials) Domain(com.cloudbees.plugins.credentials.domains.Domain) SchemeRequirement(com.cloudbees.plugins.credentials.domains.SchemeRequirement) Credentials(com.cloudbees.plugins.credentials.Credentials) StandardUsernameCredentials(com.cloudbees.plugins.credentials.common.StandardUsernameCredentials) Issue(org.jvnet.hudson.test.Issue) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

StandardUsernameCredentials (com.cloudbees.plugins.credentials.common.StandardUsernameCredentials)4 Test (org.junit.Test)3 HostnameRequirement (com.cloudbees.plugins.credentials.domains.HostnameRequirement)2 FreeStyleBuild (hudson.model.FreeStyleBuild)2 Credentials (com.cloudbees.plugins.credentials.Credentials)1 Domain (com.cloudbees.plugins.credentials.domains.Domain)1 DomainRequirement (com.cloudbees.plugins.credentials.domains.DomainRequirement)1 PathRequirement (com.cloudbees.plugins.credentials.domains.PathRequirement)1 SchemeRequirement (com.cloudbees.plugins.credentials.domains.SchemeRequirement)1 FilePath (hudson.FilePath)1 File (java.io.File)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 NPMConfig (jenkins.plugins.nodejs.configfiles.NPMConfig)1 NPMRegistry (jenkins.plugins.nodejs.configfiles.NPMRegistry)1 Npmrc (jenkins.plugins.nodejs.configfiles.Npmrc)1 Config (org.jenkinsci.lib.configprovider.model.Config)1 ConfigFile (org.jenkinsci.lib.configprovider.model.ConfigFile)1 Issue (org.jvnet.hudson.test.Issue)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1