Skip to content

Commit 71730d4

Browse files
committed
Skip secret rotation for non-renewable leases with zero TTL
We now skip secret rotation for secrets that have a zero TTL, typically kv1/kv2 secrets that don't have a TTL configured to avoid excessive Vault calls. Closes gh-601. See also spring-cloud/spring-cloud-vault#391
1 parent 02adcd1 commit 71730d4

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

spring-vault-core/src/main/java/org/springframework/vault/core/lease/SecretLeaseContainer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ private boolean isLeaseRenewable(@Nullable Lease lease, RequestedSecret requeste
897897
return true;
898898
}
899899

900-
if (!lease.hasLeaseId() && requestedSecret.getMode() == Mode.ROTATE) {
900+
if (!lease.hasLeaseId() && !lease.getLeaseDuration().isZero() && requestedSecret.getMode() == Mode.ROTATE) {
901901
return true;
902902
}
903903

@@ -915,7 +915,8 @@ private boolean isLeaseRotateOnly(Lease lease, RequestedSecret requestedSecret)
915915
return false;
916916
}
917917

918-
return lease.hasLeaseId() && !lease.isRenewable() && requestedSecret.getMode() == Mode.ROTATE;
918+
return lease.hasLeaseId() && !lease.getLeaseDuration().isZero() && !lease.isRenewable()
919+
&& requestedSecret.getMode() == Mode.ROTATE;
919920
}
920921

921922
}

spring-vault-core/src/test/java/org/springframework/vault/core/lease/RotatingGenericSecretsIntegrationTestConfiguration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@
3232
* @author Steven Swor
3333
*/
3434
@Configuration
35+
@VaultPropertySource(propertyNamePrefix = "generic.rotating.", value = "versioned/rotating",
36+
renewal = VaultPropertySource.Renewal.ROTATE)
3537
public class RotatingGenericSecretsIntegrationTestConfiguration {
3638

3739
/**
3840
* Utility class which will give our tests a reference to the
3941
* {@link LeaseAwareVaultPropertySource} which holds our secrets.
4042
*/
41-
@VaultPropertySource(propertyNamePrefix = "generic.rotating.", value = "secret/rotating",
42-
renewal = VaultPropertySource.Renewal.ROTATE)
43+
4344
public static class PropertySourceHolder implements InitializingBean {
4445

4546
@Autowired
@@ -60,7 +61,7 @@ public void afterPropertiesSet() throws Exception {
6061
Map<String, LeaseAwareVaultPropertySource> leaseAwareVaultPropertySources = this.appContext
6162
.getBeansOfType(LeaseAwareVaultPropertySource.class);
6263
for (LeaseAwareVaultPropertySource candidate : leaseAwareVaultPropertySources.values()) {
63-
if (candidate.getRequestedSecret().getPath().equals("secret/rotating")) {
64+
if (candidate.getRequestedSecret().getPath().equals("versioned/rotating")) {
6465
this.propertySource = candidate;
6566
break;
6667
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.vault.core.lease;
17+
18+
import java.util.Collections;
19+
20+
import org.junit.jupiter.api.BeforeAll;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ExtendWith;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.test.context.junit.jupiter.SpringExtension;
26+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
27+
import org.springframework.vault.core.VaultIntegrationTestConfiguration;
28+
import org.springframework.vault.core.VaultKeyValueOperations;
29+
import org.springframework.vault.core.VaultKeyValueOperationsSupport;
30+
import org.springframework.vault.util.IntegrationTestSupport;
31+
import org.springframework.vault.util.PrepareVault;
32+
import org.springframework.vault.util.VaultInitializer;
33+
34+
import static org.assertj.core.api.Assertions.*;
35+
36+
/**
37+
* Integration tests for rotating generic secrets.
38+
*
39+
* @author Mark Paluch
40+
*/
41+
@ExtendWith(SpringExtension.class)
42+
@SpringJUnitConfig(
43+
classes = { VaultIntegrationTestConfiguration.class, RotatingGenericSecretsIntegrationTestConfiguration.class })
44+
public class RotatingGenericSecretsIntegrationTests extends IntegrationTestSupport {
45+
46+
@BeforeAll
47+
static void beforeAll() {
48+
49+
VaultInitializer initializer = new VaultInitializer();
50+
51+
initializer.initialize();
52+
PrepareVault prepare = initializer.prepare();
53+
54+
VaultKeyValueOperations versioned = prepare.getVaultOperations().opsForKeyValue("versioned",
55+
VaultKeyValueOperationsSupport.KeyValueBackend.KV_2);
56+
57+
versioned.put("rotating", Collections.singletonMap("key", "value"));
58+
}
59+
60+
@Test
61+
void name(@Autowired RotatingGenericSecretsIntegrationTestConfiguration.PropertySourceHolder holder) {
62+
63+
assertThat(holder.propertySource.getProperty("generic.rotating.key")).isEqualTo("value");
64+
}
65+
66+
}

0 commit comments

Comments
 (0)