com.cloudbees.plugins.credentials.CredentialsMatcher Java Examples

The following examples show how to use com.cloudbees.plugins.credentials.CredentialsMatcher. 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: ListGitBranchesParameterDefinition.java    From list-git-branches-parameter-plugin with MIT License 6 votes vote down vote up
private GitClient createGitClient(Job job) throws IOException, InterruptedException {
    EnvVars env = Objects.requireNonNull(Objects.requireNonNull(Jenkins.getInstance()).toComputer()).getEnvironment();
    Git git = Git.with(TaskListener.NULL, env);

    GitClient c = git.getClient();
    List<StandardUsernameCredentials> urlCredentials = CredentialsProvider.lookupCredentials(
            StandardUsernameCredentials.class, job, ACL.SYSTEM, URIRequirementBuilder.fromUri(remoteURL).build()
    );
    CredentialsMatcher ucMatcher = CredentialsMatchers.withId(credentialsId);
    CredentialsMatcher idMatcher = CredentialsMatchers.allOf(ucMatcher, GitClient.CREDENTIALS_MATCHER);
    StandardUsernameCredentials credentials = CredentialsMatchers.firstOrNull(urlCredentials, idMatcher);

    if (credentials != null) {
        c.addCredentials(remoteURL, credentials);
        if (job != null && job.getLastBuild() != null) {
            CredentialsProvider.track(job.getLastBuild(), credentials);
        }
    }
    return c;
}
 
Example #2
Source File: ConduitCredentialsDescriptor.java    From phabricator-jenkins-plugin with MIT License 6 votes vote down vote up
public static ConduitCredentials getCredentials(Job owner, String credentialsID) {
    List<ConduitCredentials> available = availableCredentials(owner);
    if (available.size() == 0) {
        return null;
    }
    CredentialsMatcher matcher;
    if (credentialsID != null) {
        matcher = CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialsID));
    } else {
        matcher = CredentialsMatchers.always();
    }
    return CredentialsMatchers.firstOrDefault(
            available,
            matcher,
            available.get(0)
    );
}
 
Example #3
Source File: SecretRetriever.java    From atlassian-jira-software-cloud-plugin with Apache License 2.0 5 votes vote down vote up
public Optional<String> getSecretFor(final String credentialsId) {

        final List<StringCredentials> credentials =
                CredentialsProvider.lookupCredentials(
                        StringCredentials.class,
                        Jenkins.get(),
                        ACL.SYSTEM,
                        Collections.emptyList());
        final CredentialsMatcher matcher = CredentialsMatchers.withId(credentialsId);

        return Optional.ofNullable(CredentialsMatchers.firstOrNull(credentials, matcher))
                .flatMap(creds -> Optional.ofNullable(creds.getSecret()))
                .flatMap(secret -> Optional.ofNullable(secret.getPlainText()));
    }
 
Example #4
Source File: Site.java    From jira-steps-plugin with Apache License 2.0 5 votes vote down vote up
public ListBoxModel doFillCredentialsIdItems(final @AncestorInPath Item item,
    @QueryParameter String credentialsId,
    final @QueryParameter String url) {

  StandardListBoxModel result = new StandardListBoxModel();

  credentialsId = StringUtils.trimToEmpty(credentialsId);
  if (item == null) {
    if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
      return result.includeCurrentValue(credentialsId);
    }
  } else {
    if (!item.hasPermission(Item.EXTENDED_READ) && !item.hasPermission(CredentialsProvider.USE_ITEM)) {
      return result.includeCurrentValue(credentialsId);
    }
  }

  Authentication authentication = getAuthentication(item);
  List<DomainRequirement> domainRequirements = URIRequirementBuilder.fromUri(url).build();
  CredentialsMatcher always = CredentialsMatchers.always();
  Class<? extends StandardUsernameCredentials> type = UsernamePasswordCredentialsImpl.class;

  result.includeEmptyValue();
  if (item != null) {
    result.includeMatchingAs(authentication, item, type, domainRequirements, always);
  } else {
    result.includeMatchingAs(authentication, Jenkins.get(), type, domainRequirements, always);
  }
  return result;
}
 
Example #5
Source File: BlueOceanCredentialsProvider.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Nonnull
@Override
public <C extends IdCredentials> ListBoxModel getCredentialIds(@Nonnull Class<C> type,
                                                               @Nullable ItemGroup itemGroup,
                                                               @Nullable Authentication authentication,
                                                               @Nonnull List<DomainRequirement> domainRequirements,
                                                               @Nonnull CredentialsMatcher matcher) {
    ListBoxModel result = new ListBoxModel();
    FolderPropertyImpl prop = propertyOf(itemGroup);
    if (prop != null && prop.domain.test(domainRequirements)) {
        result.add(Messages.BlueOceanCredentialsProvider_DisplayName(), prop.getId());
    }
    return result;
}
 
Example #6
Source File: GithubScmTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
void mockCredentials(String userId, String accessToken, String credentialId, String domainName) throws Exception {
    //Mock Credentials
    UsernamePasswordCredentialsImpl credentials = mock(UsernamePasswordCredentialsImpl.class);
    whenNew(UsernamePasswordCredentialsImpl.class).withAnyArguments().thenReturn(credentials);
    when(credentials.getId()).thenReturn(credentialId);
    when(credentials.getUsername()).thenReturn(userId);

    Secret secret = mock(Secret.class);
    when(secret.getPlainText()).thenReturn(accessToken);
    when(credentials.getPassword()).thenReturn(secret);
    CredentialsMatcher credentialsMatcher = mock(CredentialsMatcher.class);
    mockStatic(CredentialsMatchers.class);
    mockStatic(CredentialsProvider.class);
    when(CredentialsMatchers.withId(
        credentialId)).thenReturn(credentialsMatcher);

    BlueOceanDomainRequirement blueOceanDomainRequirement = mock(BlueOceanDomainRequirement.class);
    whenNew(BlueOceanDomainRequirement.class).withNoArguments().thenReturn(blueOceanDomainRequirement);

    when(CredentialsProvider.class, "lookupCredentials",
         StandardUsernamePasswordCredentials.class, jenkins, authentication, blueOceanDomainRequirement)
        .thenReturn(Lists.newArrayList(credentials));

    when(CredentialsMatchers.class, "firstOrNull", Lists.newArrayList(credentials), credentialsMatcher).thenReturn(credentials);

    when(CredentialsMatchers.allOf(credentialsMatcher)).thenReturn(credentialsMatcher);

    //Mock credentials Domain
    Domain domain = mock(Domain.class);
    when(domain.getName()).thenReturn(domainName);

    //Mock credentials Store
    CredentialsStore credentialsStore = mock(CredentialsStore.class);
    when(credentialsStore.hasPermission(CredentialsProvider.CREATE)).thenReturn(true);
    when(credentialsStore.hasPermission(CredentialsProvider.UPDATE)).thenReturn(true);
    when(credentialsStore.getDomainByName(domainName)).thenReturn(domain);

    when(CredentialsProvider.class, "lookupStores", user).thenReturn(Lists.newArrayList(credentialsStore));

    when(credentialsStore.updateCredentials(domain, credentials, credentials)).thenReturn(true);
}
 
Example #7
Source File: Connector.java    From github-branch-source-plugin with MIT License 4 votes vote down vote up
private static CredentialsMatcher githubScanCredentialsMatcher() {
    // TODO OAuth credentials
    return CredentialsMatchers.anyOf(CredentialsMatchers.instanceOf(StandardUsernamePasswordCredentials.class));
}