@@ -7,14 +7,14 @@ import (
7
7
"github.com/OctopusSolutionsEngineering/OctopusTerraformExport/cmd/internal/environment"
8
8
"github.com/OctopusSolutionsEngineering/OctopusTerraformExport/cmd/internal/logger"
9
9
"github.com/OctopusSolutionsEngineering/OctopusTerraformExport/cmd/internal/strutil"
10
+ "github.com/samber/lo"
10
11
"go.uber.org/zap"
11
12
"io"
12
13
"log"
13
14
"net/http"
14
15
"net/url"
15
16
"os"
16
17
"path/filepath"
17
- "strconv"
18
18
"strings"
19
19
)
20
20
@@ -41,27 +41,13 @@ func octoterraHandler(w http.ResponseWriter, r *http.Request) {
41
41
// Allow the more sensitive values to be passed as headers
42
42
apiKey := r .Header .Get ("X-Octopus-ApiKey" )
43
43
accessToken := r .Header .Get ("X-Octopus-AccessToken" )
44
- octopusUrl := r .Header .Get ("X-Octopus-Url" )
44
+ url := r .Header .Get ("X-Octopus-Url" )
45
45
redirectorRedirections := r .Header .Get ("X_REDIRECTION_REDIRECTIONS" )
46
46
redirectorApiKey := r .Header .Get ("X_REDIRECTION_API_KEY" )
47
47
redirectorServiceApiKey , _ := os .LookupEnv ("REDIRECTION_SERVICE_API_KEY" )
48
48
redirectorHost , _ := os .LookupEnv ("REDIRECTION_HOST" )
49
- disableRedirector , _ := os .LookupEnv ("DISABLE_REDIRECTION" )
50
49
51
- parsedUrl , err := url .Parse (octopusUrl )
52
-
53
- if err != nil {
54
- handleError (err , w )
55
- return
56
- }
57
-
58
- disableRedirectorParsed , err := strconv .ParseBool (disableRedirector )
59
-
60
- if err != nil {
61
- disableRedirectorParsed = false
62
- }
63
-
64
- useRedirector := ! disableRedirectorParsed && ! hostIsCloudOrLocal (parsedUrl .Hostname ()) && redirectorServiceApiKey != "" && redirectorHost != ""
50
+ enableRedirector , err := useRedirector (url , redirectorServiceApiKey , redirectorHost , redirectorRedirections , redirectorApiKey )
65
51
66
52
respBytes , err := io .ReadAll (r .Body )
67
53
@@ -119,12 +105,12 @@ func octoterraHandler(w http.ResponseWriter, r *http.Request) {
119
105
commandLineArgs = append (commandLineArgs , "-accessToken" , accessToken )
120
106
}
121
107
122
- if octopusUrl != "" {
123
- commandLineArgs = append (commandLineArgs , "-url" , octopusUrl )
108
+ if url != "" {
109
+ commandLineArgs = append (commandLineArgs , "-url" , url )
124
110
}
125
111
126
- if useRedirector {
127
- zap .L ().Info ("Using redirector for host " + octopusUrl )
112
+ if enableRedirector {
113
+ zap .L ().Info ("Using redirector for host " + url )
128
114
commandLineArgs = append (commandLineArgs , "-useRedirector" )
129
115
commandLineArgs = append (commandLineArgs , "-redirectorHost" , redirectorHost )
130
116
commandLineArgs = append (commandLineArgs , "-redirectorServiceApiKey" , redirectorServiceApiKey )
@@ -158,6 +144,35 @@ func octoterraHandler(w http.ResponseWriter, r *http.Request) {
158
144
}
159
145
}
160
146
147
+ func useRedirector (octopusUrl string , redirectorServiceApiKey string , redirectorHost string , redirections string , redirectorApiKey string ) (bool , error ) {
148
+ parsedUrl , err := url .Parse (octopusUrl )
149
+
150
+ if err != nil {
151
+ return false , err
152
+ }
153
+
154
+ bypassList := environment .GetRedirectionBypass ()
155
+
156
+ // Allow bypassing specific domains via environment variable
157
+ if lo .Contains (bypassList , parsedUrl .Hostname ()) {
158
+ return false , nil
159
+ }
160
+
161
+ // Allow forcing all traffic through the redirection service
162
+ if environment .GetRedirectionForce () {
163
+ return true , nil
164
+ }
165
+
166
+ // All redirections can be disabled via environment variable
167
+ if environment .GetRedirectionDisable () {
168
+ return false , nil
169
+ }
170
+
171
+ return redirectorServiceApiKey != "" && redirectorHost != "" &&
172
+ (! hostIsCloudOrLocal (parsedUrl .Hostname ()) ||
173
+ (redirections != "" && redirectorApiKey != "" )), nil
174
+ }
175
+
161
176
// sanitizeConfig removes sensitive information from the config so it is not
162
177
// persisted to the disk.
163
178
func sanitizeConfig (rawConfig []byte ) ([]byte , error ) {
0 commit comments