Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #21 from jenkinsci/config-id-JENKINS-26099
[FIXED JENKINS-26099] Pick an ID for credentials
  • Loading branch information
jglick committed Jan 15, 2015
2 parents fc7055d + 843a23c commit f2639a4
Show file tree
Hide file tree
Showing 20 changed files with 761 additions and 95 deletions.
Expand Up @@ -190,7 +190,7 @@ public Set<CredentialsScope> getScopes(ModelObject object) {
* {@link ModelObject} or {@code null} if either the object is not a credentials container or this
* {@link CredentialsProvider} does not maintain a store specifically bound to this {@link ModelObject}.
*
* @param object the {@link Item} or {@link ItemGroup} that the store is being requested of.
* @param object the {@link Item} or {@link ItemGroup} or {@link User} that the store is being requested of.
* @return either {@code null} or a scoped {@link CredentialsStore} where
* {@link com.cloudbees.plugins.credentials.CredentialsStore#getContext()} {@code == object}.
* @since 1.8
Expand Down Expand Up @@ -588,7 +588,7 @@ public static Set<CredentialsScope> lookupScopes(ModelObject object) {
* supplied
* object.
*
* @param object the {@link Item} or {@link ItemGroup} to get the {@link CredentialsStore}s of.
* @param object the {@link Item} or {@link ItemGroup} or {@link User} to get the {@link CredentialsStore}s of.
* @return a lazy {@link Iterable} of all {@link CredentialsStore} instances.
* @since 1.8
*/
Expand Down
Expand Up @@ -24,12 +24,24 @@
package com.cloudbees.plugins.credentials.impl;

import com.cloudbees.plugins.credentials.BaseCredentials;
import com.cloudbees.plugins.credentials.CredentialsDescriptor;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.common.IdCredentials;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.domains.Domain;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Util;
import hudson.model.Item;
import hudson.model.ModelObject;
import hudson.model.User;
import hudson.util.FormValidation;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.QueryParameter;

import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
Expand Down Expand Up @@ -109,4 +121,72 @@ public final boolean equals(Object o) {
public final int hashCode() {
return IdCredentials.Helpers.hashCode(this);
}

/**
* Descriptor to use for subclasses of {@link BaseStandardCredentials}.
* <p>{@code <st:include page="id-and-description" class="${descriptor.clazz}"/>} in {@code credentials.jelly} to pick up standard controls for {@link #getId} and {@link #getDescription}.
*/
protected static abstract class BaseStandardCredentialsDescriptor extends CredentialsDescriptor {

protected BaseStandardCredentialsDescriptor() {
clazz.asSubclass(BaseStandardCredentials.class);
}

protected BaseStandardCredentialsDescriptor(Class<? extends BaseStandardCredentials> clazz) {
super(clazz);
}

public final FormValidation doCheckId(@QueryParameter String value, @AncestorInPath ModelObject context) {
if (value.isEmpty()) {
return FormValidation.ok();
}
if (!value.matches("[a-zA-Z0-9_.-]+")) { // anything else considered kosher?
return FormValidation.error("Unacceptable characters");
}
FormValidation problem = checkForDuplicates(value, context, context);
if (problem != null) {
return problem;
}
if (!(context instanceof User)) {
User me = User.current();
if (me != null) {
problem = checkForDuplicates(value, context, me);
if (problem != null) {
return problem;
}
}
}
if (!(context instanceof Jenkins)) {
// CredentialsProvider.lookupStores(User) does not return SystemCredentialsProvider.
Jenkins j = Jenkins.getInstance();
if (j != null) {
problem = checkForDuplicates(value, context, j);
if (problem != null) {
return problem;
}
}
}
return FormValidation.ok();
}
private static @CheckForNull FormValidation checkForDuplicates(String value, ModelObject context, ModelObject object) {
for (CredentialsStore store : CredentialsProvider.lookupStores(object)) {
if (!store.hasPermission(CredentialsProvider.VIEW)) {
continue;
}
ModelObject storeContext = store.getContext();
for (Domain domain : store.getDomains()) {
if (CredentialsMatchers.firstOrNull(store.getCredentials(domain), CredentialsMatchers.withId(value)) != null) {
if (storeContext == context) {
return FormValidation.error("This ID is already in use");
} else {
return FormValidation.warning("The ID ‘%s’ is already in use in %s", value, storeContext instanceof Item ? ((Item) storeContext).getFullDisplayName() : storeContext.getDisplayName());
}
}
}
}
return null;
}

}

}
@@ -1,6 +1,5 @@
package com.cloudbees.plugins.credentials.impl;

import com.cloudbees.plugins.credentials.CredentialsDescriptor;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsSnapshotTaker;
Expand Down Expand Up @@ -144,7 +143,7 @@ private static char[] toCharArray(Secret password) {
}

@Extension(ordinal = -1)
public static class DescriptorImpl extends CredentialsDescriptor {
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {

@Override
public String getDisplayName() {
Expand All @@ -155,17 +154,6 @@ public DescriptorExtensionList<KeyStoreSource, Descriptor<KeyStoreSource>> getKe
return Hudson.getInstance().getDescriptorList(KeyStoreSource.class);
}


public CertificateCredentialsImpl fixInstance(CertificateCredentialsImpl instance) {
if (instance == null) {
return new CertificateCredentialsImpl(CredentialsScope.GLOBAL, null, "", "",
new FileOnMasterKeyStoreSource(""));
} else {
return instance;
}
}


}

public static abstract class KeyStoreSource extends AbstractDescribableImpl<KeyStoreSource> {
Expand Down
Expand Up @@ -23,8 +23,6 @@
*/
package com.cloudbees.plugins.credentials.impl;

import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsDescriptor;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import edu.umd.cs.findbugs.annotations.CheckForNull;
Expand Down Expand Up @@ -94,7 +92,7 @@ public String getUsername() {
* {@inheritDoc}
*/
@Extension(ordinal = 1)
public static class DescriptorImpl extends CredentialsDescriptor {
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {

/**
* {@inheritDoc}
Expand Down
@@ -0,0 +1,5 @@
<div>
An internal unique ID by which these credentials are identified from jobs and other configuration.
Normally left blank, in which case an ID will be generated, which is fine for jobs created using visual forms.
Useful to specify explicitly when using credentials from scripted configuration.
</div>
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright 2015 Jesse Glick.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry title="${%Description}" field="description">
<f:textbox/>
</f:entry>
<f:advanced>
<!-- When instance != null, we must not have checkUrl, since doCheckId has no way of knowing that this is *supposed* to exist already. -->
<!-- But there appears to be no way to turn off checkUrl when field is specified. -->
<!-- So the only apparent workaround is to disable field when instance != null, and code name and value manually. -->
<f:entry field="${instance != null ? null : 'id'}" title="${%ID}">
<f:textbox name="_.id" value="${instance != null ? instance.id : null}" readonly="${instance != null ? 'readonly' : null}"/>
</f:entry>
</f:advanced>
</j:jelly>
@@ -0,0 +1,24 @@
#
# The MIT License
#
# Copyright (c) 2013 Harald Albers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

Description=Beschreibung
@@ -0,0 +1,23 @@
# The MIT License
#
# Copyright (c) 2013 Seiji Sogabe.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

Description=\u8aac\u660e
Expand Up @@ -23,20 +23,12 @@
~ THE SOFTWARE.
-->

<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:invisibleEntry>
<f:textbox style="display:none" field="id"/>
</f:invisibleEntry>
<j:if test="${instance==null}">
<j:set var="instance" value="${descriptor.fixInstance(instance)}"/>
</j:if>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler">
<f:entry title="${%Certificate}" field="keyStoreSource">
<f:hetero-radio field="keyStoreSource" descriptors="${descriptor.KeyStoreSources}"/>
</f:entry>
<f:entry title="${%Password}" field="password">
<f:password value="${instance==null || instance.passwordEmpty ? '' : instance.password}"/>
</f:entry>
<f:entry title="${%Description}" field="description">
<f:textbox/>
</f:entry>
</j:jelly>
<st:include page="id-and-description" class="${descriptor.clazz}"/>
</j:jelly>
Expand Up @@ -22,4 +22,3 @@

Certificate=Zertifikat
Password=Passwort
Description=Beschreibung
Expand Up @@ -22,4 +22,3 @@

Certificate=\u8a3c\u660e\u66f8
Password=\u30d1\u30b9\u30ef\u30fc\u30c9
Description=\u8aac\u660e

This file was deleted.

Expand Up @@ -23,17 +23,12 @@
~ THE SOFTWARE.
-->

<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:invisibleEntry>
<f:textbox style="display:none" field="id"/>
</f:invisibleEntry>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:st="jelly:stapler">
<f:entry title="${%Username}" field="username">
<f:textbox/>
</f:entry>
<f:entry title="${%Password}" field="password">
<f:password/>
</f:entry>
<f:entry title="${%Description}" field="description">
<f:textbox/>
</f:entry>
</j:jelly>
<st:include page="id-and-description" class="${descriptor.clazz}"/>
</j:jelly>
Expand Up @@ -23,4 +23,3 @@

Username=Benutzername
Password=Passwort
Description=Beschreibung
Expand Up @@ -22,4 +22,3 @@

Username=\u30e6\u30fc\u30b6\u30fc\u540d
Password=\u30d1\u30b9\u30ef\u30fc\u30c9
Description=\u8aac\u660e

This file was deleted.

0 comments on commit f2639a4

Please sign in to comment.