-
Notifications
You must be signed in to change notification settings - Fork 3
/
SecretsStepDefs.java
226 lines (193 loc) · 11 KB
/
SecretsStepDefs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package com.github.nagyesta.lowkeyvault.steps;
import com.azure.core.http.rest.PagedIterable;
import com.azure.security.keyvault.secrets.models.DeletedSecret;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
import com.azure.security.keyvault.secrets.models.SecretProperties;
import com.github.nagyesta.lowkeyvault.context.SecretTestContext;
import com.github.nagyesta.lowkeyvault.context.TestContextConfig;
import com.github.nagyesta.lowkeyvault.http.ApacheHttpClientProvider;
import com.github.nagyesta.lowkeyvault.http.AuthorityOverrideFunction;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static com.github.nagyesta.lowkeyvault.context.KeyTestContext.NOW;
import static com.github.nagyesta.lowkeyvault.context.TestContextConfig.CONTAINER_AUTHORITY;
import static java.lang.Boolean.TRUE;
public class SecretsStepDefs extends CommonAssertions {
private final SecretTestContext context;
public SecretsStepDefs(final TestContextConfig config) {
context = config.secretContext();
}
@Given("secret API version {api} is used")
public void apiVersionApiIsUsed(final String version) {
context.setApiVersion(version);
}
@Given("a secret client is created with the vault named {name}")
public void theSecretClientIsCreatedWithVaultNameSelected(final String vaultName) {
final String vaultAuthority = vaultName + ".localhost:8443";
final String vaultUrl = "https://" + vaultAuthority;
final AuthorityOverrideFunction overrideFunction = new AuthorityOverrideFunction(vaultAuthority, CONTAINER_AUTHORITY);
context.setProvider(new ApacheHttpClientProvider(vaultUrl, overrideFunction));
}
@Given("a secret named {name} and valued {secretValue} is prepared")
public void aSecretNamedSecretNameIsPreparedWithValueSet(
final String secretName, final String secretValue) {
context.setCreateSecretOptions(new KeyVaultSecret(secretName, secretValue));
}
@Given("{int} version of the secret is created")
public void versionsCountVersionOfTheSecretIsCreated(final int versionsCount) {
final KeyVaultSecret secretCreateInfo = context.getCreateSecretOptions();
IntStream.range(0, versionsCount).forEach(i -> {
final KeyVaultSecret secret = context.getClient(context.getSecretServiceVersion()).setSecret(secretCreateInfo);
context.addCreatedEntity(secretCreateInfo.getName(), secret);
});
}
@When("the secret is created")
public void secretCreationRequestIsSent() {
final KeyVaultSecret secretCreateInfo = context.getCreateSecretOptions();
final KeyVaultSecret secret = context.getClient(context.getSecretServiceVersion()).setSecret(secretCreateInfo);
context.addCreatedEntity(secretCreateInfo.getName(), secret);
}
@When("the first secret version of {name} is fetched with providing a version")
public void fetchFirstSecretVersion(final String name) {
final List<KeyVaultSecret> versionsCreated = context.getCreatedEntities().get(name);
final String version = versionsCreated.get(0).getProperties().getVersion();
final KeyVaultSecret secret = context.getClient(context.getSecretServiceVersion()).getSecret(name, version);
context.addFetchedSecret(name, secret);
assertEquals(version, secret.getProperties().getVersion());
}
@When("the last secret version of {name} is fetched without providing a version")
public void fetchLatestSecretVersion(final String name) {
final KeyVaultSecret secret = context.getClient(context.getSecretServiceVersion()).getSecret(name);
final List<KeyVaultSecret> versionsCreated = context.getCreatedEntities().get(name);
final String expectedLastVersionId = versionsCreated.get(versionsCreated.size() - 1).getId();
context.addFetchedSecret(name, secret);
assertEquals(expectedLastVersionId, secret.getId());
}
@Given("the secret is set to expire {optionalInt} seconds after creation")
public void theSecretIsSetToExpireExpiresSecondsAfterCreation(final Integer expire) {
Optional.ofNullable(expire).ifPresent(e -> context.getCreateSecretOptions().getProperties().setExpiresOn(NOW.plusSeconds(e)));
}
@Given("the secret is set to be not usable until {optionalInt} seconds after creation")
public void theSecretIsSetToBeNotUsableUntilNotBeforeSecondsAfterCreation(final Integer notBefore) {
Optional.ofNullable(notBefore).ifPresent(n -> context.getCreateSecretOptions().getProperties().setNotBefore(NOW.plusSeconds(n)));
}
@Given("the secret is set to use {tagMap} as tags")
public void theSecretIsSetToUseTagMapAsTags(final Map<String, String> tags) {
context.getCreateSecretOptions().getProperties().setTags(tags);
}
@Given("the secret is set to have {contentType} as content type")
public void theSecretIsSetToHaveContentTypeAsContentType(final String contentType) {
context.getCreateSecretOptions().getProperties().setContentType(contentType);
}
@Given("the secret is set to be {enabled}")
public void theSecretIsSetToBeEnabledStatus(final boolean enabledStatus) {
context.getCreateSecretOptions().getProperties().setEnabled(enabledStatus);
}
@And("the secret is deleted")
public void theSecretIsDeleted() {
final DeletedSecret actual = context.getClient(context.getSecretServiceVersion())
.beginDeleteSecret(context.getLastResult().getName()).waitForCompletion().getValue();
context.setLastDeleted(actual);
}
@Given("{int} secrets with {name} prefix are created valued {secretValue}")
public void secretsWithSecretNamePrefixAreCreatedWithValueSet(
final int count, final String prefix, final String value) {
IntStream.range(0, count).forEach(i -> {
aSecretNamedSecretNameIsPreparedWithValueSet(prefix + (i + 1), value);
secretCreationRequestIsSent();
});
}
@When("the secret properties are listed")
public void theSecretPropertiesAreListed() {
final PagedIterable<SecretProperties> actual = context.getClient(context.getSecretServiceVersion()).listPropertiesOfSecrets();
final List<SecretProperties> propertyList = actual.stream()
.toList();
final List<String> list = propertyList.stream()
.map(SecretProperties::getId)
.collect(Collectors.toList());
context.setListedIds(list);
final List<String> managedList = propertyList.stream()
.filter(secretProperties -> TRUE == secretProperties.isManaged())
.map(SecretProperties::getId)
.collect(Collectors.toList());
context.setListedManagedIds(managedList);
}
@Given("{int} secrets with {name} prefix are deleted")
public void countSecretsWithKeyNamePrefixAreDeleted(
final int count, final String prefix) {
final List<String> deleted = IntStream.range(0, count).mapToObj(i -> {
final DeletedSecret actual = context.getClient(context.getSecretServiceVersion())
.beginDeleteSecret(prefix + (i + 1)).waitForCompletion().getValue();
context.setLastDeleted(actual);
return actual;
}).map(DeletedSecret::getRecoveryId).collect(Collectors.toList());
context.setDeletedRecoveryIds(deleted);
}
@When("the deleted secret properties are listed")
public void theDeletedSecretPropertiesAreListed() {
final PagedIterable<DeletedSecret> actual = context.getClient(context.getSecretServiceVersion()).listDeletedSecrets();
final List<String> list = actual.stream()
.map(DeletedSecret::getRecoveryId)
.collect(Collectors.toList());
context.setListedIds(list);
}
@When("secret is recovered")
public void secretIsRecovered() {
final DeletedSecret deleted = context.getLastDeleted();
final KeyVaultSecret secret = context.getClient(context.getSecretServiceVersion())
.beginRecoverDeletedSecret(deleted.getName()).waitForCompletion().getValue();
context.addFetchedSecret(secret.getName(), secret);
}
@When("the secret is purged")
public void theSecretIsPurged() {
final DeletedSecret deleted = context.getLastDeleted();
context.getClient(context.getSecretServiceVersion()).purgeDeletedSecret(deleted.getName());
}
@When("the last version of the secret is prepared for an update")
public void theLastVersionOfTheSecretIsPreparedForAnUpdate() {
final KeyVaultSecret lastResult = context.getLastResult();
final SecretProperties updatedProperties = context.getClient(context.getSecretServiceVersion())
.getSecret(lastResult.getName(), lastResult.getProperties().getVersion()).getProperties();
context.setUpdateProperties(updatedProperties);
}
@When("the secret is updated to expire {optionalInt} seconds after creation")
public void theSecretIsUpdatedToExpireExpiresSecondsAfterCreation(final Integer expire) {
Optional.ofNullable(expire).ifPresent(e -> context.getUpdateProperties().setExpiresOn(NOW.plusSeconds(e)));
}
@When("the secret is updated to be not usable until {optionalInt} seconds after creation")
public void theSecretIsUpdatedToBeNotUsableUntilNotBeforeSecondsAfterCreation(final Integer notBefore) {
Optional.ofNullable(notBefore).ifPresent(n -> context.getUpdateProperties().setNotBefore(NOW.plusSeconds(n)));
}
@When("the secret is updated to use {tagMap} as tags")
public void theSecretIsUpdatedToUseTagMapAsTags(final Map<String, String> tags) {
context.getUpdateProperties().setTags(tags);
}
@When("the secret is updated to be {enabled}")
public void theSecretIsUpdatedToBeEnabledStatus(final boolean enabledStatus) {
context.getUpdateProperties().setEnabled(enabledStatus);
}
@When("the secret update request is sent")
public void theUpdateRequestIsSent() {
final SecretProperties properties = context.getClient(context.getSecretServiceVersion())
.updateSecretProperties(context.getUpdateProperties());
//only update properties as the secret might be disabled due to our actions
context.getLastResult().setProperties(properties);
}
@And("the secret named {name} is backed up")
public void theSecretNamedNameIsBackedUp(final String name) {
final byte[] bytes = context.getClient(context.getSecretServiceVersion()).backupSecret(name);
context.setBackupBytes(name, bytes);
}
@And("the secret named {name} is restored")
public void theSecretNamedNameIsRestored(final String name) {
final byte[] bytes = context.getBackupBytes(name);
final KeyVaultSecret secret = context.getClient(context.getSecretServiceVersion()).restoreSecretBackup(bytes);
context.addFetchedSecret(name, secret);
}
}