Skip to content

Commit

Permalink
Use Jenkins project-wide formatter configuration (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Dec 14, 2023
1 parent 8c9c0ae commit 20786a9
Show file tree
Hide file tree
Showing 25 changed files with 3,206 additions and 3,468 deletions.
295 changes: 126 additions & 169 deletions pom.xml

Large diffs are not rendered by default.

100 changes: 50 additions & 50 deletions src/main/java/com/google/jenkins/plugins/k8sengine/ClusterUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,58 +23,58 @@

/** Utility functions for converting between {@link Cluster}s and their String representations. */
class ClusterUtil {
/**
* Given a GKE {@link Cluster} return a String representation containing the name and location.
*
* @param cluster The {@link Cluster} object to extract values from.
* @return A String of the form "name (location)" based on the given cluster's properties.
*/
static String toNameAndLocation(Cluster cluster) {
Preconditions.checkNotNull(cluster);
return toNameAndLocation(cluster.getName(), cluster.getLocation());
}
/**
* Given a GKE {@link Cluster} return a String representation containing the name and location.
*
* @param cluster The {@link Cluster} object to extract values from.
* @return A String of the form "name (location)" based on the given cluster's properties.
*/
static String toNameAndLocation(Cluster cluster) {
Preconditions.checkNotNull(cluster);
return toNameAndLocation(cluster.getName(), cluster.getLocation());
}

/**
* Given a name and location for a cluster, return the combined String representation.
*
* @param name A non-empty cluster name
* @param location A non-empty GCP resource location.
* @return A String of the form "name (location)".
*/
static String toNameAndLocation(String name, String location) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
Preconditions.checkArgument(!Strings.isNullOrEmpty(location));
return String.format("%s (%s)", name, location);
}
/**
* Given a name and location for a cluster, return the combined String representation.
*
* @param name A non-empty cluster name
* @param location A non-empty GCP resource location.
* @return A String of the form "name (location)".
*/
static String toNameAndLocation(String name, String location) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
Preconditions.checkArgument(!Strings.isNullOrEmpty(location));
return String.format("%s (%s)", name, location);
}

/**
* Only used for mocking the {@link
* com.google.cloud.graphite.platforms.plugin.client.ContainerClient}. Constructs a {@link
* Cluster} from the given nameAndLocation value.
*
* @param nameAndLocation A non-empty String of the form "name (location)"
* @return A cluster with the name and location properties from the provided nameAndLocation.
*/
@VisibleForTesting
static Cluster fromNameAndLocation(String nameAndLocation) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(nameAndLocation));
String[] values = valuesFromNameAndLocation(nameAndLocation);
return new Cluster().setName(values[0]).setLocation(values[1]);
}
/**
* Only used for mocking the {@link
* com.google.cloud.graphite.platforms.plugin.client.ContainerClient}. Constructs a {@link
* Cluster} from the given nameAndLocation value.
*
* @param nameAndLocation A non-empty String of the form "name (location)"
* @return A cluster with the name and location properties from the provided nameAndLocation.
*/
@VisibleForTesting
static Cluster fromNameAndLocation(String nameAndLocation) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(nameAndLocation));
String[] values = valuesFromNameAndLocation(nameAndLocation);
return new Cluster().setName(values[0]).setLocation(values[1]);
}

/**
* Extracts the individual values from a combined nameAndLocation String.
*
* @param nameAndLocation A non-empty String of the form "name (location)"
* @return The String array {name, location} from the provided value.
*/
static String[] valuesFromNameAndLocation(String nameAndLocation) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(nameAndLocation));
String[] clusters = nameAndLocation.split(" [(]");
if (clusters.length != 2) {
throw new IllegalArgumentException("nameAndLocation should be of the form 'name (location)'");
/**
* Extracts the individual values from a combined nameAndLocation String.
*
* @param nameAndLocation A non-empty String of the form "name (location)"
* @return The String array {name, location} from the provided value.
*/
static String[] valuesFromNameAndLocation(String nameAndLocation) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(nameAndLocation));
String[] clusters = nameAndLocation.split(" [(]");
if (clusters.length != 2) {
throw new IllegalArgumentException("nameAndLocation should be of the form 'name (location)'");
}
clusters[1] = clusters[1].substring(0, clusters[1].length() - 1);
return clusters;
}
clusters[1] = clusters[1].substring(0, clusters[1].length() - 1);
return clusters;
}
}
216 changes: 104 additions & 112 deletions src/main/java/com/google/jenkins/plugins/k8sengine/CredentialsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,128 +35,120 @@

/** Provides a library of utility functions for credentials-related work. */
public class CredentialsUtil {
/**
* Get the Google Robot Credentials for the given credentialsId.
*
* @param itemGroup A handle to the Jenkins instance. Must be non-null.
* @param domainRequirements A list of domain requirements. Must be non-null.
* @param credentialsId The ID of the GoogleRobotCredentials to be retrieved from Jenkins and
* utilized for authorization. Must be non-empty or non-null and exist in credentials store.
* @return Google Robot Credential for the given credentialsId.
* @throws AbortException If there was an issue retrieving the Google Robot Credentials.
*/
public static GoogleRobotCredentials getRobotCredentials(
ItemGroup itemGroup,
ImmutableList<DomainRequirement> domainRequirements,
String credentialsId)
throws AbortException {
Preconditions.checkNotNull(itemGroup);
Preconditions.checkNotNull(domainRequirements);
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
/**
* Get the Google Robot Credentials for the given credentialsId.
*
* @param itemGroup A handle to the Jenkins instance. Must be non-null.
* @param domainRequirements A list of domain requirements. Must be non-null.
* @param credentialsId The ID of the GoogleRobotCredentials to be retrieved from Jenkins and
* utilized for authorization. Must be non-empty or non-null and exist in credentials store.
* @return Google Robot Credential for the given credentialsId.
* @throws AbortException If there was an issue retrieving the Google Robot Credentials.
*/
public static GoogleRobotCredentials getRobotCredentials(
ItemGroup itemGroup, ImmutableList<DomainRequirement> domainRequirements, String credentialsId)
throws AbortException {
Preconditions.checkNotNull(itemGroup);
Preconditions.checkNotNull(domainRequirements);
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));

GoogleRobotCredentials robotCreds =
CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(
GoogleRobotCredentials.class, itemGroup, ACL.SYSTEM, domainRequirements),
CredentialsMatchers.withId(credentialsId));
GoogleRobotCredentials robotCreds = CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(
GoogleRobotCredentials.class, itemGroup, ACL.SYSTEM, domainRequirements),
CredentialsMatchers.withId(credentialsId));

if (robotCreds == null) {
throw new AbortException(Messages.ClientFactory_FailedToRetrieveCredentials(credentialsId));
if (robotCreds == null) {
throw new AbortException(Messages.ClientFactory_FailedToRetrieveCredentials(credentialsId));
}

return robotCreds;
}

return robotCreds;
}
/**
* Get the Credential from the Google robot credentials for GKE access.
*
* @param robotCreds Google Robot Credential for desired service account.
* @return Google Credential for the service account.
* @throws AbortException if there was an error initializing HTTP transport.
*/
public static Credential getGoogleCredential(GoogleRobotCredentials robotCreds) throws AbortException {
Credential credential;
try {
credential = robotCreds.getGoogleCredential(new ContainerScopeRequirement());
} catch (GeneralSecurityException gse) {
throw new AbortException(Messages.ClientFactory_FailedToInitializeHTTPTransport(gse.getMessage()));
}

/**
* Get the Credential from the Google robot credentials for GKE access.
*
* @param robotCreds Google Robot Credential for desired service account.
* @return Google Credential for the service account.
* @throws AbortException if there was an error initializing HTTP transport.
*/
public static Credential getGoogleCredential(GoogleRobotCredentials robotCreds)
throws AbortException {
Credential credential;
try {
credential = robotCreds.getGoogleCredential(new ContainerScopeRequirement());
} catch (GeneralSecurityException gse) {
throw new AbortException(
Messages.ClientFactory_FailedToInitializeHTTPTransport(gse.getMessage()));
return credential;
}

return credential;
}

/**
* Given a credentialsId and Jenkins context, returns the access token.
*
* @param itemGroup A handle to the Jenkins instance. Must be non-null.
* @param credentialsId The service account credential's id. Must be non-null.
* @return Access token from OAuth to allow kubectl to interact with the cluster.
* @throws IOException If an error occurred fetching the access token.
*/
static String getAccessToken(ItemGroup itemGroup, String credentialsId) throws IOException {
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
Preconditions.checkNotNull(itemGroup);
GoogleRobotCredentials robotCreds =
getRobotCredentials(itemGroup, ImmutableList.of(), credentialsId);
/**
* Given a credentialsId and Jenkins context, returns the access token.
*
* @param itemGroup A handle to the Jenkins instance. Must be non-null.
* @param credentialsId The service account credential's id. Must be non-null.
* @return Access token from OAuth to allow kubectl to interact with the cluster.
* @throws IOException If an error occurred fetching the access token.
*/
static String getAccessToken(ItemGroup itemGroup, String credentialsId) throws IOException {
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
Preconditions.checkNotNull(itemGroup);
GoogleRobotCredentials robotCreds = getRobotCredentials(itemGroup, ImmutableList.of(), credentialsId);

Credential googleCredential = getGoogleCredential(robotCreds);
return getAccessToken(googleCredential);
}
/**
* Wrapper to get access token for service account with this credentialsId. Uses Jenkins.get() as
* context.
*
* @param credentialsId The service account credential's id. Must be non-null.
* @return Access token from OAuth to allow kubectl to interact with the cluster.
* @throws IOException If an error occurred fetching the access token.
*/
static String getAccessToken(String credentialsId) throws IOException {
return getAccessToken(Jenkins.get(), credentialsId);
}
Credential googleCredential = getGoogleCredential(robotCreds);
return getAccessToken(googleCredential);
}
/**
* Wrapper to get access token for service account with this credentialsId. Uses Jenkins.get() as
* context.
*
* @param credentialsId The service account credential's id. Must be non-null.
* @return Access token from OAuth to allow kubectl to interact with the cluster.
* @throws IOException If an error occurred fetching the access token.
*/
static String getAccessToken(String credentialsId) throws IOException {
return getAccessToken(Jenkins.get(), credentialsId);
}

/**
* Given the Google Credential, retrieve the access token.
*
* @param googleCredential Google Credential to get an access token. Must be non-null.
* @return Access token from OAuth to allow kubectl to interact with the cluster.
* @throws IOException If an error occured fetching the access token.
*/
static String getAccessToken(Credential googleCredential) throws IOException {
Preconditions.checkNotNull(googleCredential);
/**
* Given the Google Credential, retrieve the access token.
*
* @param googleCredential Google Credential to get an access token. Must be non-null.
* @return Access token from OAuth to allow kubectl to interact with the cluster.
* @throws IOException If an error occured fetching the access token.
*/
static String getAccessToken(Credential googleCredential) throws IOException {
Preconditions.checkNotNull(googleCredential);

googleCredential.refreshToken();
return googleCredential.getAccessToken();
}
googleCredential.refreshToken();
return googleCredential.getAccessToken();
}

/**
* Given a credentialsId and Jenkins context, return the default project ID for the service
* account credentials specified.
*
* @param itemGroup A handle to the Jenkins instance. Must be non-null.
* @param credentialsId The service account credential's ID. Must be non-null.
* @return The project ID specified when creating the credential in the Jenkins credential store.
* @throws AbortException If an error occurred fetching the credential.
*/
static String getDefaultProjectId(ItemGroup itemGroup, String credentialsId)
throws AbortException {
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
Preconditions.checkNotNull(itemGroup);
GoogleRobotCredentials robotCreds =
getRobotCredentials(itemGroup, ImmutableList.of(), credentialsId);
return Strings.isNullOrEmpty(robotCreds.getProjectId()) ? "" : robotCreds.getProjectId();
}
/**
* Given a credentialsId and Jenkins context, return the default project ID for the service
* account credentials specified.
*
* @param itemGroup A handle to the Jenkins instance. Must be non-null.
* @param credentialsId The service account credential's ID. Must be non-null.
* @return The project ID specified when creating the credential in the Jenkins credential store.
* @throws AbortException If an error occurred fetching the credential.
*/
static String getDefaultProjectId(ItemGroup itemGroup, String credentialsId) throws AbortException {
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
Preconditions.checkNotNull(itemGroup);
GoogleRobotCredentials robotCreds = getRobotCredentials(itemGroup, ImmutableList.of(), credentialsId);

Check warning on line 139 in src/main/java/com/google/jenkins/plugins/k8sengine/CredentialsUtil.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 79-139 are not covered by tests
return Strings.isNullOrEmpty(robotCreds.getProjectId()) ? "" : robotCreds.getProjectId();
}

/**
* Wrapper to get the default project ID for a credential using Jenkins.get() as the context.
*
* @param credentialsId The service account credential's ID. Must be non-null.
* @return The project ID specified when creating the credential in the Jenkins credential store.
* @throws AbortException If an error occurred fetching the credential.
*/
static String getDefaultProjectId(String credentialsId) throws AbortException {
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
return getDefaultProjectId(Jenkins.get(), credentialsId);
}
/**
* Wrapper to get the default project ID for a credential using Jenkins.get() as the context.
*
* @param credentialsId The service account credential's ID. Must be non-null.
* @return The project ID specified when creating the credential in the Jenkins credential store.
* @throws AbortException If an error occurred fetching the credential.
*/
static String getDefaultProjectId(String credentialsId) throws AbortException {
Preconditions.checkArgument(!Strings.isNullOrEmpty(credentialsId));
return getDefaultProjectId(Jenkins.get(), credentialsId);
}
}

0 comments on commit 20786a9

Please sign in to comment.