Skip to content

Write per-host config info to hosts.yml instead of config.yml#1077

Merged
mislav merged 15 commits intotrunkfrom
auth-split
Jun 4, 2020
Merged

Write per-host config info to hosts.yml instead of config.yml#1077
mislav merged 15 commits intotrunkfrom
auth-split

Conversation

@mislav
Copy link
Contributor

@mislav mislav commented Jun 2, 2020

All per-host configuration info, including authentication credentials, is now written to hosts.yml instead of to config.yml. This is to make config.yml safe to check into version control and share. The migration from old to new config style is done whenever new configuration is written to disk.

The biggest functional change that I've done as part of this PR is that accessing the Config object does not trigger authentication process anymore if the config file is missing. Instead, a blank Config object is returned. This is to facilitate reading configuration such as aliases #970 without triggering any side-effects.

Authentication is now triggered whenever context.AuthToken() is accessed, but when there is no oauth_token for the current hostname.

Another change in this PR is that the config implementation does not hardcode github.com anymore and handles well when other hostnames are written to and read from. This is mainly to pave the way to Enterprise compatibility #273.

Fixes #937

mislav added 9 commits June 2, 2020 13:19
It's not being depended on anywhere right now, so this minimizes the
public interface.
Previously we would trigger OAuth flow when the config file did not
exist. Now we will let an empty Config object be initialized in that
case, but trigger OAuth flow when the Context caller requests an
AuthToken.
We dynamically add hosts on `Set`, so this `hosts` cache might fall out
of date. We could ensure to keep it updated, but I'm not convinced it's
necessary for speed right now.
Read from and write to the `hosts.yml` file every time `config.yml` is
accessed. Everything that before went under the `hosts:` map now belongs
to `hosts.yml`.
@mislav mislav requested review from probablycorey and vilmibm June 2, 2020 13:52
Comment on lines +111 to +112
var hosts map[string][]map[string]string
err = yaml.Unmarshal(b, &hosts)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

migrateConfig was simplified to write to a new blank config and have all internal YAML- or write-related logic handled by fileConfig implementation

Comment on lines -79 to -87
// TODO this sucks. It precludes us laying out a nice config with comments and such.
type yamlConfig struct {
Hosts map[string]map[string]string
}

yamlHosts := map[string]map[string]string{}
yamlHosts[oauthHost] = map[string]string{}
yamlHosts[oauthHost]["user"] = userLogin
yamlHosts[oauthHost]["oauth_token"] = token
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥🔥🔥

Comment on lines +176 to +179
if nodes[i].Value == "hosts" {
hostsData.Content = append(hostsData.Content, nodes[i+1].Content...)
} else {
mainData.Content = append(mainData.Content, nodes[i], nodes[i+1])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting config into config.yml and hosts.yml is done at Write time

Copy link
Contributor

@vilmibm vilmibm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how this came out and just had one minor nitpick.

We should keep in mind as a team that until users re-authenticate or set a config value, their configs will not be split. I think that's ok for now but it's something that anyone responding to support issues is going to be want to be aware of.

return token, nil
}

func AuthFlow(oauthHost, notice string) (string, string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this still be its own function? or at least made non-exported? I only see it used in AuthFlowWithConfig

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! I'll make it private.

Copy link
Contributor

@probablycorey probablycorey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The biggest functional change that I've done as part of this PR is that accessing the Config object does not trigger authentication process anymore if the config file is missing.

Nice!

This works great.

return err
}

cfgFile, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) // cargo coded from setup
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought fn was a function for a bit and was very confused. Maybe filename or just f?

@mislav
Copy link
Contributor Author

mislav commented Jun 3, 2020

We should keep in mind as a team that until users re-authenticate or set a config value, their configs will not be split.

@vilmibm Do you think that we should split it immediately on read when we detect "legacy" format like we did with migrateConfig in the past? I don't think making that change would be hard, but for now I've opted to not do it since I didn't feel that there is a strong reason to do the split until the user interacts with gh config set or gh alias or re-authentication

@vilmibm
Copy link
Contributor

vilmibm commented Jun 3, 2020

Do you think that we should split it immediately on read when we detect "legacy" format like we did with migrateConfig in the past?

I'm fine with not splitting immediately; I was just pointing out that whatever we do we (ie the people that field support requests via issues) should all be aware of it.

@OJFord
Copy link

OJFord commented Sep 18, 2020

Thanks very much for that comment @vilmibm - I was initially disappointed the token was still there post-1.0, but then saw this merge and wasn't sure what I needed to do in order to get it out.

For anyone else:
git config get ... is insufficient to trigger it, but any set will do it, even if it's a noop, e.g. git config set git_protocol "$(git config get git_protocol)".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Split config file - to separate credentials

4 participants