-
Notifications
You must be signed in to change notification settings - Fork 234
/
BaseStandardCredentials.java
230 lines (212 loc) · 8.59 KB
/
BaseStandardCredentials.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* The MIT License
*
* Copyright (c) 2011-2012, CloudBees, Inc., Stephen Connolly.
*
* 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.
*/
package com.cloudbees.plugins.credentials.impl;
import com.cloudbees.plugins.credentials.BaseCredentials;
import com.cloudbees.plugins.credentials.ContextInPath;
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsDescriptor;
import com.cloudbees.plugins.credentials.CredentialsMatcher;
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.ExtensionList;
import hudson.Util;
import hudson.model.Item;
import hudson.model.ModelObject;
import hudson.model.User;
import hudson.util.FormValidation;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import static com.cloudbees.plugins.credentials.CredentialsSelectHelper.*;
/**
* Base class for {@link StandardCredentials}.
*/
@ExportedBean
public abstract class BaseStandardCredentials extends BaseCredentials implements StandardCredentials {
/**
* Our ID.
*/
@NonNull
private final String id;
/**
* Our description.
*/
@NonNull
private final String description;
/**
* Constructor.
*
* @param id the id.
* @param description the description.
*/
public BaseStandardCredentials(@CheckForNull String id, @CheckForNull String description) {
super();
this.id = IdCredentials.Helpers.fixEmptyId(id);
this.description = Util.fixNull(description);
}
/**
* Constructor.
*
* @param scope the scope.
* @param id the id.
* @param description the description.
*/
public BaseStandardCredentials(@CheckForNull CredentialsScope scope, @CheckForNull String id,
@CheckForNull String description) {
super(scope);
this.id = IdCredentials.Helpers.fixEmptyId(id);
this.description = Util.fixNull(description);
}
/**
* {@inheritDoc}
*/
@NonNull
@Exported
public String getDescription() {
return description;
}
/**
* {@inheritDoc}
*/
@NonNull
@Exported
public String getId() {
return id;
}
/**
* {@inheritDoc}
*/
@Override
public final int hashCode() {
return IdCredentials.Helpers.hashCode(this);
}
/**
* {@inheritDoc}
*/
@Override
public final boolean equals(Object o) {
return IdCredentials.Helpers.equals(this, o);
}
/**
* 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);
}
@CheckForNull
private static FormValidation checkForDuplicates(String value, ModelObject context, ModelObject object) {
CredentialsMatcher withId = CredentialsMatchers.withId(value);
for (CredentialsStore store : CredentialsProvider.lookupStores(object)) {
if (!store.hasPermission(CredentialsProvider.VIEW)) {
continue;
}
ModelObject storeContext = store.getContext();
for (Domain domain : store.getDomains()) {
for (Credentials match : CredentialsMatchers.filter(store.getCredentials(domain), withId)) {
if (storeContext == context) {
return FormValidation.error("This ID is already in use");
} else {
CredentialsScope scope = match.getScope();
if (scope != null && !scope.isVisible(context)) {
// scope is not exported to child contexts
continue;
}
return FormValidation.warning("The ID ‘%s’ is already in use in %s", value,
storeContext instanceof Item
? ((Item) storeContext).getFullDisplayName()
: storeContext.getDisplayName());
}
}
}
}
return null;
}
/**
* Gets the check id url for the specified store.
*
* @param store the store.
* @return the url of the id check endpoint.
* @throws UnsupportedEncodingException if the JVM does not implement the JLS.
*/
public String getCheckIdUrl(CredentialsStore store) throws UnsupportedEncodingException {
ModelObject context = store.getContext();
for (ContextResolver r : ExtensionList.lookup(ContextResolver.class)) {
String token = r.getToken(context);
if (token != null) {
return Jenkins.get().getRootUrlFromRequest() + "/" + getDescriptorUrl()
+ "/checkId?provider=" + r.getClass().getName() + "&token="
+ URLEncoder.encode(token, "UTF-8");
}
}
return Jenkins.get().getRootUrlFromRequest() + "/" + getDescriptorUrl()
+ "/checkId?provider=null&token=null";
}
public final FormValidation doCheckId(@ContextInPath ModelObject context, @QueryParameter String value) {
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.get();
problem = checkForDuplicates(value, context, j);
if (problem != null) {
return problem;
}
}
return FormValidation.ok();
}
}
}