|
26 | 26 | import com.google.cloud.security.privateca.v1.DisableCertificateAuthorityRequest; |
27 | 27 | import com.google.cloud.security.privateca.v1.ListCaPoolsRequest; |
28 | 28 | import com.google.cloud.security.privateca.v1.LocationName; |
| 29 | +import com.google.protobuf.Timestamp; |
29 | 30 | import java.io.IOException; |
| 31 | +import java.time.Instant; |
| 32 | +import java.time.temporal.ChronoUnit; |
30 | 33 | import java.util.concurrent.ExecutionException; |
31 | 34 | import java.util.concurrent.TimeUnit; |
32 | 35 | import java.util.concurrent.TimeoutException; |
33 | 36 |
|
34 | 37 | public class Util { |
35 | 38 |
|
| 39 | + private static final int DELETION_THRESHOLD_TIME_HOURS = 24; |
| 40 | + |
36 | 41 | // Delete Ca pools which starts with the given prefixToDelete. |
37 | 42 | public static void cleanUpCaPool(String projectId, |
38 | 43 | String location) |
@@ -74,6 +79,11 @@ public static void deleteCertificateAuthority(String caPoolName) |
74 | 79 | CertificateAuthorityServiceClient.create()) { |
75 | 80 | for (CertificateAuthority certificateAuthority : |
76 | 81 | certificateAuthorityServiceClient.listCertificateAuthorities(caPoolName).iterateAll()) { |
| 82 | + // Check if the CA was created before the threshold time. |
| 83 | + if (!isCreatedBeforeThresholdTime(certificateAuthority.getCreateTime())) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
77 | 87 | // Check if the CA is enabled. |
78 | 88 | State caState = |
79 | 89 | certificateAuthorityServiceClient |
@@ -112,4 +122,10 @@ public static void disableCertificateAuthority(String caName) |
112 | 122 | .get(5, TimeUnit.MINUTES); |
113 | 123 | } |
114 | 124 | } |
| 125 | + |
| 126 | + public static boolean isCreatedBeforeThresholdTime(Timestamp timestamp) { |
| 127 | + Instant instant = Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos()); |
| 128 | + return instant |
| 129 | + .isBefore(Instant.now().minus(DELETION_THRESHOLD_TIME_HOURS, ChronoUnit.HOURS)); |
| 130 | + } |
115 | 131 | } |
0 commit comments