Skip to content

Commit 7be9a18

Browse files
committed
Add support for snapshotting
1 parent 82d0a6d commit 7be9a18

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

src/main/java/io/jenkins/plugins/credentials/secretsmanager/factory/BaseAwsJsonCredentials.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,31 @@ public abstract class BaseAwsJsonCredentials extends BaseStandardCredentials {
2424
/** How to access the JSON */
2525
private final Supplier<Secret> supplierOfSecretData;
2626

27+
/**
28+
* Constructs a new instance with new data.
29+
*
30+
* @param id The value for {@link #getId()}.
31+
* @param description The value for {@link #getDescription()}.
32+
* @param secretJsonSupplier Supplies the data that
33+
* {@link #getFieldFromSecretJson(String)} will
34+
* decode.
35+
*/
2736
protected BaseAwsJsonCredentials(String id, String description, Supplier<Secret> secretJsonSupplier) {
2837
super(id, description);
2938
this.supplierOfSecretData = secretJsonSupplier;
3039
}
3140

41+
/**
42+
* Constructs an instance that is an unchanging snapshot of another instance.
43+
*
44+
* @param toSnapshot The instance to be copied.
45+
*/
46+
protected BaseAwsJsonCredentials(BaseAwsJsonCredentials toSnapshot) {
47+
super(toSnapshot.getId(), toSnapshot.getDescription());
48+
final Secret secretDataToSnapshot = toSnapshot.supplierOfSecretData.get();
49+
this.supplierOfSecretData = new Snapshot<Secret>(secretDataToSnapshot);
50+
}
51+
3252
/**
3353
* Reads the secret JSON and returns the field requested.
3454
*

src/main/java/io/jenkins/plugins/credentials/secretsmanager/factory/username_password/AwsJsonUsernamePasswordCredentials.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,31 @@ public class AwsJsonUsernamePasswordCredentials extends BaseAwsJsonCredentials
3939
@Restricted(NoExternalUse.class)
4040
public static final String JSON_FIELDNAME_FOR_PASSWORD = "password";
4141

42+
/**
43+
* Constructs a new instance.
44+
*
45+
* @param id The value for {@link #getId()}.
46+
* @param description The value for {@link #getDescription()}.
47+
* @param usernameAndPasswordJson Supplies JSON containing a
48+
* {@value #JSON_FIELDNAME_FOR_USERNAME} field
49+
* and a {@value #JSON_FIELDNAME_FOR_PASSWORD}
50+
* field.
51+
*/
4252
public AwsJsonUsernamePasswordCredentials(String id, String description, Supplier<Secret> usernameAndPasswordJson) {
4353
super(id, description, usernameAndPasswordJson);
4454
}
4555

56+
/**
57+
* Constructs a snapshot of an existing instance.
58+
*
59+
* @param toBeSnapshotted The instance that contains the live data to be
60+
* snapshotted.
61+
*/
62+
@Restricted(NoExternalUse.class)
63+
AwsJsonUsernamePasswordCredentials(AwsJsonUsernamePasswordCredentials toBeSnapshotted) {
64+
super(toBeSnapshotted);
65+
}
66+
4667
@NonNull
4768
@Override
4869
public Secret getPassword() {
@@ -56,11 +77,11 @@ public String getUsername() {
5677
}
5778

5879
@Override
59-
public boolean isUsernameSecret() {
60-
return true;
61-
}
80+
public boolean isUsernameSecret() {
81+
return true;
82+
}
6283

63-
@Extension
84+
@Extension
6485
@SuppressWarnings("unused")
6586
public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {
6687
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.jenkins.plugins.credentials.secretsmanager.factory.username_password;
2+
3+
import com.cloudbees.plugins.credentials.CredentialsSnapshotTaker;
4+
import hudson.Extension;
5+
import hudson.util.Secret;
6+
import io.jenkins.plugins.credentials.secretsmanager.factory.Snapshot;
7+
8+
@Extension
9+
@SuppressWarnings("unused")
10+
public class AwsJsonUsernamePasswordCredentialsSnapshotTaker extends CredentialsSnapshotTaker<AwsJsonUsernamePasswordCredentials> {
11+
@Override
12+
public Class<AwsJsonUsernamePasswordCredentials> type() {
13+
return AwsJsonUsernamePasswordCredentials.class;
14+
}
15+
16+
@Override
17+
public AwsJsonUsernamePasswordCredentials snapshot(AwsJsonUsernamePasswordCredentials credential) {
18+
return new AwsJsonUsernamePasswordCredentials(credential);
19+
}
20+
}

0 commit comments

Comments
 (0)