提交 0d672939 编写于 作者: G Goel

Move checkWriteable to config.

If file is not present before checking for writing, remove it after check.
Add test to check for writeability in default location
上级 b88906b6
......@@ -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 contain ""
And the file "../home/.config/hub" should not exist
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 contain ""
And the file "../home/.config/hub" should not exist
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 contain ""
And the file "../home/.config/hub" should not exist
Scenario: Two-factor authentication, create authorization
Given the GitHub API server:
......@@ -333,14 +333,23 @@ Feature: OAuth authentication
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 |
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,7 +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()))
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.
......@@ -80,7 +80,7 @@ func (c *Config) PromptForHost(host string) (h *Host, err error) {
client := NewClientWithHost(h)
if !tokenFromEnv {
utils.Check(newConfigService().CheckWriteable(configsFile()))
utils.Check(CheckWriteable(configsFile()))
err = c.authorizeClient(client, host)
if err != nil {
return
......@@ -279,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")
......
......@@ -41,20 +41,3 @@ 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.
先完成此消息的编辑!
想要评论请 注册