Skip to content

Commit

Permalink
Move Landscape config modification to the WSL-Pro-Service
Browse files Browse the repository at this point in the history
We need to modify the config on WSL's side because it'll need access to
wslpath once we implement support for the ssl certificate.
  • Loading branch information
EduardGomezEscandell committed Oct 9, 2023
1 parent e9ac100 commit 1de8c8f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 125 deletions.
4 changes: 1 addition & 3 deletions windows-agent/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,8 @@ func (c *Config) ProvisioningTasks(ctx context.Context, distroName string) ([]ta

if conf, err := c.LandscapeClientConfig(ctx); err != nil {
log.Errorf(ctx, "Could not generate provisioning task LandscapeConfigure: %v", err)
} else if landscape, err := tasks.NewLandscapeConfigure(ctx, conf, distroName); err != nil {
log.Errorf(ctx, "Could not generate provisioning task LandscapeConfigure: %v", err)
} else {
// Success
landscape := tasks.LandscapeConfigure{Config: conf}
taskList = append(taskList, landscape)
}

Expand Down
48 changes: 0 additions & 48 deletions windows-agent/internal/tasks/landscape_configure.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package tasks

import (
"bytes"
"context"
"fmt"
"strings"

"github.com/canonical/ubuntu-pro-for-windows/windows-agent/internal/distros/task"
log "github.com/canonical/ubuntu-pro-for-windows/windows-agent/internal/grpc/logstreamer"
"github.com/canonical/ubuntu-pro-for-windows/wslserviceapi"
"github.com/ubuntu/decorate"
"gopkg.in/ini.v1"
)

func init() {
Expand All @@ -24,48 +18,6 @@ type LandscapeConfigure struct {
Config string
}

// NewLandscapeConfigure creates a LandscapeConfigure. It overrides the name of the Landscape
// client so it matches the name of the distro.
func NewLandscapeConfigure(ctx context.Context, Config string, distroName string) (conf LandscapeConfigure, err error) {
defer decorate.OnError(&err, "NewLandscapeConfigure error")

if Config == "" {
// Landscape disablement
return LandscapeConfigure{}, nil
}

r := strings.NewReader(Config)
data, err := ini.Load(r)
if err != nil {
return LandscapeConfigure{}, fmt.Errorf("could not parse config: %v", err)
}

const section = "client"
s, err := data.GetSection(section)
if err != nil {
return LandscapeConfigure{}, fmt.Errorf("could not find [%s] section: %v", section, err)
}

const key = "computer_title"
if s.HasKey(key) {
log.Infof(ctx, "Landscape config contains key %q. Its value will be overridden with %s", key, distroName)
s.DeleteKey(key)
}

if _, err := s.NewKey(key, distroName); err != nil {
return LandscapeConfigure{}, fmt.Errorf("could not create %q key", key)
}

w := &bytes.Buffer{}
if _, err := data.WriteTo(w); err != nil {
return LandscapeConfigure{}, fmt.Errorf("could not write modified config: %v", err)
}

return LandscapeConfigure{
Config: w.String(),
}, nil
}

// Execute sends the config to the target WSL-Pro-Service so that the distro can be
// registered in Landscape.
func (t LandscapeConfigure) Execute(ctx context.Context, client wslserviceapi.WSLClient) error {
Expand Down
69 changes: 0 additions & 69 deletions windows-agent/internal/tasks/landscape_configure_test.go

This file was deleted.

62 changes: 62 additions & 0 deletions wsl-pro-service/internal/system/landscape.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package system

import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

log "github.com/canonical/ubuntu-pro-for-windows/wsl-pro-service/internal/grpc/logstreamer"
"github.com/ubuntu/decorate"
"gopkg.in/ini.v1"
)

const (
Expand All @@ -19,6 +23,10 @@ func (s *System) LandscapeEnable(ctx context.Context, landscapeConfig string) (e
// Decorating here to avoid stuttering the URL (url package prints it as well)
defer decorate.OnError(&err, "could not register to landscape")

if landscapeConfig, err = modifyConfig(ctx, s, landscapeConfig); err != nil {
return err
}

if err := s.writeConfig(landscapeConfig); err != nil {
return err
}
Expand Down Expand Up @@ -66,3 +74,57 @@ func (s *System) writeConfig(landscapeConfig string) (err error) {

return nil
}

// modifyConfig overrides the computer name so that it matches the distro's name.
// This is necessary to prevent different distros from sharing the same name.
func modifyConfig(ctx context.Context, s *System, landscapeConfig string) (string, error) {
if landscapeConfig == "" {
return "", nil
}

r := strings.NewReader(landscapeConfig)
data, err := ini.Load(r)
if err != nil {
return "", fmt.Errorf("could not parse config: %v", err)
}

if err := overrideComputerTitle(ctx, s, data); err != nil {
return "", err
}

w := &bytes.Buffer{}
if _, err := data.WriteTo(w); err != nil {
return "", fmt.Errorf("could not write modified config: %v", err)
}

return w.String(), nil
}

// overrideComputerTitle overrides the computer_title field in the Landscape config
func overrideComputerTitle(ctx context.Context, s *System, data *ini.File) error {
const section = "client"
const key = "computer_title"

distroName, err := s.wslDistroName(ctx)
if err != nil {
return err
}

sec, err := data.GetSection(section)
if err != nil {
if sec, err = data.NewSection(section); err != nil {
return fmt.Errorf("could not find nor create section %q: %v", section, err)
}
}

if sec.HasKey(key) {
log.Infof(ctx, "Landscape config contains key %q. Its value will be overridden with %s", key, distroName)
sec.DeleteKey(key)
}

if _, err := sec.NewKey(key, distroName); err != nil {
return fmt.Errorf("could not create %q key", key)
}

return nil
}
14 changes: 9 additions & 5 deletions wsl-pro-service/internal/system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,10 @@ func TestLandscapeEnable(t *testing.T) {

wantErr bool
}{
"Success": {},
"Success": {},
"Success overriding computer_title": {},

"Error when the file cannot be parsed": {wantErr: true},
"Error when the config file cannot be written": {breakWriteConfig: true, wantErr: true},
"Error when the landscape-config command fails": {breakLandscapeConfig: true, wantErr: true},
}
Expand All @@ -377,9 +379,10 @@ func TestLandscapeEnable(t *testing.T) {
mock.SetControlArg(testutils.LandscapeEnableErr)
}

config := "[example]\nnumber = 5\ntext = Lorem ipsum dolot sit amet"
config, err := os.ReadFile(filepath.Join(golden.TestFixturePath(t), "landscape.conf"))
require.NoError(t, err, "Setup: could not load golden file")

err := s.LandscapeEnable(ctx, config)
err = s.LandscapeEnable(ctx, string(config))
if tc.wantErr {
require.Error(t, err, "LandscapeEnable should have returned an error")
return
Expand All @@ -388,10 +391,11 @@ func TestLandscapeEnable(t *testing.T) {

exeProof := s.Path("/.landscape-enabled")
require.FileExists(t, exeProof, "Landscape executable never ran")
out, err := os.ReadFile(exeProof)
got, err := os.ReadFile(exeProof)
require.NoErrorf(t, err, "could not read file %q", exeProof)

require.Equal(t, config, string(out), "Landscape executable did not receive the right config")
want := golden.LoadWithUpdateFromGolden(t, string(config))
require.Equal(t, want, string(got), "Landscape executable did not receive the right config")
})
}
}
Expand Down

0 comments on commit 1de8c8f

Please sign in to comment.