Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/config/fileconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"bytes"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"io/fs"
"net"
"net/url"
"os"
Expand Down Expand Up @@ -91,7 +93,10 @@ type FileConfig struct {
func ReadFromFile(filePath string) (*FileConfig, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, trace.Wrap(err, fmt.Sprintf("failed to open file: %v", filePath))
if errors.Is(err, fs.ErrPermission) {
return nil, trace.Wrap(err, "failed to open file for Teleport configuration: %v. Ensure that you are running as a user with appropriate permissions.", filePath)
}
return nil, trace.Wrap(err, "failed to open file for Teleport configuration at %v", filePath)
}
defer f.Close()
return ReadConfig(f)
Expand Down
11 changes: 11 additions & 0 deletions lib/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/hex"
"errors"
"fmt"
"io"
"io/fs"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -626,6 +628,9 @@ func NewTeleport(cfg *Config) (*TeleportProcess, error) {
if os.IsNotExist(err) {
err := os.MkdirAll(cfg.DataDir, os.ModeDir|0700)
if err != nil {
if errors.Is(err, fs.ErrPermission) {
cfg.Log.Errorf("Teleport does not have permission to write to: %v. Ensure that you are running as a user with appropriate permissions.", cfg.DataDir)
}
return nil, trace.ConvertSystemError(err)
}
}
Expand All @@ -642,6 +647,9 @@ func NewTeleport(cfg *Config) (*TeleportProcess, error) {
cfg.HostUUID, err = utils.ReadHostUUID(cfg.DataDir)
if err != nil {
if !trace.IsNotFound(err) {
if errors.Is(err, fs.ErrPermission) {
cfg.Log.Errorf("Teleport does not have permission to write to: %v. Ensure that you are running as a user with appropriate permissions.", cfg.DataDir)
}
return nil, trace.Wrap(err)
}
if len(cfg.Identities) != 0 {
Expand All @@ -662,6 +670,9 @@ func NewTeleport(cfg *Config) (*TeleportProcess, error) {
cfg.Log.Infof("Generating new host UUID: %v.", cfg.HostUUID)
}
if err := utils.WriteHostUUID(cfg.DataDir, cfg.HostUUID); err != nil {
if errors.Is(err, fs.ErrPermission) {
cfg.Log.Errorf("Teleport does not have permission to write to: %v. Ensure that you are running as a user with appropriate permissions.", cfg.DataDir)
}
return nil, trace.Wrap(err)
}
}
Expand Down
18 changes: 18 additions & 0 deletions lib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package utils

import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"net"
"net/url"
"os"
Expand Down Expand Up @@ -320,10 +322,18 @@ func ReadPath(path string) ([]byte, error) {
}
abs, err := filepath.EvalSymlinks(s)
if err != nil {
if errors.Is(err, fs.ErrPermission) {
//do not convert to system error as this loses the ability to compare that it is a permission error
return nil, err
}
return nil, trace.ConvertSystemError(err)
}
bytes, err := os.ReadFile(abs)
if err != nil {
if errors.Is(err, fs.ErrPermission) {
//do not convert to system error as this loses the ability to compare that it is a permission error
return nil, err
}
return nil, trace.ConvertSystemError(err)
}
return bytes, nil
Expand Down Expand Up @@ -429,6 +439,10 @@ func GetFreeTCPPorts(n int, offset ...int) (PortList, error) {
func ReadHostUUID(dataDir string) (string, error) {
out, err := ReadPath(filepath.Join(dataDir, HostUUIDFile))
if err != nil {
if errors.Is(err, fs.ErrPermission) {
//do not convert to system error as this loses the ability to compare that it is a permission error
return "", err
}
return "", trace.Wrap(err)
}
return strings.TrimSpace(string(out)), nil
Expand All @@ -438,6 +452,10 @@ func ReadHostUUID(dataDir string) (string, error) {
func WriteHostUUID(dataDir string, id string) error {
err := os.WriteFile(filepath.Join(dataDir, HostUUIDFile), []byte(id), os.ModeExclusive|0400)
if err != nil {
if errors.Is(err, fs.ErrPermission) {
//do not convert to system error as this loses the ability to compare that it is a permission error
Comment thread
stevenGravy marked this conversation as resolved.
return err
}
return trace.ConvertSystemError(err)
}
return nil
Expand Down