提交 b88906b6 编写于 作者: G Goel

Check if config location is writeable before authenticating

Fixes #1314
上级 4a7f8cb5
......@@ -95,7 +95,7 @@ Feature: OAuth authentication
Duplicate value for "description"
"""
And the exit status should be 1
And the file "../home/.config/hub" should not exist
And the file "../home/.config/hub" should contain ""
Scenario: Credentials from GITHUB_USER & GITHUB_PASSWORD
Given the GitHub API server:
......@@ -172,7 +172,7 @@ Feature: OAuth authentication
"""
And the exit status should be 1
And the file "../home/.config/hub" should not exist
And the file "../home/.config/hub" should contain ""
Scenario: Personal access token used instead of password
Given the GitHub API server:
......@@ -192,7 +192,7 @@ Feature: OAuth authentication
"""
And the exit status should be 1
And the file "../home/.config/hub" should not exist
And the file "../home/.config/hub" should contain ""
Scenario: Two-factor authentication, create authorization
Given the GitHub API server:
......@@ -332,3 +332,15 @@ Feature: OAuth authentication
Then the output should contain "github.com username:"
And the output should contain "missing user"
And the file "../home/.config/hub" should not contain "user"
Scenario: Config file is not writeable, should exit before asking for credentails
Given I set the environment variables to:
| variable | value |
| HUB_CONFIG | /InvalidConfigFile |
When I run `hub create` interactively
Then the output should contain exactly:
"""
open /InvalidConfigFile: permission denied\n
"""
And the exit status should be 1
\ No newline at end of file
......@@ -51,6 +51,7 @@ func (c *Config) PromptForHost(host string) (h *Host, err error) {
h = c.Find(host)
if h != nil {
if h.User == "" {
utils.Check(newConfigService().CheckWriteable(configsFile()))
// User is missing from the config: this is a broken config probably
// because it was created with an old (broken) version of hub. Let's fix
// it now. See issue #1007 for details.
......@@ -79,6 +80,7 @@ func (c *Config) PromptForHost(host string) (h *Host, err error) {
client := NewClientWithHost(h)
if !tokenFromEnv {
utils.Check(newConfigService().CheckWriteable(configsFile()))
err = c.authorizeClient(client, host)
if err != nil {
return
......
......@@ -41,3 +41,20 @@ func (s *configService) Load(filename string, c *Config) error {
return s.Decoder.Decode(r, c)
}
// CheckWriteable checks if config file is writeable. This should
// be called before asking for credentials and only if current
// operation needs to update the file. See issue #1314 for details.
func (s *configService) CheckWriteable(filename string) error {
err := os.MkdirAll(filepath.Dir(filename), 0771)
if err != nil {
return err
}
w, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return err
}
w.Close()
return nil
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册