Skip to content
Permalink
Browse files

[JENKINS-38465] it now search credentials also into folders

  • Loading branch information...
kuisathaverat committed Oct 5, 2016
1 parent 0b09e43 commit 7572ce2975f9c5a8ebc8a2509fd980dd6a979dd9
@@ -40,9 +40,11 @@
import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.ItemGroup;
import hudson.security.ACL;
import hudson.util.Secret;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;

import java.util.Collections;
import java.util.List;
@@ -57,25 +59,30 @@

private final Regions region;

public AmazonECSRegistryCredential(@CheckForNull CredentialsScope scope, String credentialsId) {
super(scope, "ecr:"+credentialsId, "Amazon ECR Registry");
this.credentialsId = credentialsId;
region = Regions.US_EAST_1;
private final ItemGroup itemGroup;

public AmazonECSRegistryCredential(CredentialsScope scope, @NonNull String credentialsId,
String description, ItemGroup itemGroup) {
this(scope, credentialsId, Regions.US_EAST_1, description, itemGroup);
}

public AmazonECSRegistryCredential(@CheckForNull CredentialsScope scope, String credentialsId, Regions region) {
super(scope, "ecr:" + region.getName() + ":" + credentialsId, "Amazon ECR Registry");
public AmazonECSRegistryCredential(@CheckForNull CredentialsScope scope, @NonNull String credentialsId,
Regions region, String description, ItemGroup itemGroup) {
super(scope, "ecr:" + region.getName() + ":" + credentialsId, "Amazon ECR Registry : "
+ (StringUtils.isNotBlank(description) ? description + " - " : "" ) + region);
this.credentialsId = credentialsId;
this.region = region;
this.itemGroup = itemGroup;
}


public String getCredentialsId() {
return credentialsId;
}

public @CheckForNull AmazonWebServicesCredentials getCredentials() {
List<AmazonWebServicesCredentials> credentials = CredentialsProvider.lookupCredentials(
AmazonWebServicesCredentials.class, Jenkins.getInstance(), ACL.SYSTEM, Collections.EMPTY_LIST);
AmazonWebServicesCredentials.class, itemGroup, ACL.SYSTEM, Collections.EMPTY_LIST);

if (credentials.isEmpty()) {
return null;
@@ -91,7 +98,8 @@ public String getCredentialsId() {

public String getDescription() {
final AmazonWebServicesCredentials credentials = getCredentials();
return credentials == null ? "" : CredentialsNameProvider.name(credentials);
return credentials == null ? "No Valid Credential" : CredentialsNameProvider.name(credentials)
+ " " + (StringUtils.isNotBlank(credentials.getDescription()) ? region : super.getDescription());
}

@NonNull
@@ -30,17 +30,22 @@
import com.cloudbees.jenkins.plugins.awscredentials.AmazonWebServicesCredentials;
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.security.ACL;
import org.acegisecurity.Authentication;

import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* This class automatically wraps existing {@link AmazonWebServicesCredentials} instances
@@ -50,6 +55,7 @@
@Extension
public class AmazonECSRegistryCredentialsProvider extends CredentialsProvider {

private static final Logger LOGGER = Logger.getLogger(AmazonECSRegistryCredentialsProvider.class.getName());

@NonNull
@Override
@@ -61,19 +67,20 @@

List<C> derived = Lists.newLinkedList();

final List<AmazonWebServicesCredentials> list = lookupCredentials(AmazonWebServicesCredentials.class, itemGroup,
ACL.SYSTEM, Collections.EMPTY_LIST);
final List<AmazonWebServicesCredentials> list = lookupCredentials(AmazonWebServicesCredentials.class, itemGroup, ACL.SYSTEM , Collections.EMPTY_LIST);

for (AmazonWebServicesCredentials credentials : list) {
derived.add((C) new AmazonECSRegistryCredential(
credentials.getScope(),
credentials.getId()));
LOGGER.log(Level.FINE, "Resolving Amazon Web Services credentials of scope {0} with id {1} , itemgroup {2}",
new Object[]{credentials.getScope(), credentials.getId(),itemGroup});
derived.add((C) new AmazonECSRegistryCredential( credentials.getScope(),
credentials.getId(),credentials.getDescription(),itemGroup));

for (Regions region : Regions.values()) {
derived.add((C) new AmazonECSRegistryCredential(
credentials.getScope(),
credentials.getId(),
region));
LOGGER.log(Level.FINE, "Resolving Amazon Web Services credentials of scope {0} with id {1} and region {2}",
new Object[]{credentials.getScope(), credentials.getId(),region});
derived.add((C) new AmazonECSRegistryCredential( credentials.getScope(),
credentials.getId(),
region, credentials.getDescription(),itemGroup));
}
}

0 comments on commit 7572ce2

Please sign in to comment.
You can’t perform that action at this time.