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"));
}
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;
}
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));
}
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);
}
Aggregations