Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit 4aa7e80

Browse files
authored
Merge pull request #28 from splunk/mw/te-188-short-hostnames
Use host value from cluster master to connect to
2 parents 1914b98 + 4e1528c commit 4aa7e80

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

path_config_connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func (b *backend) connectionWriteHandler(ctx context.Context, req *logical.Reque
252252

253253
func (b *backend) pathConnectionsList() *framework.Path {
254254
return &framework.Path{
255-
Pattern: fmt.Sprintf("config/?$"),
255+
Pattern: "config/?$",
256256

257257
Callbacks: map[logical.Operation]framework.OperationFunc{
258258
logical.ListOperation: b.connectionListHandler,

path_creds_create.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,30 +118,31 @@ func (b *backend) credsReadHandlerStandalone(ctx context.Context, req *logical.R
118118
"username": username,
119119
"role": name,
120120
"connection": role.Connection,
121+
"url": conn.Params().BaseURL, // new in v0.7.0
121122
})
122123
resp.Secret.TTL = role.DefaultTTL
123124
resp.Secret.MaxTTL = role.MaxTTL
124125

125126
return resp, nil
126127
}
127128

128-
func findNode(nodeFQDN string, hosts []splunk.ServerInfoEntry, roleConfig *roleConfig) (bool, error) {
129+
func findNode(nodeFQDN string, hosts []splunk.ServerInfoEntry, roleConfig *roleConfig) (*splunk.ServerInfoEntry, error) {
129130
for _, host := range hosts {
130131
// check if node_fqdn is in either of HostFQDN or Host. User might not always the FQDN on the cli input
131132
if strings.EqualFold(host.Content.HostFQDN, nodeFQDN) || strings.EqualFold(host.Content.Host, nodeFQDN) {
132-
// Return true if the requested node type is allowed
133+
// Return host if the requested node type is allowed
133134
if strutil.StrListContains(roleConfig.AllowedServerRoles, "*") {
134-
return true, nil
135+
return &host, nil
135136
}
136137
for _, role := range host.Content.Roles {
137138
if strutil.StrListContainsGlob(roleConfig.AllowedServerRoles, role) {
138-
return true, nil
139+
return &host, nil
139140
}
140141
}
141-
return false, fmt.Errorf("host %q does not have any of the allowed server roles: %q", nodeFQDN, roleConfig.AllowedServerRoles)
142+
return nil, fmt.Errorf("host %q does not have any of the allowed server roles: %q", nodeFQDN, roleConfig.AllowedServerRoles)
142143
}
143144
}
144-
return false, fmt.Errorf("host %q not found", nodeFQDN)
145+
return nil, fmt.Errorf("host %q not found", nodeFQDN)
145146
}
146147

147148
func (b *backend) credsReadHandlerMulti(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
@@ -181,15 +182,17 @@ func (b *backend) credsReadHandlerMulti(ctx context.Context, req *logical.Reques
181182
return nil, errwrap.Wrapf("unable to read searchpeers from cluster master: {{err}}", err)
182183
}
183184

184-
_, err = findNode(nodeFQDN, nodes, role)
185+
foundNode, err := findNode(nodeFQDN, nodes, role)
185186
if err != nil {
186187
return nil, err
187188
}
189+
if foundNode.Content.Host == "" {
190+
return nil, fmt.Errorf("host field unexpectedly empty for %q", nodeFQDN)
191+
}
192+
nodeFQDN = foundNode.Content.Host // the actual FQDN as returned by the cluster master, confusingly
188193

189194
// Re-create connection for node
190-
config.URL = "https://" + nodeFQDN + ":8089"
191-
// XXX config.ID = ""
192-
conn, err = config.newConnection(ctx) // XXX cache
195+
conn, err = b.ensureNodeConnection(ctx, config, nodeFQDN)
193196
if err != nil {
194197
return nil, err
195198
}
@@ -232,6 +235,7 @@ func (b *backend) credsReadHandlerMulti(ctx context.Context, req *logical.Reques
232235
"role": name,
233236
"connection": role.Connection,
234237
"node_fqdn": nodeFQDN,
238+
"url": conn.Params().BaseURL, // new in v0.7.0
235239
})
236240
resp.Secret.TTL = role.DefaultTTL
237241
resp.Secret.MaxTTL = role.MaxTTL

path_creds_create_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func Test_findNode(t *testing.T) {
136136
t.Errorf("findNode() error = %v, wantErr %v", err, tt.wantErr)
137137
return
138138
}
139-
if got != tt.want {
139+
if (got != nil) != tt.want {
140140
t.Errorf("findNode() = %v, want %v", got, tt.want)
141141
}
142142
})

0 commit comments

Comments
 (0)