Skip to content

Commit

Permalink
cmd/tailscale: propagate tailscaled 403s as AccessDeniedErrors
Browse files Browse the repository at this point in the history
So Linux/etc CLI users get helpful advice to run tailscale
with --operator=$USER when they try to 'tailscale file {cp,get}'
but are mysteriously forbidden.

Signed-off-by: David Eger <eger@google.com>
Signed-off-by: David Eger <david.eger@gmail.com>
  • Loading branch information
davideger committed Jan 25, 2022
1 parent f3c0023 commit f315468
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
12 changes: 6 additions & 6 deletions client/tailscale/tailscale.go
Expand Up @@ -104,6 +104,10 @@ func doLocalRequestNiceError(req *http.Request) (*http.Response, error) {
if server := res.Header.Get("Tailscale-Version"); server != "" && server != version.Long && onVersionMismatch != nil {
onVersionMismatch(version.Long, server)
}
if res.StatusCode == 403 {
all, _ := ioutil.ReadAll(res.Body)
return nil, &AccessDeniedError{errors.New(errorMessageFromBody(all))}
}
return res, nil
}
if ue, ok := err.(*url.Error); ok {
Expand Down Expand Up @@ -179,10 +183,6 @@ func send(ctx context.Context, method, path string, wantStatus int, body io.Read
return nil, err
}
if res.StatusCode != wantStatus {
if res.StatusCode == 403 {
return nil, &AccessDeniedError{errors.New(errorMessageFromBody(slurp))}
}
err := fmt.Errorf("HTTP %s: %s (expected %v)", res.Status, slurp, wantStatus)
return nil, bestError(err, slurp)
}
return slurp, nil
Expand Down Expand Up @@ -294,7 +294,7 @@ func GetWaitingFile(ctx context.Context, baseName string) (rc io.ReadCloser, siz
if err != nil {
return nil, 0, err
}
res, err := DoLocalRequest(req)
res, err := doLocalRequestNiceError(req)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -343,7 +343,7 @@ func PushFile(ctx context.Context, target tailcfg.StableNodeID, size int64, name
return nil
}
all, _ := io.ReadAll(res.Body)
return fmt.Errorf("%s: %s", res.Status, all)
return bestError(fmt.Errorf("%s: %s", res.Status, all), all)
}

func CheckIPForwarding(ctx context.Context) error {
Expand Down
4 changes: 0 additions & 4 deletions cmd/tailscale/cli/cert.go
Expand Up @@ -13,7 +13,6 @@ import (
"log"
"net/http"
"os"
"runtime"
"strings"

"github.com/peterbourgon/ff/v3/ffcli"
Expand Down Expand Up @@ -92,9 +91,6 @@ func runCert(ctx context.Context, args []string) error {
certArgs.keyFile = domain + ".key"
}
certPEM, keyPEM, err := tailscale.CertPair(ctx, domain)
if tailscale.IsAccessDeniedError(err) && os.Getuid() != 0 && runtime.GOOS != "windows" {
return fmt.Errorf("%v\n\nUse 'sudo tailscale cert' or 'tailscale up --operator=$USER' to not require root.", err)
}
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/tailscale/cli/cli.go
Expand Up @@ -171,6 +171,9 @@ change in the future.
})

err := rootCmd.Run(context.Background())
if tailscale.IsAccessDeniedError(err) && os.Getuid() != 0 && runtime.GOOS != "windows" {
return fmt.Errorf("%v\n\nUse 'sudo tailscale %s' or 'tailscale up --operator=$USER' to not require root.", err, strings.Join(args, " "))
}
if errors.Is(err, flag.ErrHelp) {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/tailscale/cli/file.go
Expand Up @@ -324,7 +324,7 @@ func runFileGet(ctx context.Context, args []string) error {
for {
wfs, err = tailscale.WaitingFiles(ctx)
if err != nil {
return fmt.Errorf("getting WaitingFiles: %v", err)
return fmt.Errorf("getting WaitingFiles: %w", err)
}
if len(wfs) != 0 || !getArgs.wait {
break
Expand Down Expand Up @@ -379,7 +379,7 @@ func wipeInbox(ctx context.Context) error {
}
wfs, err := tailscale.WaitingFiles(ctx)
if err != nil {
return fmt.Errorf("getting WaitingFiles: %v", err)
return fmt.Errorf("getting WaitingFiles: %w", err)
}
deleted := 0
for _, wf := range wfs {
Expand Down

0 comments on commit f315468

Please sign in to comment.