Repository navigation
config.yml is opened for writing on gh auth switch when it should not be #8496
Description
Activity
I can reproduce the file opening behavior that you mentioned, but..
However, they do not actually fail, and the account is switched. This error is rather misleading
..it seems that the switch action doesn't actually go through, thus matching the error message.
98% ~ ❯ gh auth token gho_redacted5sh (base) 98% ~ ❯ gh auth switch X Failed to switch account for github.com to arunsathiya open /Users/arun/.config/gh/hosts.yml: operation not permitted (base) 98% ~ ❯ gh auth token gho_redacted5sh
We can notice that both tokens end in 5sh, thus indicating the same account.
Ah, in my (and most
home-managerusers') case, thehosts.ymlfile is mutable. Just not theconfig.ymlfile.I can reproduce the same thing you did if
hosts.ymlis also immutable, but that's not the exact issue I was running into.Interesting. I have been using keyring/keychain for secure storage, so I was not familiar with
hosts.ymlvsconfig.ymldifferences.When I used
--insecure-storageflag ingh auth login, it stored credentials tohosts.yml, so I just assumed thatconfig.ymlhas been phased out in favor ofhosts.ymlHey, thanks for opening this issue and sorry for the inconvenience and delay (catching up on issues that need triage after the holidays).
I can reproduce this on Mac:
➜ ~ gh auth status github.com ✓ Logged in to github.com account wilmartin_microsoft (keyring) - Active account: true - Git operations protocol: https - Token: gho_************************************ - Token scopes: 'gist', 'read:org', 'read:project', 'repo', 'workflow' ✓ Logged in to github.com account williammartin (keyring) - Active account: false - Git operations protocol: https - Token: gho_************************************ - Token scopes: 'gist', 'read:org', 'read:project', 'repo', 'workflow' ➜ ~ sudo chflags uchg ~/.config/gh/config.yml ➜ ~ gh auth switch X Failed to switch account for github.com to williammartin open /Users/williammartin/.config/gh/config.yml: operation not permittedThis is definitely a bug and I will look into it. As you noted, it shouldn't actually result in any functional problem because well, the
configfile shouldn't be written at all when switching.
When I used --insecure-storage flag in gh auth login, it stored credentials to hosts.yml, so I just assumed that config.yml has been phased out in favor of hosts.yml
The historical reason for the split as far as I know is that it should be possible to put the config in version control without worrying about tokens being committed. I don't know if that's true or not as no one from the team is still around 😬
- addedpriority-3Affects a small number of users or is largely cosmeticAffects a small number of users or is largely cosmeticpriority-2Affects more than a few users but doesn't prevent core functionsAffects more than a few users but doesn't prevent core functionsand removedneeds-triageneeds to be reviewedneeds to be reviewedpriority-3Affects a small number of users or is largely cosmeticAffects a small number of users or is largely cosmetic
on Jan 22, 2024 Actually there is a bug here as well because the rollback logic in case of error leaves things in an inconsistent state.
The problem is that the hosts file has been persisted while the config file hasn't. Thus the
hostsfile says the switch has taken place, but we've rolled back the active token to the previous user.// We are currently williammartin ➜ ~ gh api /user | jq .login "williammartin" // We switch which fails ➜ ~ gh auth switch X Failed to switch account for github.com to wilmartin_microsoft open /Users/williammartin/.config/gh/config.yml: operation not permitted // gh auth status says the switch was successful ➜ ~ gh auth status github.com ✓ Logged in to github.com account wilmartin_microsoft (keyring) - Active account: true - Git operations protocol: https - Token: gho_************************************ - Token scopes: 'gist', 'read:org', 'read:project', 'repo', 'workflow' ✓ Logged in to github.com account williammartin (keyring) - Active account: false - Git operations protocol: https - Token: gho_************************************ - Token scopes: 'gist', 'read:org', 'read:project', 'repo', 'workflow' // But the token is actually still for the old user ➜ ~ gh api /user | jq .login "williammartin"I've upgraded this to a
p2as a result.As far as I can tell this has been an issue for a long long time for any command that made changes to the
hostsfile. Checkout the difference betweenv2.13.0andv2.14.0:➜ cli git:(v2.13.0) ✗ ./bin/gh version gh version 2.13.0 (2024-01-22) https://github.com/cli/cli/releases/tag/v2.13.0 ➜ cli git:(v2.13.0) ✗ ./bin/gh auth logout ✓ Logged out of github.com account 'williammartin'➜ cli git:(v2.14.0) ✗ ./bin/gh version gh version 2.14.0 (2024-01-22) https://github.com/cli/cli/releases/tag/v2.14.0 ➜ cli git:(v2.14.0) ✗ ./bin/gh auth logout failed to write config, authentication configuration not updated: open /Users/williammartin/.config/gh/config.yml: operation not permittedThis change coincided with cli/go-gh#44 which moved config management from
cli/cliintocli/go-ghin June 2022 and as far as I can tell the logic has been incorrect since then. The intention was that only if a file had changed, should it be persisted:But the thing is that it appears to me as if the general node is marked as modified if there is a hosts file:
Here's a test that demonstrates this incorrect behaviour:
func TestIsModifiedOnLoad(t *testing.T) { // Given we have a persisted config and hosts file tempDir := t.TempDir() t.Setenv("GH_CONFIG_DIR", tempDir) require.NoError(t, writeFile(hostsConfigFile(), []byte(testHostsData()))) require.NoError(t, writeFile(generalConfigFile(), []byte(testGlobalData()))) // When we load that config cfg, err := load(generalConfigFile(), hostsConfigFile(), nil) require.NoError(t, err) // Then the general entries should be unmodified (because we didn't mutate anything) require.False(t, cfg.entries.IsModified()) }Created cli/go-gh#147 to address this.
Reacted by Varun Narravula- addedgh-authrelating to the gh auth commandrelating to the gh auth commandgh-configrelating to the gh config commandrelating to the gh config command
on Jan 22, 2024 That PR has been merged, so when we ship a new version of
go-ghwe'll include it in the next CLI release which will be in the next week or two.Reacted by Varun Narravula- added a commit that references this issue
on Jan 31, 2024
Describe the bug
Certain commands, like
gh auth switch, appear to fail entirely with an error failing to open theconfig.ymlwhen it is immutable (i.e. when using home-manager):However, they do not actually fail, and the account is switched. This error is rather misleading, because I thought this was a fatal error at first and the account switch didn't happen when it did, and I don't believe that
config.ymlshould ever be opened in situations like this where it never is touched at all.This occurs on
gh version 2.40.1.Steps to reproduce the behavior
config.ymlimmutable. (chattr +i ~/.config/gh/config.ymlfor testing)gh auth switchExpected vs actual behavior
I expected the account to be switched without errors like this, and the
config.ymlto not be opened for writing at all.Logs