Skip to content

Commit

Permalink
Merge pull request #123 from darxriggs/replace-deprecated-jenkins-api
Browse files Browse the repository at this point in the history
Replace deprecated API use of Jenkins' getInstance() and getActiveIntance()
  • Loading branch information
jvz committed Aug 13, 2019
2 parents cb2aed0 + ccebb35 commit 3cfa932
Show file tree
Hide file tree
Showing 24 changed files with 92 additions and 116 deletions.
8 changes: 4 additions & 4 deletions docs/consumer.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public ListBoxModel doFillCredentialsIdItems(
) {
StandardListBoxModel result = new StandardListBoxModel();
if (item == null) {
if (!Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER)) {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return result.includeCurrentValue(credentialsId); // <2>
}
} else {
Expand Down Expand Up @@ -155,7 +155,7 @@ public FormValidation doCheckCredentialsId(
... // <2>
) {
if (item == null) {
if (!Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER)) {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return FormValidation.ok(); // <3>
}
} else {
Expand Down Expand Up @@ -315,7 +315,7 @@ This drop down list would typically be displayed from one of the _Manage Jenkins
----
CredentialsProvider.listCredentials(
StandardUsernameCredentials.class, // <1>
Jenkins.getInstance(), // <2>
Jenkins.get(), // <2>
ACL.SYSTEM, // <2>
URIRequirementBuilder.fromUri(scmUrl),
AuthenticationTokens.matcher(MySCMAuthentication.class) // <1>
Expand All @@ -324,7 +324,7 @@ CredentialsProvider.listCredentials(
<1> For this SCM, management of post commit hooks requires authentication that has specified a username, so even though there are other authentication mechanisms supported by `AuthenticationTokens.matcher(...)` we limit at the type level as that reduces the response that needs to be filtered.
The alternative would have been a matcher that combined `CredentialsMatchers.instanceOf(StandardUsernameCredentials.class)` but this reduces the ability of an external credentials provider to filter the query on the remote side.
<2> We are doing this operation outside of the context of a single job, rather this is being performed on behalf of the entire Jenkins instance.
Thus we should be performing this as `ACL.SYSTEM` and in the context of `Jenkins.getInstance()`.
Thus we should be performing this as `ACL.SYSTEM` and in the context of `Jenkins.get()`.
This has the additional benefit that the admin can restrict the high permission hook management credentials to `CredentialsScope.SYSTEM` which will prevent access by jobs.

=== Persist a reference to a specific credential instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public CredentialsScope getScope() {
@NonNull
@SuppressWarnings("unchecked")
public CredentialsDescriptor getDescriptor() {
// TODO switch to Jenkins.getInstance() once 2.0+ is the baseline
return (CredentialsDescriptor) Jenkins.getActiveInstance().getDescriptorOrDie(getClass());
return (CredentialsDescriptor) Jenkins.get().getDescriptorOrDie(getClass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ public static <T extends ModelObject> T findContextInPath(@NonNull StaplerReques
}
}
}
Jenkins jenkins = Jenkins.getActiveInstance();
Jenkins jenkins = Jenkins.get();
while (!pathSegments.isEmpty()) {
String fullName = StringUtils.join(pathSegments, "/");
Item item = jenkins.getItemByFullName(fullName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ private boolean match(Set<Class<? extends StandardCredentials>> allowed, Standar

public StandardListBoxModel doFillDefaultValueItems(@AncestorInPath Item context,
@QueryParameter(required = true) String credentialType) {
// TODO switch to Jenkins.getInstance() once 2.0+ is the baseline
Jenkins jenkins = Jenkins.getActiveInstance();
Jenkins jenkins = Jenkins.get();
final ACL acl = context == null ? jenkins.getACL() : context.getACL();
final Class<? extends StandardCredentials> typeClass = decodeType(credentialType);
final List<DomainRequirement> domainRequirements = Collections.<DomainRequirement>emptyList();
Expand All @@ -182,8 +181,7 @@ public StandardListBoxModel doFillValueItems(@AncestorInPath Item context,
@QueryParameter(required = true) String credentialType,
@QueryParameter String value,
@QueryParameter boolean required) {
// TODO switch to Jenkins.getInstance() once 2.0+ is the baseline
Jenkins jenkins = Jenkins.getActiveInstance();
Jenkins jenkins = Jenkins.get();
final ACL acl = context == null ? jenkins.getACL() : context.getACL();
final Authentication authentication = Jenkins.getAuthentication();
final Authentication itemAuthentication = CredentialsProvider.getDefaultAuthenticationOf(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,12 @@ public CredentialsProvider() {
* @return all the registered {@link com.cloudbees.plugins.credentials.Credentials} descriptors.
*/
public static DescriptorExtensionList<Credentials, CredentialsDescriptor> allCredentialsDescriptors() {
// TODO switch to Jenkins.getInstance() once 2.0+ is the baseline
return Jenkins.getActiveInstance().getDescriptorList(Credentials.class);
return Jenkins.get().getDescriptorList(Credentials.class);
}

/**
* Returns all credentials which are available to the {@link ACL#SYSTEM} {@link Authentication}
* within the {@link jenkins.model.Jenkins#getInstance()}.
* within the {@link Jenkins#get()}.
*
* @param type the type of credentials to get.
* @param <C> the credentials type.
Expand All @@ -248,7 +247,7 @@ public static <C extends Credentials> List<C> lookupCredentials(@NonNull Class<C

/**
* Returns all credentials which are available to the specified {@link Authentication}
* within the {@link jenkins.model.Jenkins#getInstance()}.
* within the {@link Jenkins#get()}.
*
* @param type the type of credentials to get.
* @param authentication the authentication.
Expand All @@ -264,7 +263,7 @@ public static <C extends Credentials> List<C> lookupCredentials(@NonNull Class<C
@SuppressWarnings("unused") // API entry point for consumers
public static <C extends Credentials> List<C> lookupCredentials(@NonNull Class<C> type,
@Nullable Authentication authentication) {
return lookupCredentials(type, Jenkins.getInstance(), authentication);
return lookupCredentials(type, Jenkins.get(), authentication);
}

/**
Expand All @@ -284,7 +283,7 @@ public static <C extends Credentials> List<C> lookupCredentials(@NonNull Class<C
public static <C extends Credentials> List<C> lookupCredentials(@NonNull Class<C> type,
@Nullable Item item) {
return item == null
? lookupCredentials(type, Jenkins.getInstance(), ACL.SYSTEM)
? lookupCredentials(type, Jenkins.get(), ACL.SYSTEM)
: lookupCredentials(type, item, ACL.SYSTEM);
}

Expand Down Expand Up @@ -390,8 +389,7 @@ public static <C extends Credentials> List<C> lookupCredentials(@NonNull Class<C
@Nullable List<DomainRequirement>
domainRequirements) {
type.getClass(); // throw NPE if null
// TODO switch to Jenkins.getInstance() once 2.0+ is the baseline
Jenkins jenkins = Jenkins.getActiveInstance();
Jenkins jenkins = Jenkins.get();
itemGroup = itemGroup == null ? jenkins : itemGroup;
authentication = authentication == null ? ACL.SYSTEM : authentication;
domainRequirements = domainRequirements
Expand Down Expand Up @@ -448,8 +446,7 @@ public static <C extends IdCredentials> ListBoxModel listCredentials(@NonNull Cl
domainRequirements,
@Nullable CredentialsMatcher matcher) {
type.getClass(); // throw NPE if null
// TODO switch to Jenkins.getInstance() once 2.0+ is the baseline
Jenkins jenkins = Jenkins.getActiveInstance();
Jenkins jenkins = Jenkins.get();
itemGroup = itemGroup == null ? jenkins : itemGroup;
authentication = authentication == null ? ACL.SYSTEM : authentication;
domainRequirements =
Expand Down Expand Up @@ -526,7 +523,7 @@ public static <C extends Credentials> List<C> lookupCredentials(@NonNull Class<C
domainRequirements) {
type.getClass(); // throw NPE if null
if (item == null) {
return lookupCredentials(type, Jenkins.getInstance(), authentication, domainRequirements);
return lookupCredentials(type, Jenkins.get(), authentication, domainRequirements);
}
if (item instanceof ItemGroup) {
return lookupCredentials(type, (ItemGroup)item, authentication, domainRequirements);
Expand Down Expand Up @@ -589,7 +586,7 @@ public static <C extends IdCredentials> ListBoxModel listCredentials(@NonNull Cl
@Nullable CredentialsMatcher matcher) {
type.getClass(); // throw NPE if null
if (item == null) {
return listCredentials(type, Jenkins.getInstance(), authentication, domainRequirements, matcher);
return listCredentials(type, Jenkins.get(), authentication, domainRequirements, matcher);
}
if (item instanceof ItemGroup) {
return listCredentials(type, (ItemGroup) item, authentication, domainRequirements, matcher);
Expand Down Expand Up @@ -712,7 +709,7 @@ public boolean hasNext() {
current = ((Item) current).getParent();
iterator = providers.iterator();
} else if (current instanceof User) {
Jenkins jenkins = Jenkins.getActiveInstance();
Jenkins jenkins = Jenkins.get();
Authentication a;
if (jenkins.hasPermission(USE_ITEM) && current == User.current()) {
// this is the fast path for the 99% of cases
Expand All @@ -734,17 +731,17 @@ public boolean hasNext() {
// escape
current = null;
} else if (current instanceof ComputerSet) {
current = Jenkins.getActiveInstance();
current = Jenkins.get();
iterator = providers.iterator();
} else if (current instanceof Computer) {
current = Jenkins.getActiveInstance();
current = Jenkins.get();
iterator = providers.iterator();
} else if (current instanceof Node) {
current = Jenkins.getActiveInstance();
current = Jenkins.get();
iterator = providers.iterator();
} else {
// fall back to Jenkins as the ultimate parent of everything else
current = Jenkins.getActiveInstance();
current = Jenkins.get();
iterator = providers.iterator();
}
}
Expand Down Expand Up @@ -1104,8 +1101,8 @@ public CredentialsStore getStore(@CheckForNull ModelObject object) {
* for items in the specified {@link ItemGroup}
*
* @param type the type of credentials to return.
* @param itemGroup the item group (if {@code null} assume {@link hudson.model.Hudson#getInstance()}.
* @param authentication the authentication (if {@code null} assume {@link hudson.security.ACL#SYSTEM}.
* @param itemGroup the item group (if {@code null} assume {@link Jenkins#get()}.
* @param authentication the authentication (if {@code null} assume {@link ACL#SYSTEM}.
* @param <C> the credentials type.
* @return the list of credentials.
*/
Expand All @@ -1120,8 +1117,8 @@ public abstract <C extends Credentials> List<C> getCredentials(@NonNull Class<C>
* .plugins.credentials.domains.DomainRequirement}s.
*
* @param type the type of credentials to return.
* @param itemGroup the item group (if {@code null} assume {@link hudson.model.Hudson#getInstance()}.
* @param authentication the authentication (if {@code null} assume {@link hudson.security.ACL#SYSTEM}.
* @param itemGroup the item group (if {@code null} assume {@link Jenkins#get()}.
* @param authentication the authentication (if {@code null} assume {@link ACL#SYSTEM}.
* @param domainRequirements the credential domains to match (if the {@link CredentialsProvider} does not support
* {@link DomainRequirement}s then it should
* assume the match is true).
Expand Down Expand Up @@ -1150,7 +1147,7 @@ public <C extends Credentials> List<C> getCredentials(@NonNull Class<C> type,
*
* @param <C> the credentials type.
* @param type the type of credentials to return.
* @param itemGroup the item group (if {@code null} assume {@link hudson.model.Hudson#getInstance()}.
* @param itemGroup the item group (if {@code null} assume {@link Jenkins#get()}.
* @param authentication the authentication (if {@code null} assume {@link ACL#SYSTEM}.
* @param domainRequirements the credential domain to match.
* @param matcher the additional filtering to apply to the credentials
Expand Down Expand Up @@ -1180,7 +1177,7 @@ public <C extends IdCredentials> ListBoxModel getCredentialIds(@NonNull Class<C>
*
* @param type the type of credentials to return.
* @param item the item.
* @param authentication the authentication (if {@code null} assume {@link hudson.security.ACL#SYSTEM}.
* @param authentication the authentication (if {@code null} assume {@link ACL#SYSTEM}.
* @param <C> the credentials type.
* @return the list of credentials.
*/
Expand All @@ -1198,7 +1195,7 @@ public <C extends Credentials> List<C> getCredentials(@NonNull Class<C> type,
*
* @param type the type of credentials to return.
* @param item the item.
* @param authentication the authentication (if {@code null} assume {@link hudson.security.ACL#SYSTEM}.
* @param authentication the authentication (if {@code null} assume {@link ACL#SYSTEM}.
* @param domainRequirements the credential domain to match.
* @param <C> the credentials type.
* @return the list of credentials.
Expand All @@ -1225,7 +1222,7 @@ public <C extends Credentials> List<C> getCredentials(@NonNull Class<C> type,
*
* @param type the type of credentials to return.
* @param item the item.
* @param authentication the authentication (if {@code null} assume {@link hudson.security.ACL#SYSTEM}.
* @param authentication the authentication (if {@code null} assume {@link ACL#SYSTEM}.
* @param domainRequirements the credential domain to match.
* @param matcher the additional filtering to apply to the credentials
* @param <C> the credentials type.
Expand Down Expand Up @@ -1376,7 +1373,7 @@ public static Fingerprint getFingerprintOf(@NonNull Credentials c) throws IOExce
} finally {
IOUtils.closeQuietly(out);
}
return Jenkins.getActiveInstance().getFingerprintMap().get(Util.toHexString(md5.digest()));
return Jenkins.get().getFingerprintMap().get(Util.toHexString(md5.digest()));
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("JLS mandates MD5 as a supported digest algorithm");
}
Expand All @@ -1402,7 +1399,7 @@ public static Fingerprint getOrCreateFingerprintOf(@NonNull Credentials c) throw
} finally {
IOUtils.closeQuietly(out);
}
return Jenkins.getActiveInstance().getFingerprintMap().getOrCreate(null, pseudoFilename, md5.digest());
return Jenkins.get().getFingerprintMap().getOrCreate(null, pseudoFilename, md5.digest());
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("JLS mandates MD5 as a supported digest algorithm");
}
Expand Down Expand Up @@ -1517,8 +1514,7 @@ public static <C extends Credentials> List<C> trackAll(@NonNull Node node, @NonN
// TODO (JENKINS-51694): This breaks tracking for cloud agents. We should
// track those agents against the cloud instead of the node itself.
Set<String> jenkinsNodeNames = new HashSet<>();
// TODO: Switch to Jenkins.get() once 2.98+ is the baseline
for (Node n: Jenkins.getActiveInstance().getNodes()) {
for (Node n: Jenkins.get().getNodes()) {
jenkinsNodeNames.add(n.getNodeName());
}
for (Credentials c : credentials) {
Expand Down Expand Up @@ -1652,7 +1648,7 @@ public static <C extends Credentials> List<C> trackAll(@NonNull Item item, @NonN
public static void saveAll() throws IOException {
LOGGER.entering(CredentialsProvider.class.getName(), "saveAll");
try {
final Jenkins jenkins = Jenkins.getActiveInstance();
Jenkins jenkins = Jenkins.get();
jenkins.checkPermission(Jenkins.ADMINISTER);
LOGGER.log(Level.INFO, "Forced save credentials stores: Requested by {0}",
StringUtils.defaultIfBlank(Jenkins.getAuthentication().getName(), "anonymous"));
Expand Down

0 comments on commit 3cfa932

Please sign in to comment.