Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support defining file credentials as SecretString #154

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ target
work
*.releaseBackup
release.properties

.classpath
.factorypath
.project
.settings/
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.jenkins.plugins.credentials.secretsmanager.factory.string.AwsStringCredentials;
import io.jenkins.plugins.credentials.secretsmanager.factory.username_password.AwsUsernamePasswordCredentials;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
Expand Down Expand Up @@ -68,7 +69,7 @@ public SecretBytes get() {
return getSecretValue().match(new SecretValue.Matcher<SecretBytes>() {
@Override
public SecretBytes string(String str) {
return null;
return SecretBytes.fromBytes(str.getBytes(StandardCharsets.UTF_8));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List;

import static io.jenkins.plugins.credentials.secretsmanager.util.assertions.CustomAssertions.assertThat;
Expand All @@ -30,6 +31,7 @@ public class FileCredentialsIT implements CredentialsTests {

private static final String FILENAME = "hello.txt";
private static final byte[] CONTENT = {0x01, 0x02, 0x03};
private static final String STRING_CONTENT = "This is a text file";

public MyJenkinsConfiguredWithCodeRule jenkins = new MyJenkinsConfiguredWithCodeRule();
public final AWSSecretsManagerRule secretsManager = new AutoErasingAWSSecretsManagerRule();
Expand Down Expand Up @@ -84,7 +86,7 @@ public void shouldHaveCustomisableFileName() {

@Test
@ConfiguredWithCode(value = "/integration.yml")
public void shouldHaveContent() {
public void shouldHaveFileContent() {
// Given
final CreateSecretResult foo = createFileSecret(CONTENT);

Expand All @@ -96,6 +98,20 @@ public void shouldHaveContent() {
.hasContent(CONTENT);
}

@Test
@ConfiguredWithCode(value = "/integration.yml")
public void shouldHaveStringContent() {
// Given
final CreateSecretResult foo = createFileSecret(STRING_CONTENT);

// When
final FileCredentials credential = lookup(FileCredentials.class, foo.getName());

// Then
assertThat(credential)
.hasContent(STRING_CONTENT.getBytes(StandardCharsets.UTF_8));
}

@Test
@ConfiguredWithCode(value = "/integration.yml")
public void shouldHaveDescriptorIcon() {
Expand Down Expand Up @@ -201,6 +217,12 @@ private CreateSecretResult createFileSecret(byte[] content) {
return createSecret(content,tags);
}

private CreateSecretResult createFileSecret(String content) {
final List<Tag> tags = Lists.of(AwsTags.type(Type.file));

return createSecret(content,tags);
}

private CreateSecretResult createFileSecret(byte[] content, String filename) {
final List<Tag> tags = Lists.of(AwsTags.type(Type.file), AwsTags.filename(filename));

Expand All @@ -216,6 +238,15 @@ private CreateSecretResult createSecret(byte[] content, List<Tag> tags) {
return secretsManager.getClient().createSecret(request);
}

private CreateSecretResult createSecret(String content, List<Tag> tags) {
final CreateSecretRequest request = new CreateSecretRequest()
.withName(CredentialNames.random())
.withSecretString(content)
.withTags(tags);

return secretsManager.getClient().createSecret(request);
}

private <C extends StandardCredentials> C lookup(Class<C> type, String id) {
return jenkins.getCredentials().lookup(type, id);
}
Expand Down