Skip to content

Commit

Permalink
Merge pull request #1049 from alesstimec/add-controller-add-port-check
Browse files Browse the repository at this point in the history
AddController: added public address port check.
  • Loading branch information
alesstimec authored Sep 28, 2023
2 parents 3cf789e + 00d7389 commit 61f995e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/jujuapi/jimm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package jujuapi

import (
"context"
"fmt"
"net"
"strings"
"time"

Expand Down Expand Up @@ -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,
Expand Down
44 changes: 44 additions & 0 deletions internal/jujuapi/jimm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 61f995e

Please sign in to comment.