Skip to content

Commit 00f7dd0

Browse files
Add API key property to Springboot (#2408)
Add API property to Springboot
1 parent fc4d714 commit 00f7dd0

File tree

5 files changed

+103
-5
lines changed

5 files changed

+103
-5
lines changed

temporal-spring-boot-autoconfigure/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ spring.temporal:
7575
# insecure-trust-manager: true # or add ca.pem to java default truststore
7676
```
7777

78+
## API Keys
79+
80+
You can also authenticate with Temporal Cloud using API keys
81+
82+
```yml
83+
spring.temporal:
84+
connection:
85+
apiKey: <API key>
86+
```
87+
88+
If an API key is specified, https will automatically be enabled.
89+
7890
## Data Converter
7991
8092
Define a bean of type `io.temporal.common.converter.DataConverter` in Spring context to be used as a custom data converter.

temporal-spring-boot-autoconfigure/src/main/java/io/temporal/spring/boot/autoconfigure/properties/ConnectionProperties.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class ConnectionProperties {
3636

3737
private final @Nullable MTLSProperties mtls;
3838

39+
private final @Nullable String apiKey;
40+
3941
/**
4042
* @param target {@link WorkflowServiceStubsOptions.Builder#setTarget(String)} also support
4143
* "local" alias for local temporal setup
@@ -44,10 +46,14 @@ public class ConnectionProperties {
4446
*/
4547
@ConstructorBinding
4648
public ConnectionProperties(
47-
@Nonnull String target, @Nullable Boolean enableHttps, @Nullable MTLSProperties mtls) {
49+
@Nonnull String target,
50+
@Nullable Boolean enableHttps,
51+
@Nullable MTLSProperties mtls,
52+
@Nullable String apiKey) {
4853
this.target = target;
4954
this.enableHttps = Boolean.TRUE.equals(enableHttps);
5055
this.mtls = mtls;
56+
this.apiKey = apiKey;
5157
}
5258

5359
@Nonnull
@@ -65,6 +71,11 @@ public MTLSProperties getMTLS() {
6571
return mtls;
6672
}
6773

74+
@Nullable
75+
public String getApiKey() {
76+
return apiKey;
77+
}
78+
6879
public static class MTLSProperties {
6980
private final @Nullable Integer pkcs;
7081

temporal-spring-boot-autoconfigure/src/main/java/io/temporal/spring/boot/autoconfigure/template/ServiceStubOptionsTemplate.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public WorkflowServiceStubsOptions createServiceStubOptions() {
6464

6565
stubsOptionsBuilder.setEnableHttps(Boolean.TRUE.equals(connectionProperties.isEnableHttps()));
6666

67+
if (connectionProperties.getApiKey() != null && connectionProperties.getApiKey().isEmpty()) {
68+
stubsOptionsBuilder.addApiKey(() -> connectionProperties.getApiKey());
69+
// Unless HTTPS is explicitly disabled, enable it by default for API keys
70+
if (connectionProperties.isEnableHttps() == null) {
71+
stubsOptionsBuilder.setEnableHttps(true);
72+
}
73+
}
74+
6775
configureMTLS(connectionProperties.getMTLS(), stubsOptionsBuilder);
6876

6977
if (metricsScope != null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3+
*
4+
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this material except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package io.temporal.spring.boot.autoconfigure;
22+
23+
import io.temporal.client.WorkflowClient;
24+
import io.temporal.spring.boot.autoconfigure.properties.TemporalProperties;
25+
import org.junit.jupiter.api.*;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.test.context.SpringBootTest;
28+
import org.springframework.context.ConfigurableApplicationContext;
29+
import org.springframework.context.annotation.ComponentScan;
30+
import org.springframework.context.annotation.FilterType;
31+
import org.springframework.test.context.ActiveProfiles;
32+
33+
@SpringBootTest(classes = ApiKeyAuthTest.Configuration.class)
34+
@ActiveProfiles(profiles = "api-key-auth")
35+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
36+
public class ApiKeyAuthTest {
37+
@Autowired ConfigurableApplicationContext applicationContext;
38+
39+
@Autowired TemporalProperties temporalProperties;
40+
@Autowired WorkflowClient workflowClient;
41+
42+
@BeforeEach
43+
void setUp() {
44+
applicationContext.start();
45+
}
46+
47+
@Test
48+
public void testProperties() {
49+
Assertions.assertEquals("my-api-key", temporalProperties.getConnection().getApiKey());
50+
}
51+
52+
@ComponentScan(
53+
excludeFilters =
54+
@ComponentScan.Filter(
55+
pattern = "io\\.temporal\\.spring\\.boot\\.autoconfigure\\.byworkername\\..*",
56+
type = FilterType.REGEX))
57+
public static class Configuration {}
58+
}

temporal-spring-boot-autoconfigure/src/test/resources/application.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020

2121
spring.temporal:
2222
connection:
23-
mtls:
24-
key-file: /path/to/key.key
25-
cert-chain-file: /path/to/cert.pem
2623
target: local
2724
test-server:
2825
enabled: true
@@ -125,4 +122,16 @@ spring:
125122
server-name: myservername
126123
target: 127.0.0.1:7233
127124
test-server:
128-
enabled: false
125+
enabled: false
126+
127+
---
128+
spring:
129+
config:
130+
activate:
131+
on-profile: api-key-auth
132+
temporal:
133+
connection:
134+
apiKey: my-api-key
135+
target: 127.0.0.1:7233
136+
test-server:
137+
enabled: false

0 commit comments

Comments
 (0)