Skip to content

Commit

Permalink
Merge pull request #417 from aneveux/google-credential-tests
Browse files Browse the repository at this point in the history
Adapt tests to updated version of google-oauth-plugin
  • Loading branch information
jtnord authored Oct 25, 2023
2 parents d1c9429 + 40df285 commit 86bbaa7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 29 deletions.
11 changes: 6 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- TODO: Remove the version here when it is integrated in the BOM -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>google-oauth-plugin</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -154,11 +160,6 @@
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-text-api</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud.graphite</groupId>
<artifactId>gcp-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.Assert.assertThrows;

import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.SecretBytes;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
Expand Down Expand Up @@ -61,17 +62,18 @@ public void construction() throws Exception {
serviceAccountConfig.setSecretJsonKey(bytes);
assertNotNull(serviceAccountConfig.getAccountId());
// Create a credential
Credentials c =
(Credentials) new GoogleRobotPrivateKeyCredentials(ACCOUNT_ID, serviceAccountConfig, null);
final String credentialId = "test-credential-id";
GoogleRobotPrivateKeyCredentials c = new GoogleRobotPrivateKeyCredentials(CredentialsScope.GLOBAL, credentialId, ACCOUNT_ID,
serviceAccountConfig, null);
CredentialsStore store = new SystemCredentialsProvider.ProviderImpl().getStore(r.jenkins);
assertNotNull(store);
store.addCredentials(Domain.global(), c);

// Add a few InstanceConfigurations
List<InstanceConfiguration> ics = new ArrayList<>();
ics.add(instanceConfigurationBuilder().build());
ics.add(instanceConfigurationBuilder().build());
ComputeEngineCloud cloud =
new ComputeEngineCloud(CLOUD_NAME, PROJECT_ID, PROJECT_ID, INSTANCE_CAP_STR);
ComputeEngineCloud cloud = new ComputeEngineCloud(CLOUD_NAME, PROJECT_ID, credentialId, INSTANCE_CAP_STR);
cloud.setInstanceId(INSTANCE_ID);
cloud.setConfigurations(ics);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,16 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;

import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.SecretBytes;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotPrivateKeyCredentials;
import com.google.jenkins.plugins.credentials.oauth.JsonServiceAccountConfig;
import hudson.model.Node;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import java.nio.charset.StandardCharsets;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class ConfigAsCodeTest {

private static final byte[] PK_BYTES =
"{\"client_email\": \"[email protected]\"}".getBytes(StandardCharsets.UTF_8);

@Rule public JenkinsRule jenkinsRule = new JenkinsConfiguredWithCodeRule();

@Test
Expand Down Expand Up @@ -64,16 +54,6 @@ public void shouldCreateCloudInstanceFromCode() {
@Test
@ConfiguredWithCode("configuration-as-code.yml")
public void shouldCreateGCEClientFromCode() throws Exception {
SecretBytes bytes = SecretBytes.fromBytes(PK_BYTES);
JsonServiceAccountConfig serviceAccountConfig = new JsonServiceAccountConfig();
serviceAccountConfig.setSecretJsonKey(bytes);
assertNotNull(serviceAccountConfig.getAccountId());
Credentials credentials =
new GoogleRobotPrivateKeyCredentials("gce-jenkins", serviceAccountConfig, null);

CredentialsStore store =
new SystemCredentialsProvider.ProviderImpl().getStore(jenkinsRule.jenkins);
store.addCredentials(Domain.global(), credentials);

ComputeEngineCloud cloud =
(ComputeEngineCloud) jenkinsRule.jenkins.clouds.getByName("gce-jenkins-build");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,14 @@ jenkins:
bootDiskSizeGbStr: 50
bootDiskAutoDelete: true
serviceAccountEmail: '[email protected]'
credentials:
system:
domainCredentials:
- credentials:
- googleRobotPrivateKeyCredentials:
id: gce-jenkins
projectId: gce-jenkins
serviceAccountConfig:
jsonServiceAccountConfig:
# The encoded version of {"client_email": "[email protected]"}
secretJsonKey: 'eyJjbGllbnRfZW1haWwiOiAiZXhhbXBsZUBleGFtcGxlLmNvbSJ9'

0 comments on commit 86bbaa7

Please sign in to comment.