Skip to content

Commit 23c9cdc

Browse files
authored
Add TLS support to Temporal server connection #3
1 parent e6eec52 commit 23c9cdc

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ provider "temporal" {
3535

3636
- `address` (String) Address of the Temporal server. Of the form `host:port`.
3737
- `namespace` (String) Namespace to operate in.
38+
- `tls` (Bool) Whether to use TLS for the Temporal server connection. Defaults to `false`.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/hashicorp/terraform-plugin-testing v1.9.0
1111
go.temporal.io/api v1.34.0
1212
go.temporal.io/sdk v1.27.0
13+
google.golang.org/grpc v1.64.0
1314
google.golang.org/protobuf v1.34.1
1415
)
1516

@@ -94,7 +95,6 @@ require (
9495
google.golang.org/appengine v1.6.8 // indirect
9596
google.golang.org/genproto/googleapis/api v0.0.0-20240521202816-d264139d666e // indirect
9697
google.golang.org/genproto/googleapis/rpc v0.0.0-20240521202816-d264139d666e // indirect
97-
google.golang.org/grpc v1.64.0 // indirect
9898
gopkg.in/yaml.v2 v2.3.0 // indirect
9999
gopkg.in/yaml.v3 v3.0.1 // indirect
100100
)

internal/provider/namespace_resource.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ func (r *namespaceResource) Schema(_ context.Context, _ resource.SchemaRequest,
164164
},
165165
"history_archival_state": schema.StringAttribute{
166166
MarkdownDescription: "History archival state. Accepted values: `disabled`, `enabled`. History archival must be enabled at the cluster level first to be able to enable it for a namespace.",
167-
Optional: true,
168-
Computed: true,
169-
Default: stringdefault.StaticString("disabled"),
167+
Optional: true,
168+
Computed: true,
169+
Default: stringdefault.StaticString("disabled"),
170170
Validators: []validator.String{
171171
validators.StringInSliceValidator{
172172
AllowedValues: []string{"enabled", "disabled"},
@@ -376,6 +376,7 @@ func (r *namespaceResource) Delete(ctx context.Context, req resource.DeleteReque
376376

377377
_, err := r.client.OperatorService().DeleteNamespace(ctx, &operatorservice.DeleteNamespaceRequest{
378378
NamespaceId: data.ID.ValueString(),
379+
Namespace: data.Name.ValueString(),
379380
})
380381
if err != nil {
381382
resp.Diagnostics.AddError("Error while deleting namespace "+data.Name.ValueString(), err.Error())

internal/provider/provider.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ package provider
22

33
import (
44
"context"
5+
"crypto/tls"
6+
"crypto/x509"
7+
"fmt"
58
"github.com/hashicorp/terraform-plugin-framework/path"
69
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"google.golang.org/grpc"
11+
"google.golang.org/grpc/credentials"
712
"os"
13+
"strconv"
814

915
"github.com/hashicorp/terraform-plugin-framework/datasource"
1016
"github.com/hashicorp/terraform-plugin-framework/provider"
@@ -22,6 +28,7 @@ var (
2228
type temporalProviderModel struct {
2329
Address types.String `tfsdk:"address"`
2430
Namespace types.String `tfsdk:"namespace"`
31+
TLS types.Bool `tfsdk:"tls"`
2532
}
2633

2734
type providerConfig struct {
@@ -55,13 +62,17 @@ func (p *temporalProvider) Schema(_ context.Context, _ provider.SchemaRequest, r
5562
MarkdownDescription: "The Temporal Terraform Provider allows you to manage [Temporal](https://temporal.io/) resources using [Terraform](https://www.terraform.io/).",
5663
Attributes: map[string]schema.Attribute{
5764
"address": schema.StringAttribute{
58-
Optional: true,
65+
Optional: true,
5966
MarkdownDescription: "Address of the Temporal server. Of the form `host:port`.",
6067
},
6168
"namespace": schema.StringAttribute{
6269
Optional: true,
6370
Description: "Namespace to operate in.",
6471
},
72+
"tls": schema.BoolAttribute{
73+
Optional: true,
74+
MarkdownDescription: "Whether to use TLS for the Temporal server connection. Defaults to `false`.",
75+
},
6576
},
6677
}
6778
}
@@ -83,6 +94,22 @@ func (p *temporalProvider) Configure(ctx context.Context, req provider.Configure
8394
address = config.Address.ValueString()
8495
}
8596

97+
tlsEnabled := false
98+
if !config.TLS.IsNull() {
99+
tlsEnabled = config.TLS.ValueBool()
100+
} else if os.Getenv("TLS") != "" {
101+
var err error
102+
tlsEnabled, err = strconv.ParseBool(os.Getenv("TLS"))
103+
if err != nil {
104+
resp.Diagnostics.AddAttributeError(
105+
path.Root("tls"),
106+
fmt.Sprintf("Invalid value for TLS parameter: %s", os.Getenv("TLS")),
107+
"TLS parameter value must be one of: true, false",
108+
)
109+
return
110+
}
111+
}
112+
86113
namespace := "default"
87114
if !config.Namespace.IsNull() {
88115
namespace = config.Namespace.ValueString()
@@ -104,10 +131,27 @@ func (p *temporalProvider) Configure(ctx context.Context, req provider.Configure
104131
return
105132
}
106133

107-
temporalClient, err := client.Dial(client.Options{
134+
clientOptions := client.Options{
108135
HostPort: address,
109136
Namespace: namespace,
110-
})
137+
}
138+
if tlsEnabled {
139+
pool, err := x509.SystemCertPool()
140+
if err != nil {
141+
resp.Diagnostics.AddError("Couldn't load the system CA certificate pool", err.Error())
142+
return
143+
}
144+
creds := credentials.NewTLS(&tls.Config{
145+
RootCAs: pool,
146+
})
147+
dialOptions := []grpc.DialOption{
148+
grpc.WithTransportCredentials(creds),
149+
}
150+
clientOptions.ConnectionOptions = client.ConnectionOptions{
151+
DialOptions: dialOptions,
152+
}
153+
}
154+
temporalClient, err := client.Dial(clientOptions)
111155
if err != nil {
112156
resp.Diagnostics.AddError("Failed to establish a connection with the Temporal server on "+address, err.Error())
113157
return

0 commit comments

Comments
 (0)