4
4
"cmp"
5
5
"fmt"
6
6
"io"
7
+ "log/slog"
7
8
"net/http"
8
9
"net/url"
9
10
"os"
@@ -104,12 +105,20 @@ func (p *githubPlugin) FileContent(subpath string) ([]byte, error) {
104
105
}
105
106
defer res .Body .Close ()
106
107
if res .StatusCode != http .StatusOK {
108
+ authInfo := "No auth header was send with this request."
109
+ if req .Header .Get ("Authorization" ) != "" {
110
+ authInfo = fmt .Sprintf (
111
+ "The auth header `%s` was send with this request." ,
112
+ getRedactedAuthHeader (req ),
113
+ )
114
+ }
107
115
return nil , 0 , usererr .New (
108
- "failed to get plugin %s @ %s (Status code %d). \n Please make " +
116
+ "failed to get plugin %s @ %s (Status code %d).\n %s \n Please make " +
109
117
"sure a plugin.json file exists in plugin directory." ,
110
118
p .LockfileKey (),
111
119
req .URL .String (),
112
120
res .StatusCode ,
121
+ authInfo ,
113
122
)
114
123
}
115
124
body , err := io .ReadAll (res .Body )
@@ -147,6 +156,11 @@ func (p *githubPlugin) request(contentURL string) (*http.Request, error) {
147
156
if ghToken != "" {
148
157
authValue := fmt .Sprintf ("token %s" , ghToken )
149
158
req .Header .Add ("Authorization" , authValue )
159
+ slog .Debug (
160
+ "GITHUB_TOKEN env var found, adding to request's auth header" ,
161
+ "headerValue" ,
162
+ getRedactedAuthHeader (req ),
163
+ )
150
164
}
151
165
152
166
return req , nil
@@ -155,3 +169,22 @@ func (p *githubPlugin) request(contentURL string) (*http.Request, error) {
155
169
func (p * githubPlugin ) LockfileKey () string {
156
170
return p .ref .String ()
157
171
}
172
+
173
+ func getRedactedAuthHeader (req * http.Request ) string {
174
+ authHeader := req .Header .Get ("Authorization" )
175
+ parts := strings .SplitN (authHeader , " " , 2 )
176
+
177
+ if len (authHeader ) < 10 || len (parts ) < 2 {
178
+ // too short to safely reveal any part
179
+ return strings .Repeat ("*" , len (authHeader ))
180
+ }
181
+
182
+ authType , token := parts [0 ], parts [1 ]
183
+ if len (token ) < 10 {
184
+ // second word to short to reveal any, but show first word
185
+ return authType + " " + strings .Repeat ("*" , len (token ))
186
+ }
187
+
188
+ // show first 4 chars of token to help with debugging (will often be "ghp_")
189
+ return authType + " " + token [:4 ] + strings .Repeat ("*" , len (token )- 4 )
190
+ }
0 commit comments