Skip to content

Commit ba7d01a

Browse files
Import upstream commits (#79)
* ufs: improve error handling Signed-off-by: Matthew Penner <me@matthewp.io> * chore: remove outdated `wings-api.paw` Signed-off-by: Matthew Penner <me@matthewp.io> * chore: add `.editorconfig` Signed-off-by: Matthew Penner <me@matthewp.io> * feat: add support for loading token from env and file (`WINGS_TOKEN` and `WINGS_TOKEN_ID`) Signed-off-by: Matthew Penner <me@matthewp.io> * system: fix test relying on reflection to determine if mutex is locked Signed-off-by: Matthew Penner <me@matthewp.io> * fix: `Duplicated key token in config` panic Signed-off-by: Matthew Penner <me@matthewp.io> * fix: use of old `AuthenticationToken` value Signed-off-by: Matthew Penner <me@matthewp.io> * config: handle old `AuthenticationToken` value Signed-off-by: Matthew Penner <me@matthewp.io> * chore: avoid exiting if config file is not writable --------- Signed-off-by: Matthew Penner <me@matthewp.io> Co-authored-by: Matthew Penner <me@matthewp.io>
1 parent 9d09674 commit ba7d01a

11 files changed

Lines changed: 452 additions & 173 deletions

File tree

.editorconfig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
indent_size = 4
6+
tab_width = 4
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.go]
13+
max_line_length = 100
14+
15+
[*.md]
16+
trim_trailing_whitespace = false
17+
18+
[*.{md,nix,yaml}]
19+
indent_style = space
20+
indent_size = 2
21+
tab_width = 2

cmd/root.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"runtime"
1515
"strconv"
1616
"strings"
17+
"syscall"
1718
"time"
1819

1920
"github.com/NYTimes/logrotate"
@@ -127,6 +128,7 @@ func rootCmdRun(cmd *cobra.Command, _ []string) {
127128

128129
if err := config.ConfigureTimezone(); err != nil {
129130
log.WithField("error", err).Fatal("failed to detect system timezone or use supplied configuration value")
131+
return
130132
}
131133
log.WithField("timezone", config.Get().System.Timezone).Info("configured wings with system timezone")
132134
if err := config.ConfigureDirectories(); err != nil {
@@ -135,6 +137,7 @@ func rootCmdRun(cmd *cobra.Command, _ []string) {
135137
}
136138
if err := config.EnsurePelicanUser(); err != nil {
137139
log.WithField("error", err).Fatal("failed to create pelican system user")
140+
return
138141
}
139142
log.WithFields(log.Fields{
140143
"username": config.Get().System.Username,
@@ -146,29 +149,37 @@ func rootCmdRun(cmd *cobra.Command, _ []string) {
146149
return
147150
}
148151

152+
t := config.Get().Token
149153
pclient := remote.New(
150154
config.Get().PanelLocation,
151-
remote.WithCredentials(config.Get().AuthenticationTokenId, config.Get().AuthenticationToken),
155+
remote.WithCredentials(t.ID, t.Token),
152156
remote.WithHttpClient(&http.Client{
153157
Timeout: time.Second * time.Duration(config.Get().RemoteQuery.Timeout),
154158
}),
155159
)
156160

157161
if err := database.Initialize(); err != nil {
158162
log.WithField("error", err).Fatal("failed to initialize database")
163+
return
159164
}
160165

161166
manager, err := server.NewManager(cmd.Context(), pclient)
162167
if err != nil {
163168
log.WithField("error", err).Fatal("failed to load server configurations")
169+
return
164170
}
165171

166172
if err := environment.ConfigureDocker(cmd.Context()); err != nil {
167173
log.WithField("error", err).Fatal("failed to configure docker environment")
174+
return
168175
}
169176

170177
if err := config.WriteToDisk(config.Get()); err != nil {
171-
log.WithField("error", err).Fatal("failed to write configuration to disk")
178+
if !errors.Is(err, syscall.EROFS) {
179+
log.WithField("error", err).Error("failed to write configuration to disk")
180+
} else {
181+
log.WithField("error", err).Debug("failed to write configuration to disk")
182+
}
172183
}
173184

174185
// Just for some nice log output.

config/config.go

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"bytes"
45
"context"
56
"crypto/tls"
67
"fmt"
@@ -287,7 +288,14 @@ type ConsoleThrottles struct {
287288
Period uint64 `json:"line_reset_interval" yaml:"line_reset_interval" default:"100"`
288289
}
289290

291+
type Token struct {
292+
ID string
293+
Token string
294+
}
295+
290296
type Configuration struct {
297+
Token Token `json:"-" yaml:"-"`
298+
291299
// The location from which this configuration instance was instantiated.
292300
path string
293301

@@ -375,21 +383,26 @@ func NewAtPath(path string) (*Configuration, error) {
375383
// will be paused until it is complete.
376384
func Set(c *Configuration) {
377385
mu.Lock()
378-
if _config == nil || _config.AuthenticationToken != c.AuthenticationToken {
379-
_jwtAlgo = jwt.NewHS256([]byte(c.AuthenticationToken))
386+
defer mu.Unlock()
387+
token := c.Token.Token
388+
if token == "" {
389+
c.Token.Token = c.AuthenticationToken
390+
token = c.Token.Token
391+
}
392+
if _config == nil || _config.Token.Token != token {
393+
_jwtAlgo = jwt.NewHS256([]byte(token))
380394
}
381395
_config = c
382-
mu.Unlock()
383396
}
384397

385398
// SetDebugViaFlag tracks if the application is running in debug mode because of
386399
// a command line flag argument. If so we do not want to store that configuration
387400
// change to the disk.
388401
func SetDebugViaFlag(d bool) {
389402
mu.Lock()
403+
defer mu.Unlock()
390404
_config.Debug = d
391405
_debugViaFlag = d
392-
mu.Unlock()
393406
}
394407

395408
// Get returns the global configuration instance. This is a thread-safe operation
@@ -414,8 +427,8 @@ func Get() *Configuration {
414427
// the global configuration.
415428
func Update(callback func(c *Configuration)) {
416429
mu.Lock()
430+
defer mu.Unlock()
417431
callback(_config)
418-
mu.Unlock()
419432
}
420433

421434
// GetJwtAlgorithm returns the in-memory JWT algorithm.
@@ -540,6 +553,26 @@ func FromFile(path string) error {
540553
return err
541554
}
542555

556+
c.Token = Token{
557+
ID: os.Getenv("WINGS_TOKEN_ID"),
558+
Token: os.Getenv("WINGS_TOKEN"),
559+
}
560+
if c.Token.ID == "" {
561+
c.Token.ID = c.AuthenticationTokenId
562+
}
563+
if c.Token.Token == "" {
564+
c.Token.Token = c.AuthenticationToken
565+
}
566+
567+
c.Token.ID, err = Expand(c.Token.ID)
568+
if err != nil {
569+
return err
570+
}
571+
c.Token.Token, err = Expand(c.Token.Token)
572+
if err != nil {
573+
return err
574+
}
575+
543576
// Store this configuration in the global state.
544577
Set(c)
545578
return nil
@@ -748,3 +781,36 @@ func UseOpenat2() bool {
748781
return true
749782
}
750783
}
784+
785+
// Expand expands an input string by calling [os.ExpandEnv] to expand all
786+
// environment variables, then checks if the value is prefixed with `file://`
787+
// to support reading the value from a file.
788+
//
789+
// NOTE: the order of expanding environment variables first then checking if
790+
// the value references a file is important. This behaviour allows a user to
791+
// pass a value like `file://${CREDENTIALS_DIRECTORY}/token` to allow us to
792+
// work with credentials loaded by systemd's `LoadCredential` (or `LoadCredentialEncrypted`)
793+
// options without the user needing to assume the path of `CREDENTIALS_DIRECTORY`
794+
// or use a preStart script to read the files for us.
795+
func Expand(v string) (string, error) {
796+
// Expand environment variables within the string.
797+
//
798+
// NOTE: this may cause issues if the string contains `$` and doesn't intend
799+
// on getting expanded, however we are using this for our tokens which are
800+
// all alphanumeric characters only.
801+
v = os.ExpandEnv(v)
802+
803+
// Handle files.
804+
const filePrefix = "file://"
805+
if strings.HasPrefix(v, filePrefix) {
806+
p := v[len(filePrefix):]
807+
808+
b, err := os.ReadFile(p)
809+
if err != nil {
810+
return "", nil
811+
}
812+
v = string(bytes.TrimRight(bytes.TrimRight(b, "\r"), "\n"))
813+
}
814+
815+
return v, nil
816+
}

0 commit comments

Comments
 (0)