Skip to content

Commit 6469774

Browse files
committed
Allow checking tokens that cannot read user info
1 parent c067ff4 commit 6469774

File tree

2 files changed

+56
-31
lines changed

2 files changed

+56
-31
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Check Test Token not expired
2323
env:
2424
TEST_TOKEN: ${{ secrets.TEST_TOKEN }}
25-
run: ./github-token-monitor --token-env-vars TEST_TOKEN --expiration-threshold=0
25+
run: ./github-token-monitor --token-env-vars GITHUB_TOKEN,TEST_TOKEN --expiration-threshold=0
2626

2727
- name: Check Test Token expiring in next year
2828
env:

main.go

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"go.opentelemetry.io/otel/attribute"
2323
"go.opentelemetry.io/otel/codes"
2424
sdkTrace "go.opentelemetry.io/otel/sdk/trace"
25+
"go.opentelemetry.io/otel/trace"
2526
)
2627

2728
const (
@@ -161,40 +162,27 @@ func checkToken(ctx context.Context, name, token string) (happy bool, err error)
161162

162163
fmt.Printf("Checking %q...\n", name)
163164

164-
// Make request to e.g. 'https://api.github.com/user' with token
165-
userURL := flags.BaseURL.JoinPath("user").String()
166-
req, err := http.NewRequestWithContext(ctx, "GET", userURL, nil)
167-
if err != nil {
168-
return false, fmt.Errorf("new request: %w", err)
169-
}
170-
req.Header.Set("Authorization", "Bearer "+token)
171-
resp, err := http.DefaultClient.Do(req)
165+
// Make request to check token
166+
resp, _, err := request(ctx, flags.BaseURL.String(), token)
172167
if err != nil {
173-
return false, fmt.Errorf("do request: %w", err)
174-
}
175-
defer resp.Body.Close()
176-
body, err := io.ReadAll(resp.Body)
177-
if err != nil {
178-
return false, fmt.Errorf("reading body: %w", err)
179-
}
180-
if resp.StatusCode != 200 {
181-
if len(body) > 1024 {
182-
body = body[:1024]
183-
}
184-
span.SetAttributes(attribute.String("ghtokmon.error_body", strconv.QuoteToASCII(string(body))))
185-
return false, fmt.Errorf("got status code %d != 200", resp.StatusCode)
168+
return false, fmt.Errorf("checking token: %w", err)
186169
}
187170

188-
// Parse user login
189-
var user struct {
190-
Login string `json:"login"`
191-
}
192-
err = json.Unmarshal(body, &user)
193-
if err != nil {
194-
return false, fmt.Errorf("deserializing user: %w", err)
171+
// Get user info (if permitted)
172+
userURL := flags.BaseURL.JoinPath("user").String()
173+
_, userJSON, err := request(ctx, userURL, token)
174+
if err == nil {
175+
// Parse user login
176+
var user struct {
177+
Login string `json:"login"`
178+
}
179+
err = json.Unmarshal(userJSON, &user)
180+
if err != nil {
181+
return false, fmt.Errorf("deserializing user: %w", err)
182+
}
183+
span.SetAttributes(attribute.String("ghtokmon.token.login", user.Login))
184+
fmt.Printf("Token user login: %s\n", user.Login)
195185
}
196-
span.SetAttributes(attribute.String("ghtokmon.token.login", user.Login))
197-
fmt.Printf("Token user login: %s\n", user.Login)
198186

199187
happy = true
200188

@@ -249,6 +237,43 @@ func checkToken(ctx context.Context, name, token string) (happy bool, err error)
249237
return happy, nil
250238
}
251239

240+
func request(ctx context.Context, url, token string) (resp *http.Response, body []byte, err error) {
241+
ctx, span := otel.Tracer("").Start(ctx, url)
242+
defer func() {
243+
if err != nil {
244+
span.SetStatus(codes.Error, err.Error())
245+
}
246+
span.End()
247+
}()
248+
249+
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
250+
if err != nil {
251+
return nil, nil, fmt.Errorf("new request: %w", err)
252+
}
253+
254+
req.Header.Set("Authorization", "Bearer "+token)
255+
256+
resp, err = http.DefaultClient.Do(req)
257+
if err != nil {
258+
return nil, nil, fmt.Errorf("do request: %w", err)
259+
}
260+
defer resp.Body.Close()
261+
262+
body, err = io.ReadAll(resp.Body)
263+
if err != nil {
264+
return nil, nil, fmt.Errorf("reading body: %w", err)
265+
}
266+
267+
if resp.StatusCode != 200 {
268+
if len(body) > 1024 {
269+
body = body[:1024]
270+
}
271+
trace.SpanFromContext(ctx).SetAttributes(attribute.String("ghtokmon.error_body", strconv.QuoteToASCII(string(body))))
272+
return nil, nil, fmt.Errorf("got status code %d != 200", resp.StatusCode)
273+
}
274+
return
275+
}
276+
252277
type failedChecksError []string
253278

254279
func (ut failedChecksError) Error() string {

0 commit comments

Comments
 (0)