/** * Resolves the given serverCredential mappings and returns a map paring serverId to credential * * @param item * authentication scope * @param serverCredentialMappings * the mappings to be resolved * @return map of serverId - credential */ public static Map<String, StandardUsernameCredentials> resolveCredentials(Run<?,?> build, final List<ServerCredentialMapping> serverCredentialMappings) { Map<String, StandardUsernameCredentials> serverId2credential = new HashMap<String, StandardUsernameCredentials>(); for (ServerCredentialMapping serverCredentialMapping : serverCredentialMappings) { final String credentialsId = serverCredentialMapping.getCredentialsId(); final String serverId = serverCredentialMapping.getServerId(); List<DomainRequirement> domainRequirements = Collections.emptyList(); if (StringUtils.isNotBlank(serverId)) { domainRequirements = Collections.<DomainRequirement> singletonList(new MavenServerIdRequirement(serverId)); } final StandardUsernameCredentials c = CredentialsProvider.findCredentialById(credentialsId, StandardUsernameCredentials.class, build, domainRequirements); if (c != null) { serverId2credential.put(serverId, c); } } return serverId2credential; }
@Nonnull public static CredentialsBindingHandler forId(String id, Run context) throws CredentialNotFoundException { IdCredentials cred = CredentialsProvider.findCredentialById(id, IdCredentials.class, context); if (cred==null) { throw new CredentialNotFoundException(id); } if (cred instanceof StandardCredentials) { CredentialsBindingHandler handler = forCredential((StandardCredentials)cred); if (handler == null) { throw new CredentialNotFoundException(String.format("No suitable binding handler could be found for type %s. " + "Supported types are %s.", cred.getClass().getName(), StringUtils.join(supportedTypeNames(), ','))); } return handler; } else { throw new CredentialNotFoundException(String.format("Credentials %s is of type %s where " + "StandardCredentials is the expected type.", id, cred.getClass().getName())); } }
final StandardUsernameCredentials credentials = CredentialsProvider.findCredentialById(serverCredentialMapping.getCredentialsId(), StandardUsernameCredentials.class, build, domainRequirements);
/** * 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; }
/** * A common requirement for plugins is to resolve a specific credential by id in the context of a specific run. * Given that the credential itself could be resulting from a build parameter expression and the complexities of * determining the scope of items from which the credential should be resolved in a chain of builds, this method * provides the correct answer. * * @param id either the id of the credential to find or a parameter expression for the id. * @param type the type of credential to find. * @param run the {@link Run} defining the context within which to find the credential. * @param domainRequirements the domain requirements of the credential. * @param <C> the credentials type. * @return the credential or {@code null} if either the credential cannot be found or the user triggering the run * is not permitted to use the credential in the context of the run. * @since 1.16 */ @CheckForNull public static <C extends IdCredentials> C findCredentialById(@NonNull String id, @NonNull Class<C> type, @NonNull Run<?, ?> run, DomainRequirement... domainRequirements) { return findCredentialById(id, type, run, Arrays.asList(domainRequirements)); }
/** * A common requirement for plugins is to resolve a specific credential by id in the context of a specific run. * Given that the credential itself could be resulting from a build parameter expression and the complexities of * determining the scope of items from which the credential should be resolved in a chain of builds, this method * provides the correct answer. * * @param id either the id of the credential to find or a parameter expression for the id. * @param type the type of credential to find. * @param run the {@link Run} defining the context within which to find the credential. * @param domainRequirements the domain requirements of the credential. * @param <C> the credentials type. * @return the credential or {@code null} if either the credential cannot be found or the user triggering the run * is not permitted to use the credential in the context of the run. * @since 1.16 */ @CheckForNull public static <C extends IdCredentials> C findCredentialById(@NonNull String id, @NonNull Class<C> type, @NonNull Run<?, ?> run, DomainRequirement... domainRequirements) { return findCredentialById(id, type, run, Arrays.asList(domainRequirements)); }
final SSHUserPrivateKey c = CredentialsProvider.findCredentialById(id, SSHUserPrivateKey.class, build); if (c == null && !step.isIgnoreMissing()) { listener.fatalError(Messages.SSHAgentBuildWrapper_CredentialsNotFound());
private Environment createSSHAgentEnvironment(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException { List<SSHUserPrivateKey> userPrivateKeys = new ArrayList<SSHUserPrivateKey>(); for (String id: new LinkedHashSet<String>(getCredentialIds())) { final SSHUserPrivateKey c = CredentialsProvider.findCredentialById( id, SSHUserPrivateKey.class, build ); if (c == null && !ignoreMissing) { listener.fatalError(Messages.SSHAgentBuildWrapper_CredentialsNotFound()); } if (c != null && !userPrivateKeys.contains(c)) { userPrivateKeys.add(c); } } for (SSHUserPrivateKey userPrivateKey : userPrivateKeys) { listener.getLogger().println(Messages.SSHAgentBuildWrapper_UsingCredentials(description(userPrivateKey))); } try { return new SSHAgentEnvironment(launcher, listener, userPrivateKeys); } catch (IOException e) { throw new IOException2(Messages.SSHAgentBuildWrapper_CouldNotStartAgent(), e); } catch (InterruptedException e) { e.printStackTrace(listener.fatalError(Messages.SSHAgentBuildWrapper_CouldNotStartAgent())); throw e; } catch (Throwable e) { throw new IOException2(Messages.SSHAgentBuildWrapper_CouldNotStartAgent(), e); } }
.findCredentialById(actualCredentialsId, OpenShiftTokenCredentials.class, build, new ArrayList<DomainRequirement>());
private void withCredentials(@Nonnull Run<?, ?> run, @Nonnull EnvVars localEnv) throws IOException, InterruptedException { if (!StringUtils.isNullOrEmpty(this.step.getCredentials())) { StandardUsernamePasswordCredentials usernamePasswordCredentials = CredentialsProvider.findCredentialById(this.step.getCredentials(), StandardUsernamePasswordCredentials.class, run, Collections.emptyList()); AmazonWebServicesCredentials amazonWebServicesCredentials = CredentialsProvider.findCredentialById(this.step.getCredentials(), AmazonWebServicesCredentials.class, run, Collections.emptyList()); if (usernamePasswordCredentials != null) {
@Override public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { IdCredentials credentials = CredentialsProvider.findCredentialById(id, IdCredentials.class, build); if (credentials == null) { listener.getLogger().printf("Could not find any credentials with id %s%n", id); build.setResult(Result.FAILURE); return false; } else { listener.getLogger() .printf("Found %s credentials with id %s%n", CredentialsNameProvider.name(credentials), id); if (matcher != null) { if (matcher.matches(credentials)) { listener.getLogger().println("Credentials match criteria"); } else { StringDescription description = new StringDescription(); matcher.describeMismatch(credentials, description); listener.getLogger().println(description.toString()); return false; } } return true; } }