Skip to content

Commit 62bc27a

Browse files
the agent identifiers pass to the proxy agent must be URL encoded
1 parent 0a13b9f commit 62bc27a

File tree

5 files changed

+55
-40
lines changed

5 files changed

+55
-40
lines changed

cmd/agent/main.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ package main
1818

1919
import (
2020
"crypto/tls"
21-
"errors"
2221
"flag"
2322
"fmt"
2423
"net"
2524
"net/http"
25+
"net/url"
2626
"os"
27-
"strings"
2827
"time"
2928

3029
"github.com/google/uuid"
@@ -105,7 +104,7 @@ func (o *GrpcProxyAgentOptions) Flags() *pflag.FlagSet {
105104
flags.DurationVar(&o.syncInterval, "sync-interval", o.syncInterval, "The initial interval by which the agent periodically checks if it has connections to all instances of the proxy server.")
106105
flags.DurationVar(&o.probeInterval, "probe-interval", o.probeInterval, "The interval by which the agent periodically checks if its connections to the proxy server are ready.")
107106
flags.StringVar(&o.serviceAccountTokenPath, "service-account-token-path", o.serviceAccountTokenPath, "If non-empty proxy agent uses this token to prove its identity to the proxy server.")
108-
flags.StringVar(&o.agentIdentifiers, "agent-identifiers", o.agentIdentifiers, "Identifiers of the agent that will be used by the server when choosing agent. e.g.,host=localhost,host=node1.mydomain.com,cidr=127.0.0.1/16,ipv4=1.2.3.4,ipv4=5.6.7.8,ipv6=:::::")
107+
flags.StringVar(&o.agentIdentifiers, "agent-identifiers", o.agentIdentifiers, "Identifiers of the agent that will be used by the server when choosing agent. N.B. the list of identifiers must be in URL encoded format. e.g.,host=localhost&host=node1.mydomain.com&cidr=127.0.0.1/16&ipv4=1.2.3.4&ipv4=5.6.7.8&ipv6=:::::")
109108
return flags
110109
}
111110

@@ -168,20 +167,18 @@ func (o *GrpcProxyAgentOptions) Validate() error {
168167
}
169168

170169
func validateAgentIdentifiers(agentIdentifiers string) error {
171-
entries := strings.Split(agentIdentifiers, ",")
172-
for _, entry := range entries {
173-
kv := strings.Split(entry, "=")
174-
if len(kv) != 2 {
175-
return errors.New("invalid arguments format, the valid format is " +
176-
"<addressType1>=<address1>,<addressType2>=<address2>")
177-
}
178-
switch agent.IdentifierType(kv[0]) {
170+
decoded, err := url.ParseQuery(agentIdentifiers)
171+
if err != nil {
172+
return err
173+
}
174+
for idType := range decoded {
175+
switch agent.IdentifierType(idType) {
179176
case agent.IPv4:
180177
case agent.IPv6:
181178
case agent.CIDR:
182179
case agent.Host:
183180
default:
184-
return fmt.Errorf("unknown address type: %s", kv[0])
181+
return fmt.Errorf("unknown address type: %s", idType)
185182
}
186183
}
187184
return nil

cmd/server/main.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,12 @@ func (o *ProxyRunOptions) Validate() error {
294294
}
295295

296296
// validate the proxy strategies
297-
pss := strings.Split(o.proxyStrategies, ",")
298-
for _, ps := range pss {
299-
if ps != string(server.ProxyStrategyDestHost) {
300-
return fmt.Errorf("unknown proxy strategy: %s, available strategy is: destHost", ps)
297+
if o.proxyStrategies != "" {
298+
pss := strings.Split(o.proxyStrategies, ",")
299+
for _, ps := range pss {
300+
if ps != string(server.ProxyStrategyDestHost) {
301+
return fmt.Errorf("unknown proxy strategy: %s, available strategy is: destHost", ps)
302+
}
301303
}
302304
}
303305

pkg/agent/client.go

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ limitations under the License.
1717
package agent
1818

1919
import (
20+
"bytes"
2021
"context"
2122
"fmt"
2223
"io"
2324
"io/ioutil"
2425
"net"
26+
"net/url"
2527
"strconv"
2628
"strings"
2729
"sync"
@@ -99,37 +101,48 @@ const (
99101
UID IdentifierType = "uid"
100102
)
101103

104+
// String prints out each <identifierType>=<identifier> separately
105+
func (a AgentIdentifiers) String() string {
106+
var buf bytes.Buffer
107+
for _, ipv4 := range a.IPv4 {
108+
buf.WriteString(fmt.Sprintf("ipv4=%s,", ipv4))
109+
}
110+
for _, ipv6 := range a.IPv6 {
111+
buf.WriteString(fmt.Sprintf("ipv6=%s,", ipv6))
112+
}
113+
for _, host := range a.Host {
114+
buf.WriteString(fmt.Sprintf("host=%s,", host))
115+
}
116+
for _, cidr := range a.Host {
117+
buf.WriteString(fmt.Sprintf("cidr=%s,", cidr))
118+
}
119+
return strings.TrimSuffix(buf.String(), ",")
120+
}
121+
102122
// GenAgentIdentifiers generates an AgentIdentifiers based on the input string, the
103123
// input string should be a comma-seprated list with each item in the format
104124
// of <IdentifierType>=<address>
105-
func GenAgentIdentifiers(addrs string) AgentIdentifiers {
125+
func GenAgentIdentifiers(addrs string) (AgentIdentifiers, error) {
106126
var agentIDs AgentIdentifiers
107-
entries := strings.Split(addrs, ",")
108-
for _, entry := range entries {
109-
kv := strings.Split(entry, "=")
110-
if len(kv) != 2 {
111-
klog.V(4).InfoS("Invalid agent address input format",
112-
"got", entry, "expect", "<IdentifierType>=<address>")
113-
continue
114-
}
115-
if kv[1] == "" {
116-
continue
117-
}
118-
switch IdentifierType(kv[0]) {
127+
decoded, err := url.ParseQuery(addrs)
128+
if err != nil {
129+
return agentIDs, fmt.Errorf("fail to parse url encoded string: %v", err)
130+
}
131+
for idType, ids := range decoded {
132+
switch IdentifierType(idType) {
119133
case IPv4:
120-
agentIDs.IPv4 = append(agentIDs.IPv4, kv[1])
134+
agentIDs.IPv4 = append(agentIDs.IPv4, ids...)
121135
case IPv6:
122-
agentIDs.IPv6 = append(agentIDs.IPv6, kv[1])
136+
agentIDs.IPv6 = append(agentIDs.IPv6, ids...)
123137
case Host:
124-
agentIDs.Host = append(agentIDs.Host, kv[1])
138+
agentIDs.Host = append(agentIDs.Host, ids...)
125139
case CIDR:
126-
agentIDs.CIDR = append(agentIDs.CIDR, kv[1])
140+
agentIDs.CIDR = append(agentIDs.CIDR, ids...)
127141
default:
128-
klog.V(5).InfoS("Unknown address type", "Address Type", kv[0])
129-
continue
142+
return agentIDs, fmt.Errorf("Unknown address type: %s", idType)
130143
}
131144
}
132-
return agentIDs
145+
return agentIDs, nil
133146
}
134147

135148
// AgentClient runs on the node network side. It connects to proxy server and establishes

pkg/server/backend_manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,18 @@ func containIdType(idTypes []pkgagent.IdentifierType, idType pkgagent.Identifier
168168
// AddBackend adds a backend.
169169
func (s *DefaultBackendStorage) AddBackend(identifier string, idType pkgagent.IdentifierType, conn agent.AgentService_ConnectServer) Backend {
170170
if !containIdType(s.idTypes, idType) {
171-
klog.ErrorS(&ErrWrongIDType{idType, s.idTypes}, "fail to add backend")
171+
klog.V(4).InfoS("fial to add backend", "backend", identifier, "error", &ErrWrongIDType{idType, s.idTypes})
172172
return nil
173173
}
174-
klog.V(2).InfoS("Register backend for agent", "connection", conn, "agentID", agentID)
174+
klog.V(2).InfoS("Register backend for agent", "connection", conn, "agentID", identifier)
175175
s.mu.Lock()
176176
defer s.mu.Unlock()
177177
_, ok := s.backends[identifier]
178178
addedBackend := newBackend(conn)
179179
if ok {
180180
for _, v := range s.backends[identifier] {
181181
if v.conn == conn {
182-
klog.V(1).InfoS("This should not happen. Adding existing backend for agent", "connection", conn, "agentID", agentID)
182+
klog.V(1).InfoS("This should not happen. Adding existing backend for agent", "connection", conn, "agentID", identifier)
183183
return v
184184
}
185185
}

pkg/server/server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,10 @@ func getAgentIdentifiers(stream agent.AgentService_ConnectServer) (pkgagent.Agen
485485
return agentIdentifiers, nil
486486
}
487487

488-
agentIdentifiers = pkgagent.GenAgentIdentifiers(agentIDs[0])
488+
agentIdentifiers, err := pkgagent.GenAgentIdentifiers(agentIDs[0])
489+
if err != nil {
490+
return agentIdentifiers, err
491+
}
489492
return agentIdentifiers, nil
490493
}
491494

0 commit comments

Comments
 (0)