From 00d73891c1bf75f5a8d05c2cd950df3f10f1dfad Mon Sep 17 00:00:00 2001 From: alesstimec Date: Thu, 28 Sep 2023 08:14:05 +0200 Subject: [PATCH] AddController: added public address port check. --- internal/jujuapi/jimm.go | 15 ++++++++++++ internal/jujuapi/jimm_test.go | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/internal/jujuapi/jimm.go b/internal/jujuapi/jimm.go index 296f6c634..4fb3da8f5 100644 --- a/internal/jujuapi/jimm.go +++ b/internal/jujuapi/jimm.go @@ -4,6 +4,8 @@ package jujuapi import ( "context" + "fmt" + "net" "strings" "time" @@ -169,6 +171,19 @@ func (r *controllerRoot) AddCloudToController(ctx context.Context, req apiparams func (r *controllerRoot) AddController(ctx context.Context, req apiparams.AddControllerRequest) (apiparams.ControllerInfo, error) { const op = errors.Op("jujuapi.AddController") + if req.PublicAddress != "" { + host, port, err := net.SplitHostPort(req.PublicAddress) + if err != nil { + return apiparams.ControllerInfo{}, errors.E(op, err, errors.CodeBadRequest) + } + if host == "" { + return apiparams.ControllerInfo{}, errors.E(op, fmt.Sprintf("address %s: host not specified in public address", req.PublicAddress), errors.CodeBadRequest) + } + if port == "" { + return apiparams.ControllerInfo{}, errors.E(op, fmt.Sprintf("address %s: port not specified in public address", req.PublicAddress), errors.CodeBadRequest) + } + } + ctl := dbmodel.Controller{ Name: req.Name, PublicAddress: req.PublicAddress, diff --git a/internal/jujuapi/jimm_test.go b/internal/jujuapi/jimm_test.go index f24a8096b..9dc640898 100644 --- a/internal/jujuapi/jimm_test.go +++ b/internal/jujuapi/jimm_test.go @@ -153,6 +153,50 @@ func (s *jimmSuite) TestListControllersV3Unauthorized(c *gc.C) { }}) } +func (s *jimmSuite) TestAddControllerPublicAddressWithoutPort(c *gc.C) { + conn := s.open(c, nil, "alice") + defer conn.Close() + client := api.NewClient(conn) + + tests := []struct { + req apiparams.AddControllerRequest + expectedError string + }{{ + req: apiparams.AddControllerRequest{ + Name: "controller-2", + PublicAddress: "controller.test.com", + CACertificate: s.APIInfo(c).CACert, + Username: s.APIInfo(c).Tag.Id(), + Password: s.APIInfo(c).Password, + }, + expectedError: `address controller.test.com: missing port in address \(bad request\)`, + }, { + req: apiparams.AddControllerRequest{ + Name: "controller-2", + PublicAddress: ":8080", + CACertificate: s.APIInfo(c).CACert, + Username: s.APIInfo(c).Tag.Id(), + Password: s.APIInfo(c).Password, + }, + expectedError: `address :8080: host not specified in public address \(bad request\)`, + }, { + req: apiparams.AddControllerRequest{ + Name: "controller-2", + PublicAddress: "localhost:", + CACertificate: s.APIInfo(c).CACert, + Username: s.APIInfo(c).Tag.Id(), + Password: s.APIInfo(c).Password, + }, + expectedError: `address localhost:: port not specified in public address \(bad request\)`, + }} + + for _, test := range tests { + ci, err := client.AddController(&test.req) + c.Assert(err, gc.ErrorMatches, test.expectedError) + c.Check(ci, jc.DeepEquals, apiparams.ControllerInfo{}) + } +} + func (s *jimmSuite) TestAddController(c *gc.C) { conn := s.open(c, nil, "alice") defer conn.Close()