From 3b54f03b474f93f28af835dc9247818d336a7762 Mon Sep 17 00:00:00 2001 From: Stephen Shank Date: Thu, 4 Apr 2019 17:25:07 -0700 Subject: [PATCH 01/17] Use DataboundSetter throughout except for final field emailAddress for p12 keys. --- .../GoogleRobotPrivateKeyCredentials.java | 3 +- .../oauth/JsonServiceAccountConfig.java | 75 +++++++++++------- .../oauth/P12ServiceAccountConfig.java | 77 +++++++++++++------ .../GoogleRobotPrivateKeyCredentialsTest.java | 15 ++-- .../oauth/JsonServiceAccountConfigTest.java | 51 ++++++------ .../oauth/P12ServiceAccountConfigTest.java | 50 ++++++------ 6 files changed, 160 insertions(+), 111 deletions(-) diff --git a/src/main/java/com/google/jenkins/plugins/credentials/oauth/GoogleRobotPrivateKeyCredentials.java b/src/main/java/com/google/jenkins/plugins/credentials/oauth/GoogleRobotPrivateKeyCredentials.java index f037ea7..557ed3e 100644 --- a/src/main/java/com/google/jenkins/plugins/credentials/oauth/GoogleRobotPrivateKeyCredentials.java +++ b/src/main/java/com/google/jenkins/plugins/credentials/oauth/GoogleRobotPrivateKeyCredentials.java @@ -73,8 +73,7 @@ public GoogleRobotPrivateKeyCredentials(String projectId, public Object readResolve() { if (serviceAccountConfig == null) { String clientEmail = getClientEmailFromSecretsFileAndLogErrors(); - serviceAccountConfig = new P12ServiceAccountConfig(clientEmail, null, - p12File); + serviceAccountConfig = new P12ServiceAccountConfig(clientEmail, p12File); } return this; } diff --git a/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java b/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java index 0b06583..4afb1b7 100644 --- a/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java +++ b/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java @@ -15,6 +15,8 @@ */ package com.google.jenkins.plugins.credentials.oauth; +import com.google.api.client.util.Strings; +import com.google.common.annotations.VisibleForTesting; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; @@ -42,6 +44,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import hudson.Extension; import jenkins.model.Jenkins; +import org.kohsuke.stapler.DataBoundSetter; /** * Provides authentication mechanism for a service account by setting a .json @@ -67,44 +70,53 @@ public class JsonServiceAccountConfig extends ServiceAccountConfig { private SecretBytes secretJsonKey; @Deprecated // for migration purpose @CheckForNull - private transient String jsonKeyFile; + private transient String prevJsonKeyFile; private transient JsonKey jsonKey; - /** - * @param jsonKeyFile uploaded json key file - * @param filename - * previous json key file name. - * used if jsonKeyFile is not provided. - * @param secretJsonKey - * previous json key file content. - * used if jsonKeyFile is not provided. - * @since 0.7 - */ @DataBoundConstructor - public JsonServiceAccountConfig(FileItem jsonKeyFile, - String filename, SecretBytes secretJsonKey) { + public JsonServiceAccountConfig() {} + + /**@param jsonKeyFile uploaded json key file */ + @DataBoundSetter // Called on form submission, only used when key file is uploaded + public void setJsonKeyFile(FileItem jsonKeyFile) { if (jsonKeyFile != null && jsonKeyFile.getSize() > 0) { try { JsonKey jsonKey = JsonKey.load(new JacksonFactory(), - jsonKeyFile.getInputStream()); + jsonKeyFile.getInputStream()); if (jsonKey.getClientEmail() != null && - jsonKey.getPrivateKey() != null) { + jsonKey.getPrivateKey() != null) { this.filename = extractFilename(jsonKeyFile.getName()); this.secretJsonKey = SecretBytes.fromBytes(jsonKeyFile.get()); } } catch (IOException e) { LOGGER.log(Level.SEVERE, "Failed to read json key from file", e); } - } else { - this.filename = extractFilename(filename); + } + } + + /** @param filename json key file name.*/ + @DataBoundSetter + public void setFilename(String filename) { + String newFilename = extractFilename(filename); + if (!Strings.isNullOrEmpty(newFilename)) { + this.filename = newFilename; + } + } + + /** @param secretJsonKey json key file content.*/ + @DataBoundSetter + public void setSecretJsonKey(SecretBytes secretJsonKey) { + if (secretJsonKey != null && secretJsonKey.getPlainData().length > 0) { this.secretJsonKey = secretJsonKey; } } + @Deprecated - public JsonServiceAccountConfig(FileItem jsonKeyFile, - String prevJsonKeyFile) { - this(null, prevJsonKeyFile, getSecretBytesFromFile(prevJsonKeyFile)); + // Used for JsonServiceAccountConfig + public JsonServiceAccountConfig(String prevJsonKeyFile) { + this.filename = extractFilename(prevJsonKeyFile); + this.secretJsonKey = getSecretBytesFromFile(prevJsonKeyFile); } @Deprecated // used only for compatibility purpose @@ -139,8 +151,7 @@ private Object readResolve() { if (secretJsonKey == null) { // google-oauth-plugin < 0.7 return new JsonServiceAccountConfig( - null, - getJsonKeyFile() + getPrevJsonKeyFile() ); } return this; @@ -168,8 +179,18 @@ public SecretBytes getSecretJsonKey() { } @Deprecated - public String getJsonKeyFile() { - return jsonKeyFile; + public String getPrevJsonKeyFile() { + return prevJsonKeyFile; + } + + /** + * For use in UI, do not use. + * @return The uploaded json key file + */ + @Deprecated + @Restricted(DoNotUse.class) // Required by stapler to call setJsonKeyFile above. + public FileItem getJsonKeyFile() { + return null; } @Override @@ -193,11 +214,7 @@ public PrivateKey getPrivateKey() { PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(section.getBase64DecodedBytes()); return KeyFactory.getInstance("RSA").generatePrivate(keySpec); - } catch (IOException e) { - LOGGER.log(Level.SEVERE, "Failed to read private key", e); - } catch (InvalidKeySpecException e) { - LOGGER.log(Level.SEVERE, "Failed to read private key", e); - } catch (NoSuchAlgorithmException e) { + } catch (IOException | InvalidKeySpecException | NoSuchAlgorithmException e) { LOGGER.log(Level.SEVERE, "Failed to read private key", e); } } diff --git a/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java b/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java index 2401b50..a40b1b2 100644 --- a/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java +++ b/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java @@ -15,6 +15,7 @@ */ package com.google.jenkins.plugins.credentials.oauth; +import com.google.api.client.util.Strings; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; @@ -42,6 +43,8 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import hudson.Extension; import jenkins.model.Jenkins; +import org.kohsuke.stapler.DataBoundSetter; +import org.kohsuke.stapler.QueryParameter; /** * provides authentication mechanism for a service account by setting a service @@ -60,37 +63,52 @@ public class P12ServiceAccountConfig extends ServiceAccountConfig { private SecretBytes secretP12Key; @Deprecated // for migration purpose @CheckForNull - private transient String p12KeyFile; + private transient String prevP12KeyFile; /** * @param emailAddress email address - * @param p12KeyFile uploaded p12 key file - * @param filename - * previous json key file name. - * used if p12KeyFile is not provided. - * @param secretP12Key - * previous p12 key file content. - * used if p12KeyFile is not provided. * @since 0.7 */ @DataBoundConstructor - public P12ServiceAccountConfig(String emailAddress, FileItem p12KeyFile, - String filename, SecretBytes secretP12Key) { + public P12ServiceAccountConfig(String emailAddress) { this.emailAddress = emailAddress; + } + + /** + * @param emailAddress email address + * @param prevP12KeyFile The path of the previous p12 key file + */ + @Deprecated + public P12ServiceAccountConfig(String emailAddress, String prevP12KeyFile) { + this(emailAddress); + this.setFilename(prevP12KeyFile); + this.setSecretP12Key(getSecretBytesFromFile(prevP12KeyFile)); + } + + /** @param p12KeyFile uploaded p12 key file */ + @Deprecated + @DataBoundSetter // Called on form submission, only used when credentials are uploaded. + public void setP12KeyFile(FileItem p12KeyFile) { if (p12KeyFile != null && p12KeyFile.getSize() > 0) { this.filename = extractFilename(p12KeyFile.getName()); this.secretP12Key = SecretBytes.fromBytes(p12KeyFile.get()); - } else { + } + } + + /** @param filename previous json key file name. */ + @DataBoundSetter + public void setFilename(String filename) { + if (!Strings.isNullOrEmpty(filename)) { this.filename = extractFilename(filename); - this.secretP12Key = secretP12Key; } } - @Deprecated - public P12ServiceAccountConfig(String emailAddress, FileItem p12KeyFile, - String prevP12KeyFile) { - this(emailAddress, p12KeyFile, - prevP12KeyFile, getSecretBytesFromFile(prevP12KeyFile)); + /** @param secretP12Key previous p12 key file content.*/ + @DataBoundSetter + public void setSecretP12Key(SecretBytes secretP12Key) { + if (secretP12Key != null && secretP12Key.getPlainData().length > 0) { + this.secretP12Key = secretP12Key; + } } @Deprecated // used only for compatibility purpose @@ -126,8 +144,7 @@ private Object readResolve() { // google-oauth-plugin < 0.7 return new P12ServiceAccountConfig( getEmailAddress(), - null, - getP12KeyFile() + getPrevP12KeyFile() ); } return this; @@ -152,15 +169,31 @@ public String getFilename() { return filename; } - @Restricted(DoNotUse.class) // for UI purpose only + /** + * Do not use, required for UI. + * + * @return secretP12Key + */ + @Restricted(DoNotUse.class) // Required by stapler for being able to call setSecretP12Key @CheckForNull public SecretBytes getSecretP12Key() { return secretP12Key; } + /** @return the path of the previous p12 key file. */ @Deprecated - public String getP12KeyFile() { - return p12KeyFile; + public String getPrevP12KeyFile() { + return prevP12KeyFile; + } + + /** + * Do not use, required for UI. + * @return The uploaded p12 key file + */ + @Deprecated + @Restricted(DoNotUse.class) // Required by stapler for being able to call setP12KeyFile. + public FileItem getP12KeyFile() { + return null; } @Override diff --git a/src/test/java/com/google/jenkins/plugins/credentials/oauth/GoogleRobotPrivateKeyCredentialsTest.java b/src/test/java/com/google/jenkins/plugins/credentials/oauth/GoogleRobotPrivateKeyCredentialsTest.java index 0bbbf12..acda82e 100644 --- a/src/test/java/com/google/jenkins/plugins/credentials/oauth/GoogleRobotPrivateKeyCredentialsTest.java +++ b/src/test/java/com/google/jenkins/plugins/credentials/oauth/GoogleRobotPrivateKeyCredentialsTest.java @@ -118,10 +118,10 @@ public void testCreatePrivateKeyCredentialsWithJsonKeyType() .thenReturn(new FileInputStream(jsonKeyPath)); when(mockFileItem.get()) .thenReturn(FileUtils.readFileToByteArray(new File(jsonKeyPath))); + JsonServiceAccountConfig jsonServiceAccountConfig = new JsonServiceAccountConfig(); + jsonServiceAccountConfig.setJsonKeyFile(mockFileItem); GoogleRobotPrivateKeyCredentials credentials = - new GoogleRobotPrivateKeyCredentials(PROJECT_ID, - new JsonServiceAccountConfig( - mockFileItem, null, null), module); + new GoogleRobotPrivateKeyCredentials(PROJECT_ID, jsonServiceAccountConfig, module); assertEquals(CredentialsScope.GLOBAL, credentials.getScope()); assertEquals(SERVICE_ACCOUNT_EMAIL_ADDRESS, credentials.getUsername()); @@ -153,7 +153,8 @@ public void testCreatePrivateKeyCredentialsWithP12KeyType() throws Exception { when(mockFileItem.get()) .thenReturn(FileUtils.readFileToByteArray(new File(p12KeyPath))); P12ServiceAccountConfig keyType = new P12ServiceAccountConfig( - SERVICE_ACCOUNT_EMAIL_ADDRESS, mockFileItem, null, null); + SERVICE_ACCOUNT_EMAIL_ADDRESS); + keyType.setP12KeyFile(mockFileItem); GoogleRobotPrivateKeyCredentials credentials = new GoogleRobotPrivateKeyCredentials(PROJECT_ID, keyType, module); @@ -390,10 +391,10 @@ public void testGetById() throws Exception { .thenReturn(new FileInputStream(jsonKeyPath)); when(mockFileItem.get()) .thenReturn(FileUtils.readFileToByteArray(new File(jsonKeyPath))); + JsonServiceAccountConfig jsonServiceAccountConfig = new JsonServiceAccountConfig(); + jsonServiceAccountConfig.setJsonKeyFile(mockFileItem); GoogleRobotPrivateKeyCredentials credentials = - new GoogleRobotPrivateKeyCredentials(PROJECT_ID, - new JsonServiceAccountConfig( - mockFileItem, null, null), null); + new GoogleRobotPrivateKeyCredentials(PROJECT_ID, jsonServiceAccountConfig, null); SystemCredentialsProvider.getInstance().getCredentials().add(credentials); diff --git a/src/test/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfigTest.java b/src/test/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfigTest.java index d6a738a..ac90a58 100644 --- a/src/test/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfigTest.java +++ b/src/test/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfigTest.java @@ -41,8 +41,7 @@ * Tests for {@link JsonServiceAccountConfig}. */ public class JsonServiceAccountConfigTest { - private static final String SERVICE_ACCOUNT_EMAIL_ADDRESS = - "service@account.com"; + private static final String SERVICE_ACCOUNT_EMAIL_ADDRESS = "service@account.com"; private static PrivateKey privateKey; private static String jsonKeyPath; @Rule @@ -58,7 +57,7 @@ public static void preparePrivateKey() throws Exception { } @Before - public void setUp() throws Exception { + public void setUp() { MockitoAnnotations.initMocks(this); } @@ -70,17 +69,16 @@ public void testCreateJsonKeyTypeWithNewJsonKeyFile() throws Exception { when(mockFileItem.getName()).thenReturn(jsonKeyPath); when(mockFileItem.get()) .thenReturn(FileUtils.readFileToByteArray(new File(jsonKeyPath))); - JsonServiceAccountConfig jsonKeyType = - new JsonServiceAccountConfig(mockFileItem, null, null); + JsonServiceAccountConfig jsonKeyType = new JsonServiceAccountConfig(); + jsonKeyType.setJsonKeyFile(mockFileItem); assertEquals(SERVICE_ACCOUNT_EMAIL_ADDRESS, jsonKeyType.getAccountId()); assertEquals(privateKey, jsonKeyType.getPrivateKey()); } @Test - public void testCreateJsonKeyTypeWithNullParameters() throws Exception { - JsonServiceAccountConfig jsonServiceAccountConfig = - new JsonServiceAccountConfig(null, null, null); + public void testCreateJsonKeyTypeWithNullParameters() { + JsonServiceAccountConfig jsonServiceAccountConfig = new JsonServiceAccountConfig(); assertNull(jsonServiceAccountConfig.getAccountId()); assertNull(jsonServiceAccountConfig.getPrivateKey()); @@ -89,10 +87,10 @@ public void testCreateJsonKeyTypeWithNullParameters() throws Exception { @Test public void testCreateJsonKeyTypeWithEmptyJsonKeyFile() throws Exception { when(mockFileItem.getSize()).thenReturn(0L); - JsonServiceAccountConfig jsonKeyType = new JsonServiceAccountConfig - (mockFileItem, null); + JsonServiceAccountConfig jsonKeyType = new JsonServiceAccountConfig(null); + jsonKeyType.setJsonKeyFile(mockFileItem); - assertNull(jsonKeyType.getJsonKeyFile()); + assertNull(jsonKeyType.getPrevJsonKeyFile()); assertNull(jsonKeyType.getAccountId()); assertNull(jsonKeyType.getPrivateKey()); } @@ -107,17 +105,16 @@ public void testCreateJsonKeyTypeWithInvalidJsonKeyFile() throws Exception { when(mockFileItem.get()) .thenReturn(bytes); JsonServiceAccountConfig jsonServiceAccountConfig = - new JsonServiceAccountConfig(mockFileItem, null, null); + new JsonServiceAccountConfig(); + jsonServiceAccountConfig.setJsonKeyFile(mockFileItem); assertNull(jsonServiceAccountConfig.getAccountId()); assertNull(jsonServiceAccountConfig.getPrivateKey()); } @Test - public void testCreateJsonKeyTypeWithPrevJsonKeyFileForCompatibility() - throws Exception { - JsonServiceAccountConfig jsonServiceAccountConfig = - new JsonServiceAccountConfig(null, jsonKeyPath); + public void testCreateJsonKeyTypeWithPrevJsonKeyFileForCompatibility() { + JsonServiceAccountConfig jsonServiceAccountConfig = new JsonServiceAccountConfig(jsonKeyPath); assertEquals(SERVICE_ACCOUNT_EMAIL_ADDRESS, jsonServiceAccountConfig.getAccountId()); @@ -128,8 +125,9 @@ public void testCreateJsonKeyTypeWithPrevJsonKeyFileForCompatibility() public void testCreateJsonKeyTypeWithPrevJsonKeyFile() throws Exception { SecretBytes prev = SecretBytes .fromBytes(FileUtils.readFileToByteArray(new File(jsonKeyPath))); - JsonServiceAccountConfig jsonServiceAccountConfig = - new JsonServiceAccountConfig(null, jsonKeyPath, prev); + JsonServiceAccountConfig jsonServiceAccountConfig = new JsonServiceAccountConfig(); + jsonServiceAccountConfig.setFilename(jsonKeyPath); + jsonServiceAccountConfig.setSecretJsonKey(prev); assertEquals(SERVICE_ACCOUNT_EMAIL_ADDRESS, jsonServiceAccountConfig.getAccountId()); @@ -137,21 +135,20 @@ public void testCreateJsonKeyTypeWithPrevJsonKeyFile() throws Exception { } @Test - public void testCreateJsonKeyTypeWithEmptyPrevJsonKeyFile() throws Exception { + public void testCreateJsonKeyTypeWithEmptyPrevJsonKeyFile() { SecretBytes prev = SecretBytes.fromString(""); - JsonServiceAccountConfig jsonServiceAccountConfig = - new JsonServiceAccountConfig(null, "", prev); + JsonServiceAccountConfig jsonServiceAccountConfig = new JsonServiceAccountConfig(); + jsonServiceAccountConfig.setFilename(""); + jsonServiceAccountConfig.setSecretJsonKey(prev); assertNull(jsonServiceAccountConfig.getAccountId()); assertNull(jsonServiceAccountConfig.getPrivateKey()); } @Test - public void testCreateJsonKeyTypeWithInvalidPrevJsonKeyFile() - throws Exception { - String invalidPrevJsonKeyFile = "invalidPrevJsonKeyFile.json"; + public void testCreateJsonKeyTypeWithInvalidPrevJsonKeyFile() { JsonServiceAccountConfig jsonServiceAccountConfig = - new JsonServiceAccountConfig(null, invalidPrevJsonKeyFile, null); + new JsonServiceAccountConfig("invalidPrevJsonKeyFile.json"); assertNull(jsonServiceAccountConfig.getAccountId()); assertNull(jsonServiceAccountConfig.getPrivateKey()); @@ -165,8 +162,8 @@ public void testSerialization() throws Exception { .thenReturn(new FileInputStream(jsonKeyPath)); when(mockFileItem.get()) .thenReturn(FileUtils.readFileToByteArray(new File(jsonKeyPath))); - JsonServiceAccountConfig jsonServiceAccountConfig = - new JsonServiceAccountConfig(mockFileItem, null, null); + JsonServiceAccountConfig jsonServiceAccountConfig = new JsonServiceAccountConfig(); + jsonServiceAccountConfig.setJsonKeyFile(mockFileItem); ByteArrayOutputStream out = new ByteArrayOutputStream(); SerializationUtil.serialize(jsonServiceAccountConfig, out); diff --git a/src/test/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfigTest.java b/src/test/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfigTest.java index e30f844..581cb1e 100644 --- a/src/test/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfigTest.java +++ b/src/test/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfigTest.java @@ -68,8 +68,8 @@ public void testCreateWithNewP12KeyFile() throws Exception { when(mockFileItem.get()) .thenReturn(FileUtils.readFileToByteArray(new File(p12KeyPath))); P12ServiceAccountConfig p12ServiceAccountConfig = - new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS, - mockFileItem, null, null); + new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS); + p12ServiceAccountConfig.setP12KeyFile(mockFileItem); assertEquals(SERVICE_ACCOUNT_EMAIL_ADDRESS, p12ServiceAccountConfig .getAccountId()); @@ -81,7 +81,9 @@ public void testCreateWithNullAccountId() throws Exception { SecretBytes prev = SecretBytes.fromBytes( FileUtils.readFileToByteArray(new File(p12KeyPath))); P12ServiceAccountConfig p12ServiceAccountConfig = - new P12ServiceAccountConfig(null, null, p12KeyPath, prev); + new P12ServiceAccountConfig(null); + p12ServiceAccountConfig.setFilename(p12KeyPath); + p12ServiceAccountConfig.setSecretP12Key(prev); assertNull(p12ServiceAccountConfig.getAccountId()); assertEquals(keyPair.getPrivate(), p12ServiceAccountConfig.getPrivateKey()); @@ -89,10 +91,9 @@ public void testCreateWithNullAccountId() throws Exception { @Test @WithoutJenkins - public void testCreateWithNullP12KeyFile() throws Exception { + public void testCreateWithNullP12KeyFile() { P12ServiceAccountConfig p12ServiceAccountConfig = - new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS, null, - null, null); + new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS); assertEquals(SERVICE_ACCOUNT_EMAIL_ADDRESS, p12ServiceAccountConfig.getAccountId()); @@ -105,8 +106,8 @@ public void testCreateWithEmptyP12KeyFile() throws Exception { when(mockFileItem.getSize()).thenReturn(0L); when(mockFileItem.get()).thenReturn(new byte[]{}); P12ServiceAccountConfig p12ServiceAccountConfig = - new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS, - mockFileItem, null, null); + new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS); + p12ServiceAccountConfig.setP12KeyFile(mockFileItem); assertEquals(SERVICE_ACCOUNT_EMAIL_ADDRESS, p12ServiceAccountConfig.getAccountId()); @@ -114,15 +115,15 @@ public void testCreateWithEmptyP12KeyFile() throws Exception { } @Test - public void testCreateWithInvalidP12KeyFile() throws Exception { + public void testCreateWithInvalidP12KeyFile() { byte[] bytes = "invalidP12KeyFile".getBytes(); when(mockFileItem.getSize()).thenReturn((long) bytes.length); when(mockFileItem.getName()).thenReturn("invalidP12KeyFile"); when(mockFileItem.get()) .thenReturn(bytes); P12ServiceAccountConfig p12ServiceAccountConfig = - new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS, - mockFileItem, null, null); + new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS); + p12ServiceAccountConfig.setP12KeyFile(mockFileItem); assertEquals(SERVICE_ACCOUNT_EMAIL_ADDRESS, p12ServiceAccountConfig.getAccountId()); @@ -130,10 +131,9 @@ public void testCreateWithInvalidP12KeyFile() throws Exception { } @Test - public void testCreateWithPrevP12KeyFileForCompatibility() throws Exception { + public void testCreateWithPrevP12KeyFileForCompatibility() { P12ServiceAccountConfig p12ServiceAccountConfig = - new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS, null, - p12KeyPath); + new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS, p12KeyPath); assertEquals(SERVICE_ACCOUNT_EMAIL_ADDRESS, p12ServiceAccountConfig.getAccountId()); @@ -145,8 +145,9 @@ public void testCreateWithPrevP12KeyFile() throws Exception { SecretBytes prev = SecretBytes.fromBytes( FileUtils.readFileToByteArray(new File(p12KeyPath))); P12ServiceAccountConfig p12ServiceAccountConfig = - new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS, null, - p12KeyPath, prev); + new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS); + p12ServiceAccountConfig.setFilename(p12KeyPath); + p12ServiceAccountConfig.setSecretP12Key(prev); assertEquals(SERVICE_ACCOUNT_EMAIL_ADDRESS, p12ServiceAccountConfig.getAccountId()); @@ -154,11 +155,12 @@ public void testCreateWithPrevP12KeyFile() throws Exception { } @Test - public void testCreateWithEmptyPrevP12KeyFile() throws Exception { + public void testCreateWithEmptyPrevP12KeyFile() { SecretBytes prev = SecretBytes.fromString(""); P12ServiceAccountConfig p12ServiceAccountConfig = - new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS, null, - "", prev); + new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS); + p12ServiceAccountConfig.setFilename(""); + p12ServiceAccountConfig.setSecretP12Key(prev); assertEquals(SERVICE_ACCOUNT_EMAIL_ADDRESS, p12ServiceAccountConfig.getAccountId()); @@ -167,10 +169,10 @@ public void testCreateWithEmptyPrevP12KeyFile() throws Exception { @Test @WithoutJenkins - public void testCreateWithInvalidPrevP12KeyFile() throws Exception { + public void testCreateWithInvalidPrevP12KeyFile() { P12ServiceAccountConfig p12ServiceAccountConfig = - new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS, null, - "invalidPrevP12KeyFile.p12", null); + new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS); + p12ServiceAccountConfig.setFilename("invalidPrevP12KeyFile.p12"); assertEquals(SERVICE_ACCOUNT_EMAIL_ADDRESS, p12ServiceAccountConfig.getAccountId()); @@ -184,8 +186,8 @@ public void testSerialization() throws Exception { when(mockFileItem.get()) .thenReturn(FileUtils.readFileToByteArray(new File(p12KeyPath))); P12ServiceAccountConfig p12ServiceAccountConfig = - new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS, - mockFileItem, null, null); + new P12ServiceAccountConfig(SERVICE_ACCOUNT_EMAIL_ADDRESS); + p12ServiceAccountConfig.setP12KeyFile(mockFileItem); ByteArrayOutputStream out = new ByteArrayOutputStream(); SerializationUtil.serialize(p12ServiceAccountConfig, out); From 17d8275c0e805821a0c1dcc0ff5a3b3cf14eb36c Mon Sep 17 00:00:00 2001 From: Stephen Shank Date: Thu, 4 Apr 2019 21:57:49 -0700 Subject: [PATCH 02/17] Update dependencies required for using JCasC plugin in tests. --- pom.xml | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 6e1adf6..8b5ee1a 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ org.jenkins-ci.plugins plugin - 3.8 + 3.36 - 1.653 + 2.60.3 1.24.1 - 7 + 8 findbugs-exclude.xml Max Medium + 1.9 @@ -176,6 +178,25 @@ 1.8.4 test + + io.jenkins + configuration-as-code + ${configuration-as-code.version} + test + + + io.jenkins + configuration-as-code + ${configuration-as-code.version} + tests + test + + + io.jenkins.configuration-as-code + configuration-as-code-support + ${configuration-as-code.version} + test + @@ -212,11 +233,20 @@ + + org.jenkins-ci.plugins + plain-credentials + 1.5 + + + org.jenkins-ci.plugins + ssh-credentials + 1.13 + org.jenkins-ci.plugins credentials - 2.1.12 - compile + 2.1.16 + true io.jenkins @@ -190,12 +193,28 @@ ${configuration-as-code.version} tests test + true io.jenkins.configuration-as-code configuration-as-code-support ${configuration-as-code.version} test + true + + + org.jenkins-ci.plugins + plain-credentials + 1.5 + test + true + + + org.jenkins-ci.plugins + ssh-credentials + 1.13 + test + true @@ -233,20 +252,10 @@ - - org.jenkins-ci.plugins - plain-credentials - 1.5 - - - org.jenkins-ci.plugins - ssh-credentials - 1.13 - org.jenkins-ci.plugins credentials - 2.1.16 + ${credentials.version} com.google.guava From 499364358ddf71ebe6d47590a57b57f9d69ad3a4 Mon Sep 17 00:00:00 2001 From: Stephen Shank Date: Wed, 10 Apr 2019 10:55:27 -0700 Subject: [PATCH 12/17] Add eof new line to test-key.json --- .../com/google/jenkins/plugins/credentials/oauth/test-key.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/com/google/jenkins/plugins/credentials/oauth/test-key.json b/src/test/resources/com/google/jenkins/plugins/credentials/oauth/test-key.json index e0d8aba..569108f 100644 --- a/src/test/resources/com/google/jenkins/plugins/credentials/oauth/test-key.json +++ b/src/test/resources/com/google/jenkins/plugins/credentials/oauth/test-key.json @@ -9,4 +9,4 @@ "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test-account%40test-project.iam.gserviceaccount.com" -} \ No newline at end of file +} From c02b5621e55a9202388a83baec7adda326f83a55 Mon Sep 17 00:00:00 2001 From: Stephen Shank Date: Wed, 10 Apr 2019 11:27:00 -0700 Subject: [PATCH 13/17] Add missing javadoc on JsonServiceAccountConfig. Use correct capitalization and punctuation in javadoc. --- .../oauth/JsonServiceAccountConfig.java | 27 +++++++++++++------ .../oauth/P12ServiceAccountConfig.java | 27 ++++++++++--------- .../oauth/ConfigurationAsCodeTest.java | 2 +- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java b/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java index 6c0a7d2..9307079 100644 --- a/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java +++ b/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java @@ -80,8 +80,8 @@ public JsonServiceAccountConfig() {} * For being able to load credentials created with versions < 0.8 * and backwards compatibility with external callers. * - * @param jsonKeyFile The uploaded json key file - * @param prevJsonKeyFile The path of the previous json key file + * @param jsonKeyFile The uploaded json key file. + * @param prevJsonKeyFile The path of the previous json key file. * @since 0.3 */ @Deprecated @@ -94,7 +94,7 @@ public JsonServiceAccountConfig( } } - /**@param jsonKeyFileUpload uploaded json key file */ + /** @param jsonKeyFileUpload The uploaded json key file. */ @DataBoundSetter // Called on form submit, only used when key file is uploaded public void setJsonKeyFileUpload(FileItem jsonKeyFileUpload) { if (jsonKeyFileUpload != null && jsonKeyFileUpload.getSize() > 0) { @@ -112,7 +112,7 @@ public void setJsonKeyFileUpload(FileItem jsonKeyFileUpload) { } } - /** @param filename json key file name.*/ + /** @param filename The json key file name. */ @DataBoundSetter public void setFilename(String filename) { String newFilename = extractFilename(filename); @@ -121,7 +121,7 @@ public void setFilename(String filename) { } } - /** @param secretJsonKey json key file content.*/ + /** @param secretJsonKey The json key file content. */ @DataBoundSetter public void setSecretJsonKey(SecretBytes secretJsonKey) { if (secretJsonKey != null && secretJsonKey.getPlainData().length > 0) { @@ -175,7 +175,7 @@ public DescriptorImpl getDescriptor() { } /** - * @return Original uploaded file name + * @return Original uploaded file name. * @since 0.7 */ @CheckForNull @@ -196,7 +196,7 @@ public String getJsonKeyFile() { /** * For use in UI, do not use. - * @return The uploaded json key file + * @return The uploaded json key file. */ @Deprecated @Restricted(DoNotUse.class) // UI: Required for stapler call of setter. @@ -204,6 +204,13 @@ public FileItem getJsonKeyFileUpload() { return null; } + /** + * In this context the service account id is represented by the email address + * for that service account, which should be contained in the json key. + * + * @return The service account identifier. Null if no json key has been + * provided. + */ @Override public String getAccountId() { JsonKey jsonKey = getJsonKey(); @@ -213,6 +220,10 @@ public String getAccountId() { return null; } + /** + * @return The {@link PrivateKey} that comes from the secret json key. Null if + * this service account config contains no key or if the key is malformed. + */ @Override public PrivateKey getPrivateKey() { JsonKey jsonKey = getJsonKey(); @@ -252,7 +263,7 @@ private JsonKey getJsonKey() { } /** - * descriptor for .json service account authentication + * Descriptor for .json service account authentication. */ @Extension public static final class DescriptorImpl extends Descriptor { diff --git a/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java b/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java index 9ced4e1..0c04d63 100644 --- a/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java +++ b/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java @@ -46,8 +46,8 @@ import jenkins.model.Jenkins; /** - * provides authentication mechanism for a service account by setting a service - * account email address and .p12 private key file + * Provides authentication mechanism for a service account by setting a service + * account email address and .p12 private key file. */ public class P12ServiceAccountConfig extends ServiceAccountConfig { private static final long serialVersionUID = 8706353638974721795L; @@ -65,7 +65,7 @@ public class P12ServiceAccountConfig extends ServiceAccountConfig { private transient String p12KeyFile; /** - * @param emailAddress email address + * @param emailAddress The service account email address. * @since 0.8 */ @DataBoundConstructor @@ -77,9 +77,9 @@ public P12ServiceAccountConfig(String emailAddress) { * For being able to load credentials created with versions < 0.8 * and backwards compatibility with external callers. * - * @param emailAddress email address + * @param emailAddress The service account email address. * @param p12KeyFileUpload The uploaded p12 key file. - * @param prevP12KeyFile The path of the previous p12 key file + * @param prevP12KeyFile The path of the previous p12 key file. * @since 0.3 */ @Deprecated @@ -95,7 +95,7 @@ public P12ServiceAccountConfig( } } - /** @param p12KeyFile uploaded p12 key file */ + /** @param p12KeyFile The uploaded p12 key file. */ @Deprecated @DataBoundSetter // Called on form submit, only used when key file is uploaded public void setP12KeyFileUpload(FileItem p12KeyFile) { @@ -105,7 +105,7 @@ public void setP12KeyFileUpload(FileItem p12KeyFile) { } } - /** @param filename previous json key file name. */ + /** @param filename The previous p12 key file name. */ @DataBoundSetter public void setFilename(String filename) { if (!Strings.isNullOrEmpty(filename)) { @@ -113,7 +113,7 @@ public void setFilename(String filename) { } } - /** @param secretP12Key previous p12 key file content.*/ + /** @param secretP12Key The previous p12 key file content. */ @DataBoundSetter public void setSecretP12Key(SecretBytes secretP12Key) { if (secretP12Key != null && secretP12Key.getPlainData().length > 0) { @@ -172,7 +172,7 @@ public String getEmailAddress() { } /** - * @return Original uploaded file name + * @return Original uploaded file name. * @since 0.7 */ @CheckForNull @@ -183,7 +183,7 @@ public String getFilename() { /** * Do not use, required for UI. * - * @return secretP12Key + * @return The secret p12 key. */ @Restricted(DoNotUse.class) // UI: Required for stapler call of setter. @CheckForNull @@ -191,7 +191,7 @@ public SecretBytes getSecretP12Key() { return secretP12Key; } - /** @return the path of the previous p12 key file. */ + /** @return The path of the previous p12 key file. */ @Deprecated public String getP12KeyFile() { return p12KeyFile; @@ -199,7 +199,8 @@ public String getP12KeyFile() { /** * Do not use, required for UI. - * @return The uploaded p12 key file + * + * @return The uploaded p12 key file. */ @Deprecated @Restricted(DoNotUse.class) // UI: Required for stapler call of setter. @@ -247,7 +248,7 @@ private KeyStore getP12KeyStore() throws KeyStoreException, } /** - * descriptor for .p12 service account authentication + * Descriptor for .p12 service account authentication. */ @Extension public static final class DescriptorImpl extends Descriptor { diff --git a/src/test/java/com/google/jenkins/plugins/credentials/oauth/ConfigurationAsCodeTest.java b/src/test/java/com/google/jenkins/plugins/credentials/oauth/ConfigurationAsCodeTest.java index 0d1eaf4..ffb3287 100644 --- a/src/test/java/com/google/jenkins/plugins/credentials/oauth/ConfigurationAsCodeTest.java +++ b/src/test/java/com/google/jenkins/plugins/credentials/oauth/ConfigurationAsCodeTest.java @@ -35,7 +35,7 @@ /** * Tests that the credentials are correctly processed by the Configuration as - * Code plugin + * Code plugin. */ public class ConfigurationAsCodeTest { From 6b24344469929c08ae7877d6da92ac0778b0afac Mon Sep 17 00:00:00 2001 From: Stephen Shank Date: Wed, 10 Apr 2019 11:32:03 -0700 Subject: [PATCH 14/17] Add TODO tracking issue #50 for deduplication. --- .../plugins/credentials/oauth/JsonServiceAccountConfig.java | 5 +++++ .../plugins/credentials/oauth/P12ServiceAccountConfig.java | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java b/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java index 9307079..c86987d 100644 --- a/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java +++ b/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java @@ -60,6 +60,11 @@ * */ public class JsonServiceAccountConfig extends ServiceAccountConfig { + /* + * TODO(jenkinsci/google-oauth-plugin#50): Dedupe shared functionality in + * google-auth-library. + */ + private static final long serialVersionUID = 6818111194672325387L; private static final Logger LOGGER = Logger.getLogger(JsonServiceAccountConfig.class.getSimpleName()); diff --git a/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java b/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java index 0c04d63..42e124a 100644 --- a/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java +++ b/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java @@ -50,6 +50,11 @@ * account email address and .p12 private key file. */ public class P12ServiceAccountConfig extends ServiceAccountConfig { + /* + * TODO(jenkinsci/google-oauth-plugin#50): Dedupe shared functionality in + * google-auth-library. + */ + private static final long serialVersionUID = 8706353638974721795L; private static final Logger LOGGER = Logger.getLogger(P12ServiceAccountConfig.class.getSimpleName()); From 5972c00f3968a5ffa7a22a1c1b7e803e4f214537 Mon Sep 17 00:00:00 2001 From: Stephen Shank Date: Wed, 10 Apr 2019 12:31:58 -0700 Subject: [PATCH 15/17] Fix secret bytes to account for newline in file. --- .../plugins/credentials/oauth/json-service-account-config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/com/google/jenkins/plugins/credentials/oauth/json-service-account-config.yml b/src/test/resources/com/google/jenkins/plugins/credentials/oauth/json-service-account-config.yml index bb96bb5..0cada0d 100644 --- a/src/test/resources/com/google/jenkins/plugins/credentials/oauth/json-service-account-config.yml +++ b/src/test/resources/com/google/jenkins/plugins/credentials/oauth/json-service-account-config.yml @@ -10,4 +10,4 @@ credentials: serviceAccountConfig: json: # Tbe contents of test-key.json in base64 - secretJsonKey: 'ewogICJ0eXBlIjogInNlcnZpY2VfYWNjb3VudCIsCiAgInByb2plY3RfaWQiOiAidGVzdC1wcm9q ZWN0IiwKICAicHJpdmF0ZV9rZXlfaWQiOiAidGVzdC1wcml2YXRlLWtleS1pZCIsCiAgInByaXZh dGVfa2V5IjogInRlc3QtcHJpdmF0ZS1rZXkiLAogICJjbGllbnRfZW1haWwiOiAidGVzdC1hY2Nv dW50QHRlc3QtcHJvamVjdC5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsCiAgImNsaWVudF9pZCI6 ICJ0ZXN0LWNsaWVudC1pZCIsCiAgImF1dGhfdXJpIjogImh0dHBzOi8vYWNjb3VudHMuZ29vZ2xl LmNvbS9vL29hdXRoMi9hdXRoIiwKICAidG9rZW5fdXJpIjogImh0dHBzOi8vb2F1dGgyLmdvb2ds ZWFwaXMuY29tL3Rva2VuIiwKICAiYXV0aF9wcm92aWRlcl94NTA5X2NlcnRfdXJsIjogImh0dHBz Oi8vd3d3Lmdvb2dsZWFwaXMuY29tL29hdXRoMi92MS9jZXJ0cyIsCiAgImNsaWVudF94NTA5X2Nl cnRfdXJsIjogImh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL3JvYm90L3YxL21ldGFkYXRhL3g1 MDkvdGVzdC1hY2NvdW50JTQwdGVzdC1wcm9qZWN0LmlhbS5nc2VydmljZWFjY291bnQuY29tIgp9' + secretJsonKey: 'ewogICJ0eXBlIjogInNlcnZpY2VfYWNjb3VudCIsCiAgInByb2plY3RfaWQiOiAidGVzdC1wcm9q ZWN0IiwKICAicHJpdmF0ZV9rZXlfaWQiOiAidGVzdC1wcml2YXRlLWtleS1pZCIsCiAgInByaXZh dGVfa2V5IjogInRlc3QtcHJpdmF0ZS1rZXkiLAogICJjbGllbnRfZW1haWwiOiAidGVzdC1hY2Nv dW50QHRlc3QtcHJvamVjdC5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsCiAgImNsaWVudF9pZCI6 ICJ0ZXN0LWNsaWVudC1pZCIsCiAgImF1dGhfdXJpIjogImh0dHBzOi8vYWNjb3VudHMuZ29vZ2xl LmNvbS9vL29hdXRoMi9hdXRoIiwKICAidG9rZW5fdXJpIjogImh0dHBzOi8vb2F1dGgyLmdvb2ds ZWFwaXMuY29tL3Rva2VuIiwKICAiYXV0aF9wcm92aWRlcl94NTA5X2NlcnRfdXJsIjogImh0dHBz Oi8vd3d3Lmdvb2dsZWFwaXMuY29tL29hdXRoMi92MS9jZXJ0cyIsCiAgImNsaWVudF94NTA5X2Nl cnRfdXJsIjogImh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL3JvYm90L3YxL21ldGFkYXRhL3g1 MDkvdGVzdC1hY2NvdW50JTQwdGVzdC1wcm9qZWN0LmlhbS5nc2VydmljZWFjY291bnQuY29tIgp9 Cg== -' From dd9bc3c2b9e0ea1d18534ac56df6c5800005c4d7 Mon Sep 17 00:00:00 2001 From: Stephen Shank Date: Mon, 15 Apr 2019 13:56:19 -0700 Subject: [PATCH 16/17] Incorporate PR feedback. --- .../oauth/JsonServiceAccountConfig.java | 26 +++++++++---------- .../oauth/P12ServiceAccountConfig.java | 8 +++--- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java b/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java index c86987d..048b7c9 100644 --- a/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java +++ b/src/main/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfig.java @@ -46,8 +46,8 @@ import jenkins.model.Jenkins; /** - * Provides authentication mechanism for a service account by setting a .json - * private key file. The .json file structure needs to be: + * Provides authentication mechanism for a service account by setting a JSON + * private key file. The JSON file structure needs to be: *

* * { @@ -85,8 +85,8 @@ public JsonServiceAccountConfig() {} * For being able to load credentials created with versions < 0.8 * and backwards compatibility with external callers. * - * @param jsonKeyFile The uploaded json key file. - * @param prevJsonKeyFile The path of the previous json key file. + * @param jsonKeyFile The uploaded JSON key file. + * @param prevJsonKeyFile The path of the previous JSON key file. * @since 0.3 */ @Deprecated @@ -99,7 +99,7 @@ public JsonServiceAccountConfig( } } - /** @param jsonKeyFileUpload The uploaded json key file. */ + /** @param jsonKeyFileUpload The uploaded JSON key file. */ @DataBoundSetter // Called on form submit, only used when key file is uploaded public void setJsonKeyFileUpload(FileItem jsonKeyFileUpload) { if (jsonKeyFileUpload != null && jsonKeyFileUpload.getSize() > 0) { @@ -112,12 +112,12 @@ public void setJsonKeyFileUpload(FileItem jsonKeyFileUpload) { this.secretJsonKey = SecretBytes.fromBytes(jsonKeyFileUpload.get()); } } catch (IOException e) { - LOGGER.log(Level.SEVERE, "Failed to read json key from file", e); + LOGGER.log(Level.SEVERE, "Failed to read JSON key from file", e); } } } - /** @param filename The json key file name. */ + /** @param filename The JSON key file name. */ @DataBoundSetter public void setFilename(String filename) { String newFilename = extractFilename(filename); @@ -126,7 +126,7 @@ public void setFilename(String filename) { } } - /** @param secretJsonKey The json key file content. */ + /** @param secretJsonKey The JSON key file content. */ @DataBoundSetter public void setSecretJsonKey(SecretBytes secretJsonKey) { if (secretJsonKey != null && secretJsonKey.getPlainData().length > 0) { @@ -201,7 +201,7 @@ public String getJsonKeyFile() { /** * For use in UI, do not use. - * @return The uploaded json key file. + * @return The uploaded JSON key file. */ @Deprecated @Restricted(DoNotUse.class) // UI: Required for stapler call of setter. @@ -211,9 +211,9 @@ public FileItem getJsonKeyFileUpload() { /** * In this context the service account id is represented by the email address - * for that service account, which should be contained in the json key. + * for that service account, which should be contained in the JSON key. * - * @return The service account identifier. Null if no json key has been + * @return The service account identifier. Null if no JSON key has been * provided. */ @Override @@ -226,7 +226,7 @@ public String getAccountId() { } /** - * @return The {@link PrivateKey} that comes from the secret json key. Null if + * @return The {@link PrivateKey} that comes from the secret JSON key. Null if * this service account config contains no key or if the key is malformed. */ @Override @@ -268,7 +268,7 @@ private JsonKey getJsonKey() { } /** - * Descriptor for .json service account authentication. + * Descriptor for JSON service account authentication. */ @Extension public static final class DescriptorImpl extends Descriptor { diff --git a/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java b/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java index 42e124a..250821a 100644 --- a/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java +++ b/src/main/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfig.java @@ -47,7 +47,7 @@ /** * Provides authentication mechanism for a service account by setting a service - * account email address and .p12 private key file. + * account email address and P12 private key file. */ public class P12ServiceAccountConfig extends ServiceAccountConfig { /* @@ -227,9 +227,7 @@ public PrivateKey getPrivateKey() { } return (PrivateKey) p12KeyStore.getKey(DEFAULT_P12_ALIAS, DEFAULT_P12_SECRET.toCharArray()); - } catch (IOException e) { - LOGGER.log(Level.SEVERE, "Failed to read private key", e); - } catch (GeneralSecurityException e) { + } catch (IOException | GeneralSecurityException e) { LOGGER.log(Level.SEVERE, "Failed to read private key", e); } return null; @@ -253,7 +251,7 @@ private KeyStore getP12KeyStore() throws KeyStoreException, } /** - * Descriptor for .p12 service account authentication. + * Descriptor for P12 service account authentication. */ @Extension public static final class DescriptorImpl extends Descriptor { From db6e79f2acb7a88ee103ae006362ceb3e28934e1 Mon Sep 17 00:00:00 2001 From: Stephen Shank Date: Mon, 15 Apr 2019 13:56:42 -0700 Subject: [PATCH 17/17] Fix typos in yml comments. --- .../plugins/credentials/oauth/json-service-account-config.yml | 2 +- .../plugins/credentials/oauth/p12-service-account-config.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/resources/com/google/jenkins/plugins/credentials/oauth/json-service-account-config.yml b/src/test/resources/com/google/jenkins/plugins/credentials/oauth/json-service-account-config.yml index 0cada0d..4226bfd 100644 --- a/src/test/resources/com/google/jenkins/plugins/credentials/oauth/json-service-account-config.yml +++ b/src/test/resources/com/google/jenkins/plugins/credentials/oauth/json-service-account-config.yml @@ -9,5 +9,5 @@ credentials: projectId: 'test-project' serviceAccountConfig: json: - # Tbe contents of test-key.json in base64 + # The contents of test-key.json in base 64. secretJsonKey: 'ewogICJ0eXBlIjogInNlcnZpY2VfYWNjb3VudCIsCiAgInByb2plY3RfaWQiOiAidGVzdC1wcm9q ZWN0IiwKICAicHJpdmF0ZV9rZXlfaWQiOiAidGVzdC1wcml2YXRlLWtleS1pZCIsCiAgInByaXZh dGVfa2V5IjogInRlc3QtcHJpdmF0ZS1rZXkiLAogICJjbGllbnRfZW1haWwiOiAidGVzdC1hY2Nv dW50QHRlc3QtcHJvamVjdC5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsCiAgImNsaWVudF9pZCI6 ICJ0ZXN0LWNsaWVudC1pZCIsCiAgImF1dGhfdXJpIjogImh0dHBzOi8vYWNjb3VudHMuZ29vZ2xl LmNvbS9vL29hdXRoMi9hdXRoIiwKICAidG9rZW5fdXJpIjogImh0dHBzOi8vb2F1dGgyLmdvb2ds ZWFwaXMuY29tL3Rva2VuIiwKICAiYXV0aF9wcm92aWRlcl94NTA5X2NlcnRfdXJsIjogImh0dHBz Oi8vd3d3Lmdvb2dsZWFwaXMuY29tL29hdXRoMi92MS9jZXJ0cyIsCiAgImNsaWVudF94NTA5X2Nl cnRfdXJsIjogImh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL3JvYm90L3YxL21ldGFkYXRhL3g1 MDkvdGVzdC1hY2NvdW50JTQwdGVzdC1wcm9qZWN0LmlhbS5nc2VydmljZWFjY291bnQuY29tIgp9 Cg== -' diff --git a/src/test/resources/com/google/jenkins/plugins/credentials/oauth/p12-service-account-config.yml b/src/test/resources/com/google/jenkins/plugins/credentials/oauth/p12-service-account-config.yml index b7978a1..419c844 100644 --- a/src/test/resources/com/google/jenkins/plugins/credentials/oauth/p12-service-account-config.yml +++ b/src/test/resources/com/google/jenkins/plugins/credentials/oauth/p12-service-account-config.yml @@ -10,5 +10,5 @@ credentials: serviceAccountConfig: p12: emailAddress: 'test-account@test-project.iam.gserviceaccount.com' - # 'test-p12-key' in base 64 + # 'test-p12-key' in base 64. secretP12Key: 'dGVzdC1wMTIta2V5'