|
| 1 | +package io.jenkins.plugins.credentials.secretsmanager.factory.ssh_user_private_key; |
| 2 | + |
| 3 | +import static io.jenkins.plugins.credentials.secretsmanager.factory.BaseAwsJsonCredentialsTest.mkJson; |
| 4 | +import static io.jenkins.plugins.credentials.secretsmanager.factory.BaseAwsJsonCredentialsTest.assertThatJsonCredentialsDescriptorIsTheSameAsTheDescriptorForNonJsonCredentials; |
| 5 | +import static org.assertj.core.api.Assertions.assertThat; |
| 6 | + |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | +import java.util.function.Supplier; |
| 10 | + |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import com.cloudbees.plugins.credentials.CredentialsUnavailableException; |
| 14 | + |
| 15 | +import hudson.util.Secret; |
| 16 | +import io.jenkins.plugins.credentials.secretsmanager.Messages; |
| 17 | +import io.jenkins.plugins.credentials.secretsmanager.factory.BaseAwsJsonCredentialsTest.StubSingleShotSupplier; |
| 18 | +import io.jenkins.plugins.credentials.secretsmanager.factory.BaseAwsJsonCredentialsTest.StubSupplier; |
| 19 | + |
| 20 | +public class AwsJsonSshUserPrivateKeyTest { |
| 21 | + @Test |
| 22 | + public void getPassphraseGivenValidJsonThenReturnsSecretPassphrase() { |
| 23 | + // Given |
| 24 | + final String id = "testId"; |
| 25 | + final String description = "some test description"; |
| 26 | + final String expected = "mySecretPassphrase"; |
| 27 | + final String json = mkUsernameKeyAndPassphraseJson("myUsername", "myKey", expected); |
| 28 | + final Secret stubSecret = Secret.fromString(json); |
| 29 | + final Supplier<Secret> stubSupplier = new StubSupplier<>(stubSecret); |
| 30 | + final AwsJsonSshUserPrivateKey instance = new AwsJsonSshUserPrivateKey(id, description, stubSupplier); |
| 31 | + |
| 32 | + // When |
| 33 | + final Secret actualSecret = instance.getPassphrase(); |
| 34 | + final String actualPassphrase = actualSecret.getPlainText(); |
| 35 | + |
| 36 | + // Then |
| 37 | + assertThat(actualPassphrase).isEqualTo(expected); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void getPassphraseGivenValidJsonWithNoPassphraseThenReturnsEmpty() { |
| 42 | + // Given |
| 43 | + final String id = "testId"; |
| 44 | + final String description = "some test description"; |
| 45 | + final String expected = ""; |
| 46 | + final String json = mkUsernameKeyAndNoPassphraseJson("myUsername", "myKey"); |
| 47 | + final Secret stubSecret = Secret.fromString(json); |
| 48 | + final Supplier<Secret> stubSupplier = new StubSupplier<>(stubSecret); |
| 49 | + final AwsJsonSshUserPrivateKey instance = new AwsJsonSshUserPrivateKey(id, description, stubSupplier); |
| 50 | + |
| 51 | + // When |
| 52 | + final Secret actualSecret = instance.getPassphrase(); |
| 53 | + final String actualPassphrase = actualSecret.getPlainText(); |
| 54 | + |
| 55 | + // Then |
| 56 | + assertThat(actualPassphrase).isEqualTo(expected); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void getPrivateKeysGivenValidJsonThenReturnsSingleKey() { |
| 61 | + // Given |
| 62 | + final String id = "testId"; |
| 63 | + final String description = "some test description"; |
| 64 | + final List<String> expected = Collections.singletonList("theKeyThatISet"); |
| 65 | + final String json = mkUsernameKeyAndPassphraseJson("myUsername", expected.get(0), "mySecretPassphrase"); |
| 66 | + final Secret stubSecret = Secret.fromString(json); |
| 67 | + final Supplier<Secret> stubSupplier = new StubSupplier<>(stubSecret); |
| 68 | + final AwsJsonSshUserPrivateKey instance = new AwsJsonSshUserPrivateKey(id, description, stubSupplier); |
| 69 | + |
| 70 | + // When |
| 71 | + final List<String> actual = instance.getPrivateKeys(); |
| 72 | + |
| 73 | + // Then |
| 74 | + assertThat(actual).isEqualTo(expected); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void getUsernameGivenValidJsonThenReturnsUsername() { |
| 79 | + // Given |
| 80 | + final String id = "testId"; |
| 81 | + final String description = "some test description"; |
| 82 | + final String expected = "myUsername"; |
| 83 | + final String json = mkUsernameKeyAndPassphraseJson(expected, "myKey", "mySecretPassphrase"); |
| 84 | + final Secret stubSecret = Secret.fromString(json); |
| 85 | + final Supplier<Secret> stubSupplier = new StubSupplier<>(stubSecret); |
| 86 | + final AwsJsonSshUserPrivateKey instance = new AwsJsonSshUserPrivateKey(id, description, stubSupplier); |
| 87 | + |
| 88 | + // When |
| 89 | + final String actual = instance.getUsername(); |
| 90 | + |
| 91 | + // Then |
| 92 | + assertThat(actual).isEqualTo(expected); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void isUsernameSecretGivenAnythingThenReturnsTrue() { |
| 97 | + // Given |
| 98 | + final String id = "testId"; |
| 99 | + final String description = "some test description"; |
| 100 | + final boolean expected = true; |
| 101 | + final String json = mkUsernameKeyAndPassphraseJson("myUser", "myKey", "mySecretPassphrase"); |
| 102 | + final Secret stubSecret = Secret.fromString(json); |
| 103 | + final Supplier<Secret> stubSupplier = new StubSupplier<>(stubSecret); |
| 104 | + final AwsJsonSshUserPrivateKey instance = new AwsJsonSshUserPrivateKey(id, description, stubSupplier); |
| 105 | + |
| 106 | + // When |
| 107 | + final boolean actual = instance.isUsernameSecret(); |
| 108 | + |
| 109 | + // Then |
| 110 | + assertThat(actual).isEqualTo(expected); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void getUsernameGivenInvalidJsonThenReturnsThrows() { |
| 115 | + // Given |
| 116 | + final String id = "testId"; |
| 117 | + final String description = "some test description"; |
| 118 | + final String unexpectedFieldName = "potentiallySecretFieldName"; |
| 119 | + final String unexpectedValue = "potentiallySecretValue"; |
| 120 | + final String json = mkJson(unexpectedFieldName, unexpectedValue, unexpectedFieldName + "2", unexpectedValue); |
| 121 | + final Secret stubSecret = Secret.fromString(json); |
| 122 | + final Supplier<Secret> stubSupplier = new StubSupplier<>(stubSecret); |
| 123 | + final AwsJsonSshUserPrivateKey instance = new AwsJsonSshUserPrivateKey(id, description, stubSupplier); |
| 124 | + |
| 125 | + // When |
| 126 | + CredentialsUnavailableException actual = null; |
| 127 | + try { |
| 128 | + instance.getUsername(); |
| 129 | + } catch (CredentialsUnavailableException ex) { |
| 130 | + actual = ex; |
| 131 | + } |
| 132 | + |
| 133 | + // Then |
| 134 | + assertThat(actual).isNotNull(); |
| 135 | + assertThat(actual.getProperty()).isEqualTo("secret"); |
| 136 | + assertThat(actual.getMessage()).contains(id); |
| 137 | + assertThat(actual.getMessage()).contains(AwsJsonSshUserPrivateKey.JSON_FIELDNAME_FOR_USERNAME); |
| 138 | + assertThat(actual.getMessage()) |
| 139 | + .contains(Messages.wrongJsonError(id, AwsJsonSshUserPrivateKey.JSON_FIELDNAME_FOR_USERNAME)); |
| 140 | + assertThat(actual.getMessage()).doesNotContain(unexpectedFieldName); |
| 141 | + assertThat(actual.getMessage()).doesNotContain(unexpectedValue); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void snapshotConstructorGivenInstanceToSnapshotThenReturnsClone() { |
| 146 | + // Given |
| 147 | + final String expectedUsername = "myUser"; |
| 148 | + final String expectedPassphrase = "mySecretPassphrase"; |
| 149 | + final String expectedId = "someId"; |
| 150 | + final String expectedDescription = "someDescription"; |
| 151 | + final String expectedKey = "someKeyThatIMade"; |
| 152 | + final String json = mkUsernameKeyAndPassphraseJson(expectedUsername, expectedKey, expectedPassphrase); |
| 153 | + final Secret secretJson = Secret.fromString(json); |
| 154 | + final StubSingleShotSupplier<Secret> stubSupplier = new StubSingleShotSupplier<Secret>(secretJson); |
| 155 | + final AwsJsonSshUserPrivateKey original = new AwsJsonSshUserPrivateKey(expectedId, expectedDescription, |
| 156 | + stubSupplier); |
| 157 | + |
| 158 | + // When |
| 159 | + final AwsJsonSshUserPrivateKey actual = new AwsJsonSshUserPrivateKey(original); |
| 160 | + |
| 161 | + // Then |
| 162 | + final String actualId = actual.getId(); |
| 163 | + assertThat(actualId).isEqualTo(expectedId); |
| 164 | + final String actualDescription = actual.getDescription(); |
| 165 | + assertThat(actualDescription).isEqualTo(expectedDescription); |
| 166 | + final String actualUsername = actual.getUsername(); |
| 167 | + assertThat(actualUsername).isEqualTo(expectedUsername); |
| 168 | + final String actualPassphrase = actual.getPassphrase().getPlainText(); |
| 169 | + assertThat(actualPassphrase).isEqualTo(expectedPassphrase); |
| 170 | + final String actualKey = actual.getPrivateKeys().get(0); |
| 171 | + assertThat(actualKey).isEqualTo(expectedKey); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void ourDescriptorIsTheSameAsDescriptorForNonJsonCredentials() { |
| 176 | + // Given |
| 177 | + final AwsSshUserPrivateKey.DescriptorImpl expected = new AwsSshUserPrivateKey.DescriptorImpl(); |
| 178 | + final AwsJsonSshUserPrivateKey.DescriptorImpl instance = new AwsJsonSshUserPrivateKey.DescriptorImpl(); |
| 179 | + assertThatJsonCredentialsDescriptorIsTheSameAsTheDescriptorForNonJsonCredentials(instance, expected); |
| 180 | + } |
| 181 | + |
| 182 | + private static String mkUsernameKeyAndPassphraseJson(String username, String key, String passphrase) { |
| 183 | + return mkJson(AwsJsonSshUserPrivateKey.JSON_FIELDNAME_FOR_USERNAME, username, |
| 184 | + AwsJsonSshUserPrivateKey.JSON_FIELDNAME_FOR_PASSPHRASE, passphrase, |
| 185 | + AwsJsonSshUserPrivateKey.JSON_FIELDNAME_FOR_PRIVATE_KEY, key); |
| 186 | + } |
| 187 | + |
| 188 | + private static String mkUsernameKeyAndNoPassphraseJson(String username, String key) { |
| 189 | + return mkJson(AwsJsonSshUserPrivateKey.JSON_FIELDNAME_FOR_USERNAME, username, |
| 190 | + AwsJsonSshUserPrivateKey.JSON_FIELDNAME_FOR_PRIVATE_KEY, key); |
| 191 | + } |
| 192 | +} |
0 commit comments