From bfd15d8d8a63cf92c4b5433f2fcd47d4bfbc2d0a Mon Sep 17 00:00:00 2001 From: David Dymko Date: Mon, 9 Mar 2020 19:50:18 -0400 Subject: [PATCH 1/4] addSSL call --- load_balancer.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/load_balancer.go b/load_balancer.go index 623b666..c848e62 100644 --- a/load_balancer.go +++ b/load_balancer.go @@ -27,6 +27,8 @@ type LoadBalancerService interface { HasSSL(ctx context.Context, ID int) (*struct{ SSLInfo bool `json:"has_ssl"` }, error) Create(ctx context.Context, region int, genericInfo *GenericInfo, healthCheck *HealthCheck, rules []ForwardingRule) (*LoadBalancers, error) UpdateGenericInfo(ctx context.Context, ID int, label string, genericInfo *GenericInfo) error + AddSSL(ctx context.Context, ID int, ssl *SSL) error + //RemoveSSL(ctx context.Context, ID int) error } // LoadBalancerHandler handles interaction with the server methods for the Vultr API @@ -98,6 +100,13 @@ type LBConfig struct { InstanceList } +// SSL represents valid SSL config +type SSL struct { + PrivateKey string `json:"ssl_private_key"` + Certificate string `json:"ssl_certificate"` + Chain string `json:"chain,omitempty"` +} + // List all load balancer subscriptions on the current account. func (l *LoadBalancerHandler) List(ctx context.Context) ([]LoadBalancers, error) { uri := "/v1/loadbalancer/list" @@ -533,3 +542,30 @@ func (l *LoadBalancerHandler) UpdateGenericInfo(ctx context.Context, ID int, lab return nil } + +// AddSSL will attach an SSL certificate to a given load balancer +func (l *LoadBalancerHandler) AddSSL(ctx context.Context, ID int, ssl *SSL) error { + uri := "/v1/loadbalancer/ssl_add" + + values := url.Values{ + "SUBID": {strconv.Itoa(ID)}, + "ssl_private_key": {ssl.PrivateKey}, + "ssl_certificate": {ssl.Certificate}, + } + + if ssl.Chain != "" { + values.Add("ssl_chain", ssl.Chain) + } + + req, err := l.client.NewRequest(ctx, http.MethodPost, uri, values) + if err != nil { + return err + } + + err = l.client.DoWithContext(ctx, req, nil) + if err != nil { + return err + } + + return nil +} From 19261d7c51d520ef8d4b553c8da04b4ffec49cdb Mon Sep 17 00:00:00 2001 From: David Dymko Date: Mon, 9 Mar 2020 19:55:32 -0400 Subject: [PATCH 2/4] remove ssl call --- load_balancer.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/load_balancer.go b/load_balancer.go index c848e62..6528f42 100644 --- a/load_balancer.go +++ b/load_balancer.go @@ -28,7 +28,7 @@ type LoadBalancerService interface { Create(ctx context.Context, region int, genericInfo *GenericInfo, healthCheck *HealthCheck, rules []ForwardingRule) (*LoadBalancers, error) UpdateGenericInfo(ctx context.Context, ID int, label string, genericInfo *GenericInfo) error AddSSL(ctx context.Context, ID int, ssl *SSL) error - //RemoveSSL(ctx context.Context, ID int) error + RemoveSSL(ctx context.Context, ID int) error } // LoadBalancerHandler handles interaction with the server methods for the Vultr API @@ -569,3 +569,23 @@ func (l *LoadBalancerHandler) AddSSL(ctx context.Context, ID int, ssl *SSL) erro return nil } + +// RemoveSSL will remove an SSL certificate from a load balancer +func (l *LoadBalancerHandler) RemoveSSL(ctx context.Context, ID int) error { + uri := "/v1/loadbalancer/ssl_remove" + + values := url.Values{ + "SUBID": {strconv.Itoa(ID)}, + } + + req, err := l.client.NewRequest(ctx, http.MethodPost, uri, values) + if err != nil { + return err + } + + err = l.client.DoWithContext(ctx, req, nil) + if err != nil { + return err + } + return nil +} From d493cc55f3490af69aa33463269ecaa61d315d8b Mon Sep 17 00:00:00 2001 From: David Dymko Date: Mon, 9 Mar 2020 20:06:16 -0400 Subject: [PATCH 3/4] lb ssl tests --- load_balancer_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/load_balancer_test.go b/load_balancer_test.go index b39956b..d96647f 100644 --- a/load_balancer_test.go +++ b/load_balancer_test.go @@ -421,3 +421,39 @@ func TestLoadBalancerHandler_UpdateGenericInfo(t *testing.T) { t.Errorf("LoadBalancer.UpdateGenericInfo returned %+v", err) } } + +func TestLoadBalancerHandler_AddSSL (t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/v1/loadbalancer/ssl_add", func(writer http.ResponseWriter, request *http.Request) { + fmt.Fprint(writer) + }) + + + ssl := &SSL{ + PrivateKey: "key", + Certificate: "crt", + Chain: "chain", + } + err := client.LoadBalancer.AddSSL(ctx, 12345, ssl) + + if err != nil { + t.Errorf("LoadBalancer.AddSSL returned %+v", err) + } +} + +func TestLoadBalancerHandler_RemoveSSL (t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/v1/loadbalancer/ssl_remove", func(writer http.ResponseWriter, request *http.Request) { + fmt.Fprint(writer) + }) + + err := client.LoadBalancer.RemoveSSL(ctx, 12345) + + if err != nil { + t.Errorf("LoadBalancer.RemoveSSL returned %+v", err) + } +} \ No newline at end of file From 8271c028da5a7b48a532b56fd23f5b726bd15cda Mon Sep 17 00:00:00 2001 From: David Dymko Date: Tue, 10 Mar 2020 08:17:49 -0400 Subject: [PATCH 4/4] lb allow label + ssl in create --- load_balancer.go | 17 +++++++++++++++-- load_balancer_test.go | 17 +++++++++++------ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/load_balancer.go b/load_balancer.go index 6528f42..2fd4fe3 100644 --- a/load_balancer.go +++ b/load_balancer.go @@ -25,7 +25,7 @@ type LoadBalancerService interface { CreateForwardingRule(ctx context.Context, ID int, rule *ForwardingRule) (*ForwardingRule, error) GetFullConfig(ctx context.Context, ID int) (*LBConfig, error) HasSSL(ctx context.Context, ID int) (*struct{ SSLInfo bool `json:"has_ssl"` }, error) - Create(ctx context.Context, region int, genericInfo *GenericInfo, healthCheck *HealthCheck, rules []ForwardingRule) (*LoadBalancers, error) + Create(ctx context.Context, region int, label string, genericInfo *GenericInfo, healthCheck *HealthCheck, rules []ForwardingRule, ssl *SSL) (*LoadBalancers, error) UpdateGenericInfo(ctx context.Context, ID int, label string, genericInfo *GenericInfo) error AddSSL(ctx context.Context, ID int, ssl *SSL) error RemoveSSL(ctx context.Context, ID int) error @@ -451,13 +451,17 @@ func (l *LoadBalancerHandler) HasSSL(ctx context.Context, ID int) (*struct{ SSLI } // Create a load balancer -func (l *LoadBalancerHandler) Create(ctx context.Context, region int, genericInfo *GenericInfo, healthCheck *HealthCheck, rules []ForwardingRule) (*LoadBalancers, error) { +func (l *LoadBalancerHandler) Create(ctx context.Context, region int, label string, genericInfo *GenericInfo, healthCheck *HealthCheck, rules []ForwardingRule, ssl *SSL) (*LoadBalancers, error) { uri := "/v1/loadbalancer/create" values := url.Values{ "DCID": {strconv.Itoa(region)}, } + if label != "" { + values.Add("label", label) + } + // Check generic info struct if genericInfo != nil { if *genericInfo.SSLRedirect == true { @@ -489,6 +493,15 @@ func (l *LoadBalancerHandler) Create(ctx context.Context, region int, genericInf values.Add("forwarding_rules", string(t)) } + if ssl != nil { + values.Add("ssl_private_key", ssl.PrivateKey) + values.Add("ssl_certificate", ssl.Certificate) + + if ssl.Chain != "" { + values.Add("ssl_chain", ssl.Chain) + } + } + req, err := l.client.NewRequest(ctx, http.MethodPost, uri, values) if err != nil { return nil, err diff --git a/load_balancer_test.go b/load_balancer_test.go index d96647f..fe747eb 100644 --- a/load_balancer_test.go +++ b/load_balancer_test.go @@ -377,14 +377,20 @@ func TestLoadBalancerHandler_Create(t *testing.T) { rules := []ForwardingRule{ { - FrontendProtocol: "http", + FrontendProtocol: "https", FrontendPort: 80, BackendProtocol: "http", BackendPort: 80, }, } - lb, err := client.LoadBalancer.Create(ctx, 1, &info, &health, rules) + ssl := SSL{ + PrivateKey: "key", + Certificate: "cert", + Chain: "chain", + } + + lb, err := client.LoadBalancer.Create(ctx, 1, "label", &info, &health, rules, &ssl) if err != nil { t.Errorf("LoadBalancer.Create returned %+v", err) } @@ -422,7 +428,7 @@ func TestLoadBalancerHandler_UpdateGenericInfo(t *testing.T) { } } -func TestLoadBalancerHandler_AddSSL (t *testing.T) { +func TestLoadBalancerHandler_AddSSL(t *testing.T) { setup() defer teardown() @@ -430,7 +436,6 @@ func TestLoadBalancerHandler_AddSSL (t *testing.T) { fmt.Fprint(writer) }) - ssl := &SSL{ PrivateKey: "key", Certificate: "crt", @@ -443,7 +448,7 @@ func TestLoadBalancerHandler_AddSSL (t *testing.T) { } } -func TestLoadBalancerHandler_RemoveSSL (t *testing.T) { +func TestLoadBalancerHandler_RemoveSSL(t *testing.T) { setup() defer teardown() @@ -456,4 +461,4 @@ func TestLoadBalancerHandler_RemoveSSL (t *testing.T) { if err != nil { t.Errorf("LoadBalancer.RemoveSSL returned %+v", err) } -} \ No newline at end of file +}