Skip to content

Commit be5bc21

Browse files
committed
config: add client-authority
1 parent 50d1621 commit be5bc21

4 files changed

Lines changed: 387 additions & 33 deletions

File tree

internal/temporalcli/client.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func dialClient(cctx *CommandContext, c *cliext.ClientOptions) (client.Client, e
3838
c.Identity = "temporal-cli:" + username + "@" + hostname
3939
}
4040

41+
if err := applyClientAuthorityFromConfig(cctx, c); err != nil {
42+
return nil, err
43+
}
44+
4145
// Build client options using cliext
4246
builder := &cliext.ClientOptionsBuilder{
4347
CommonOptions: cctx.RootCommand.CommonOptions,
@@ -88,6 +92,28 @@ func dialClient(cctx *CommandContext, c *cliext.ClientOptions) (client.Client, e
8892
return cl, nil
8993
}
9094

95+
func applyClientAuthorityFromConfig(cctx *CommandContext, c *cliext.ClientOptions) error {
96+
if c.ClientAuthority != "" || (c.FlagSet != nil && c.FlagSet.Changed("client-authority")) {
97+
return nil
98+
}
99+
if cctx.RootCommand.DisableConfigFile {
100+
return nil
101+
}
102+
_, additionalProfileFields, err := loadEnvConfigFile(cctx)
103+
if err != nil {
104+
return err
105+
}
106+
if v, ok, err := clientAuthorityFromAdditionalProfileFields(
107+
additionalProfileFields,
108+
envConfigProfileName(cctx),
109+
); err != nil {
110+
return err
111+
} else if ok {
112+
c.ClientAuthority = v
113+
}
114+
return nil
115+
}
116+
91117
func fixedHeaderOverrideInterceptor(
92118
ctx context.Context,
93119
method string, req, reply any,
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package temporalcli
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/spf13/pflag"
8+
"github.com/stretchr/testify/require"
9+
"github.com/temporalio/cli/cliext"
10+
)
11+
12+
func TestApplyClientAuthorityFromConfig(t *testing.T) {
13+
f, err := os.CreateTemp("", "")
14+
require.NoError(t, err)
15+
defer os.Remove(f.Name())
16+
_, err = f.Write([]byte(`
17+
[profile.foo]
18+
client_authority = "profile-authority"`))
19+
require.NoError(t, err)
20+
require.NoError(t, f.Close())
21+
22+
cctx := &CommandContext{
23+
RootCommand: &TemporalCommand{
24+
CommonOptions: cliext.CommonOptions{
25+
ConfigFile: f.Name(),
26+
Profile: "foo",
27+
},
28+
},
29+
}
30+
var opts cliext.ClientOptions
31+
32+
require.NoError(t, applyClientAuthorityFromConfig(cctx, &opts))
33+
require.Equal(t, "profile-authority", opts.ClientAuthority)
34+
}
35+
36+
func TestApplyClientAuthorityFromConfig_ExplicitFlagWins(t *testing.T) {
37+
f, err := os.CreateTemp("", "")
38+
require.NoError(t, err)
39+
defer os.Remove(f.Name())
40+
_, err = f.Write([]byte(`
41+
[profile.foo]
42+
client_authority = "profile-authority"`))
43+
require.NoError(t, err)
44+
require.NoError(t, f.Close())
45+
46+
cctx := &CommandContext{
47+
RootCommand: &TemporalCommand{
48+
CommonOptions: cliext.CommonOptions{
49+
ConfigFile: f.Name(),
50+
Profile: "foo",
51+
},
52+
},
53+
}
54+
opts := cliext.ClientOptions{ClientAuthority: "flag-authority"}
55+
56+
require.NoError(t, applyClientAuthorityFromConfig(cctx, &opts))
57+
require.Equal(t, "flag-authority", opts.ClientAuthority)
58+
}
59+
60+
func TestApplyClientAuthorityFromConfig_ExplicitEmptyFlagWins(t *testing.T) {
61+
f, err := os.CreateTemp("", "")
62+
require.NoError(t, err)
63+
defer os.Remove(f.Name())
64+
_, err = f.Write([]byte(`
65+
[profile.foo]
66+
client_authority = "profile-authority"`))
67+
require.NoError(t, err)
68+
require.NoError(t, f.Close())
69+
70+
cctx := &CommandContext{
71+
RootCommand: &TemporalCommand{
72+
CommonOptions: cliext.CommonOptions{
73+
ConfigFile: f.Name(),
74+
Profile: "foo",
75+
},
76+
},
77+
}
78+
var opts cliext.ClientOptions
79+
opts.BuildFlags(pflag.NewFlagSet("test", pflag.ContinueOnError))
80+
require.NoError(t, opts.FlagSet.Set("client-authority", ""))
81+
82+
require.NoError(t, applyClientAuthorityFromConfig(cctx, &opts))
83+
require.Empty(t, opts.ClientAuthority)
84+
}

0 commit comments

Comments
 (0)