Skip to content

Commit a3b27b8

Browse files
committed
Allow updating */* scoped vars for backwards compat
1 parent d903498 commit a3b27b8

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

components/gitpod-cli/cmd/env.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ var scope = string(envScopeRepo)
3434
type envScope string
3535

3636
var (
37-
envScopeRepo envScope = "repo"
38-
envScopeUser envScope = "user"
37+
envScopeRepo envScope = "repo"
38+
envScopeUser envScope = "user"
39+
envScopeLegacyUser envScope = "legacy-user"
3940
)
4041

4142
func envScopeFromString(s string) envScope {
@@ -152,6 +153,11 @@ func connectToServer(ctx context.Context, options *connectToServerOptions) (*con
152153
repositoryPattern = "*/**"
153154
operations = "update"
154155
}
156+
if options != nil && options.setEnvScope == envScopeLegacyUser {
157+
// Updating user env vars requires a different token with a special scope
158+
repositoryPattern = "*/*"
159+
operations = "update"
160+
}
155161

156162
clientToken, err := supervisorClient.Token.GetToken(ctx, &supervisorapi.GetTokenRequest{
157163
Host: wsinfo.GitpodApi.Host,
@@ -228,11 +234,25 @@ func setEnvs(ctx context.Context, setEnvScope envScope, args []string) error {
228234
err = result.client.SetEnvVar(ctx, v)
229235
if err != nil {
230236
if ferr, ok := err.(*jsonrpc2.Error); ok && ferr.Code == http.StatusForbidden && setEnvScope == envScopeUser {
231-
return fmt.Errorf(""+
232-
"Can't automatically create env var `%s` for security reasons.\n"+
233-
"Please create the var manually under %s/user/variables using Name=%s, Scope=*/**, Value=foobar", v.Name, result.gitpodHost, v.Name)
237+
// If we tried updating an env var with */** and it doesn't exist, it may exist with the */* scope
238+
options.setEnvScope = envScopeLegacyUser
239+
result, err := connectToServer(ctx, &options)
240+
if err != nil {
241+
return err
242+
}
243+
defer result.client.Close()
244+
245+
v.RepositoryPattern = "*/*"
246+
err = result.client.SetEnvVar(ctx, v)
247+
if ferr, ok := err.(*jsonrpc2.Error); ok && ferr.Code == http.StatusForbidden {
248+
fmt.Println(ferr.Message, ferr.Data)
249+
return fmt.Errorf(""+
250+
"Can't automatically create env var `%s` for security reasons.\n"+
251+
"Please create the var manually under %s/user/variables using Name=%s, Scope=*/**, Value=foobar", v.Name, result.gitpodHost, v.Name)
252+
}
253+
} else {
254+
return err
234255
}
235-
return err
236256
}
237257
printVar(v.Name, v.Value, exportEnvs)
238258
return nil

components/server/src/workspace/workspace-starter.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,15 @@ export class WorkspaceStarter {
17211721
operations: ["update"],
17221722
}),
17231723
);
1724+
// For updating environment variables created with */* instead of */**, we fall back to updating those
1725+
scopes.push(
1726+
"resource:" +
1727+
ScopedResourceGuard.marshalResourceScope({
1728+
kind: "envVar",
1729+
subjectID: "*/*",
1730+
operations: ["update"],
1731+
}),
1732+
);
17241733
return scopes;
17251734
}
17261735

0 commit comments

Comments
 (0)