mirror of
https://github.com/netbirdio/netbird.git
synced 2026-09-01 20:41:28 +02:00
* [client] Reuse the profile's account for iOS SSO logins Android reads the profile's stored account and passes it as the OIDC login_hint, and records it again after a successful login. iOS did neither: it called GetOAuthFlow with an empty hint, so a re-login was resolved by whatever session the browser's cookie jar held rather than by the account the profile belongs to. With a non-ephemeral browser session that is the wrong account as soon as more than one is signed in. Mirror client/android/login.go: hint from mobile.ReadProfileEmail before the flow, mobile.WriteProfileEmail after Login succeeds. Storing after Login and not before keeps a rejected token from leaving a hint that points at an account which cannot be used. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * [client] Persist the account email on tvOS and on the device flow Two paths left a profile with no account bound, so every later login went out without a login_hint — the case this change exists to remove. WriteProfileEmail went through util.WriteJsonWithRestrictedPermission, which writes a temp file and renames it over the target. The tvOS App Group sandbox blocks exactly that, which is why the config sitting next to this file is written with DirectWriteOutConfig. On tvOS the email write therefore failed and was dropped with a warning. Use DirectWriteJson: the file is rewritten whole from a single key, so the only thing atomicity buys here is surviving a crash mid-write, and a torn file reads back as "no email" and is replaced by the next login. The device authorization flow never populated TokenInfo.Email, unlike the PKCE flow, so a client driven through it — Android TV and tvOS — bound no account at all. Parse the ID token there too. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * [client] Report a failed close from DirectWriteJson The deferred close assigned its error to err, but the return value was not named, so the assignment went nowhere: a close that failed was logged and the function still returned nil. The write is only durable once the file closes cleanly, so every caller — the management config, the profile configs and the profile account email — could be told the data landed when it had not. Name the return so the assignment does what its shape always intended, and report the failure once. When the body succeeded the close error is returned and the caller logs it. When the body already failed, that error is the one that explains the failure and is what the caller gets, which leaves the deferred log as the only place the close failure can surface — at debug, per the logging rules for close errors on writes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
333 lines
7.6 KiB
Go
333 lines
7.6 KiB
Go
package util
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"text/template"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func WriteBytesWithRestrictedPermission(ctx context.Context, file string, bs []byte) error {
|
|
configDir, configFileName, err := prepareConfigFileDir(file)
|
|
if err != nil {
|
|
return fmt.Errorf("prepare config file dir: %w", err)
|
|
}
|
|
|
|
if err = EnforcePermission(file); err != nil {
|
|
return fmt.Errorf("enforce permission: %w", err)
|
|
}
|
|
|
|
return writeBytes(ctx, file, configDir, configFileName, bs)
|
|
}
|
|
|
|
// WriteJsonWithRestrictedPermission writes JSON config object to a file. Enforces permission on the parent directory
|
|
func WriteJsonWithRestrictedPermission(ctx context.Context, file string, obj interface{}) error {
|
|
configDir, configFileName, err := prepareConfigFileDir(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = EnforcePermission(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return writeJson(ctx, file, obj, configDir, configFileName)
|
|
}
|
|
|
|
// WriteJson writes JSON config object to a file creating parent directories if required
|
|
// The output JSON is pretty-formatted
|
|
func WriteJson(ctx context.Context, file string, obj interface{}) error {
|
|
configDir, configFileName, err := prepareConfigFileDir(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return writeJson(ctx, file, obj, configDir, configFileName)
|
|
}
|
|
|
|
// DirectWriteJson writes JSON config object to a file creating parent directories if required without creating a temporary file
|
|
func DirectWriteJson(ctx context.Context, file string, obj interface{}) (err error) {
|
|
|
|
_, _, err = prepareConfigFileDir(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
targetFile, err := openOrCreateFile(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Named return so a failed Close is reported rather than logged and
|
|
// swallowed: the write is only durable once the file closes cleanly, and a
|
|
// caller told "written" would carry on with data that never landed.
|
|
defer func() {
|
|
cerr := targetFile.Close()
|
|
if cerr == nil {
|
|
return
|
|
}
|
|
if err == nil {
|
|
// Returned, not logged: the caller reports it once.
|
|
err = cerr
|
|
return
|
|
}
|
|
// The body already failed and that error is the one the caller gets, so
|
|
// it is the one that explains the failure. This is then the only place
|
|
// the close failure can surface — at debug, per the logging rules for
|
|
// close errors on writes.
|
|
log.Debugf("failed to close file %s after %v: %v", file, err, cerr)
|
|
}()
|
|
|
|
// make it pretty
|
|
bs, err := json.MarshalIndent(obj, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = targetFile.Truncate(0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = targetFile.Write(bs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func writeJson(ctx context.Context, file string, obj interface{}, configDir string, configFileName string) error {
|
|
// Check context before expensive operations
|
|
if ctx.Err() != nil {
|
|
return fmt.Errorf("write json start: %w", ctx.Err())
|
|
}
|
|
|
|
// make it pretty
|
|
bs, err := json.MarshalIndent(obj, "", " ")
|
|
if err != nil {
|
|
return fmt.Errorf("marshal: %w", err)
|
|
}
|
|
|
|
return writeBytes(ctx, file, configDir, configFileName, bs)
|
|
}
|
|
|
|
func writeBytes(ctx context.Context, file string, configDir string, configFileName string, bs []byte) error {
|
|
if ctx.Err() != nil {
|
|
return fmt.Errorf("write bytes start: %w", ctx.Err())
|
|
}
|
|
|
|
tempFile, err := os.CreateTemp(configDir, ".*"+configFileName)
|
|
if err != nil {
|
|
return fmt.Errorf("create temp: %w", err)
|
|
}
|
|
|
|
tempFileName := tempFile.Name()
|
|
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
if err := tempFile.SetDeadline(deadline); err != nil && !errors.Is(err, os.ErrNoDeadline) {
|
|
log.Warnf("failed to set deadline: %v", err)
|
|
}
|
|
}
|
|
|
|
_, err = tempFile.Write(bs)
|
|
if err != nil {
|
|
_ = tempFile.Close()
|
|
return fmt.Errorf("write: %w", err)
|
|
}
|
|
|
|
if err = tempFile.Close(); err != nil {
|
|
return fmt.Errorf("close %s: %w", tempFileName, err)
|
|
}
|
|
|
|
defer func() {
|
|
_, err = os.Stat(tempFileName)
|
|
if err == nil {
|
|
os.Remove(tempFileName)
|
|
}
|
|
}()
|
|
|
|
// Check context again
|
|
if ctx.Err() != nil {
|
|
return fmt.Errorf("after temp file: %w", ctx.Err())
|
|
}
|
|
|
|
if err = os.Rename(tempFileName, file); err != nil {
|
|
return fmt.Errorf("move %s to %s: %w", tempFileName, file, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func openOrCreateFile(file string) (*os.File, error) {
|
|
s, err := os.Stat(file)
|
|
if err == nil {
|
|
return os.OpenFile(file, os.O_WRONLY, s.Mode())
|
|
}
|
|
|
|
if !os.IsNotExist(err) {
|
|
return nil, err
|
|
}
|
|
|
|
targetFile, err := os.Create(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
//no:lint
|
|
err = targetFile.Chmod(0640)
|
|
if err != nil {
|
|
_ = targetFile.Close()
|
|
return nil, err
|
|
}
|
|
return targetFile, nil
|
|
}
|
|
|
|
// ReadJson reads JSON config file and maps to a provided interface
|
|
func ReadJson(file string, res interface{}) (interface{}, error) {
|
|
|
|
f, err := os.Open(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
bs, err := io.ReadAll(f)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = json.Unmarshal(bs, &res)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// RemoveJson removes the specified JSON file if it exists
|
|
func RemoveJson(file string) error {
|
|
// Check if the file exists
|
|
if _, err := os.Stat(file); errors.Is(err, os.ErrNotExist) {
|
|
return nil // File does not exist, nothing to remove
|
|
}
|
|
|
|
// Attempt to remove the file
|
|
if err := os.Remove(file); err != nil {
|
|
return fmt.Errorf("failed to remove JSON file %s: %w", file, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ListFiles returns the full paths of all files in dir that match pattern.
|
|
// Pattern uses shell-style globbing (e.g. "*.json").
|
|
func ListFiles(dir, pattern string) ([]string, error) {
|
|
// glob pattern like "/path/to/dir/*.json"
|
|
globPattern := filepath.Join(dir, pattern)
|
|
|
|
matches, err := filepath.Glob(globPattern)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sort.Strings(matches)
|
|
return matches, nil
|
|
}
|
|
|
|
// ReadJsonWithEnvSub reads JSON config file and maps to a provided interface with environment variable substitution
|
|
func ReadJsonWithEnvSub(file string, res interface{}) (interface{}, error) {
|
|
envVars := getEnvMap()
|
|
|
|
f, err := os.Open(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
bs, err := io.ReadAll(f)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
t, err := template.New("").Parse(string(bs))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error parsing template: %v", err)
|
|
}
|
|
|
|
var output bytes.Buffer
|
|
// Execute the template, substituting environment variables
|
|
err = t.Execute(&output, envVars)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error executing template: %v", err)
|
|
}
|
|
|
|
err = json.Unmarshal(output.Bytes(), &res)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed parsing Json file after template was executed, err: %v", err)
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// getEnvMap Convert the output of os.Environ() to a map
|
|
func getEnvMap() map[string]string {
|
|
envMap := make(map[string]string)
|
|
|
|
for _, env := range os.Environ() {
|
|
parts := strings.SplitN(env, "=", 2)
|
|
if len(parts) == 2 {
|
|
envMap[parts[0]] = parts[1]
|
|
}
|
|
}
|
|
|
|
return envMap
|
|
}
|
|
|
|
// CopyFileContents copies contents of the given src file to the dst file
|
|
func CopyFileContents(src, dst string) (err error) {
|
|
in, err := os.Open(src)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer in.Close()
|
|
out, err := os.Create(dst)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer func() {
|
|
cErr := out.Close()
|
|
if err == nil {
|
|
err = cErr
|
|
}
|
|
}()
|
|
if _, err = io.Copy(out, in); err != nil {
|
|
return
|
|
}
|
|
err = out.Sync()
|
|
return
|
|
}
|
|
|
|
func prepareConfigFileDir(file string) (string, string, error) {
|
|
configDir, configFileName := filepath.Split(file)
|
|
if configDir == "" {
|
|
return filepath.Dir(file), configFileName, nil
|
|
}
|
|
|
|
err := os.MkdirAll(configDir, 0750)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
|
|
return configDir, configFileName, err
|
|
}
|