Skip to content

Commit 98e5c5f

Browse files
committed
Fixed up redirector headers
1 parent d664f02 commit 98e5c5f

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package client_wrapper
2+
3+
import "net/http"
4+
5+
type HeaderRoundTripper struct {
6+
Transport http.RoundTripper
7+
Headers map[string]string
8+
}
9+
10+
func (h *HeaderRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
11+
for key, value := range h.Headers {
12+
req.Header.Set(key, value)
13+
}
14+
return h.Transport.RoundTrip(req)
15+
}

internal/entry/entry.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/spaces"
1010
"github.com/OctopusSolutionsEngineering/OctopusRecommendationEngine/internal/checks"
1111
"github.com/OctopusSolutionsEngineering/OctopusRecommendationEngine/internal/checks/factory"
12+
"github.com/OctopusSolutionsEngineering/OctopusRecommendationEngine/internal/client_wrapper"
1213
"github.com/OctopusSolutionsEngineering/OctopusRecommendationEngine/internal/config"
1314
"github.com/OctopusSolutionsEngineering/OctopusRecommendationEngine/internal/executor"
1415
"github.com/briandowns/spinner"
@@ -69,14 +70,16 @@ func Entry(octolintConfig *config.OctolintConfig) ([]checks.OctopusCheckResult,
6970
octolintConfig.Space = spaceId
7071
}
7172

72-
client, err := createClient(octolintConfig)
73+
httpClient, err := createHttpClient(octolintConfig)
7374

7475
if err != nil {
75-
return nil, errors.New("Failed to create the Octopus client_wrapper. Check that the url, api key, and space are correct.\nThe error was: " + err.Error())
76+
return nil, errors.New("Failed to create the HTTP client. Check that the url is correct.\nThe error was: " + err.Error())
7677
}
7778

78-
if err := setClientHeaders(client, octolintConfig); err != nil {
79-
return nil, err
79+
client, err := createClient(httpClient, octolintConfig)
80+
81+
if err != nil {
82+
return nil, errors.New("Failed to create the Octopus client_wrapper. Check that the url, api key, and space are correct.\nThe error was: " + err.Error())
8083
}
8184

8285
checkFactory := factory.NewOctopusCheckFactory(client, octolintConfig.Url, octolintConfig.Space)
@@ -113,7 +116,7 @@ func Entry(octolintConfig *config.OctolintConfig) ([]checks.OctopusCheckResult,
113116
return results, nil
114117
}
115118

116-
func createClient(octolintConfig *config.OctolintConfig) (*client.Client, error) {
119+
func createClient(httpClient *http.Client, octolintConfig *config.OctolintConfig) (*client.Client, error) {
117120

118121
parsedUrl, err := getHost(octolintConfig)
119122

@@ -122,10 +125,10 @@ func createClient(octolintConfig *config.OctolintConfig) (*client.Client, error)
122125
}
123126

124127
if octolintConfig.ApiKey != "" {
125-
return createClientApiKey(parsedUrl, octolintConfig.Space, octolintConfig.ApiKey)
128+
return createClientApiKey(httpClient, parsedUrl, octolintConfig.Space, octolintConfig.ApiKey)
126129
}
127130

128-
return createClientAccessToken(parsedUrl, octolintConfig.Space, octolintConfig.AccessToken)
131+
return createClientAccessToken(httpClient, parsedUrl, octolintConfig.Space, octolintConfig.AccessToken)
129132
}
130133

131134
func getHost(octolintConfig *config.OctolintConfig) (*url.URL, error) {
@@ -136,38 +139,47 @@ func getHost(octolintConfig *config.OctolintConfig) (*url.URL, error) {
136139
return url.Parse(octolintConfig.Url)
137140
}
138141

139-
func setClientHeaders(client *client.Client, octolintConfig *config.OctolintConfig) error {
142+
func createHttpClient(octolintConfig *config.OctolintConfig) (*http.Client, error) {
140143
if octolintConfig.UseRedirector {
141144
parsedUrl, err := url.Parse(octolintConfig.Url)
142145

143146
if err != nil {
144-
return err
147+
return nil, err
148+
}
149+
150+
headers := map[string]string{
151+
"X_REDIRECTION_UPSTREAM_HOST": parsedUrl.Hostname(),
152+
"X_REDIRECTION_REDIRECTIONS": octolintConfig.RedirectorRedirections,
153+
"X_REDIRECTION_API_KEY": octolintConfig.RedirecrtorApiKey,
154+
"X_REDIRECTION_SERVICE_API_KEY": octolintConfig.RedirectorServiceApiKey,
145155
}
146156

147-
headers := client.HttpSession().DefaultHeaders
148-
headers["X_REDIRECTION_UPSTREAM_HOST"] = parsedUrl.Hostname()
149-
headers["X_REDIRECTION_REDIRECTIONS"] = octolintConfig.RedirectorRedirections
150-
headers["X_REDIRECTION_API_KEY"] = octolintConfig.RedirecrtorApiKey
151-
headers["X_REDIRECTION_SERVICE_API_KEY"] = octolintConfig.RedirectorServiceApiKey
157+
return &http.Client{
158+
Transport: &client_wrapper.HeaderRoundTripper{
159+
Transport: http.DefaultTransport,
160+
Headers: headers,
161+
},
162+
}, nil
152163
}
153164

154-
return nil
165+
return &http.Client{}, nil
155166
}
156167

157-
func createClientApiKey(apiURL *url.URL, spaceId string, apiKey string) (*client.Client, error) {
168+
func createClientApiKey(httpClient *http.Client, apiURL *url.URL, spaceId string, apiKey string) (*client.Client, error) {
158169
apiKeyCredential, err := client.NewApiKey(apiKey)
159170
if err != nil {
160171
return nil, err
161172
}
162-
return client.NewClientWithCredentials(nil, apiURL, apiKeyCredential, spaceId, "")
173+
174+
return client.NewClientWithCredentials(httpClient, apiURL, apiKeyCredential, spaceId, "")
163175
}
164176

165-
func createClientAccessToken(apiURL *url.URL, spaceId string, accessToken string) (*client.Client, error) {
177+
func createClientAccessToken(httpClient *http.Client, apiURL *url.URL, spaceId string, accessToken string) (*client.Client, error) {
166178
accessTokenCredential, err := client.NewAccessToken(accessToken)
167179
if err != nil {
168180
return nil, err
169181
}
170-
return client.NewClientWithCredentials(nil, apiURL, accessTokenCredential, spaceId, "")
182+
return client.NewClientWithCredentials(httpClient, apiURL, accessTokenCredential, spaceId, "")
171183
}
172184

173185
func ErrorExit(message string) {

0 commit comments

Comments
 (0)