Search in sources :

Example 1 with HostnameRequirement

use of com.cloudbees.plugins.credentials.domains.HostnameRequirement 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 2 with HostnameRequirement

use of com.cloudbees.plugins.credentials.domains.HostnameRequirement 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)2 HostnameRequirement (com.cloudbees.plugins.credentials.domains.HostnameRequirement)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 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 Issue (org.jvnet.hudson.test.Issue)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1