Skip to content

Commit ad1dabc

Browse files
authored
SpringBoot - add server-name to mtls config options (#1998)
Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io>
1 parent 2b05f07 commit ad1dabc

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

temporal-spring-boot-autoconfigure-alpha/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ spring.temporal:
5656
cert-chain-file: /path/to/cert.pem # If you use PKCS12 (.pkcs12, .pfx or .p12), you don't need to set it because certificates chain is bundled into the key file
5757
# key-password: <password_for_the_key>
5858
# insecure-trust-manager: true # or add ca.pem to java default truststore
59+
# server-name: <server_name_override> # optional server name overrider, used as authority of ManagedChannelBuilder
5960
```
6061

6162
Alternatively with PKCS8 you can pass the content of the key and certificates chain as strings, which allows to pass them from the environment variable for example:

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public static class MTLSProperties {
7474
private final @Nullable String certChainFile;
7575
private final @Nullable String keyPassword;
7676
private final @Nullable Boolean insecureTrustManager;
77+
private final @Nullable String serverName;
7778

7879
/**
7980
* @param pkcs number of PKCS standard to use (8 and 12 are supported). Selects if {@link
@@ -96,14 +97,16 @@ public MTLSProperties(
9697
@Nullable String keyFile,
9798
@Nullable String certChainFile,
9899
@Nullable String keyPassword,
99-
@Nullable Boolean insecureTrustManager) {
100+
@Nullable Boolean insecureTrustManager,
101+
@Nullable String serverName) {
100102
this.pkcs = pkcs;
101103
this.key = key;
102104
this.certChain = certChain;
103105
this.keyFile = keyFile;
104106
this.certChainFile = certChainFile;
105107
this.keyPassword = keyPassword;
106108
this.insecureTrustManager = insecureTrustManager;
109+
this.serverName = serverName;
107110
}
108111

109112
@Nullable
@@ -140,5 +143,10 @@ public String getKeyPassword() {
140143
public Boolean getInsecureTrustManager() {
141144
return insecureTrustManager;
142145
}
146+
147+
@Nullable
148+
public String getServerName() {
149+
return serverName;
150+
}
143151
}
144152
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ private void configureMTLS(
151151
throw new BeanCreationException("Failure reading PKCS12 mTLS cert key file", e);
152152
}
153153
}
154+
155+
String serverName = mtlsProperties.getServerName();
156+
if (serverName != null) {
157+
stubsOptionsBuilder.setChannelInitializer(
158+
channelBuilder -> channelBuilder.overrideAuthority(serverName));
159+
}
154160
}
155161

156162
private void applyMTLSProperties(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 static org.junit.jupiter.api.Assertions.*;
24+
25+
import io.temporal.client.WorkflowClient;
26+
import io.temporal.spring.boot.autoconfigure.properties.TemporalProperties;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.TestInstance;
30+
import org.springframework.beans.factory.annotation.Autowired;
31+
import org.springframework.boot.test.context.SpringBootTest;
32+
import org.springframework.context.ConfigurableApplicationContext;
33+
import org.springframework.context.annotation.ComponentScan;
34+
import org.springframework.context.annotation.FilterType;
35+
import org.springframework.test.context.ActiveProfiles;
36+
37+
@SpringBootTest(classes = MTLSWithServerNameOverrideTest.Configuration.class)
38+
@ActiveProfiles(profiles = "mtls-with-server-name-override")
39+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
40+
public class MTLSWithServerNameOverrideTest {
41+
@Autowired ConfigurableApplicationContext applicationContext;
42+
@Autowired TemporalProperties temporalProperties;
43+
@Autowired WorkflowClient workflowClient;
44+
45+
@BeforeEach
46+
void setUp() {
47+
applicationContext.start();
48+
}
49+
50+
@Test
51+
public void testProperties() {
52+
assertEquals("myservername", temporalProperties.getConnection().getMTLS().getServerName());
53+
}
54+
55+
@Test
56+
public void testClient() {
57+
assertEquals(
58+
"myservername", workflowClient.getWorkflowServiceStubs().getRawChannel().authority());
59+
}
60+
61+
@ComponentScan(
62+
excludeFilters =
63+
@ComponentScan.Filter(
64+
pattern = "io\\.temporal\\.spring\\.boot\\.autoconfigure\\.byworkername\\..*",
65+
type = FilterType.REGEX))
66+
public static class Configuration {}
67+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,17 @@ spring:
110110
- io.temporal.spring.boot.autoconfigure.bytaskqueue
111111
start-workers: false
112112

113+
---
114+
spring:
115+
config:
116+
activate:
117+
on-profile: mtls-with-server-name-override
118+
temporal:
119+
connection:
120+
mtls:
121+
key-file: classpath:pkcs8-pk.pem
122+
cert-chain-file: classpath:pkcs8-crt-chain.pem
123+
server-name: myservername
124+
target: 127.0.0.1:7233
125+
test-server:
126+
enabled: false

0 commit comments

Comments
 (0)