Skip to content

Commit

Permalink
Landscape mock: fix issue were distros would receive a new UID every …
Browse files Browse the repository at this point in the history
…time
  • Loading branch information
EduardGomezEscandell committed Oct 19, 2023
1 parent 3eef39a commit dcb8fa6
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions mocks/landscape/landscapemockservice/landscape_mock_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,38 +82,56 @@ func New() *Service {
// Connect implements the Connect API call.
// Upon first contact ever, a UID is randombly assigned to the host and sent to it.
// In subsequent contacts, this UID will be its unique identifier.
func (s *Service) Connect(stream landscapeapi.LandscapeHostAgent_ConnectServer) error {
func (s *Service) Connect(stream landscapeapi.LandscapeHostAgent_ConnectServer) (err error) {
ctx, cancel := context.WithCancel(stream.Context())
defer cancel()

firstContact := true
ch := make(chan HostInfo)

type msg struct {
info HostInfo
err error
}

ch := make(chan msg)
defer close(ch)

var hostInfo HostInfo
for {
go func() {
recv, err := stream.Recv()
if err != nil {
return
}

select {
case <-ctx.Done():
return
default:
case ch <- msg{newHostInfo(recv), err}:
}

ch <- newHostInfo(recv)
return

Check failure on line 109 in mocks/landscape/landscapemockservice/landscape_mock_service.go

View workflow job for this annotation

GitHub Actions / Go Quality checks (ubuntu, mocks)

S1023: redundant `return` statement (gosimple)
}()

var hostInfo HostInfo
var m msg
select {
case m = <-ch:
case <-ctx.Done():
slog.Info(fmt.Sprintf("Landscape: %s: terminated connection: %v", hostInfo.Hostname, ctx.Err()))
return nil
}

// Avoid races when ctx.Done and ch were both ready
select {
case hostInfo = <-ch:
case <-ctx.Done():
slog.Info(fmt.Sprintf("Landscape: %s: terminated connection: %v", hostInfo.Hostname, ctx.Err()))
return nil
default:
}

if m.err != nil {
slog.Info(fmt.Sprintf("Landscape: %s: terminated connection: %v", hostInfo.Hostname, err))
return err
}

hostInfo = m.info

if hostInfo.Token == "" {
// No token: no Landscape
err := errors.New("connection rejected: no Ubuntu Pro token provided")
Expand Down

0 comments on commit dcb8fa6

Please sign in to comment.