Skip to content

Commit 1e5f502

Browse files
committed
Allow proxy agent to connect to non local server.
Added configuration for where the proxy server is. Also improvend cert related errors for easier debug.
1 parent cff3efc commit 1e5f502

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ cfssljson:
6363
chmod +x cfssljson
6464

6565
certs: easy-rsa-master cfssl cfssljson
66+
# set up easy-rsa
6667
cp -rf easy-rsa-master/easyrsa3 easy-rsa-master/master
6768
cp -rf easy-rsa-master/easyrsa3 easy-rsa-master/agent
69+
# create the client <-> server-proxy connection certs
6870
cd easy-rsa-master/master; \
6971
./easyrsa init-pki; \
7072
./easyrsa --batch "--req-cn=127.0.0.1@$(date +%s)" build-ca nopass; \
@@ -76,6 +78,7 @@ certs: easy-rsa-master cfssl cfssljson
7678
cp -r easy-rsa-master/master/pki/private certs/master
7779
cp -r easy-rsa-master/master/pki/issued certs/master
7880
cp easy-rsa-master/master/pki/ca.crt certs/master/issued
81+
# create the agent <-> server-proxy connection certs
7982
cd easy-rsa-master/agent; \
8083
./easyrsa init-pki; \
8184
./easyrsa --batch "--req-cn=127.0.0.1@$(date +%s)" build-ca nopass; \

cmd/agent/main.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,47 +55,59 @@ func main() {
5555
}
5656

5757
type GrpcProxyAgentOptions struct {
58+
// Configuration for authenticating with the proxy-server
5859
agentCert string
5960
agentKey string
6061
caCert string
62+
63+
// Configuration for connecting to the proxy-server
64+
proxyServerHost string
65+
proxyServerPort int
6166
}
6267

6368
func (o *GrpcProxyAgentOptions) Flags() *pflag.FlagSet {
6469
flags := pflag.NewFlagSet("proxy-agent", pflag.ContinueOnError)
6570
flags.StringVar(&o.agentCert, "agentCert", o.agentCert, "If non-empty secure communication with this cert.")
6671
flags.StringVar(&o.agentKey, "agentKey", o.agentKey, "If non-empty secure communication with this key.")
6772
flags.StringVar(&o.caCert, "caCert", o.caCert, "If non-empty the CAs we use to validate clients.")
73+
flags.StringVar(&o.proxyServerHost, "proxyServerHost", o.proxyServerHost, "The hostname to use to connect to the proxy-server.")
74+
flags.IntVar(&o.proxyServerPort, "proxyServerPort", o.proxyServerPort, "The port the proxy server is listening on.")
6875
return flags
6976
}
7077

7178
func (o *GrpcProxyAgentOptions) Print() {
7279
klog.Warningf("AgentCert set to \"%s\".\n", o.agentCert)
7380
klog.Warningf("AgentKey set to \"%s\".\n", o.agentKey)
7481
klog.Warningf("CACert set to \"%s\".\n", o.caCert)
82+
klog.Warningf("ProxyServerHost set to \"%s\".\n", o.proxyServerHost)
83+
klog.Warningf("ProxyServerPort set to %d.\n", o.proxyServerPort)
7584
}
7685

7786
func (o *GrpcProxyAgentOptions) Validate() error {
7887
if o.agentKey != "" {
7988
if _, err := os.Stat(o.agentKey); os.IsNotExist(err) {
80-
return err
89+
return fmt.Errorf("error checking agent key %s, got %v", o.agentKey, err)
8190
}
8291
if o.agentCert == "" {
8392
return fmt.Errorf("cannot have agent cert empty when agent key is set to \"%s\"", o.agentKey)
8493
}
8594
}
8695
if o.agentCert != "" {
8796
if _, err := os.Stat(o.agentCert); os.IsNotExist(err) {
88-
return err
97+
return fmt.Errorf("error checking agent cert %s, got %v", o.agentCert, err)
8998
}
9099
if o.agentKey == "" {
91100
return fmt.Errorf("cannot have agent key empty when agent cert is set to \"%s\"", o.agentCert)
92101
}
93102
}
94103
if o.caCert != "" {
95104
if _, err := os.Stat(o.caCert); os.IsNotExist(err) {
96-
return err
105+
return fmt.Errorf("error checking agent CA cert %s, got %v", o.caCert, err)
97106
}
98107
}
108+
if o.proxyServerPort <= 0 {
109+
return fmt.Errorf("proxy server port %d must be greater than 0", o.proxyServerPort)
110+
}
99111
return nil
100112
}
101113

@@ -104,6 +116,8 @@ func newGrpcProxyAgentOptions() *GrpcProxyAgentOptions {
104116
agentCert: "",
105117
agentKey: "",
106118
caCert: "",
119+
proxyServerHost: "127.0.0.1",
120+
proxyServerPort: 8091,
107121
}
108122
return &o
109123
}
@@ -126,15 +140,15 @@ type Agent struct {
126140
func (a *Agent) run(o *GrpcProxyAgentOptions) error {
127141
o.Print()
128142
if err := o.Validate(); err != nil {
129-
return err
143+
return fmt.Errorf("failed to validate agent options with %v", err)
130144
}
131145

132146
if err := a.runProxyConnection(o); err != nil {
133-
return err
147+
return fmt.Errorf("failed to run proxy connection with %v", err)
134148
}
135149

136150
if err := a.runAdminServer(o); err != nil {
137-
return err
151+
return fmt.Errorf("failed to run admin server with %v", err)
138152
}
139153

140154
stopCh := make(chan struct{})
@@ -146,28 +160,28 @@ func (a *Agent) run(o *GrpcProxyAgentOptions) error {
146160
func (p *Agent) runProxyConnection(o *GrpcProxyAgentOptions) error {
147161
agentCert, err := tls.LoadX509KeyPair(o.agentCert, o.agentKey)
148162
if err != nil {
149-
return err
163+
return fmt.Errorf("failed to load X509 key pair %s and %s: %v", o.agentCert, o.agentKey, err)
150164
}
151165
certPool := x509.NewCertPool()
152166
caCert, err := ioutil.ReadFile(o.caCert)
153167
if err != nil {
154-
return err
168+
return fmt.Errorf("failed to read agent CA cert %s: %v", o.caCert, err)
155169
}
156170
ok := certPool.AppendCertsFromPEM(caCert)
157171
if !ok {
158172
return fmt.Errorf("failed to append CA cert to the cert pool")
159173
}
160174

161175
transportCreds := credentials.NewTLS(&tls.Config{
162-
ServerName: "127.0.0.1",
176+
ServerName: o.proxyServerHost,
163177
Certificates: []tls.Certificate{agentCert},
164178
RootCAs: certPool,
165179
})
166180
dialOption := grpc.WithTransportCredentials(transportCreds)
167-
client := agentclient.NewAgentClient("localhost:8091")
181+
client := agentclient.NewAgentClient(fmt.Sprintf("%s:%d", o.proxyServerHost, o.proxyServerPort))
168182

169183
if err := client.Connect(dialOption); err != nil {
170-
return err
184+
return fmt.Errorf("failed to connect to proxy-server: %v", err)
171185
}
172186

173187
stopCh := make(chan struct{})

cmd/proxy/main.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,44 +107,44 @@ func (o *ProxyRunOptions) Print() {
107107
func (o *ProxyRunOptions) Validate() error {
108108
if o.serverKey != "" {
109109
if _, err := os.Stat(o.serverKey); os.IsNotExist(err) {
110-
return err
110+
return fmt.Errorf("error checking server key %s, got %v", o.serverKey, err)
111111
}
112112
if o.serverCert == "" {
113113
return fmt.Errorf("cannot have server cert empty when server key is set to %q", o.serverKey)
114114
}
115115
}
116116
if o.serverCert != "" {
117117
if _, err := os.Stat(o.serverCert); os.IsNotExist(err) {
118-
return err
118+
return fmt.Errorf("error checking server cert %s, got %v", o.serverCert, err)
119119
}
120120
if o.serverKey == "" {
121121
return fmt.Errorf("cannot have server key empty when server cert is set to %q", o.serverCert)
122122
}
123123
}
124124
if o.serverCaCert != "" {
125125
if _, err := os.Stat(o.serverCaCert); os.IsNotExist(err) {
126-
return err
126+
return fmt.Errorf("error checking server CA cert %s, got %v", o.serverCaCert, err)
127127
}
128128
}
129129
if o.clusterKey != "" {
130130
if _, err := os.Stat(o.clusterKey); os.IsNotExist(err) {
131-
return err
131+
return fmt.Errorf("error checking cluster key %s, got %v", o.clusterKey, err)
132132
}
133133
if o.clusterCert == "" {
134134
return fmt.Errorf("cannot have cluster cert empty when cluster key is set to %q", o.clusterKey)
135135
}
136136
}
137137
if o.clusterCert != "" {
138138
if _, err := os.Stat(o.clusterCert); os.IsNotExist(err) {
139-
return err
139+
return fmt.Errorf("error checking cluster cert %s, got %v", o.clusterCert, err)
140140
}
141141
if o.clusterKey == "" {
142142
return fmt.Errorf("cannot have cluster key empty when cluster cert is set to %q", o.clusterCert)
143143
}
144144
}
145145
if o.clusterCaCert != "" {
146146
if _, err := os.Stat(o.clusterCaCert); os.IsNotExist(err) {
147-
return err
147+
return fmt.Errorf("error checking cluster CA cert %s, got %v", o.clusterCaCert, err)
148148
}
149149
}
150150
if o.mode != "grpc" && o.mode != "http-connect" {
@@ -205,26 +205,26 @@ type Proxy struct {
205205
func (p *Proxy) run(o *ProxyRunOptions) error {
206206
o.Print()
207207
if err := o.Validate(); err != nil {
208-
return err
208+
return fmt.Errorf("failed to validate server options with %v", err)
209209
}
210210
server := agentserver.NewProxyServer()
211211

212212
klog.Info("Starting master server for client connections.")
213213
err := p.runMasterServer(o, server)
214214
if err != nil {
215-
return err
215+
return fmt.Errorf("failed to run the master server: %v", err)
216216
}
217217

218218
klog.Info("Starting agent server for tunnel connections.")
219219
err = p.runAgentServer(o, server)
220220
if err != nil {
221-
return err
221+
return fmt.Errorf("failed to run the agent server: %v", err)
222222
}
223223

224224
klog.Info("Starting admin server for debug connections.")
225225
err = p.runAdminServer(o, server)
226226
if err != nil {
227-
return err
227+
return fmt.Errorf("failed to run the admin server: %v", err)
228228
}
229229

230230
stopCh := make(chan struct{})
@@ -236,12 +236,12 @@ func (p *Proxy) run(o *ProxyRunOptions) error {
236236
func (p *Proxy) runMasterServer(o *ProxyRunOptions, server *agentserver.ProxyServer) error {
237237
proxyCert, err := tls.LoadX509KeyPair(o.serverCert, o.serverKey)
238238
if err != nil {
239-
return err
239+
return fmt.Errorf("failed to load X509 key pair %s and %s: %v", o.serverCert, o.serverKey, err)
240240
}
241241
certPool := x509.NewCertPool()
242242
caCert, err := ioutil.ReadFile(o.serverCaCert)
243243
if err != nil {
244-
return err
244+
return fmt.Errorf("failed to read server CA cert %s: %v", o.serverCaCert, err)
245245
}
246246
ok := certPool.AppendCertsFromPEM(caCert)
247247
if !ok {
@@ -261,7 +261,7 @@ func (p *Proxy) runMasterServer(o *ProxyRunOptions, server *agentserver.ProxySer
261261
agent.RegisterProxyServiceServer(grpcServer, server)
262262
lis, err := net.Listen("tcp", addr)
263263
if err != nil {
264-
return err
264+
return fmt.Errorf("failed to listen on %s: %v", addr, err)
265265
}
266266
go grpcServer.Serve(lis)
267267
} else {
@@ -288,12 +288,12 @@ func (p *Proxy) runMasterServer(o *ProxyRunOptions, server *agentserver.ProxySer
288288
func (p *Proxy) runAgentServer(o *ProxyRunOptions, server *agentserver.ProxyServer) error {
289289
clusterCert, err := tls.LoadX509KeyPair(o.clusterCert, o.clusterKey)
290290
if err != nil {
291-
return err
291+
return fmt.Errorf("failed to load X509 key pair %s and %s: %v", o.clusterCert, o.clusterKey, err)
292292
}
293293
certPool := x509.NewCertPool()
294294
caCert, err := ioutil.ReadFile(o.clusterCaCert)
295295
if err != nil {
296-
return err
296+
return fmt.Errorf("failed to read cluster CA cert %s: %v", o.clusterCaCert, err)
297297
}
298298
ok := certPool.AppendCertsFromPEM(caCert)
299299
if !ok {
@@ -311,7 +311,7 @@ func (p *Proxy) runAgentServer(o *ProxyRunOptions, server *agentserver.ProxyServ
311311
agent.RegisterAgentServiceServer(grpcServer, server)
312312
lis, err := net.Listen("tcp", addr)
313313
if err != nil {
314-
return err
314+
return fmt.Errorf("failed to listen on %s: %v", addr, err)
315315
}
316316
go grpcServer.Serve(lis)
317317

0 commit comments

Comments
 (0)