提交 cbd0189f 编写于 作者: M Mislav Marohnić 提交者: GitHub

Merge pull request #1324 from akshatgoel/issue1314

Check if config location is writeable before authenticating
......@@ -332,3 +332,24 @@ 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 credentials
Given $HUB_CONFIG is "/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
And the file "../home/.config/hub" should not exist
Scenario: Config file is not writeable on default location, should exit before asking for credentials
Given a directory named "../home/.config" with mode "600"
When I run `hub create` interactively
Then the output should contain:
"""
/home/.config/hub: permission denied\n
"""
And the exit status should be 1
And the file "../home/.config/hub" should not exist
\ 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(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(CheckWriteable(configsFile()))
err = c.authorizeClient(client, host)
if err != nil {
return
......@@ -277,6 +279,38 @@ func (c *Config) DefaultHost() (host *Host, err error) {
return
}
// 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 CheckWriteable(filename string) error {
// Check if file exists already. if it doesn't, we will delete it after
// checking for writeabilty
fileExistsAlready := false
if _, err := os.Stat(filename); err == nil {
fileExistsAlready = true
}
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()
if !fileExistsAlready {
err := os.Remove(filename)
if err != nil {
return err
}
}
return nil
}
// Public for testing purpose
func CreateTestConfigs(user, token string) *Config {
f, _ := ioutil.TempFile("", "test-config")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册