@@ -34,8 +34,9 @@ var scope = string(envScopeRepo)
3434type envScope string
3535
3636var (
37- envScopeRepo envScope = "repo"
38- envScopeUser envScope = "user"
37+ envScopeRepo envScope = "repo"
38+ envScopeUser envScope = "user"
39+ envScopeLegacyUser envScope = "legacy-user"
3940)
4041
4142func 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
0 commit comments