|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 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 | + * http://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 | + |
| 17 | +package privateca; |
| 18 | + |
| 19 | +import com.google.cloud.security.privateca.v1.CaPool; |
| 20 | +import com.google.cloud.security.privateca.v1.CertificateAuthority; |
| 21 | +import com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient; |
| 22 | +import com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient.ListCaPoolsPagedResponse; |
| 23 | +import com.google.cloud.security.privateca.v1.DeleteCaPoolRequest; |
| 24 | +import com.google.cloud.security.privateca.v1.DeleteCertificateAuthorityRequest; |
| 25 | +import com.google.cloud.security.privateca.v1.ListCaPoolsRequest; |
| 26 | +import com.google.cloud.security.privateca.v1.LocationName; |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.concurrent.ExecutionException; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import java.util.concurrent.TimeoutException; |
| 31 | + |
| 32 | +public class Util { |
| 33 | + |
| 34 | + // Delete Ca pools which starts with the given prefixToDelete. |
| 35 | + public static void cleanUpCaPool(String prefixToDelete, String projectId, |
| 36 | + String location) |
| 37 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 38 | + |
| 39 | + try (CertificateAuthorityServiceClient client = CertificateAuthorityServiceClient.create()) { |
| 40 | + |
| 41 | + // Filter CA pools with the prefix |
| 42 | + for (CaPool caPool : filterCaPools(prefixToDelete, projectId, location).iterateAll()) { |
| 43 | + deleteCertificateAuthority(caPool.getName()); |
| 44 | + DeleteCaPoolRequest deleteCaPoolRequest = |
| 45 | + DeleteCaPoolRequest.newBuilder().setName(caPool.getName()).build(); |
| 46 | + |
| 47 | + client.deleteCaPoolCallable().futureCall(deleteCaPoolRequest).get(5, TimeUnit.MINUTES); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static ListCaPoolsPagedResponse filterCaPools(String prefixToDelete, String project, |
| 53 | + String location) throws IOException { |
| 54 | + try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = |
| 55 | + CertificateAuthorityServiceClient.create()) { |
| 56 | + |
| 57 | + LocationName locationName = |
| 58 | + LocationName.newBuilder().setProject(project).setLocation(location).build(); |
| 59 | + |
| 60 | + ListCaPoolsRequest request = ListCaPoolsRequest.newBuilder() |
| 61 | + .setParent(locationName.toString()) |
| 62 | + .setFilter(String.format("name:%s", prefixToDelete)) |
| 63 | + .build(); |
| 64 | + |
| 65 | + return |
| 66 | + certificateAuthorityServiceClient.listCaPools(request); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public static void deleteCertificateAuthority(String caPoolName) |
| 71 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 72 | + try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = |
| 73 | + CertificateAuthorityServiceClient.create()) { |
| 74 | + for (CertificateAuthority certificateAuthority : |
| 75 | + certificateAuthorityServiceClient.listCertificateAuthorities(caPoolName).iterateAll()) { |
| 76 | + |
| 77 | + DeleteCertificateAuthorityRequest deleteCertificateAuthorityRequest = |
| 78 | + DeleteCertificateAuthorityRequest.newBuilder() |
| 79 | + .setName(certificateAuthority.getName()) |
| 80 | + .setIgnoreActiveCertificates(false) |
| 81 | + .build(); |
| 82 | + |
| 83 | + certificateAuthorityServiceClient |
| 84 | + .deleteCertificateAuthorityCallable() |
| 85 | + .futureCall(deleteCertificateAuthorityRequest).get(5, TimeUnit.MINUTES); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments