Skip to content

Commit

Permalink
Remove config runtime setter: we only need it at compile-time
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardGomezEscandell committed Oct 27, 2023
1 parent 400871b commit 14e66ed
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 72 deletions.
67 changes: 30 additions & 37 deletions windows-agent/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,6 @@ func (c *Config) ProvisioningTasks(ctx context.Context, distroName string) ([]ta
return taskList, nil
}

// SetSubscription overwrites the value of the pro token and the method with which it has been acquired.
func (c *Config) SetSubscription(ctx context.Context, proToken string, source Source) error {
c.mu.Lock()
defer c.mu.Unlock()

// Load before dumping to avoid overriding recent changes to registry
if err := c.load(); err != nil {
return err
}

old := c.subscription.Get(source)
c.subscription.Set(source, proToken)

if err := c.dump(); err != nil {
log.Errorf(ctx, "Could not update subscription, token will be ignored: %v", err)
c.subscription.Set(source, old)
return err
}

return nil
}

// LandscapeClientConfig returns the value of the landscape server URL and
// the method it was acquired with (if any).
func (c *Config) LandscapeClientConfig(ctx context.Context) (string, Source, error) {
Expand All @@ -176,21 +154,23 @@ func (c *Config) LandscapeClientConfig(ctx context.Context) (string, Source, err
return conf, src, nil
}

// LandscapeAgentUID returns the UID assigned to this agent by the Landscape server.
// An empty string is returned if no UID has been assigned.
func (c *Config) LandscapeAgentUID(ctx context.Context) (string, error) {
c.mu.Lock()
defer c.mu.Unlock()

if err := c.load(); err != nil {
return "", fmt.Errorf("could not load: %v", err)
}
// SetSubscription overwrites the value of the user-provided Ubuntu Pro token.
func (c *Config) SetUserSubscription(ctx context.Context, proToken string) error {
return c.set(ctx, &c.subscription.User, proToken)
}

return c.landscape.UID, nil
// setStoreSubscription overwrites the value of the store-provided Ubuntu Pro token.
func (c *Config) setStoreSubscription(ctx context.Context, proToken string) error {
return c.set(ctx, &c.subscription.Store, proToken)
}

// SetLandscapeAgentUID overrides the Landscape agent UID.
func (c *Config) SetLandscapeAgentUID(ctx context.Context, uid string) error {
return c.set(ctx, &c.landscape.UID, uid)
}

// set is a generic method to safely modify the config.
func (c *Config) set(ctx context.Context, field *string, value string) error {
c.mu.Lock()
defer c.mu.Unlock()

Expand All @@ -199,18 +179,31 @@ func (c *Config) SetLandscapeAgentUID(ctx context.Context, uid string) error {
return err
}

old := c.landscape.UID
c.landscape.UID = uid
old := *field
*field = value

if err := c.dump(); err != nil {
log.Errorf(ctx, "Could not update landscape settings, UID will be ignored: %v", err)
c.landscape.UID = old
log.Errorf(ctx, "Could not update settings: %v", err)
*field = old
return err
}

return nil
}

// LandscapeAgentUID returns the UID assigned to this agent by the Landscape server.
// An empty string is returned if no UID has been assigned.
func (c *Config) LandscapeAgentUID(ctx context.Context) (string, error) {
c.mu.Lock()
defer c.mu.Unlock()

if err := c.load(); err != nil {
return "", fmt.Errorf("could not load: %v", err)
}

return c.landscape.UID, nil
}

// FetchMicrosoftStoreSubscription contacts Ubuntu Pro's contract server and the Microsoft Store
// to check if the user has an active subscription that provides a pro token. If so, that token is used.
func (c *Config) FetchMicrosoftStoreSubscription(ctx context.Context) (err error) {
Expand All @@ -231,7 +224,7 @@ func (c *Config) FetchMicrosoftStoreSubscription(ctx context.Context) (err error
return fmt.Errorf("could not get ProToken from Microsoft Store: %v", err)
}

if err := c.SetSubscription(ctx, proToken, SourceMicrosoftStore); err != nil {
if err := c.setStoreSubscription(ctx, proToken); err != nil {
return err
}

Expand Down
28 changes: 0 additions & 28 deletions windows-agent/internal/config/config_source.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package config

import "fmt"

// Source indicates the method a configuration parameter was acquired.
type Source int

Expand Down Expand Up @@ -43,32 +41,6 @@ func (s subscription) resolve() (string, Source) {
return "", SourceNone
}

func (s *subscription) Set(src Source, proToken string) {
ptr := s.src(src)
*ptr = proToken
}

func (s subscription) Get(src Source) string {
return *s.src(src)
}

// src is a helper to avoid duplicating the mapping in Get and Set.
func (s *subscription) src(src Source) *string {
switch src {
case SourceNone:
// TODO: Panic? Warning?
return new(string)
case SourceUser:
return &s.User
case SourceRegistry:
return &s.Organization
case SourceMicrosoftStore:
return &s.Store
}

panic(fmt.Sprintf("Unknown enum value for SubscriptionSource: %d", src))
}

type landscapeConf struct {
UserConfig string `yaml:"config"`
OrgConfig string `yaml:"-"`
Expand Down
4 changes: 2 additions & 2 deletions windows-agent/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func TestProvisioningTasks(t *testing.T) {
}
}

func TestSetSubscription(t *testing.T) {
func TestSetUserSubscription(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
Expand Down Expand Up @@ -297,7 +297,7 @@ func TestSetSubscription(t *testing.T) {
token = ""
}

err := conf.SetSubscription(ctx, token, config.SourceUser)
err := conf.SetUserSubscription(ctx, token)
if tc.wantError {
require.Error(t, err, "SetSubscription should return an error")
return
Expand Down
4 changes: 2 additions & 2 deletions windows-agent/internal/proservices/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

// Config is a provider for the subcription configuration.
type Config interface {
SetSubscription(ctx context.Context, token string, source config.Source) error
SetUserSubscription(ctx context.Context, token string) error
IsReadOnly() (bool, error)
Subscription(context.Context) (string, config.Source, error)
FetchMicrosoftStoreSubscription(context.Context) error
Expand Down Expand Up @@ -45,7 +45,7 @@ func (s *Service) ApplyProToken(ctx context.Context, info *agentapi.ProAttachInf
token := info.GetToken()
log.Debugf(ctx, "Received token %s", common.Obfuscate(token))

err := s.config.SetSubscription(ctx, token, config.SourceUser)
err := s.config.SetUserSubscription(ctx, token)
if err != nil {
return nil, err
}
Expand Down
13 changes: 10 additions & 3 deletions windows-agent/internal/proservices/ui/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ type mockConfig struct {
source config.Source // stores the configured subscription source.
}

func (m *mockConfig) SetSubscription(ctx context.Context, token string, source config.Source) error {
func (m *mockConfig) SetUserSubscription(ctx context.Context, token string) error {
if m.setSubscriptionErr {
return errors.New("SetSubscription error")
}
m.token = token
m.source = source
m.source = config.SourceUser
return nil
}
func (m mockConfig) IsReadOnly() (bool, error) {
Expand Down Expand Up @@ -259,5 +259,12 @@ func (m *mockConfig) FetchMicrosoftStoreSubscription(ctx context.Context) error
return errors.New("Already subscribed")
}

return m.SetSubscription(ctx, "MS", config.SourceMicrosoftStore)
if m.setSubscriptionErr {
return errors.New("SetSubscription error")
}

m.token = "MS"
m.source = config.SourceMicrosoftStore

return nil
}

0 comments on commit 14e66ed

Please sign in to comment.