Skip to content

Commit

Permalink
Migrate to credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
jetersen committed Jan 25, 2018
1 parent 6121212 commit 5438932
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
import hudson.util.ListBoxModel;
import jenkins.model.GlobalConfiguration;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.jvctb.config.CredentialsHelper;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.reports.Parser;

import static org.jenkinsci.plugins.jvctb.config.CredentialsHelper.migrateCredentials;

/** Created by magnayn on 07/04/2016. */
@Extension
Expand All @@ -32,10 +36,10 @@ public static Optional<ViolationsToBitbucketServerGlobalConfiguration> get() {
}

public String bitbucketServerUrl;
public String password;
@Deprecated private transient String password;
public String projectKey;
public String repoSlug;
public String username;
@Deprecated private transient String username;
private String usernamePasswordCredentialsId;
private SEVERITY minSeverity = SEVERITY.INFO;

Expand All @@ -50,15 +54,13 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
return true;
}

public static final ListBoxModel TYPES = new ListBoxModel(
new ListBoxModel.Option("Info", SEVERITY.INFO.toString()),
new ListBoxModel.Option("Warning", SEVERITY.WARN.toString()),
new ListBoxModel.Option("Error", SEVERITY.ERROR.toString())
);

@Restricted(NoExternalUse.class)
public ListBoxModel doFillMinSeverityItems() {
return TYPES;
ListBoxModel items = new ListBoxModel();
for (SEVERITY severity : SEVERITY.values()) {
items.add(severity.name());
}
return items;
}

public ListBoxModel doFillUsernamePasswordCredentialsIdItems() {
Expand All @@ -69,10 +71,6 @@ public String getBitbucketServerUrl() {
return this.bitbucketServerUrl;
}

public String getPassword() {
return this.password;
}

public String getProjectKey() {
return this.projectKey;
}
Expand All @@ -81,10 +79,6 @@ public String getRepoSlug() {
return this.repoSlug;
}

public String getUsername() {
return this.username;
}

public String getUsernamePasswordCredentialsId() {
return this.usernamePasswordCredentialsId;
}
Expand All @@ -103,11 +97,6 @@ public void setBitbucketServerUrl(String bitbucketServerUrl) {
this.bitbucketServerUrl = bitbucketServerUrl;
}

@DataBoundSetter
public void setPassword(String password) {
this.password = password;
}

@DataBoundSetter
public void setProjectKey(String projectKey) {
this.projectKey = projectKey;
Expand All @@ -118,25 +107,25 @@ public void setRepoSlug(String repoSlug) {
this.repoSlug = repoSlug;
}

@DataBoundSetter
public void setUsername(String username) {
this.username = username;
}

public void setUsernamePasswordCredentialsId(String usernamePasswordCredentialsId) {
this.usernamePasswordCredentialsId = usernamePasswordCredentialsId;
}

private Object readResolve() {
if (StringUtils.isBlank(usernamePasswordCredentialsId) && username != null && password != null) {
usernamePasswordCredentialsId = migrateCredentials(username, password);
}
return this;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (bitbucketServerUrl == null ? 0 : bitbucketServerUrl.hashCode());
result = prime * result + (minSeverity == null ? 0 : minSeverity.hashCode());
result = prime * result + (password == null ? 0 : password.hashCode());
result = prime * result + (projectKey == null ? 0 : projectKey.hashCode());
result = prime * result + (repoSlug == null ? 0 : repoSlug.hashCode());
result = prime * result + (username == null ? 0 : username.hashCode());
result =
prime * result
+ (usernamePasswordCredentialsId == null
Expand Down Expand Up @@ -168,13 +157,6 @@ public boolean equals(Object obj) {
if (minSeverity != other.minSeverity) {
return false;
}
if (password == null) {
if (other.password != null) {
return false;
}
} else if (!password.equals(other.password)) {
return false;
}
if (projectKey == null) {
if (other.projectKey != null) {
return false;
Expand All @@ -189,13 +171,6 @@ public boolean equals(Object obj) {
} else if (!repoSlug.equals(other.repoSlug)) {
return false;
}
if (username == null) {
if (other.username != null) {
return false;
}
} else if (!username.equals(other.username)) {
return false;
}
if (usernamePasswordCredentialsId == null) {
if (other.usernamePasswordCredentialsId != null) {
return false;
Expand All @@ -210,14 +185,10 @@ public boolean equals(Object obj) {
public String toString() {
return "ViolationsToBitbucketServerGlobalConfiguration [bitbucketServerUrl="
+ bitbucketServerUrl
+ ", password="
+ password
+ ", projectKey="
+ projectKey
+ ", repoSlug="
+ repoSlug
+ ", username="
+ username
+ ", usernamePasswordCredentialsId="
+ usernamePasswordCredentialsId
+ ", minSeverity="
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
package org.jenkinsci.plugins.jvctb.config;

import static com.cloudbees.plugins.credentials.CredentialsMatchers.allOf;
import static com.cloudbees.plugins.credentials.CredentialsMatchers.firstOrNull;
import static com.cloudbees.plugins.credentials.CredentialsMatchers.withId;
import static com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials;
import static com.google.common.base.Optional.absent;
import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Strings.isNullOrEmpty;
import static hudson.security.ACL.SYSTEM;
import hudson.model.ItemGroup;
import hudson.util.ListBoxModel;

import java.util.List;

import org.acegisecurity.Authentication;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.common.AbstractIdCredentialsListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import com.google.common.base.Optional;
import hudson.model.ItemGroup;
import hudson.security.ACL;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
import jenkins.model.Jenkins;
import org.acegisecurity.Authentication;
import org.apache.commons.lang.StringUtils;

import static com.cloudbees.plugins.credentials.CredentialsMatchers.allOf;
import static com.cloudbees.plugins.credentials.CredentialsMatchers.firstOrNull;
import static com.cloudbees.plugins.credentials.CredentialsMatchers.withId;
import static com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials;
import static com.google.common.base.Optional.absent;
import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Strings.isNullOrEmpty;
import static hudson.security.ACL.SYSTEM;

public class CredentialsHelper {
public static ListBoxModel doFillUsernamePasswordCredentialsIdItems() {
Expand Down Expand Up @@ -51,4 +59,42 @@ public static List<StandardUsernamePasswordCredentials> getAllCredentials() {

return lookupCredentials(type, itemGroup, authentication, domainRequirement);
}

public static String migrateCredentials(String username, String password) {
String credentialsId = null;
DomainRequirement domainRequirement = null;
List<StandardUsernamePasswordCredentials> credentials =
CredentialsMatchers.filter(
CredentialsProvider.lookupCredentials(
StandardUsernamePasswordCredentials.class,
Jenkins.getInstance(),
ACL.SYSTEM,
domainRequirement),
CredentialsMatchers.withUsername(username));
for (StandardUsernamePasswordCredentials cred : credentials) {
if (StringUtils.equals(password, Secret.toString(cred.getPassword()))) {
// If some credentials have the same username/password, use those.
credentialsId = cred.getId();
break;
}
}
if (StringUtils.isBlank(credentialsId)) {
// If we couldn't find any existing credentials,
// create new credentials with the principal and secret and use it.
StandardUsernamePasswordCredentials newCredentials =
new UsernamePasswordCredentialsImpl(
CredentialsScope.SYSTEM,
null,
"Migrated by Violation comments to bitbucket plugin",
username,
password);
SystemCredentialsProvider.getInstance().getCredentials().add(newCredentials);
credentialsId = newCredentials.getId();
}
if (StringUtils.isNotEmpty(credentialsId)) {
return credentialsId;
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import org.kohsuke.stapler.DataBoundConstructor;
import se.bjurr.violations.lib.reports.Parser;

public class ViolationConfig extends AbstractDescribableImpl<ViolationConfig> implements Serializable {
public class ViolationConfig extends AbstractDescribableImpl<ViolationConfig>
implements Serializable {
private static final long serialVersionUID = 6664329842273455651L;
private String pattern;
private String reporter;
Expand Down Expand Up @@ -118,7 +119,7 @@ public String getDisplayName() {
@Restricted(NoExternalUse.class)
public ListBoxModel doFillParserItems() {
ListBoxModel items = new ListBoxModel();
for (Parser parser: Parser.values()) {
for (Parser parser : Parser.values()) {
items.add(parser.name());
}
return items;
Expand Down
Loading

0 comments on commit 5438932

Please sign in to comment.