-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconfig.go
More file actions
371 lines (288 loc) Β· 10.1 KB
/
Copy pathconfig.go
File metadata and controls
371 lines (288 loc) Β· 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package octopusdeploy_framework
import (
"context"
"encoding/json"
"errors"
"fmt"
"go/version"
"net/http"
"net/url"
"os"
"slices"
"strings"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/configuration"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/spaces"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
type Config struct {
Address string
ApiKey string
AccessToken string
SpaceID string
Client *client.Client
OctopusVersion string
// Can be nil when server doesn't support feature toggles API endpoint
FeatureToggles map[string]bool
}
// Start of OctoAI patch
type headerRoundTripper struct {
Transport http.RoundTripper
Headers map[string]string
}
func (h *headerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
for key, value := range h.Headers {
req.Header.Set(key, value)
}
return h.Transport.RoundTrip(req)
}
func getHttpClient(ctx context.Context, octopusUrl *url.URL) (*http.Client, *url.URL, error) {
if !isDirectlyAccessibleOctopusInstance(octopusUrl) {
tflog.Info(ctx, "[SPACEBUILDER] Enabled Octopus AI Assistant redirection service")
return createHttpClient(octopusUrl)
}
tflog.Info(ctx, "[SPACEBUILDER] Did not enable Octopus AI Assistant redirection service")
return nil, octopusUrl, nil
}
func getRedirectionBypass() []string {
hostnames := []string{}
hostnamesJson := os.Getenv("REDIRECTION_BYPASS")
if hostnamesJson == "" {
return []string{} // Default to empty slice if not set
}
err := json.Unmarshal([]byte(hostnamesJson), &hostnames)
if err != nil {
return []string{}
}
return hostnames
}
func getRedirectionForce() bool {
redirectionForce := os.Getenv("REDIRECTION_FORCE")
return strings.ToLower(redirectionForce) == "true"
}
// isDirectlyAccessibleOctopusInstance determines if the host should be contacted directly
func isDirectlyAccessibleOctopusInstance(octopusUrl *url.URL) bool {
serviceEnabled, found := os.LookupEnv("REDIRECTION_SERVICE_ENABLED")
if !found || serviceEnabled != "true" {
return true
}
bypassList := getRedirectionBypass()
// Allow bypassing specific domains via environment variable
if slices.Contains(bypassList, octopusUrl.Hostname()) {
return true
}
// Allow forcing all traffic through the redirection service
if getRedirectionForce() {
return false
}
return strings.HasSuffix(octopusUrl.Hostname(), ".octopus.app") ||
strings.HasSuffix(octopusUrl.Hostname(), ".testoctopus.com") ||
octopusUrl.Hostname() == "localhost" ||
octopusUrl.Hostname() == "127.0.0.1"
}
func createHttpClient(octopusUrl *url.URL) (*http.Client, *url.URL, error) {
serviceApiKey, found := os.LookupEnv("REDIRECTION_SERVICE_API_KEY")
if !found {
return nil, nil, errors.New("REDIRECTION_SERVICE_API_KEY is required")
}
redirectionHost, found := os.LookupEnv("REDIRECTION_HOST")
if !found {
return nil, nil, errors.New("REDIRECTION_HOST is required")
}
redirectionHostUrl, err := url.Parse("https://" + redirectionHost)
if err != nil {
return nil, nil, err
}
headers := map[string]string{
"X_REDIRECTION_UPSTREAM_HOST": octopusUrl.Hostname(),
"X_REDIRECTION_SERVICE_API_KEY": serviceApiKey,
}
return &http.Client{
Transport: &headerRoundTripper{
Transport: http.DefaultTransport,
Headers: headers,
},
}, redirectionHostUrl, nil
}
// End of OctoAI patch
func (c *Config) SetOctopus(ctx context.Context) diag.Diagnostics {
tflog.Debug(ctx, "SetOctopus")
diags := diag.Diagnostics{}
if clientError := c.GetClient(ctx); clientError != nil {
diags.AddError("failed to load client", clientError.Error())
return diags
}
if versionError := c.SetOctopusVersion(ctx); versionError != nil {
diags.AddError("failed to load Octopus Server version", versionError.Error())
return diags
}
c.SetFeatureToggles(ctx)
tflog.Debug(ctx, "SetOctopus completed")
return diags
}
func (c *Config) GetClient(ctx context.Context) error {
tflog.Debug(ctx, "GetClient")
octopus, err := getClientForDefaultSpace(c, ctx)
if err != nil {
return err
}
if len(c.SpaceID) > 0 {
space, err := spaces.GetByID(octopus, c.SpaceID)
if err != nil {
return err
}
octopus, err = getClientForSpace(c, ctx, space.GetID())
if err != nil {
return err
}
}
c.Client = octopus
createdClient := octopus != nil
tflog.Debug(ctx, fmt.Sprintf("GetClient completed: %t", createdClient))
return nil
}
func (c *Config) SetFeatureToggles(ctx context.Context) {
tflog.Debug(ctx, "SetFeatureToggles")
response, err := configuration.Get(c.Client, &configuration.FeatureToggleConfigurationQuery{})
if err != nil {
tflog.Debug(ctx, fmt.Sprintf("Unable to load feature toggles: %q", err.Error()))
c.FeatureToggles = nil
return
}
features := make(map[string]bool, len(response.FeatureToggles))
for _, feature := range response.FeatureToggles {
features[feature.Name] = feature.IsEnabled
}
c.FeatureToggles = features
tflog.Debug(ctx, fmt.Sprintf("SetFeatureToggles completed with %d features", len(c.FeatureToggles)))
}
func (c *Config) SetOctopusVersion(ctx context.Context) error {
tflog.Debug(ctx, "SetOctopusVersion")
root, err := client.GetServerRoot(c.Client)
if err != nil {
return err
}
c.OctopusVersion = root.Version
tflog.Debug(ctx, fmt.Sprintf("SetOctopusVersion completed with %s", c.OctopusVersion))
return nil
}
func getClientForDefaultSpace(c *Config, ctx context.Context) (*client.Client, error) {
return getClientForSpace(c, ctx, "")
}
func getClientForSpace(c *Config, ctx context.Context, spaceID string) (*client.Client, error) {
apiURL, err := url.Parse(c.Address)
if err != nil {
return nil, err
}
credential, err := getApiCredential(c, ctx)
if err != nil {
return nil, err
}
// Start of OctoAI patch
httpClient, url, err := getHttpClient(ctx, apiURL)
if err != nil {
return nil, err
}
tflog.Info(ctx, "[SPACEBUILDER] Directing requests from "+apiURL.String())
tflog.Info(ctx, "[SPACEBUILDER] Directing requests to redirector at "+url.String())
return client.NewClientWithCredentials(httpClient, url, credential, spaceID, "TerraformProvider")
// End of OctoAI patch
}
func getApiCredential(c *Config, ctx context.Context) (client.ICredential, error) {
tflog.Debug(ctx, "GetClient: Trying the following auth methods in order of priority - APIKey, AccessToken")
if c.ApiKey != "" {
tflog.Debug(ctx, "GetClient: Attempting to authenticate with API Key")
credential, err := client.NewApiKey(c.ApiKey)
if err != nil {
return nil, err
}
return credential, nil
} else {
tflog.Debug(ctx, "GetClient: No API Key found")
}
if c.AccessToken != "" {
tflog.Debug(ctx, "GetClient: Attempting to authenticate with Access Token")
credential, err := client.NewAccessToken(c.AccessToken)
if err != nil {
return nil, err
}
return credential, nil
} else {
tflog.Debug(ctx, "GetClient: No Access Token found")
}
return nil, fmt.Errorf("either an APIKey or an AccessToken is required to connect to the Octopus Server instance")
}
func DataSourceConfiguration(req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) *Config {
if req.ProviderData == nil {
return nil
}
config, ok := req.ProviderData.(*Config)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return nil
}
return config
}
func ResourceConfiguration(req resource.ConfigureRequest, resp *resource.ConfigureResponse) *Config {
if req.ProviderData == nil {
return nil
}
config, ok := req.ProviderData.(*Config)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return nil
}
return config
}
// FeatureToggleEnabled Reports whether feature toggle enabled on connected Octopus Server instance.
func (c *Config) FeatureToggleEnabled(toggle string) bool {
if c.FeatureToggles == nil {
return true
}
if enabled, ok := c.FeatureToggles[toggle]; ok {
return enabled
}
return false
}
// EnsureResourceCompatibilityByFeature Reports whether resource is compatible with current instance of Octopus Server by .
// Returns diagnostics with error when resource is incompatible and empty diagnostics for compatible resources
func (c *Config) EnsureResourceCompatibilityByFeature(resourceName string, toggle string) diag.Diagnostics {
diags := diag.Diagnostics{}
if c.FeatureToggleEnabled(toggle) {
return diags
}
summary := fmt.Sprintf("The '%s' resource is not supported by the connected Octopus Deploy instance", resourceName)
detail := fmt.Sprintf("This resource requires feature toggle '%s' to be enabled.", toggle)
diags.AddError(summary, detail)
return diags
}
// EnsureResourceCompatibilityByVersion Reports whether resource is compatible with current version of Octopus Server.
// Returns diagnostics with error when resource is incompatible and empty diagnostics for compatible resources
//
// Example: '2025.1' - first version where resource can be used
func (c *Config) EnsureResourceCompatibilityByVersion(resourceName string, version string) diag.Diagnostics {
diags := diag.Diagnostics{}
if c.IsVersionSameOrGreaterThan(version) {
return diags
}
summary := fmt.Sprintf("The '%s' resource is not supported by the current Octopus Deploy server version", resourceName)
detail := fmt.Sprintf("This resource requires Octopus Deploy server version %s or later. The connected server is running version %s, which is incompatible with this resource.", version, c.OctopusVersion)
diags.AddError(summary, detail)
return diags
}
func (c *Config) IsVersionSameOrGreaterThan(minVersion string) bool {
if c.OctopusVersion == "0.0.0-local" {
return true // Always true for local instance
}
diff := version.Compare(fmt.Sprintf("go%s", c.OctopusVersion), fmt.Sprintf("go%s", minVersion))
return diff == 1 || diff == 0
}