Skip to content

Commit 1d67335

Browse files
ssvirSita04
andauthored
refactor(compute): create Certificate samples (#9371)
* init * added secret variable * fix lint * check that PrivateKey not null * add new secret to kokoro * use env variable instead of SecretManager --------- Co-authored-by: Sita Lakshmi Sangameswaran <[email protected]>
1 parent d6f8562 commit 1d67335

File tree

6 files changed

+427
-0
lines changed

6 files changed

+427
-0
lines changed

.kokoro/tests/run_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ if [[ "$SCRIPT_DEBUG" != "true" ]]; then
7373
"java-automl-samples-secrets.txt" \
7474
"java-bigtable-samples-secrets.txt" \
7575
"java-cloud-sql-samples-secrets.txt" \
76+
"java-compute-samples-secrets.txt" \
7677
"java-cts-v4-samples-secrets.txt" \
7778
"java-dlp-samples-secrets.txt" \
7879
"java-functions-samples-secrets.txt" \

compute/load-balancing/pom.xml

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2024 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xmlns="http://maven.apache.org/POM/4.0.0"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<groupId>com.example.compute</groupId>
22+
<artifactId>load-balancing-samples</artifactId>
23+
<version>1.0-SNAPSHOT</version>
24+
25+
<!--
26+
The parent pom defines common style checks and testing strategies for our samples.
27+
Removing or replacing it should not affect the execution of the samples in anyway.
28+
-->
29+
<parent>
30+
<artifactId>shared-configuration</artifactId>
31+
<groupId>com.google.cloud.samples</groupId>
32+
<version>1.2.0</version>
33+
</parent>
34+
35+
<properties>
36+
<maven.compiler.source>11</maven.compiler.source>
37+
<maven.compiler.target>11</maven.compiler.target>
38+
</properties>
39+
40+
<dependencies>
41+
<dependency>
42+
<artifactId>google-cloud-compute</artifactId>
43+
<groupId>com.google.cloud</groupId>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.google.api</groupId>
47+
<artifactId>gax</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.google.cloud</groupId>
51+
<artifactId>google-cloud-secretmanager</artifactId>
52+
</dependency>
53+
54+
<!-- Test dependencies -->
55+
<dependency>
56+
<artifactId>google-cloud-storage</artifactId>
57+
<groupId>com.google.cloud</groupId>
58+
<scope>test</scope>
59+
</dependency>
60+
<dependency>
61+
<artifactId>google-cloud-kms</artifactId>
62+
<groupId>com.google.cloud</groupId>
63+
<scope>test</scope>
64+
</dependency>
65+
66+
<dependency>
67+
<artifactId>truth</artifactId>
68+
<groupId>com.google.truth</groupId>
69+
<scope>test</scope>
70+
<version>1.4.0</version>
71+
</dependency>
72+
<dependency>
73+
<artifactId>junit</artifactId>
74+
<groupId>junit</groupId>
75+
<scope>test</scope>
76+
<version>4.13.2</version>
77+
</dependency>
78+
79+
<!--
80+
JUnit Jupiter dependencies to run BeforeEach and AfterEach methods
81+
(in tandem with mvn surefire) before every test.
82+
Without these, mvn surefire skips these methods and leads to concurrency
83+
issues.
84+
-->
85+
<dependency>
86+
<groupId>org.junit.jupiter</groupId>
87+
<artifactId>junit-jupiter-engine</artifactId>
88+
<version>5.10.2</version>
89+
<scope>test</scope>
90+
</dependency>
91+
</dependencies>
92+
93+
<dependencyManagement>
94+
<dependencies>
95+
<dependency>
96+
<artifactId>libraries-bom</artifactId>
97+
<groupId>com.google.cloud</groupId>
98+
<scope>import</scope>
99+
<type>pom</type>
100+
<version>26.40.0</version>
101+
</dependency>
102+
</dependencies>
103+
</dependencyManagement>
104+
105+
106+
107+
108+
<build>
109+
<plugins>
110+
<plugin>
111+
<groupId>org.apache.maven.plugins</groupId>
112+
<artifactId>maven-surefire-plugin</artifactId>
113+
<version>3.2.5</version>
114+
<configuration>
115+
<!-- run both classes and methods in parallel -->
116+
<parallel>all</parallel>
117+
<useUnlimitedThreads>true</useUnlimitedThreads>
118+
<forkCount>10C</forkCount>
119+
<reuseForks>true</reuseForks>
120+
<includes>
121+
<include>**/*IT.java</include>
122+
</includes>
123+
<skip>false</skip>
124+
</configuration>
125+
</plugin>
126+
<plugin>
127+
<groupId>org.apache.maven.plugins</groupId>
128+
<artifactId>maven-failsafe-plugin</artifactId>
129+
<version>3.2.5</version>
130+
<configuration>
131+
<skip>true</skip>
132+
</configuration>
133+
</plugin>
134+
</plugins>
135+
</build>
136+
137+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIEKzCCAxOgAwIBAgIUBqe2Dqsf7G8nyLFLUt6AxeAzM8kwDQYJKoZIhvcNAQEL
3+
BQAwgacxCzAJBgNVBAYTAlBMMRQwEgYDVQQIDAtNYXpvd2llY2tpZTEPMA0GA1UE
4+
BwwGV2Fyc2F3MRwwGgYDVQQKDBNHb29nbGUgQ2xvdWQgUG9sYW5kMQ8wDQYDVQQL
5+
DAZEZXZSZWwxFTATBgNVBAMMDEdvb2dsZSBDbG91ZDErMCkGCSqGSIb3DQEJARYc
6+
ZG9udHdyaXRlaGVyZUBub3QtZ29vZ2xlLmNvbTAeFw0yMjAxMjAxNzEzMzlaFw0z
7+
MjAxMTgxNzEzMzlaMIGnMQswCQYDVQQGEwJQTDEUMBIGA1UECAwLTWF6b3dpZWNr
8+
aWUxDzANBgNVBAcMBldhcnNhdzEcMBoGA1UECgwTR29vZ2xlIENsb3VkIFBvbG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----
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2024 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 compute;
18+
19+
// [START compute_certificate_create]
20+
21+
import com.google.cloud.compute.v1.InsertSslCertificateRequest;
22+
import com.google.cloud.compute.v1.SslCertificate;
23+
import com.google.cloud.compute.v1.SslCertificatesClient;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.TimeoutException;
28+
29+
public class CreateCertificate {
30+
31+
public static void main(String[] args)
32+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
// Project ID or project number of the Google Cloud project you want to use.
35+
String project = "your-project-id";
36+
// The certificate you want to create in your project.
37+
String certificate = "your-certificate";
38+
// The private key you used to sign the certificate with.
39+
String privateKey = "your-private-key";
40+
// Name for the certificate once it's created in your project.
41+
String certificateName = "your-certificate-name";
42+
43+
createCertificate(project, certificate, privateKey, certificateName);
44+
}
45+
46+
// Create a global SSL self-signed certificate within your Google Cloud project.
47+
public static SslCertificate createCertificate(String project, String certificate,
48+
String privateKey, String certificateName)
49+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
50+
// Initialize client that will be used to send requests. This client only needs to be created
51+
// once, and can be reused for multiple requests.
52+
try (SslCertificatesClient client = SslCertificatesClient.create()) {
53+
SslCertificate certificateResource = SslCertificate.newBuilder()
54+
.setCertificate(certificate)
55+
.setPrivateKey(privateKey)
56+
.setName(certificateName)
57+
.build();
58+
59+
InsertSslCertificateRequest request = InsertSslCertificateRequest.newBuilder()
60+
.setProject(project)
61+
.setSslCertificateResource(certificateResource)
62+
.build();
63+
64+
client.insertCallable().futureCall(request).get(60, TimeUnit.SECONDS);
65+
66+
// Wait for server update
67+
TimeUnit.SECONDS.sleep(1);
68+
69+
SslCertificate sslCert = client.get(project, certificateName);
70+
71+
System.out.printf("Certificate '%s' has been created successfully", sslCert.getName());
72+
73+
return sslCert;
74+
}
75+
}
76+
}
77+
// [END compute_certificate_create]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2024 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 compute;
18+
19+
// [START compute_certificate_create_regional]
20+
21+
import com.google.cloud.compute.v1.InsertRegionSslCertificateRequest;
22+
import com.google.cloud.compute.v1.RegionSslCertificatesClient;
23+
import com.google.cloud.compute.v1.SslCertificate;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.TimeoutException;
28+
29+
public class CreateRegionalCertificate {
30+
public static void main(String[] args)
31+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
// Project ID or project number of the Google Cloud project you want to use.
34+
String project = "your-project-id";
35+
// The certificate you want to create in your project.
36+
String certificate = "your-certificate";
37+
// The private key you used to sign the certificate with.
38+
String privateKey = "your-private-key";
39+
// Name for the certificate once it's created in your project.
40+
String certificateName = "your-certificate-name";
41+
// Name of the region you want to use.
42+
String region = "your-region";
43+
44+
createRegionCertificate(project, certificate, region, privateKey, certificateName);
45+
}
46+
47+
// Create a regional SSL self-signed certificate within your Google Cloud project.
48+
public static SslCertificate createRegionCertificate(String project, String certificate,
49+
String region, String privateKey,
50+
String certificateName)
51+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
52+
// Initialize client that will be used to send requests. This client only needs to be created
53+
// once, and can be reused for multiple requests.
54+
try (RegionSslCertificatesClient client = RegionSslCertificatesClient.create()) {
55+
SslCertificate certificateResource = SslCertificate.newBuilder()
56+
.setCertificate(certificate)
57+
.setPrivateKey(privateKey)
58+
.setName(certificateName)
59+
.build();
60+
61+
InsertRegionSslCertificateRequest request = InsertRegionSslCertificateRequest.newBuilder()
62+
.setProject(project)
63+
.setRegion(region)
64+
.setSslCertificateResource(certificateResource)
65+
.build();
66+
67+
client.insertCallable().futureCall(request).get(60, TimeUnit.SECONDS);
68+
69+
// Wait for server update
70+
TimeUnit.SECONDS.sleep(1);
71+
72+
SslCertificate sslCert = client.get(project, region, certificateName);
73+
74+
System.out.printf("Regional cert '%s' has been created successfully", sslCert.getName());
75+
76+
return sslCert;
77+
}
78+
}
79+
}
80+
// [END compute_certificate_create_regional]

0 commit comments

Comments
 (0)