Skip to content

Commit ba5d4ee

Browse files
authored
[cache] Show better error for no write cache (#2084)
## Summary Show link to configure cache if user is logged in but has no write cache. ## How was it tested? <img width="957" alt="image" src="https://github.com/jetify-com/devbox/assets/544948/5933192f-3cef-49c1-81a5-bbe35c0bacf3">
1 parent 6ee75f8 commit ba5d4ee

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

internal/build/build.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,10 @@ func SuccessRedirect() string {
9191
func Audience() []string {
9292
return []string{"https://api.jetpack.io"}
9393
}
94+
95+
func DashboardHostname() string {
96+
if IsDev {
97+
return "http://localhost:8080"
98+
}
99+
return "https://cloud.jetify.com"
100+
}

internal/devbox/cache.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77

88
"go.jetpack.io/devbox/internal/boxcli/usererr"
9+
"go.jetpack.io/devbox/internal/build"
910
"go.jetpack.io/devbox/internal/devbox/providers/identity"
1011
"go.jetpack.io/devbox/internal/devbox/providers/nixcache"
1112
"go.jetpack.io/devbox/internal/nix"
@@ -77,9 +78,19 @@ func getWriteCacheURI(
7778
if err != nil {
7879
return "", err
7980
}
81+
8082
if len(caches) == 0 {
83+
slug, err := identity.GetOrgSlug(ctx)
84+
if err != nil {
85+
return "", err
86+
}
8187
return "",
82-
usererr.New("You don't have permission to write to any Nix caches.")
88+
usererr.New(
89+
"You don't have permission to write to any Nix caches. To configure cache, go to "+
90+
"%s/teams/%s/devbox",
91+
build.DashboardHostname(),
92+
slug,
93+
)
8394
}
8495
if len(caches) > 1 {
8596
ux.Fwarning(w, "Multiple caches available, using %s.\n", caches[0].GetUri())

internal/devbox/providers/identity/identity.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package identity
22

33
import (
44
"context"
5+
"errors"
56
"os"
67

8+
"github.com/go-jose/go-jose/v4"
9+
"github.com/go-jose/go-jose/v4/jwt"
710
"go.jetify.com/typeid"
811
"go.jetpack.io/devbox/internal/build"
912
"go.jetpack.io/pkg/api"
@@ -91,3 +94,26 @@ func getAccessTokenFromAPIToken(
9194

9295
return cachedAccessTokenFromAPIToken, nil
9396
}
97+
98+
func GetOrgSlug(ctx context.Context) (string, error) {
99+
tok, err := GenSession(ctx)
100+
if err != nil {
101+
return "", err
102+
}
103+
104+
if tok.IDToken == "" {
105+
return "", errors.New("ID token is not present")
106+
}
107+
108+
jwt, err := jwt.ParseSigned(tok.IDToken, []jose.SignatureAlgorithm{jose.RS256})
109+
if err != nil {
110+
return "", err
111+
}
112+
113+
claims := map[string]any{}
114+
if err = jwt.UnsafeClaimsWithoutVerification(&claims); err != nil {
115+
return "", err
116+
}
117+
118+
return claims["org_trusted_metadata"].(map[string]any)["slug"].(string), nil
119+
}

0 commit comments

Comments
 (0)