Skip to content

Commit 1036666

Browse files
authored
Merge pull request cli#1077 from cli/auth-split
Write per-host config info to `hosts.yml` instead of `config.yml`
2 parents 24ca449 + 9a5b628 commit 1036666

File tree

15 files changed

+414
-284
lines changed

15 files changed

+414
-284
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ endif
1111
LDFLAGS := -X github.com/cli/cli/command.Version=$(GH_VERSION) $(LDFLAGS)
1212
LDFLAGS := -X github.com/cli/cli/command.BuildDate=$(BUILD_DATE) $(LDFLAGS)
1313
ifdef GH_OAUTH_CLIENT_SECRET
14-
LDFLAGS := -X github.com/cli/cli/context.oauthClientID=$(GH_OAUTH_CLIENT_ID) $(LDFLAGS)
15-
LDFLAGS := -X github.com/cli/cli/context.oauthClientSecret=$(GH_OAUTH_CLIENT_SECRET) $(LDFLAGS)
14+
LDFLAGS := -X github.com/cli/cli/internal/config.oauthClientID=$(GH_OAUTH_CLIENT_ID) $(LDFLAGS)
15+
LDFLAGS := -X github.com/cli/cli/internal/config.oauthClientSecret=$(GH_OAUTH_CLIENT_SECRET) $(LDFLAGS)
1616
endif
1717

1818
bin/gh: $(BUILD_FILES)

api/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ func (c Client) HasScopes(wantedScopes ...string) (bool, string, error) {
165165
}
166166
defer res.Body.Close()
167167

168+
if res.StatusCode != 200 {
169+
return false, "", handleHTTPError(res)
170+
}
171+
168172
appID := res.Header.Get("X-Oauth-Client-Id")
169173
hasScopes := strings.Split(res.Header.Get("X-Oauth-Scopes"), ",")
170174

auth/oauth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ func (oa *OAuthFlow) ObtainAccessToken() (accessToken string, err error) {
6666
fmt.Fprintf(os.Stderr, "Please open the following URL manually:\n%s\n", startURL)
6767
fmt.Fprintf(os.Stderr, "")
6868
// TODO: Temporary workaround for https://github.com/cli/cli/issues/297
69-
fmt.Fprintf(os.Stderr, "If you are on a server or other headless system, use this workaround instead:")
70-
fmt.Fprintf(os.Stderr, " 1. Complete authentication on a GUI system")
71-
fmt.Fprintf(os.Stderr, " 2. Copy the contents of ~/.config/gh/config.yml to this system")
69+
fmt.Fprintf(os.Stderr, "If you are on a server or other headless system, use this workaround instead:\n")
70+
fmt.Fprintf(os.Stderr, " 1. Complete authentication on a GUI system;\n")
71+
fmt.Fprintf(os.Stderr, " 2. Copy the contents of `~/.config/gh/hosts.yml` to this system.\n")
7272
}
7373

7474
_ = http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

command/alias_test.go

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212
func TestAliasSet_gh_command(t *testing.T) {
1313
initBlankContext("", "OWNER/REPO", "trunk")
1414

15-
buf := bytes.NewBufferString("")
16-
defer config.StubWriteConfig(buf)()
15+
mainBuf := bytes.Buffer{}
16+
hostsBuf := bytes.Buffer{}
17+
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
1718

1819
_, err := RunCommand("alias set pr pr status")
1920
if err == nil {
@@ -26,15 +27,14 @@ func TestAliasSet_gh_command(t *testing.T) {
2627
func TestAliasSet_empty_aliases(t *testing.T) {
2728
cfg := `---
2829
aliases:
29-
hosts:
30-
github.com:
31-
user: OWNER
32-
oauth_token: token123
30+
editor: vim
3331
`
3432
initBlankContext(cfg, "OWNER/REPO", "trunk")
3533

36-
buf := bytes.NewBufferString("")
37-
defer config.StubWriteConfig(buf)()
34+
mainBuf := bytes.Buffer{}
35+
hostsBuf := bytes.Buffer{}
36+
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
37+
3838
output, err := RunCommand("alias set co pr checkout")
3939

4040
if err != nil {
@@ -45,12 +45,9 @@ hosts:
4545

4646
expected := `aliases:
4747
co: pr checkout
48-
hosts:
49-
github.com:
50-
user: OWNER
51-
oauth_token: token123
48+
editor: vim
5249
`
53-
eq(t, buf.String(), expected)
50+
eq(t, mainBuf.String(), expected)
5451
}
5552

5653
func TestAliasSet_existing_alias(t *testing.T) {
@@ -64,8 +61,10 @@ aliases:
6461
`
6562
initBlankContext(cfg, "OWNER/REPO", "trunk")
6663

67-
buf := bytes.NewBufferString("")
68-
defer config.StubWriteConfig(buf)()
64+
mainBuf := bytes.Buffer{}
65+
hostsBuf := bytes.Buffer{}
66+
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
67+
6968
output, err := RunCommand("alias set co pr checkout -Rcool/repo")
7069

7170
if err != nil {
@@ -78,8 +77,9 @@ aliases:
7877
func TestAliasSet_space_args(t *testing.T) {
7978
initBlankContext("", "OWNER/REPO", "trunk")
8079

81-
buf := bytes.NewBufferString("")
82-
defer config.StubWriteConfig(buf)()
80+
mainBuf := bytes.Buffer{}
81+
hostsBuf := bytes.Buffer{}
82+
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
8383

8484
output, err := RunCommand(`alias set il issue list -l 'cool story'`)
8585

@@ -89,7 +89,7 @@ func TestAliasSet_space_args(t *testing.T) {
8989

9090
test.ExpectLines(t, output.String(), `Adding alias for il: issue list -l "cool story"`)
9191

92-
test.ExpectLines(t, buf.String(), `il: issue list -l "cool story"`)
92+
test.ExpectLines(t, mainBuf.String(), `il: issue list -l "cool story"`)
9393
}
9494

9595
func TestAliasSet_arg_processing(t *testing.T) {
@@ -118,77 +118,66 @@ func TestAliasSet_arg_processing(t *testing.T) {
118118
`ix: issue list --author=\$1 --label=\$2`},
119119
}
120120

121-
var buf *bytes.Buffer
122121
for _, c := range cases {
123-
buf = bytes.NewBufferString("")
124-
defer config.StubWriteConfig(buf)()
122+
mainBuf := bytes.Buffer{}
123+
hostsBuf := bytes.Buffer{}
124+
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
125+
125126
output, err := RunCommand(c.Cmd)
126127
if err != nil {
127128
t.Fatalf("got unexpected error running %s: %s", c.Cmd, err)
128129
}
129130

130131
test.ExpectLines(t, output.String(), c.ExpectedOutputLine)
131-
test.ExpectLines(t, buf.String(), c.ExpectedConfigLine)
132+
test.ExpectLines(t, mainBuf.String(), c.ExpectedConfigLine)
132133
}
133134
}
134135

135136
func TestAliasSet_init_alias_cfg(t *testing.T) {
136137
cfg := `---
137-
hosts:
138-
github.com:
139-
user: OWNER
140-
oauth_token: token123
138+
editor: vim
141139
`
142140
initBlankContext(cfg, "OWNER/REPO", "trunk")
143141

144-
buf := bytes.NewBufferString("")
145-
defer config.StubWriteConfig(buf)()
142+
mainBuf := bytes.Buffer{}
143+
hostsBuf := bytes.Buffer{}
144+
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
146145

147146
output, err := RunCommand("alias set diff pr diff")
148147
if err != nil {
149148
t.Fatalf("unexpected error: %s", err)
150149
}
151-
expected := `hosts:
152-
github.com:
153-
user: OWNER
154-
oauth_token: token123
150+
expected := `editor: vim
155151
aliases:
156152
diff: pr diff
157153
`
158154

159155
test.ExpectLines(t, output.String(), "Adding alias for diff: pr diff", "Added alias.")
160-
eq(t, buf.String(), expected)
156+
eq(t, mainBuf.String(), expected)
161157
}
162158

163159
func TestAliasSet_existing_aliases(t *testing.T) {
164160
cfg := `---
165-
hosts:
166-
github.com:
167-
user: OWNER
168-
oauth_token: token123
169161
aliases:
170162
foo: bar
171163
`
172164
initBlankContext(cfg, "OWNER/REPO", "trunk")
173165

174-
buf := bytes.NewBufferString("")
175-
defer config.StubWriteConfig(buf)()
166+
mainBuf := bytes.Buffer{}
167+
hostsBuf := bytes.Buffer{}
168+
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
176169

177170
output, err := RunCommand("alias set view pr view")
178171
if err != nil {
179172
t.Fatalf("unexpected error: %s", err)
180173
}
181-
expected := `hosts:
182-
github.com:
183-
user: OWNER
184-
oauth_token: token123
185-
aliases:
174+
expected := `aliases:
186175
foo: bar
187176
view: pr view
188177
`
189178

190179
test.ExpectLines(t, output.String(), "Adding alias for view: pr view", "Added alias.")
191-
eq(t, buf.String(), expected)
180+
eq(t, mainBuf.String(), expected)
192181

193182
}
194183

command/config_test.go

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,31 @@ func TestConfigGet_not_found(t *testing.T) {
4949
func TestConfigSet(t *testing.T) {
5050
initBlankContext("", "OWNER/REPO", "master")
5151

52-
buf := bytes.NewBufferString("")
53-
defer config.StubWriteConfig(buf)()
52+
mainBuf := bytes.Buffer{}
53+
hostsBuf := bytes.Buffer{}
54+
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
55+
5456
output, err := RunCommand("config set editor ed")
5557
if err != nil {
5658
t.Fatalf("error running command `config set editor ed`: %v", err)
5759
}
5860

59-
eq(t, output.String(), "")
61+
if len(output.String()) > 0 {
62+
t.Errorf("expected output to be blank: %q", output.String())
63+
}
6064

61-
expected := `hosts:
62-
github.com:
63-
user: OWNER
64-
oauth_token: 1234567890
65-
editor: ed
65+
expectedMain := "editor: ed\n"
66+
expectedHosts := `github.com:
67+
user: OWNER
68+
oauth_token: "1234567890"
6669
`
6770

68-
eq(t, buf.String(), expected)
71+
if mainBuf.String() != expectedMain {
72+
t.Errorf("expected config.yml to be %q, got %q", expectedMain, mainBuf.String())
73+
}
74+
if hostsBuf.String() != expectedHosts {
75+
t.Errorf("expected hosts.yml to be %q, got %q", expectedHosts, hostsBuf.String())
76+
}
6977
}
7078

7179
func TestConfigSet_update(t *testing.T) {
@@ -79,23 +87,31 @@ editor: ed
7987

8088
initBlankContext(cfg, "OWNER/REPO", "master")
8189

82-
buf := bytes.NewBufferString("")
83-
defer config.StubWriteConfig(buf)()
90+
mainBuf := bytes.Buffer{}
91+
hostsBuf := bytes.Buffer{}
92+
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
8493

8594
output, err := RunCommand("config set editor vim")
8695
if err != nil {
8796
t.Fatalf("error running command `config get editor`: %v", err)
8897
}
8998

90-
eq(t, output.String(), "")
99+
if len(output.String()) > 0 {
100+
t.Errorf("expected output to be blank: %q", output.String())
101+
}
91102

92-
expected := `hosts:
93-
github.com:
94-
user: OWNER
95-
oauth_token: MUSTBEHIGHCUZIMATOKEN
96-
editor: vim
103+
expectedMain := "editor: vim\n"
104+
expectedHosts := `github.com:
105+
user: OWNER
106+
oauth_token: MUSTBEHIGHCUZIMATOKEN
97107
`
98-
eq(t, buf.String(), expected)
108+
109+
if mainBuf.String() != expectedMain {
110+
t.Errorf("expected config.yml to be %q, got %q", expectedMain, mainBuf.String())
111+
}
112+
if hostsBuf.String() != expectedHosts {
113+
t.Errorf("expected hosts.yml to be %q, got %q", expectedHosts, hostsBuf.String())
114+
}
99115
}
100116

101117
func TestConfigGetHost(t *testing.T) {
@@ -141,23 +157,32 @@ git_protocol: ssh
141157
func TestConfigSetHost(t *testing.T) {
142158
initBlankContext("", "OWNER/REPO", "master")
143159

144-
buf := bytes.NewBufferString("")
145-
defer config.StubWriteConfig(buf)()
160+
mainBuf := bytes.Buffer{}
161+
hostsBuf := bytes.Buffer{}
162+
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
163+
146164
output, err := RunCommand("config set -hgithub.com git_protocol ssh")
147165
if err != nil {
148166
t.Fatalf("error running command `config set editor ed`: %v", err)
149167
}
150168

151-
eq(t, output.String(), "")
169+
if len(output.String()) > 0 {
170+
t.Errorf("expected output to be blank: %q", output.String())
171+
}
152172

153-
expected := `hosts:
154-
github.com:
155-
user: OWNER
156-
oauth_token: 1234567890
157-
git_protocol: ssh
173+
expectedMain := ""
174+
expectedHosts := `github.com:
175+
user: OWNER
176+
oauth_token: "1234567890"
177+
git_protocol: ssh
158178
`
159179

160-
eq(t, buf.String(), expected)
180+
if mainBuf.String() != expectedMain {
181+
t.Errorf("expected config.yml to be %q, got %q", expectedMain, mainBuf.String())
182+
}
183+
if hostsBuf.String() != expectedHosts {
184+
t.Errorf("expected hosts.yml to be %q, got %q", expectedHosts, hostsBuf.String())
185+
}
161186
}
162187

163188
func TestConfigSetHost_update(t *testing.T) {
@@ -171,21 +196,30 @@ hosts:
171196

172197
initBlankContext(cfg, "OWNER/REPO", "master")
173198

174-
buf := bytes.NewBufferString("")
175-
defer config.StubWriteConfig(buf)()
199+
mainBuf := bytes.Buffer{}
200+
hostsBuf := bytes.Buffer{}
201+
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
176202

177203
output, err := RunCommand("config set -hgithub.com git_protocol https")
178204
if err != nil {
179205
t.Fatalf("error running command `config get editor`: %v", err)
180206
}
181207

182-
eq(t, output.String(), "")
208+
if len(output.String()) > 0 {
209+
t.Errorf("expected output to be blank: %q", output.String())
210+
}
183211

184-
expected := `hosts:
185-
github.com:
186-
git_protocol: https
187-
user: OWNER
188-
oauth_token: MUSTBEHIGHCUZIMATOKEN
212+
expectedMain := ""
213+
expectedHosts := `github.com:
214+
git_protocol: https
215+
user: OWNER
216+
oauth_token: MUSTBEHIGHCUZIMATOKEN
189217
`
190-
eq(t, buf.String(), expected)
218+
219+
if mainBuf.String() != expectedMain {
220+
t.Errorf("expected config.yml to be %q, got %q", expectedMain, mainBuf.String())
221+
}
222+
if hostsBuf.String() != expectedHosts {
223+
t.Errorf("expected hosts.yml to be %q, got %q", expectedHosts, hostsBuf.String())
224+
}
191225
}

0 commit comments

Comments
 (0)