Skip to content

Commit

Permalink
Fix loading of default license (#39314)
Browse files Browse the repository at this point in the history
The behavior that we expect (and that we document) for the
license_file field in the configuration is that:
1. when empty, Teleport looks for license.pem in the configured data dir
2. when absolute, the provided path is used
3. when relative, the path is relative to the provided data dir

The actual behavior prior to this change failed to meet items 1 and 3
above. When license_file is empty  users would get an error, and when
license_file is a relative path it was always treated as relative to
/var/lib/teleport, not relative to the configured data directory.

Closes gravitational/teleport.e#481
  • Loading branch information
zmb3 committed Mar 14, 2024
1 parent b61e6bb commit 5da9296
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
14 changes: 7 additions & 7 deletions lib/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,13 +942,13 @@ func applyAuthConfig(fc *FileConfig, cfg *servicecfg.Config) error {
}

// read in and set the license file path (not used in open-source version)
licenseFile := fc.Auth.LicenseFile
if licenseFile != "" {
if filepath.IsAbs(licenseFile) {
cfg.Auth.LicenseFile = licenseFile
} else {
cfg.Auth.LicenseFile = filepath.Join(cfg.DataDir, licenseFile)
}
switch licenseFile := fc.Auth.LicenseFile; {
case licenseFile == "":
cfg.Auth.LicenseFile = filepath.Join(cfg.DataDir, defaults.LicenseFile)
case filepath.IsAbs(licenseFile):
cfg.Auth.LicenseFile = licenseFile
default:
cfg.Auth.LicenseFile = filepath.Join(cfg.DataDir, licenseFile)
}

cfg.Auth.LoadAllCAs = fc.Auth.LoadAllCAs
Expand Down
50 changes: 33 additions & 17 deletions lib/config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1857,36 +1857,52 @@ func TestMergingCAPinConfig(t *testing.T) {

func TestLicenseFile(t *testing.T) {
testCases := []struct {
path string
result string
path string
datadir string
result string
}{
// 0 - no license
// 0 - no license, no data dir
{
path: "",
result: filepath.Join(defaults.DataDir, defaults.LicenseFile),
path: "",
datadir: "",
result: filepath.Join(defaults.DataDir, defaults.LicenseFile),
},
// 1 - relative path
// 1 - relative path, default data dir
{
path: "lic.pem",
result: filepath.Join(defaults.DataDir, "lic.pem"),
path: "lic.pem",
datadir: "",
result: filepath.Join(defaults.DataDir, "lic.pem"),
},
// 2 - absolute path
// 2 - relative path, custom data dir
{
path: "baz.pem",
datadir: filepath.Join("foo", "bar"),
result: filepath.Join("foo", "bar", "baz.pem"),
},
// 3 - absolute path
{
path: "/etc/teleport/license",
result: "/etc/teleport/license",
},
}

cfg := servicecfg.MakeDefaultConfig()
require.Equal(t, filepath.Join(defaults.DataDir, defaults.LicenseFile), cfg.Auth.LicenseFile)

for _, tc := range testCases {
fc := new(FileConfig)
require.NoError(t, fc.CheckAndSetDefaults())
fc.Auth.LicenseFile = tc.path
err := ApplyFileConfig(fc, cfg)
require.NoError(t, err)
require.Equal(t, tc.result, cfg.Auth.LicenseFile)
// the license file should be empty by default, as we can only fill
// in the default (<datadir>/license.pem) after we know what the
// data dir is supposed to be
require.Empty(t, cfg.Auth.LicenseFile)

for i, tc := range testCases {
t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) {
fc := new(FileConfig)
require.NoError(t, fc.CheckAndSetDefaults())
fc.Auth.LicenseFile = tc.path
fc.DataDir = tc.datadir
err := ApplyFileConfig(fc, cfg)
require.NoError(t, err)
require.Equal(t, tc.result, cfg.Auth.LicenseFile)
})
}
}

Expand Down
1 change: 0 additions & 1 deletion lib/service/servicecfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,6 @@ func ApplyDefaults(cfg *Config) {
cfg.Auth.SessionRecordingConfig = types.DefaultSessionRecordingConfig()
cfg.Auth.Preference = types.DefaultAuthPreference()
defaults.ConfigureLimiter(&cfg.Auth.Limiter)
cfg.Auth.LicenseFile = filepath.Join(cfg.DataDir, defaults.LicenseFile)

cfg.Proxy.WebAddr = *defaults.ProxyWebListenAddr()
// Proxy service defaults.
Expand Down

0 comments on commit 5da9296

Please sign in to comment.