Skip to content

Commit 161615e

Browse files
committed
Fixed further instances of potential concurrent reads and write races
1 parent 135ce67 commit 161615e

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/tools/aws.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ func Authenticate() {
6464
log.Fatalf("unable to get authorization token, %v", authErr)
6565
}
6666
log.Printf("Authorization successful")
67+
68+
mutex.Lock()
6769
CodeArtifactAuthInfo.AuthorizationToken = *authResp.AuthorizationToken
6870
CodeArtifactAuthInfo.LastAuth = time.Now()
6971

@@ -82,6 +84,7 @@ func Authenticate() {
8284
log.Fatalf("unable to get repository endpoint, %v", urlErr)
8385
}
8486
CodeArtifactAuthInfo.Url = *urlResp.RepositoryEndpoint
87+
mutex.Unlock()
8588

8689
log.Printf("Requests will now be proxied to %s", CodeArtifactAuthInfo.Url)
8790
}
@@ -91,11 +94,11 @@ func CheckReauth() {
9194
for {
9295
timeSince := time.Since(CodeArtifactAuthInfo.LastAuth).Minutes()
9396
// Panic and shut down the proxy if we couldn't reauthenticate within the 15 minute window for some reason.
94-
if timeSince > float64(60) {
97+
if timeSince > float64(5) {
9598
log.Panic("Was unable to re-authenticate prior to our token expiring, shutting down proxty...")
9699
}
97100

98-
if CodeArtifactAuthInfo.AuthorizationToken == "" || timeSince > float64(45) {
101+
if CodeArtifactAuthInfo.AuthorizationToken == "" || timeSince > float64(1) {
99102
log.Printf("%f minutes until the CodeArtifact token expires, attempting a reauth.", 60-timeSince)
100103
Authenticate()
101104
}

src/tools/proxy.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ var mutex = &sync.Mutex{}
2020
// ProxyRequestHandler intercepts requests to CodeArtifact and add the Authorization header + correct Host header
2121
func ProxyRequestHandler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
2222
return func(w http.ResponseWriter, r *http.Request) {
23-
2423
mutex.Lock()
2524
// Store the original host header for each request
2625
originalUrlResolver[r.RemoteAddr] = r.URL
@@ -32,7 +31,6 @@ func ProxyRequestHandler(p *httputil.ReverseProxy) func(http.ResponseWriter, *ht
3231
} else {
3332
originalUrlResolver[r.RemoteAddr].Scheme = "http"
3433
}
35-
mutex.Unlock()
3634

3735
// Override the Host header with the CodeArtifact Host
3836
u, _ := url.Parse(CodeArtifactAuthInfo.Url)
@@ -44,6 +42,7 @@ func ProxyRequestHandler(p *httputil.ReverseProxy) func(http.ResponseWriter, *ht
4442
log.Printf("REQ: %s %s \"%s\" \"%s\"", r.RemoteAddr, r.Method, r.URL.RequestURI(), r.UserAgent())
4543

4644
log.Printf("Sending request to %s%s", strings.Trim(CodeArtifactAuthInfo.Url, "/"), r.URL.RequestURI())
45+
mutex.Unlock()
4746

4847
p.ServeHTTP(w, r)
4948
}
@@ -59,10 +58,10 @@ func ProxyResponseHandler() func(*http.Response) error {
5958
mutex.Lock()
6059
originalUrl := originalUrlResolver[r.Request.RemoteAddr]
6160
delete(originalUrlResolver, r.Request.RemoteAddr)
62-
mutex.Unlock()
6361

6462
u, _ := url.Parse(CodeArtifactAuthInfo.Url)
6563
hostname := u.Host + ":443"
64+
mutex.Unlock()
6665

6766
// Rewrite the 301 to point from CodeArtifact URL to the proxy instead..
6867
if r.StatusCode == 301 || r.StatusCode == 302 {
@@ -97,11 +96,13 @@ func ProxyResponseHandler() func(*http.Response) error {
9796
oldContentResponse, _ := ioutil.ReadAll(body)
9897
oldContentResponseStr := string(oldContentResponse)
9998

99+
mutex.Lock()
100100
resolvedHostname := strings.Replace(CodeArtifactAuthInfo.Url, u.Host, hostname, -1)
101101
newUrl := fmt.Sprintf("%s://%s/", originalUrl.Scheme, originalUrl.Host)
102102

103103
newResponseContent := strings.Replace(oldContentResponseStr, resolvedHostname, newUrl, -1)
104104
newResponseContent = strings.Replace(newResponseContent, CodeArtifactAuthInfo.Url, newUrl, -1)
105+
mutex.Unlock()
105106

106107
r.Body = ioutil.NopCloser(strings.NewReader(newResponseContent))
107108
r.ContentLength = int64(len(newResponseContent))

0 commit comments

Comments
 (0)